我目前正在开发一个导入 Fortran 模块的 Python 项目。这setup.py
看起来与那个类似
from numpy.distutils.core import Extension
from numpy.distutils.core import setup
ext = Extension(
name = "fortran_module",
sources = ["fortran_module.f90"],
extra_f90_compile_args = ["-some -compile -arguments"]
)
setup(
...,
...,
...,
ext_modules = [ext],
...,
...,
...
)
我目前正在安装它pip install -e .
,效果很好。
但有时我需要改变extra_f90_compile_args
我想在安装过程中将它们作为命令行参数pip
而不是改变setup.py
文件。例如这样的事情:
pip install -e --compile_args="-O3 -fbacktrace -fbounds-check -fopenmp" .
是否可以通过传递命令行参数pip
进入setup.py
file?
与此相关,我还想将不同的设置选项传递给setup.py
。例如
pip install -e --setup=setup1 .
or
pip install -e --setup=setup2 .
并取决于是否setup1
or setup2
或者它们都没有通过,我想包含不同的 Fortran 源文件并使用不同的编译它们extra_f90_compile_args
.
这可能吗?
Edit:让我们考虑以下示例:Fortran 模块与 OpenMP 并行。现在我希望用户决定是否并行编译。也许OpenMP库不可用,用户需要编译串行版本。
My setup.py
现在看起来如下
from numpy.distutils.core import Extension
from numpy.distutils.core import setup
import os
from setuptools.command.install import install as _install
extra_f90_compile_args = ""
extra_link_args = ""
class install(_install):
user_options = _install.user_options + [('build=', None, None)]
def initialize_options(self):
_install.initialize_options(self)
self.build = None
def finalize_options(self):
_install.finalize_options(self)
def run(self):
global extra_f90_compile_args
global extra_link_args
if(self.build == "parallel"):
extra_f90_compile_args += "-fopenmp"
extra_link_args += "-lgomp"
os.makedirs("~/test/")
_install.run(self)
ext = Extension(
name="test_module.fortran_module",
sources=["test_module/fortran_module.f90"],
extra_f90_compile_args=[extra_f90_compile_args],
extra_link_args=[extra_link_args]
)
setup(
name="test_module",
packages=["test_module"],
ext_modules=[ext],
cmdclass={'install': install}
)
如果我安装这个
pip install . --install-option="--build=parallel"
它正在执行代码if
-堵塞。我创建了test/
目录只是为了检查这个。如果build
没有给出或不同于parallel
, the test/
未创建目录。
但是,该代码不是使用 OpenMP 编译的。我认为这是因为Extension
对象是在调用之前创建的setup()
,其中对参数进行求值。我想首先评估参数,然后创建Extension
对象取决于参数,然后调用setup()
.
我怎样才能做到这一点?