//******************************************************************************
//  Description:  C++ Host primitive to test compile/link flags using BUILD and
//                cfg/primitives.cfg under NeXtMidas's XBC.
//
//  Author : Neon Ngo
//  Version: $Id: test_build.cc,v 1.1 2012/04/24 20:23:11 ntn Exp $
//
//******************************************************************************

#include <sstream>        // for ostringstream
#include <primitive.h>

//#define _DEBUG_NXM_TEST // uncomment to enable test framework debugging
#include "nxm_test.hh"    // XBC C++ test framework

// test #G1 cflags global settings
#if defined(TEST_BUILD_MY_GLOBAL_CFLAG)
  const bool haveGlobalCflagSetting = true;
#else
  const bool haveGlobalCflagSetting = false;
#endif

// test #G2 [_LINX] conditional in cflags global settings
#if defined(_LINX)
  const bool onLinux = TEST_BUILD_ON_LINUX_PLATFORM;
  const bool expectedOnLinux = true;
#else
  const bool onLinux = false;
  const bool expectedOnLinux = false;
#endif

// test #G3 $NMROOT OS environment variable conditional and usages in cflags global settings
const string UNDEFINED_NMROOT_VALUE = "<undefined_NMROOT>";
#if defined(TEST_BUILD_NMROOT)
  const string BUILD_NMROOT = NXM_STRINGIFY(TEST_BUILD_NMROOT);
#else
  const string BUILD_NMROOT = UNDEFINED_NMROOT_VALUE;
#endif

// test #T1 cflags target primitive setting
#if defined(TEST_BUILD_MY_TARGET_CMD_CFLAG)
  const bool haveTargetCmdCflagSetting = true;
#else
  const bool haveTargetCmdCflagSetting = false;
#endif

// test #T2 [NEXTMIDAS] conditional
#if defined(TEST_BUILD_FROM_NEXTMIDAS)
  const bool builtFromNeXtMidas = true;
#else
  const bool builtFromNeXtMidas = false;
#endif

  // test #T3 [$NM_PID] conditional and usages in target primitive settings
#if defined(TEST_BUILD_NM_PID)
  const int BUILD_NM_PID = TEST_BUILD_NM_PID;
#else
  const int BUILD_NM_PID = 0;
#endif

// test #T4 [UNIX] conditional in cflags target primitive settings
#if defined(_UNIX)
  const bool onUnix = TEST_BUILD_ON_UNIX_PLATFORM;
  const bool expectedOnUnix = true;
#else
  const bool onUnix = false;
  const bool expectedOnUnix = false;
#endif


// ****************************************************************** //
void mainroutine()
{
  // ----------------------------------------------------- //
  // preamble - Get parameters
  ostringstream ostr; // output stringstream for more detailed test case messages
  nxm_assert_init("DEBUG","STATUS");

  nxm_assert("test #G1: global cflags setting", haveGlobalCflagSetting);
  nxm_assert("test #G2: global cflags - conditional for LINX", (onLinux == expectedOnLinux));
  nxm_assert("test #G3: global cflags - conditional and usage of $NMROOT OS env variable", BUILD_NMROOT.compare(UNDEFINED_NMROOT_VALUE) != 0);

  nxm_assert("test #T1: target cflags setting", haveTargetCmdCflagSetting);
  nxm_assert("test #T2: target cflags - conditional for NEXTMIDAS", (builtFromNeXtMidas == true));

  ostr.str(""); // clear/empty the ostringstream buffer
  ostr << "test #T3: NM_PID [" << BUILD_NM_PID << "] from OS env var in cflags directive";
  nxm_assert(ostr.str(), (BUILD_NM_PID > 0));

  nxm_assert("test #T4: target cflags - conditional for UNIX", (onUnix == expectedOnUnix));

  m_sync();

  // we don't need to do any processing in this primitive

  // ----------------------------------------------------- //
  // postamble
  nxm_assert_auto_report();
}
