<commit_before>#!/bin/bash
# Copyright (c) 2012 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
#@                   PNaCl Test Helper
#@-------------------------------------------------------------------
#@
#@ SCons Test Usage:
#@
#@   test.sh test-<arch>-<mode> [extra_arguments_to_scons]
#@
#@      Runs the SCons tests with selected arch and mode.
#@      Valid arches:
#@          x86-32
#@          x86-64
#@          arm
#@      Available modes:
#@          newlib (same as omitting mode)
#@          pic
#@          sbtc
#@          glibc
#@          browser
#@          browser-glibc
#@
#@      For example: test.sh test-x86-32-glibc
#@
#@ The env variables: PNACL_CONCURRENCY, PNACL_BUILDBOT, PNACL_DEBUG
#@ control behavior of this script
#@
#@

######################################################################
# Config
######################################################################

set -o nounset
set -o errexit

# The script is located in "pnacl/".
# Set pwd to native_client/
cd "$(dirname "$0")"/..
if [[ $(basename "$(pwd)") != "native_client" ]] ; then
  echo "ERROR: cannot find native_client/ directory"
  exit -1
fi
readonly NACL_ROOT="$(pwd)"

readonly DRYRUN=${DRYRUN:-false}

# This is only used by Spec2K test scripts.
readonly PNACL_LIBMODE=${LIBMODE:-newlib}
export PNACL_LIBMODE

source pnacl/scripts/common-tools.sh
SetScriptPath "${NACL_ROOT}/pnacl/test.sh"
SetLogDirectory "${NACL_ROOT}/toolchain/test-log"

# For different levels of make parallelism change this in your env
readonly PNACL_CONCURRENCY=${PNACL_CONCURRENCY:-8}

readonly OTHER_TEST_SCRIPT="${NACL_ROOT}/buildbot/buildbot_pnacl.sh"

######################################################################
######################################################################
#
#   < TESTING >
#
######################################################################
######################################################################

# TODO(robertm): figure out what to do about concurrency in debug mode.
# Perhaps it is fine just tweaking that via PNACL_CONCURRENCY.
if ${PNACL_DEBUG} || ${PNACL_BUILDBOT}; then
  readonly SCONS_ARGS=(MODE=nacl,opt-host
                       bitcode=1
                       --verbose
                       -j${PNACL_CONCURRENCY})
else
  readonly SCONS_ARGS=(MODE=nacl,opt-host
                       bitcode=1
                       naclsdk_validate=0
                       sysinfo=0
                       -j${PNACL_CONCURRENCY})
fi

#@ show-tests            - see what tests can be run
show-tests() {
  StepBanner "SHOWING TESTS"
  cat $(find tests -name nacl.scons) | grep -o 'run_[A-Za-z_-]*' | sort | uniq
}

Run() {
  if ${DRYRUN}; then
    echo "$@"
  else
    "$@"
  fi
}

RunScons() {
  local arch="$1"
  shift 1
  Run ./scons "${SCONS_ARGS[@]}" platform=${arch} "$@"
}

