示例#1
0
def find_executable(env, file, project_name=None):
    '''find_executable

    looks for an executable script in these directories:
        1. current directory
        2. build-scripts directory
        3. external-project's scripts directory
        4. the current environment's PATH

    returns a string of the found file-node.
    '''
    search_dir_list = [os.getcwd()]
    if project_name in ['this', env['projectName']]:
        search_dir_list += [env['buildScrDir']]
    else:
        try:
            search_dir_list += [env[project_name + 'ScrDir']]
        except:
            pass
    search_dir_list += os.environ['PATH'].split(os.path.pathsep)
    search_dir_list = SCons.Script.Flatten(search_dir_list)

    ret = env.FindFile(file, search_dir_list)

    if not ret:
        print alertstr('could not find executable: ' + file)
        print 'env[\'buildScrDir\']:', env['buildScrDir']
        if project_name != 'this':
            print 'env[\'' + project_name + 'ScrDir\']', env[project_name +
                                                             'ScrDir']

    return str(env.File(ret).srcnode().abspath)
示例#2
0
def jar(env, target, source=None, manifest=None):
    '''jar

    builds jar files from java source code.

    env
        The SCons.Environment
    target
        a string specifying the target jar file to be created.
        This is a required field. The '.jar' extension may be
        omitted (recommended).
    source
        list of source files (strings or SCons Nodes)
        if type None (default) the java compiler will use the
        current directory as the source-base for class files.
    manifest
        a string specifying the manifest file to be used in the jar.
        This manifest file MUST begin with 'Manifest-Version' to ensure
        SCons recognizes it.

    any jar files in the current directory will be automatically
    added to the JAVACLASSPATH SCons variable.
    '''

    if not target:
        raise Exception(alertstr('No target specified'))

    if not source:
        source = ['.']

    build_obj_dir = build_obj_directory(env)
    classdir = os.path.join(build_obj_dir, 'classes')
    target = os.path.join(env['buildJavaDir'], target)

    env.AppendUnique(JAVACLASSPATH=[str(x) for x in env.Glob('*.jar')])

    if env['verbose'] > 1:
        print infostr('  reading in library:'), tgt2str(target)
        if env['verbose'] > 3:
            print infostr('    classes directory:'), srcstr(classdir)

    class_files = env.Java(classdir, source)

    if manifest:
        class_files += [manifest]

    jar_file = env.Jar(target, class_files)

    env.Install(env['javaDir'], jar_file)

    return (jar_file, class_files)
示例#3
0
def install_scripts(env, nodes, recursive=True):
    nodes = SCons.Script.Flatten([nodes])
    files = []
    for node in nodes:
        f = str(node)
        if os.path.isdir(f):
            if not recursive:
                env.Install(env['buildScrDir'], f)
                env.Install(env['scrDir'], f)
            else:
                for dirpath, dirnames, filenames in os.walk(f):
                    for igdir in _ignore_dirs:
                        try:
                            dirnames.remove(igdir)
                        except:
                            pass
                    files += [os.path.join(dirpath, x) for x in filenames]
        elif os.path.isfile(f):
            files += [node]
        else:
            raise Exception(alertstr('can not install script: ' + str(node)))
    env.Install(env['buildScrDir'], files)
    env.Install(env['scrDir'], files)
