# -*- mode:python;indent-tabs-mode:nil;show-trailing-whitespace:t; -*-
#
# Main build file for system.

import os, sys

def print_build(build):
    from tools.build.settings import Settings

    if build.options.log:
        print "Logging information printing for build:", \
              ",".join(build.options.log), "\n"
        if "toplevel" in build.options.log:
            print "Toplevel directory:",
            Settings.write(build.toplevel)
            print "\n"
        if "targets" in build.options.log:
            print "Command-line targets:", ", ".join(COMMAND_LINE_TARGETS) or "none"
            print "Build targets:", ", ".join(BUILD_TARGETS) or "defaults"
            print ""
        if "platform" in build.options.log:
            print "Platform:",
            Settings.write(build.platform)
            print "\n"
        if "projects" in build.options.log:
            print "Projects:",
            Settings.write(build.projects, 1)
            print "\n"
        if "options" in build.options.log:
            print "Command-line options:", \
                  ", ".join( [ "%s = %s" % item for item in ARGUMENTS.items() ] )
            print ""
            print "Requested options:",
            Settings.write(build.options_requested, 1)
            print "\n\nActual options:",
            Settings.write(build.options, 1)
            print "\n"
        if "settings" in build.options.log:
            print "Settings ids:",
            Settings.write([ s.id for s in build.settings ], 1)
            print "\n\nSettings:",
            Settings.write(build.settings, 1)
            print "\n"
        if "environment" in build.options.log:
            print "Environment:"
            env_vars = []
            env_dict = build.environment.Dictionary()
            env_keys = env_dict.keys()
            env_keys.sort()
            for key in env_keys:
                if env_dict.has_key(key):
                    env_vars += [ key + " = " + str(env_dict[key]) ]
            Settings.write(env_vars, 1)
        print "-" * 72

help = """
This is the Rosetta build system (using SCons).
For information on using it properly see the file tools/build/README.
If you are s developer also take a look at the Rosetta wiki under 'Build System.'

The basic form of a run is:

    scons [flags] [<options>] [<targets>]

[flags] are command-line options for the scons program (see more
by using -H).

<options> are key=value pairs from the following sets in any order.

    cxx:        The C++ compiler
    cxx_ver:    The version of the C++ compiler
    os:         The operating system
    os_ver:     The version of the operating system
    arch:       The processor architecture
    arch_size:  The bit-size of the processor
    mode:       debug, release, profile or coverage build
    cat:        Whether the build is for sources, tests, and/or docs.
    extras:     Additional features of the build

<targets> are lists of projects or directories you want to build.
"""

def main():
    DEBUG = True
    try:
        build = SConscript("tools/build/setup.py")
        build.toplevel = os.getcwd()

        print_build(build)

        # Put all .sconsign files (used to determine if a targets
        # has changed) into the build directory
        ### SConsignFile("build/signatures")
        SConsignFile()

        Help(help)

        # Run the SConscript to choose which subdirectories to invoke
        SConscript("SConscript", "build")
        # Rosetta++ is a special case and needs its own build script
        SConscript("SConscript.rosetta++", "build")

    except StandardError, ex:
        if not DEBUG:
            print "Error:", ex
        else:
            # Print a stack trace
            sys.excepthook(*sys.exc_info())

main()
