# file      : tests/cc/preprocessed/testscript
# license   : MIT; see accompanying LICENSE file

crosstest = false
test.arguments = config.cxx=$quote($recall($cxx.path) $cxx.config.mode) update

.include ../../common.testscript

# Trace filter.
#
# trace: cxx::compile::apply extracting (headers|modules) from: .../obje{(test).o...}
#
filter = [cmdline] sed -n -e \
  \''s/^trace: cxx::compile_rule::apply: extracting ([^ ]+) from[^{]+\{([^.]+).*/\1 \2/p'\'

+cat <<EOI >=build/root.build
cxx.std = latest

using cxx

hxx{*}: extension = hxx
cxx{*}: extension = cxx

cxx.poptions =+ "-I$src_root"
EOI

: none
:
: Include a header (makes sure headers are handled) which defines the
: TEST_VALUE macro. Import a non-existent module unless this macro is
: defined (makes sure modules are extracted from preprocessed source).
: Use the macro (makes sure compilation happens on preprocessed source).
:
cat <<EOI >=test.hxx &!test.hxx;
  #define TEST_VALUE 0
  EOI
cat <<EOI >=test.cxx &!test.cxx;
  #include <none/test.hxx>

  #ifndef TEST_VALUE
  import foo;
  #endif

  int main () {return TEST_VALUE;}
  EOI
$* --verbose 5 &test* <<EOI 2>&1 | $filter >>EOO
  exe{test}: cxx{test}
  EOI
  headers test
  modules test
  EOO

: includes
:
cat <<EOI >=test.cxx &!test.cxx;
  #ifndef TEST_VALUE
  import foo;
  #endif

  int main () {return TEST_VALUE;}
  EOI
$* --verbose 5 &test* <<EOI 2>&1 | $filter >>EOO
  cxx.preprocessed = includes
  cxx.poptions += -DTEST_VALUE=0
  exe{test}: cxx{test}
  EOI
  modules test
  EOO

: modules
:
: Define and use macro to test that compilation inclused the preprocessor.
:
cat <<EOI >=test.cxx &!test.cxx;
  int main () {return TEST_VALUE;}
  EOI
$* --verbose 5 &test* <<EOI 2>&1 | $filter >>EOO
  cxx.preprocessed = modules
  cxx.poptions += -DTEST_VALUE=0
  exe{test}: cxx{test}
  EOI
  modules test
  EOO

: modules-extract
:
: Define macro that would have disabled the module import (makes sure
: modules are extracted directly from source).
:
cat <<EOI >=test.cxx &!test.cxx;
  #define TEST_VALUE
  #ifndef TEST_VALUE
  import foo;
  #endif
  EOI
$* &test* <<EOI 2>>EOE != 0
  cxx.preprocessed = modules
  exe{test}: cxx{test}
  EOI
  error: modules support required by cxx{test}
    info: consider enabling modules with cxx.features.modules=true in root.build
  EOE

: all
:
: Test handling of comments and line continuations. Define macro on the
: command line that would have affected the result.
:
cat <<EOI >=test.cxx &!test.cxx;
  // C++ comment
  /*
  C comment
  */

  int ma\
  in () {}
  EOI
$* --verbose &test* 5 <<EOI 2>&1 | $filter >>EOO
  cxx.preprocessed = all
  cxx.poptions += -Dmain=foo
  exe{test}: cxx{test}
  EOI
  modules test
  EOO
