def build_library(tile, libname, chip):
    """Build a static ARM cortex library"""

    dirs = chip.build_dirs()

    output_name = '%s_%s.a' % (libname, chip.arch_name())

    # Support both firmware/src and just src locations for source code
    if os.path.exists('firmware'):
        VariantDir(dirs['build'], os.path.join('firmware', 'src'), duplicate=0)
    else:
        VariantDir(dirs['build'], 'src', duplicate=0)

    library_env = setup_environment(chip)
    library_env['OUTPUT'] = output_name
    library_env['OUTPUT_PATH'] = os.path.join(dirs['build'], output_name)
    library_env['BUILD_DIR'] = dirs['build']

    # Check for any dependencies this library has
    tilebus_defs = setup_dependencies(tile, library_env)

    # Create header files for all tilebus config variables and commands that are defined in ourselves
    # or in our dependencies
    tilebus_defs += tile.find_products('tilebus_definitions')
    compile_tilebus(tilebus_defs, library_env, header_only=True)

    SConscript(os.path.join(dirs['build'], 'SConscript'), exports='library_env')

    library_env.InstallAs(os.path.join(dirs['output'], output_name), os.path.join(dirs['build'], output_name))

    # See if we should copy any files over to the output:
    for src, dst in chip.property('copy_files', []):
        srcpath = os.path.join(*src)
        destpath = os.path.join(dirs['output'], dst)
        library_env.InstallAs(destpath, srcpath)

    return os.path.join(dirs['output'], output_name)
Build a static ARM cortex library