/*
 * Generally there is no need to edit this file!
 *
 * This file controls which libraries are built, as well as compiler options
 * to use.
 *
 * The contents of this file usually don't change, but having it in your
 * ownership allows you to tweak your compiler options.  If you do change
 * this file, however, on the next upgrade of the product we recommend
 * that you take "wolfssl.bld" file as supplied by the upgrade and then merge
 * your changes with it.
 */

/*
 *  ======== wolfssl.bld ========
 *  This script is run prior to all build scripts. It sets host-system-
 *  independent values for targets and platforms, then it attempts to
 *  find the host-system-specific user.bld script that sets rootDirs.
 *
 *  These settings may be a function of the following global variables:
 *
 *	environment a hash table of environment strings
 *
 *	arguments   an array of string arguments to the _config.bld script
 *		    initialized as follows:
 *		        arguments[0] - the file name of the _config.bld script
 *		        arguments[1] - the first argument specified in XDCARGS
 *		            :
 *		        arguments[n] - the n'th argument in XDCARGS
 *
 *	Build	    an alias for xdc.om.xdc.bld.BuildEnvironment
 */

var armOpts = " -ms ";
var gnuOpts = " -D_POSIX_SOURCE ";
var iarOpts = " --diag_suppress=Pa134 ";
var ndkDir = "";

/* Uncomment the following lines to build libraries for debug mode: */
// Pkg.attrs.profile = "debug";
// armOpts += " -g -o0 ";
// gnuOpts += " -g -D_POSIX_SOURCE ";
// iarOpts += " --debug ";

var ccOpts = {
    "ti.targets.arm.elf.M4"        : armOpts,
    "ti.targets.arm.elf.M4F"       : armOpts,

    "gnu.targets.arm.M4"           : gnuOpts,
    "gnu.targets.arm.M4F"          : gnuOpts,

    "iar.targets.arm.M4"           : iarOpts,
    "iar.targets.arm.M4F"          : iarOpts,
};

/* initialize local vars with those set in xdcpaths.mak (via XDCARGS) */
for (arg = 0; arg < arguments.length; arg++) {
    /*
     * Get the compiler's installation directory.
     * For "ti.targets.arm.elf.M4F=/vendors/arm/6.1.0",
     * we get "/vendors/arm/6.1.0"
     */
    var targetName = arguments[arg].split("=")[0];
    var rootDir = arguments[arg].split("=")[1];

    /* only build for the specified compilers */
    if (rootDir == "" || rootDir == undefined) {
        continue;
    }

    if (targetName.match(/^NDK/) ) {
        ndkDir = rootDir;
        continue;
    }

    var target = xdc.useModule(targetName);
    target.rootDir = rootDir;
    target.ccOpts.suffix += ccOpts[targetName];

    Build.targets.$add(target);
}

/* Include Path (needed to find NDK headers) */
var wolfsslPathInclude = " -I" + ndkDir + "/packages/ti/ndk/inc/bsd "
        + "-DWOLFSSL_TIRTOS ";

/* lib/ is a generated directory that 'xdc clean' should remove */
var Pkg = xdc.useModule('xdc.bld.PackageContents');
Pkg.generatedFiles.$add("lib/");