!******************************************************************************!
! test building option tree that depends on XBC
!
! author:  Neon Ngo
! version: $Id: test_build.mm,v 1.7 2013/08/20 14:07:43 jwb Exp $
!******************************************************************************!
startmacro
  switch BASELINE baseline    GET DEF=FALSE SDEF=TRUE
  switch KEEP     keep        GET DEF=FALSE SDEF=TRUE

  ! Points to test option trees under XBC test area
  file name rootOptDir     (^{env.nmroot},nxm,xbc,test,nmopts) "" ""
  file name xbctestopt1dir (^{rootOptDir},nxm,xbctestopt1)

  ! define option tree
  option xbctestopt1 xbctestopt1dir

  ! save previous path
  res savepath opt.path

  path add xbctestopt1
  ! build noopcc.cc (C++ host primitive)
  build noopcc;xbctestopt1

  file name hostdir (^{xbctestopt1dir},host)
  files/list=sofiles name=noopcc* ext=so aux=hostdir
  assert/text="^{reg.main.name}: built NOOP C++ host primitive." "noopcc_lin" subs sofiles

  ! Since 3.1.2:test building with custom primitives.cfg (BUG 2429 & BUG 1696)
  call test_using_primitives_cfg

  ! Since 3.2.0:test openmp with custom build.props (BUG 1379)
  invoke openmp nxm.sys.lib.Import.getEnv("NM_TEST_OPENMP")
  if openmp eqss "true"
    call test_openmp_cfg
  else
    info "SKIPPING OPENMP TEST: To run the test set NM_TEST_OPENMP=true"
  endif

  if keep isFALSE then
    ! cleanup
    option/delete xbctestopt1
  endif

  ! restore previous path
  path set ^savepath
endmacro

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "TEXT" then
    !say "GOT DEBUG message data: ^msg.data"

    if "-DTEST_BUILD_LINK_FLAG_NNNNN" subs msg.data then
      ! test #M1: lflags    + -DTEST_BUILD_LINK_FLAG_NNNNN
      res test_primitives_cfg_results.m1 true
    endif
    if "-LTEST_BUILD_BOGUS_THIRD_PARTY_LIBRARY_TTTTT" subs msg.data then
      ! test #M2: libdepend + ;$TEST_BUILD_BOGUS_THIRD_PARTY_LIBRARY_TTTTT
      res test_primitives_cfg_results.m2 true
    endif
    if "nxm/xbc/test/nmopts/nxm/xbctestopt1/lib" subs msg.data then
     ! test #M3: libdepend + ;xbctestopt1
      res test_primitives_cfg_results.m3 true
    endif
    if "^{ENV.NMROOT}lib" subs msg.data then
      ! test #M4: libdepend + ;$NMROOT
      res test_primitives_cfg_results.m4 true
    endif
    if "-lTHIS_LIB_SHOULD_NOT_SHOW_UP_GGGGG" nsubs msg.data then
      ! test #M5: libdepend + THIS_LIB_SHOULD_NOT_SHOW_UP_GGGGG;$NMROOT -> $TEST_NON_EXISTENT_ENV_VAR_GGGGG
      res test_primitives_cfg_results.m5 true
    else
      res test_primitives_cfg_results.m5 false
    endif

  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  endif
return

!******************************************************************************!
! Since 3.1.2: test building with custom primitives.cfg (BUG 2429 & BUG 1696)
!******************************************************************************!
procedure test_using_primitives_cfg
  file name xbctestopt2dir (^{rootOptDir},nxm,xbctestopt2)
  option xbctestopt2 xbctestopt2dir
  path add xbctestopt2

  ! NOTE: We have to REMOVE DEBUG from switches (to allow running this test from CONFIDENCE)
  !       otherwise, /DEBUG=False is proprogated to the BUILD command below, causing
  !       automated test case to FAIL.
  switch DEBUG prevDebug DEL
  res test_primitives_cfg_results {m1=false,m2=false,m3=false,m4=false,m5=false}

  archive to MAIN
  build/debug test_build;xbctestopt2
  archive off

  foreach item intable test_primitives_cfg_results
    assert/text="^{reg.main.name}: using primitives.cfg - test #^{item}" test_primitives_cfg_results.^{item} isTRUE
  endfor

  ! restore previous debug setting (if it existed)
  if prevDebug rexists then
    switch DEBUG ^prevDebug SET    ! changed to pass n value in 3.3.4 because CONF was throwing stack trace from PATH cmd
  endif

  test_build;xbctestopt2/status=status
  assert/text="^{reg.main.name}: using primitives.cfg additional tests." ^status eqs "PASS"

  if keep isFALSE then
    ! cleanup
    option/delete xbctestopt2
  endif

return

!******************************************************************************!
! Since 3.2.0: test openmp with custom build.props (BUG 1379)
!******************************************************************************!
procedure test_openmp_cfg
  file name xbctestopt2dir (^{rootOptDir},nxm,xbctestopt2)
  option xbctestopt2 xbctestopt2dir
  path add xbctestopt2

  archive to MAIN
  build/debug openmp_example;xbctestopt2
  archive off

  invoke fortcompiler nxm.sys.lib.Build.getFortranCompiler()

  ! verify the Intel compiler is configured for ifort
  if fortcompiler eqss "ifort"
    TEST_OPENMP
    test_build;xbctestopt2/status=status
    assert/text="^{reg.main.name}: openmp additional tests." ^status eqs "PASS"
  else
    info "ifort must be configured to perform the openmp test"
  endif

  if keep isFALSE then
    ! cleanup
    option/delete xbctestopt2
  endif

return
