[Numpy-discussion] Conditionally compiling Fortran 90 extensions using numpy.distutils

Jonathan Helmus jjhelmus at gmail.com
Fri Jun 20 11:43:44 EDT 2014


I have been working on creating a setup.py file for a sub-module with 
the aim to conditionally compile a Fortran 90 extension using F2PY 
depending on the availability of an appropriate compiler on the system.  
Basically if gfortran or another Fortran 90 compiler is available on the 
system, the extension should be built. If no compiler is available, the 
extension should not be built and the build should continue without error.

After a bit of work I was able to get a working script which uses 
numpy.distutils:

from os.path import join


def configuration(parent_package='', top_path=None):
     global config
     from numpy.distutils.misc_util import Configuration
     config = Configuration('retrieve', parent_package, top_path)
     config.add_data_dir('tests')
     # Conditionally add Steiner echo classifier extension.
     config.add_extension('echo_steiner',sources=[steiner_echo_gen_source])
     return config


def steiner_echo_gen_source(ext, build_dir):
     try:
         config.have_f90c()
         return [join(config.local_path, 'echo_steiner.pyf'),
                 join(config.local_path, 'src', 'echo_steiner.f90')]
     except:
     return None

if __name__ == '__main__':
     from numpy.distutils.core import setup
     setup(**configuration(top_path='').todict())

Is there a better way of accomplishing this conditional compiling, 
perhaps without using a global variable or a try/except block? 
Additionally is the expected behaviour of the have_90c function to raise 
an exception when a Fortran 90 compiler is not available or is this a 
bug?  From the documentation [1] it was unclear what the results should be.

I should mention that the above code snippet was aided greatly by 
information in the NumPy Packinging documentation [2], NumPy Distutils - 
Users Guide [3], and code from the f2py utils unit tests [4].

Thanks,

     - Jonathan Helmus
     nmrglue.com/jhelmus


[1] 
http://docs.scipy.org/doc/numpy/reference/distutils.html#numpy.distutils.misc_util.Configuration.have_f90c
[2] http://docs.scipy.org/doc/numpy/reference/distutils.html
[3] http://wiki.scipy.org/Wiki/Documentation/numpy_distutils
[4] https://github.com/numpy/numpy/blob/master/numpy/f2py/tests/util.py



More information about the NumPy-Discussion mailing list