示例#4
0
def initialize(project_name, deps):
    try:
        tmp = initialize.run_once
        raise Exception(
            alertstr(
                str(initialize) + ''' called twice.
            This should never happen.'''))
    except AttributeError:
        initialize.run_once = True

    optimization.init()

    env = SCons.Environment.Environment(ENV={'PATH': os.environ['PATH']})
    try:
        env.Append(ENV={'LD_LIBRARY_PATH': os.environ['LD_LIBRARY_PATH']})
    except:
        pass
    env.EnsurePythonVersion(2, 2)
    env.EnsureSConsVersion(1, 0)

    add_methods(env)

    if project_name:
        env['projectName'] = project_name
    else:
        env['projectName'] = False

    env['platformName'] = platform_string.platform_string(env)

    variables.set_config_file(env)
    variables.add(env)

    stylize.init(env)

    for tool in env['tools']:
        env.Tool(tool)

    if len(deps):
        dependency.add_variables(env, deps)

    env['verbose'] = int(env['verbose'])

    env['vars'].Save(env['configFile'], env)

    format.generate_help_text(env)
    format.cmd_output(env)

    variables.load(env)

    optimization.set_flags(env)
    set_install_targets(env)

    env.VariantDir(env['buildObjDir'], '.', duplicate=0)
    env['JAVACHDIR'] = True

    if env['verbose'] in [1, 2]:
        msg = infostr('evaluating targets')
        SCons.Script.Progress([
            '  -  ' + msg + '\r', '  \\  ' + msg + '\r', '  |  ' + msg + '\r',
            '  /  ' + msg + '\r'
        ],
                              interval=1)

    proc = Popen('which gcc',
                 env=os.environ,
                 shell=True,
                 stdout=PIPE,
                 stderr=PIPE)
    gccpath = proc.communicate()[0].split('\n')[0]
    if os.path.normpath(gccpath[:8]) != os.path.normpath('/usr/lib'):
        gccrpath = os.path.split(gccpath)[0].replace('bin', 'lib')
        if env['alignbits'] in ['native', '64']:
            gccrpath += '64'
        env.AppendUnique(RPATH=[gccrpath])

    return env
示例#5
0
def find_target_and_source(env, target, source, ignore=None):
    '''find_target_and_source

    for C/C++/FORTRAN programs and libraries, returns the name
    of the target and source files to be used based on the
    input parameters and the files in the current working directory.

    source
        can be of many forms. all lists will be flattened.
        1. source = None (default)
            source will be searched for in cwd
        2. a list of source files or nodes.
        3. a dict with specific keys:
            source
                list of source files or nodes
            static_objs
                list of static object files or nodes
            shared_objs
                list of shared object files or nodes
            static_libs
                must be a dict with keys being the library
                and data must be a list of static objects
            shared_libs
                must be a dict with keys being the library
                and data must be a list of shared objects
    target
        will be determined after source is completely determined.
        target can be:
        1. None
            it will be derived from source in this order:
                ### TODO: have first option be the file
                ### (source or object) with main() in it
                src['source'][0]
                src['static_objs'][0]
                src['shared_objs'][0]
        2. given explicitly as a string
        3. given as an SCons File object

    ignore
        a list of files to ignore. These should be basenames
        (without a directory prefix)

    return value
        (target<string>, src<dict>)
    '''
    src = {'source': [], 'static_objs': [], 'shared_objs': []}

    build_obj_dir = build_obj_directory(env)

    if not type(source) is dict:
        src['source'] += SCons.Script.Flatten([source])
        if None in src['source']:
            src['source'] = SCons.Script.Flatten([
                env.Glob(os.path.join(build_obj_dir, '*.' + x))
                for x in _extensions['src']
            ])
    else:
        try:
            src['source'] += SCons.Script.Flatten([source['source']])
        except:
            pass
        for t in ['static', 'shared']:
            try:
                src[t + '_objs'] += SCons.Script.Flatten([source[t + '_objs']])
            except:
                pass
            try:
                for lib in source[t + '_libs']:
                    src[t + '_objs'] += SCons.Script.Flatten(
                        [source[t + '_libs'][lib]])
            except:
                pass
    ignore_files(src['source'], ignore)

    n_source = len(src['source'])
    n_obj = len(src['static_objs']) + len(src['shared_objs'])
    n_input = n_source + n_obj
    if n_input == 0:
        raise Exception(alertstr('No source files found or specified.'))

    if not target:
        for t in ['source', 'static_objs', 'shared_objs']:
            try:
                target = os.path.splitext(os.path.basename(str(src[t][0])))[0]
                break
            except:
                pass
    if not target:
        raise Exception(
            alertstr('No target specified and no sources explicitly given.'))
    if type(target) is SCons.Node.FS.File:
        target = os.path.splitext(os.path.basename(str(target)))[0]

    return (target, src)