# Returns true if the arguments specify a test
# or target name to SCons.
has-target-name() {
  while [ $# -gt 0 ]; do
    # Skip arguments of the form -foo, --foo, or foo=bar.
    if [[ "$1" =~ ^-.* ]] || [[ "$1" =~ = ]]; then
      shift 1
      continue
    fi
    return 0
  done
  return 1
}

scons-clean () {
  local arch=$1
  local mode=$2
  local frontend=clang
  if [ "${mode}" == "newlib" ] ; then
    Run rm -rf scons-out/nacl-${arch}-pnacl-${frontend}
  else
    Run rm -rf scons-out/nacl-${arch}-pnacl-${mode}-${frontend}
  fi
}

build-sbtc-prerequisites() {
  local arch=$1
  # Sandboxed translators currently only require irt_core since they do not
  # use PPAPI.
  RunScons ${arch} sel_ldr sel_universal irt_core
}

# TODO(pdox):
# "mode" is presently a combination of multiple bits:
# Newlib vs. GLibC, sandboxed vs unsandboxed, PIC vs non-PIC
# These options should be isolated and kept separate.
get-mode-flags() {
  local mode="$1"
  local modeflags=""
  case ${mode} in
  newlib)
    ;;
  sbtc)
    modeflags="use_sandboxed_translator=1"
    ;;
  pic)
    modeflags="nacl_pic=1"
    ;;
  glibc)
    # Disable pexe mode with glibc for now. This will result in running the
    # nonpexe tests twice but there aren't too many of them.
    modeflags="--nacl_glibc pnacl_generate_pexe=0"
    ;;
  sbtc-glibc)
    # Note "sbtc-glibc" just happens to be the order in which the modes
    # are slapped onto the scons-out name. E.g.,
    # nacl-x86-32-pnacl-sbtc-glibc-clang
    # This affects the scons-clean() function.
    modeflags="--nacl_glibc use_sandboxed_translator=1 pnacl_generate_pexe=0"
    ;;
  *)
    echo "Unknown mode" 1>&2
    exit -1
  esac
  echo ${modeflags}
}

