def _write_crossbuild_file(file):
    root_path = cmdutil.to_cmake_path(toolchain.sysroot)
    with open(file, 'w') as f:
        f.write('# generated by buildtool\n\n')
        f.write('SET(CMAKE_SYSTEM_NAME %s)\n\n' % toolchain.target_os_cmake)
        f.write('SET(CMAKE_C_COMPILER   %s)\n' % toolchain.tool_name('gcc'))
        f.write('SET(CMAKE_CXX_COMPILER %s)\n' % toolchain.tool_name('g++'))
        f.write('SET(CMAKE_RC_COMPILER  %s)\n\n' % toolchain.tool_name('windres'))
        f.write('SET(CMAKE_FIND_ROOT_PATH %s)\n' % root_path)
        f.write('SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)\n')
        f.write('SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)\n')
        f.write('SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)\n')
def _postprocess(files, do_strip, do_extract_dbg):
    if not (do_strip or do_extract_dbg):
        return
    strip = toolchain.tool_name('strip')
    objcopy = toolchain.tool_name('objcopy')
    result = []
    for target in files:
        dbgfile = target + ".debug"
        if do_extract_dbg:
            cmdutil.native_exec(objcopy, ['--only-keep-debug', target, dbgfile])
            result.append(dbgfile)
        if do_strip:
            cmdutil.native_exec(strip, ['--strip-all', target])
        if do_extract_dbg:
            cmdutil.native_exec(objcopy, ['--add-gnu-debuglink=' + dbgfile, target])
    return result
def _build_configure_env(user_libs, user_cflags):
    paths = []
    libs = []
    cflags = []
    pkg_config_paths = []

    for dep_name in _info.dependency_map().iterkeys():
        if dep_name == _info.name:
            continue
        p = packageinfo.get(dep_name).install_dir
        bin_dir = path.join(p, 'bin')
        lib_dir = path.join(p, 'lib')
        include_dir = path.join(p, 'include')
        pkg_config_dir = path.join(p, 'lib', 'pkgconfig')

        if path.exists(bin_dir):
            paths.append(bin_dir)
        if path.exists(lib_dir):
            libs.append('-L' + cmdutil.to_unix_path(lib_dir))
        if path.exists(include_dir):
            cflags.append('-I' + cmdutil.to_unix_path(include_dir))
        if path.exists(pkg_config_dir):
            pkg_config_paths.append(cmdutil.to_unix_path(pkg_config_dir))

    env_libs = config.get_list('libs') + config.get_list(_info.name + '-libs')
    env_cflags = config.get_list('cflags') + config.get_list(_info.name + '-cflags')

    result_path = os.pathsep.join(paths)
    result_libs = ' '.join(libs + user_libs + env_libs)
    result_cflags = ' '.join(cflags + user_cflags + env_cflags)
    result_pkg_config_path = ':'.join(pkg_config_paths)

    result = {
        'PATH'            : result_path,
        'LIBS'            : result_libs,
        'CFLAGS'          : result_cflags,
        'CXXFLAGS'        : result_cflags,
        'PKG_CONFIG_PATH' : result_pkg_config_path,
    }

    if toolchain.crossbuild:
        pkg_config_libdir = path.join(toolchain.sysroot, 'lib', 'pkgconfig')
        result.update({
            'AR'      : toolchain.tool_name('ar'),
            'AS'      : toolchain.tool_name('as'),
            'CC'      : toolchain.tool_name('gcc'),
            'CXX'     : toolchain.tool_name('g++'),
            'LD'      : toolchain.tool_name('ld'),
            'NM'      : toolchain.tool_name('nm'),
            'DLLTOOL' : toolchain.tool_name('dlltool'),
            'OBJDUMP' : toolchain.tool_name('objdump'),
            'RANLIB'  : toolchain.tool_name('ranlib'),
            'STRIP'   : toolchain.tool_name('strip'),
            'WINRC'   : toolchain.tool_name('windres'),
            'PKG_CONFIG_LIBDIR' : cmdutil.to_unix_path(pkg_config_libdir)
        })

    return result