示例#6
0
def library(env,
            target=None,
            source=None,
            ignore=None,
            headers=['.'],
            lib_type='both'):
    '''library

    creates a C/C++/FORTRAN library (static, shared, or both) from
    given source or source files found in the current directory.

    env
        The SCons.Environment
    target
        a string specifying the name of the target
        if type None (default) it will be determined
        from source files in find_target_and_source() method
    source
        list of source files (strings or SCons Nodes)
        if type None (default) SCons will Glob for all
        source-type files in the current directory.
    ignore
        list of source and header files to ignore if and only
        if source or header is not specified respectively.
    headers
        list of headers passed to install_headers method
    lib_type
        'both' (default)
        'shared'
        'static'
    '''
    pre_build(env)

    if not lib_type in ['both', 'static', 'shared']:
        raise Exception(
            alertstr('''types of libraries may only be one of:
            static, shared, both
            ("''' + lib_type + '''" was given for library "''' + target +
                     '''")'''))

    env.PrependUnique(PATH=[env.Dir('.')],
                      CPPPATH=[env.Dir('.')],
                      FORTRANPATH=[env.Dir('.')])

    target, src = find_target_and_source(env, target, source, ignore)

    build_obj_dir = build_obj_directory(env)

    if env['verbose'] > 1:
        print infostr('  reading in library:'), tgt2str(target)
        if env['verbose'] > 3:
            print infostr('    source:'),
            for t in ['source', 'static_objs', 'shared_objs']:
                try:
                    for s in src[t]:
                        print srcstr(s),
                except:
                    pass
            print ''
            print infostr('    target directory:'),
            print srcstr(build_obj_dir)

    src['source'] = [
        os.path.join(build_obj_dir, str(s)) for s in src['source']
    ]
    target = os.path.join(env['buildLibDir'], target)

    if not env['static']:
        if lib_type is 'both':
            lib_type = 'shared'
        else:
            lib_type = 'none'
    if not env['shared']:
        if lib_type is 'both':
            lib_type = 'static'
        else:
            lib_type = 'none'

    static_objs = []
    shared_objs = []

    if lib_type in ['both', 'static']:
        try:
            for s in src['source']:
                obj = env.StaticObject(s)
                if os.path.splitext(str(s))[-1] in ['.F', '.Fpp', '.fpp']:
                    fpp_incs = fpp_includes(env, env.File(s))
                    env.Depends(obj, fpp_incs)
                static_objs += obj
        except:
            pass
        try:
            static_objs += src['static_objs']
        except:
            pass
        if len(static_objs):
            static_lib = env.StaticLibrary(target, static_objs)
            env.Install(env['libDir'], static_lib)

    if lib_type in ['both', 'shared']:
        try:
            for s in src['source']:
                obj = env.SharedObject(s)
                if os.path.splitext(str(s))[-1] in ['.F', '.Fpp', '.fpp']:
                    fpp_incs = fpp_includes(env, env.File(s))
                    env.Depends(obj, fpp_incs)
                shared_objs += obj
        except:
            pass
        try:
            shared_objs += src['shared_objs']
        except:
            pass
        # WORKAROUND
        # shared libs created from shared objects
        # should not be linked to any other libraries
        # hence the LIBPATH=[], LIBS=[], RPATH=[]
        # this is might be an SCons bug
        if len(shared_objs):
            shared_lib = env.SharedLibrary(target,
                                           shared_objs,
                                           LIBPATH=[],
                                           LIBS=[],
                                           RPATH=[])
            env.Install(env['libDir'], shared_lib)

    install_headers(env, headers, ignore)

    ret = {'static_libs': {}, 'shared_libs': {}}
    if lib_type in ['both', 'static']:
        try:
            ret['static_libs'][str(static_lib[0])] = static_objs
        except:
            pass
    if lib_type in ['both', 'shared']:
        try:
            ret['shared_libs'][str(shared_lib[0])] = shared_objs
        except:
            pass
    return ret