#+ Run scons test under a certain configuration
#+ scons-tests <arch> <mode={newlib,etc.}>
#+             <pexe=true/false> [optional list of test names]
#+ If <pexe> is true, we will first build pexes without running the tests,
#+ then run the tests.
#+ If no optional tests are listed, we will build all the tests then
#+ run the "smoke_tests" test suite.
scons-tests () {
  local arch="$1"
  local mode="$2"
  shift 2
  scons-clean ${arch} ${mode}<commit_msg>Also clear the *-pexe-* scons-out directory when testing with "pnacl/test.sh" (not just the non-pexe directory) so that we don't use stale pexes/nexes when testing toolchain changes on a local machine.<commit_after>#!/bin/bash
# Copyright (c) 2012 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
#@                   PNaCl Test Helper
#@-------------------------------------------------------------------
#@
#@ SCons Test Usage:
#@
#@   test.sh test-<arch>-<mode> [extra_arguments_to_scons]
#@
#@      Runs the SCons tests with selected arch and mode.
#@      Valid arches:
#@          x86-32
#@          x86-64
#@          arm
#@      Available modes:
#@          newlib (same as omitting mode)
#@          pic
#@          sbtc
#@          glibc
#@          browser
#@          browser-glibc
#@
#@      For example: test.sh test-x86-32-glibc
#@
#@ The env variables: PNACL_CONCURRENCY, PNACL_BUILDBOT, PNACL_DEBUG
#@ control behavior of this script
#@
#@

######################################################################
# Config
######################################################################

set -o nounset
set -o errexit

# The script is located in "pnacl/".
# Set pwd to native_client/
cd "$(dirname "$0")"/..
if [[ $(basename "$(pwd)") != "native_client" ]] ; then
  echo "ERROR: cannot find native_client/ directory"
  exit -1
fi
readonly NACL_ROOT="$(pwd)"

readonly DRYRUN=${DRYRUN:-false}

# This is only used by Spec2K test scripts.
readonly PNACL_LIBMODE=${LIBMODE:-newlib}
export PNACL_LIBMODE

source pnacl/scripts/common-tools.sh
SetScriptPath "${NACL_ROOT}/pnacl/test.sh"
SetLogDirectory "${NACL_ROOT}/toolchain/test-log"

# For different levels of make parallelism change this in your env
readonly PNACL_CONCURRENCY=${PNACL_CONCURRENCY:-8}

readonly OTHER_TEST_SCRIPT="${NACL_ROOT}/buildbot/buildbot_pnacl.sh"

######################################################################
######################################################################
#
#   < TESTING >
#
######################################################################
######################################################################

# TODO(robertm): figure out what to do about concurrency in debug mode.
# Perhaps it is fine just tweaking that via PNACL_CONCURRENCY.
if ${PNACL_DEBUG} || ${PNACL_BUILDBOT}; then
  readonly SCONS_ARGS=(MODE=nacl,opt-host
                       bitcode=1
                       --verbose
                       -j${PNACL_CONCURRENCY})
else
  readonly SCONS_ARGS=(MODE=nacl,opt-host
                       bitcode=1
                       naclsdk_validate=0
                       sysinfo=0
                       -j${PNACL_CONCURRENCY})
fi

#@ show-tests            - see what tests can be run
show-tests() {
  StepBanner "SHOWING TESTS"
  cat $(find tests -name nacl.scons) | grep -o 'run_[A-Za-z_-]*' | sort | uniq
}

Run() {
  if ${DRYRUN}; then
    echo "$@"
  else
    "$@"
  fi
}

RunScons() {
  local arch="$1"
  shift 1
  Run ./scons "${SCONS_ARGS[@]}" platform=${arch} "$@"
}

# Returns true if the arguments specify a test
# or target name to SCons.
has-target-name() {
  while [ $# -gt 0 ]; do
    # Skip arguments of the form -foo, --foo, or foo=bar.
    if [[ "$1" =~ ^-.* ]] || [[ "$1" =~ = ]]; then
      shift 1
      continue
    fi
    return 0
  done
  return 1
}

scons-clean () {
  local arch=$1
  local mode=$2
  local frontend=clang
  # Clear both the pexe and the nonpexe scons-out directory, since we run
  # a mix of both tests.
  if [ "${mode}" == "newlib" ] ; then
    Run rm -rf scons-out/nacl-${arch}-pnacl-${frontend}
    Run rm -rf scons-out/nacl-${arch}-pnacl-pexe-${frontend}
  else
    Run rm -rf scons-out/nacl-${arch}-pnacl-${mode}-${frontend}
    Run rm -rf scons-out/nacl-${arch}-pnacl-${mode}-pexe-${frontend}
  fi
}

build-sbtc-prerequisites() {
  local arch=$1
  # Sandboxed translators currently only require irt_core since they do not
  # use PPAPI.
  RunScons ${arch} sel_ldr sel_universal irt_core
}

# TODO(pdox):
# "mode" is presently a combination of multiple bits:
# Newlib vs. GLibC, sandboxed vs unsandboxed, PIC vs non-PIC
# These options should be isolated and kept separate.
get-mode-flags() {
  local mode="$1"
  local modeflags=""
  case ${mode} in
  newlib)
    ;;
  sbtc)
    modeflags="use_sandboxed_translator=1"
    ;;
  pic)
    modeflags="nacl_pic=1"
    ;;
  glibc)
    # Disable pexe mode with glibc for now. This will result in running the
    # nonpexe tests twice but there aren't too many of them.
    modeflags="--nacl_glibc pnacl_generate_pexe=0"
    ;;
  sbtc-glibc)
    # Note "sbtc-glibc" just happens to be the order in which the modes
    # are slapped onto the scons-out name. E.g.,
    # nacl-x86-32-pnacl-sbtc-glibc-clang
    # This affects the scons-clean() function.
    modeflags="--nacl_glibc use_sandboxed_translator=1 pnacl_generate_pexe=0"
    ;;
  *)
    echo "Unknown mode" 1>&2
    exit -1
  esac
  echo ${modeflags}
}

#+ Run scons test under a certain configuration
#+ scons-tests <arch> <mode={newlib,etc.}> [optional list of test names]
#+ If no optional tests are listed, we will build all the tests then
#+ run the "smoke_tests" test suite.
scons-tests () {
  local arch="$1"
  local mode="$2"
  shift 2
  scons-clean ${arch} ${mode}
{'fraction_non_alphanumeric': 0.2084208516326141, 'fraction_numerical': 0.009738399847240786, 'mean_word_length': 2.9424162589386524, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '565194', 'n_tokens_mistral': 3626, 'n_tokens_neox': 3465, 'n_words': 979}