! ----------------------------------------------------------------
!  Macro to build the XBC option tree
!
!  Switches (passed from MAKE):
!    /NC - Suggests that No Code files need be built.
!    /NH - Suggests that No Help files need be built.
!    /NJ - Suggests that No Jar  files need be built.
!    /NB - Suggests that No OSGi Bundle files need be built.
!
!  @author  J. Czechowski IV / Neon Ngo
!  @version $Id: buildopt.mm,v 1.35 2012/12/28 16:18:09 ntn Exp $
! ----------------------------------------------------------------
startmacro u:option["XBC"]
  switch "DEBUG"      z:debug      GET DEF=FALSE SDEF=TRUE
  switch "BLDFLAGS"   t:bldflags   GET DEF={}
  switch "XBCOPTIONS" t:options    GET DEF={} ! Used for automated installs.
  switch "NC"         noCode       GET
  switch "NH"         noHelp       GET
  switch "NJ"         noJar        GET
  switch "NB"         noBundle     GET

  ! Verify that XBC is on the path (required for building Fortran files)
  if ",XBC," NSUBS ",^{opt.path}," then
    error "Can not build XBC or option trees using XBC without XBC on the path."
    stop
  endif

if noCode isFalse then
  ! build the required files in $NMROOT/nxm/sys/libxm and $NMROOT/nxm/xbc/...
  if ^option eqs "XBC" then
    bld/verbose/bldflags=^bldflags XmMsgHandler libxm sys
    bld/verbose/bldflags=^bldflags FortranPreprocessor|NmJackets lib xbc
    bld/verbose/bldflags=^bldflags genwrappers prim xbc

    ! Generate the native Jackets for the Java calls
    say "--> Generating C jackets for Java calls <--"
    invoke ,, nxm.xbc.lib.NmJackets.generateCode()

    ! generate midaslib.c
    nxm.xbc.prim.genwrappers IN=nxm.xbc.lib.midaslib.fllib &
                             OUT=nxm.xbc.lib.midaslib.c &
                             TAB=nxm.xbc.docs.supported_calls.tbl
    ! generate xmidaslib.c
    nxm.xbc.prim.genwrappers IN=nxm.xbc.lib.xmidaslib.fllib &
                             OUT=nxm.xbc.lib.xmidaslib.c &
                             TAB=nxm.xbc.docs.supported_calls.tbl

    ! preprocess headers.incp and types.incp (this is a special step in X-Midas too).
    !say "--> Converting *.incp to *.inc for XBC <--"
    invoke path nxm.sys.lib.OptionTree.getOptPath("xbc","inc")
    invoke ok nxm.sys.lib.Build.preprocessFortran(path,"headers|types",".incp",".inc",this,true,debug,bldFlags)
    if ok isFalse then
      error "Problems preprocessing *.incp to *.inc files. Aborting rest of XBC build."
      stop
    endif
  endif

  ! build gennmmsg and generate the required include files
  CONVERT ^option MSGCFG2TBL
  CONVERT ^option MSGCFG2INC
endif

  ! then perform the standard build
  buildopt;sys/NC=^{noCode}/NH=^{noHelp}/NJ=^{noJar}/NB=^{noBundle}/debug=^debug ^option /bldflags=^bldflags

  ! perform checkxbc (DR 2587)
  CHECKXBC OPT=option /SUMMARIZE

  if ^option eqs "XBC" then
    if noCode isFalse then
      ! Build the unconfigured host primitives used for testing XBC
      say "--> Building test primitives for XBC <--"
      build/bldflags=^bldflags/verbose f3000|group|msgecho|newrate|noop|readsv|waveform|&
                                       test_* host xbc
      if noJar isFalse then
        generate;sys/verbose/skip=JARSTANDARD JARS XBC
      endif
    endif

    if noHelp isFalse then
      file name UGrawfiles_dir (^{env.nmroot},htdocs,usersguide,rawfiles)
      file name out1fname      (^{UGrawfiles_dir}) XBCSupport.html
      file name out2fname      (^{UGrawfiles_dir}) XBCSupport2.html
      file name optionsfname   (^{UGrawfiles_dir}) options.tbl
      ! Build the XBC function mappings into the NeXtMidas User's Guide.
      say "--> Updating the XBC section in the NeXtMidas User's Guide <--"
      nxm.xbc.prim.makedocs TAB=nxm.xbc.docs.supported_calls.tbl &
                            OUT=out1fname &
                            OUT2=out2fname
      nxm.sys.prim.makedocs options="^{optionsfname}"
    endif
  endif

endmacro
