/*
 * Generally there is no need to edit this file!
 *
 * This file controls which libraries are built, as well as compiler options
 * to use.
 */

/*
 *  ======== azureiot.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 script
 *                  initialized as follows:
 *                      arguments[0] - the file name of the 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 Build = xdc.useModule('xdc.bld.BuildEnvironment');
var Pkg = xdc.useModule('xdc.bld.PackageContents');

/*
 * Linux86 => -std=c99 for (at least sscanf() and strtof())
 */
var ccOpts = {
    "ti.targets.arm.elf.M4"           : " -ms -g --c99 -DTI_RTOS",
};

var lnkOpts = {
};

var platforms = {
    "ti.targets.arm.elf.M4"           : "ti.platforms.simplelink:CC3200",
};

var platform = "";

/* initialize local vars with those set in products.mak (via XDCARGS) */
for (arg = 0; arg < arguments.length; arg++) {
    /* split each arg into its '+' separated parts */
    var configParts = arguments[arg].split(";");

    if (ccOpts[configParts[0].split("=")[0]] === undefined) {
        continue;
    }

    /*
     * Get the compiler's installation directory.
     * For "ti.targets.elf.C674=/vendors/c6x/7.2.0", we get "/vendors/c6x/7.2.0"
     */
    var targetName = configParts[0].split("=")[0];
    var rootDir = configParts[0].split("=")[1];

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

    var target = xdc.useModule(targetName);
    target.rootDir = rootDir;

    if (ccOpts[targetName] != undefined) {
        target.ccOpts.suffix += ccOpts[targetName];
    }

    if (lnkOpts[targetName] != undefined) {
        target.lnkOpts.suffix += lnkOpts[targetName];
    }

    if (platforms[targetName] != undefined) {
        target.platform = platforms[targetName];
    }

    /* for all the other parts, assign target.<left> = <right> */
    for (var i = 1; i < configParts.length; i++) {
        var modCfgParam = configParts[i].split("=")[0];
        var modCfgValue =
                configParts[i].substring(configParts[i].indexOf("=") + 1);
        var modCfgIndex = modCfgParam.split(".");
        var element = target;

        for (j = 0; j < (modCfgIndex.length -1); j++) {
                element = element[modCfgIndex[j]];
        }
        element[modCfgIndex[j]] = modCfgValue;
    }

    /* and finally add this target to the Build.targets array */
    Build.targets.$add(target);
}

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

/*
 * Some packages build for 'all profiles' - but that's too much to ship.
 * Loop over all profiles in all Build.targets and remove everything
 * except 'release' and 'debug'.
 */
for (var t = 0; t < Build.targets.length; t++) {
    if (Build.targets[t].$name.match(/^iar/)) {
        continue;
    }

    for (prof in Build.targets[t].profiles) {
        if ((prof != 'release') && (prof != 'debug')) {
            delete Build.targets[t].profiles[prof];
        }
    }
}

/* add notrace profile to all targets */
var notrace_defs = " -Dxdc_runtime_Log_DISABLE_ALL " +
        "-Dxdc_runtime_Assert_DISABLE_ALL ";

/* All libraries add a new notrace profile */
for (var t = 0; t < Build.targets.length; t++) {
    var targ = Build.targets[t];

    targ.profiles["notrace"] = targ.profiles["release"].$copy();
    var defs = targ.profiles["notrace"].compileOpts.defs;
    targ.profiles["notrace"].compileOpts.defs =
        (defs == undefined ? notrace_defs : defs + notrace_defs);
}
