【问题标题】:How to do an incremental build with distutils in python 2.7?如何在 python 2.7 中使用 distutils 进行增量构建?
【发布时间】:2012-08-15 21:16:39
【问题描述】:

我正在使用 Python 2.7 distutils 来构建 C++ 库。但是,每次我使用

发出构建命令时
python setup.py build

所有目标文件都会重新构建,即使 c++ 文件没有改变。
我的一个朋友告诉我,Python 2.6 不会出现这种情况。

我对这个板的问题:

  1. 有没有办法强制 distutils 以增量方式构建代码?
  2. 如果无法增量构建代码,
    2.1。有没有办法使用 Python 2.6 distutils 或
    2.2.是否可以更改 Python 2.7 distuils 包?

【问题讨论】:

  • 你确定它正在重建吗? python setup.py --help build 表明有一个 -f 选项可以“强制构建(忽略时间戳)”,如果这是默认行为,它似乎不应该存在......
  • @mgilson 它正在大胆地重建一切。也许这是setup.py 脚本本身的一些设置?虽然,如果它在那里,我找不到它......

标签: python build python-2.7 distutils python-2.6


【解决方案1】:

你不能那样做。编译后的 .o 文件以简单且错误的方式重用,因此在 2.7 中删除了此优化。详情请见http://bugs.python.org/issue5372

【讨论】:

  • 感谢这个答案!但我更多地关注以下方向:如何调整 python 2.7 distutils 以覆盖默认重建所有 .o 文件的新“非错误”方式。是否有我可以通过的选项标志,或者我是否必须深入研究代码以使代码不会重新编译所有代码文件以创建新的 .o 文件?
  • 好吧,我的意思是告诉你不能这样做,你不应该尝试,因为我提到的问题。
【解决方案2】:

我不知道这是否能解决您的确切问题,但我有一个类似的问题,我通过以下方法解决了:

我有一个相对较大的 C++ 包,其中包含基于 cython 的 python 包装器。我最终做的是使用 CMake(它进行增量构建)将包编译为静态库,然后使用 cython 包装器在静态库中链接——尽管为了安全起见,它每次都重新编译,但它非常稳定。我的 setup.py 被黑是为了接受一些告诉 CMake 做什么的标志。

我的 setup.py 中处理 CMake 部分 (https://github.com/CoolProp/CoolProp/blob/master/wrappers/Python/setup.py) 的相关 sn-p 如下所示:

# ******************************
#       CMAKE OPTIONS
# ******************************

# Example using CMake to build static library:
# python setup.py install --cmake-compiler vc9 --cmake-bitness 64

if '--cmake-compiler' in sys.argv:
    i = sys.argv.index('--cmake-compiler')
    sys.argv.pop(i)
    cmake_compiler = sys.argv.pop(i)
else:
    cmake_compiler = ''

if '--cmake-bitness' in sys.argv:
    i = sys.argv.index('--cmake-bitness')
    sys.argv.pop(i)
    cmake_bitness = sys.argv.pop(i)
else:
    cmake_bitness = ''

USING_CMAKE = cmake_compiler or cmake_bitness

cmake_config_args = []
cmake_build_args = ['--config','"Release"']
STATIC_LIBRARY_BUILT = False
if USING_CMAKE:

    # Always force build since any changes in the C++ files will not force a rebuild
    touch('CoolProp/CoolProp.pyx')

    if 'clean' in sys.argv:
        if os.path.exists('cmake_build'):
            print('removing cmake_build folder...')
            shutil.rmtree('cmake_build')
            print('removed.')

    if cmake_compiler == 'vc9':
        if cmake_bitness == '32':
            generator = ['-G','"Visual Studio 9 2008"']
        elif cmake_bitness == '64':
            generator = ['-G','"Visual Studio 9 2008 Win64"']
        else:
            raise ValueError('cmake_bitness must be either 32 or 64; got ' + cmake_bitness)
    elif cmake_compiler == 'vc10':
        if cmake_bitness == '32':
            generator = ['-G','"Visual Studio 10 2010"']
        elif cmake_bitness == '64':
            generator = ['-G','"Visual Studio 10 2010 Win64"']
        else:
            raise ValueError('cmake_bitness must be either 32 or 64; got ' + cmake_bitness)
    else:
        raise ValueError('cmake_compiler [' + cmake_compiler + '] is invalid')

    cmake_build_dir = os.path.join('cmake_build', '{compiler}-{bitness}bit'.format(compiler=cmake_compiler, bitness=cmake_bitness))
    if not os.path.exists(cmake_build_dir):
        os.makedirs(cmake_build_dir)
    subprocess.check_call(' '.join(['cmake','../../../..','-DCOOLPROP_STATIC_LIBRARY=ON']+generator+cmake_config_args), shell = True, stdout = sys.stdout, stderr = sys.stderr, cwd = cmake_build_dir)
    subprocess.check_call(' '.join(['cmake','--build', '.']+cmake_build_args), shell = True, stdout = sys.stdout, stderr = sys.stderr, cwd = cmake_build_dir)

    # Now find the static library that we just built
    if sys.platform == 'win32':
        static_libs = []
        for search_suffix in ['Release/*.lib','Release/*.a', 'Debug/*.lib', 'Debug/*.a']:
            static_libs += glob.glob(os.path.join(cmake_build_dir,search_suffix))

    if len(static_libs) != 1:
        raise ValueError("Found more than one static library using CMake build.  Found: "+str(static_libs))
    else:
        STATIC_LIBRARY_BUILT = True
        static_library_path = os.path.dirname(static_libs[0])

【讨论】:

    猜你喜欢
    • 2015-01-02
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多