[Numpy-discussion] how to compile Fortran using setup.py

Ondrej Certik ondrej@certik...
Thu Mar 10 19:58:36 CST 2011


Hi,

I spent about an hour googling and didn't figure this out. Here is my setup.py:

setup(
    name = "libqsnake",
    cmdclass = {'build_ext': build_ext},
    version = "0.1",
    packages = [
        'qsnake',
        'qsnake.calculators',
        'qsnake.calculators.tests',
        'qsnake.data',
        'qsnake.mesh2d',
        'qsnake.tests',
        ],
    package_data = {
        'qsnake.tests': ['phaml_data/domain.*'],
        },
    include_dirs=[numpy.get_include()],
    ext_modules = [Extension("qsnake.cmesh", [
        "qsnake/cmesh.pyx",
        "qsnake/fmesh.f90",
        ])],
    description = "Qsnake standard library",
    license = "BSD",
)

The qsnake.cmesh extension needs to compile .pyx into .c and later to
.o, it needs to use gfortran to compile fmesh.f90 to fmesh.o, and then
link both things. That's it. In other words, this is what I want
distutils to do:

$ cython cmesh.pyx
$ gcc -fPIC -o cmesh.o -c cmesh.c -I$SPKG_LOCAL/include/python2.6
-I$SPKG_LOCAL/lib/python2.6/site-packages/numpy/core/include
$ gfortran -fPIC -o fmesh.o -c fmesh.f90
$ gcc -shared -o cmesh.so cmesh.o fmesh.o


If I import the relevant commands above by:

from numpy.distutils.core import setup
from numpy.distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy

then it gives me:

running install
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands
--compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands
--fcompiler options
running build_src
build_src
building extension "qsnake.cmesh" sources
f2py options: []
f2py:> build/src.linux-x86_64-2.6/qsnake/cmeshmodule.c
creating build
creating build/src.linux-x86_64-2.6
creating build/src.linux-x86_64-2.6/qsnake
Reading fortran codes...
	Reading file 'qsnake/fmesh.f90' (format:free)
Traceback (most recent call last):
  File "setup.py", line 29, in <module>
    license = "BSD",
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/distutils/core.py",
line 186, in setup
    return old_setup(**new_attr)
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/core.py",
line 152, in setup
    dist.run_commands()
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/dist.py",
line 975, in run_commands
    self.run_command(cmd)
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/dist.py",
line 995, in run_command
    cmd_obj.run()
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/distutils/command/install.py",
line 55, in run
    r = old_install.run(self)
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/command/install.py",
line 577, in run
    self.run_command('build')
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/cmd.py",
line 333, in run_command
    self.distribution.run_command(command)
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/dist.py",
line 995, in run_command
    cmd_obj.run()
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/distutils/command/build.py",
line 37, in run
    old_build.run(self)
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/command/build.py",
line 134, in run
    self.run_command(cmd_name)
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/cmd.py",
line 333, in run_command
    self.distribution.run_command(command)
  File "/home/certik1/repos/qsnake/local/lib/python/distutils/dist.py",
line 995, in run_command
    cmd_obj.run()
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/distutils/command/build_src.py",
line 152, in run
    self.build_sources()
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/distutils/command/build_src.py",
line 169, in build_sources
    self.build_extension_sources(ext)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/distutils/command/build_src.py",
line 334, in build_extension_sources
    sources = self.f2py_sources(sources, ext)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/distutils/command/build_src.py",
line 593, in f2py_sources
    ['-m',ext_name]+f_sources)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/f2py/f2py2e.py",
line 342, in run_main
    postlist=callcrackfortran(files,options)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/f2py/f2py2e.py",
line 276, in callcrackfortran
    postlist=crackfortran.crackfortran(files)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/f2py/crackfortran.py",
line 2683, in crackfortran
    readfortrancode(files,crackline)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/f2py/crackfortran.py",
line 420, in readfortrancode
    dowithline(finalline)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/f2py/crackfortran.py",
line 624, in crackline
    analyzeline(m,pat[1],line)
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/f2py/crackfortran.py",
line 755, in analyzeline
    if args: args=rmbadname([x.strip() for x in
markoutercomma(args).split('@,@')])
  File "/home/certik1/repos/qsnake/local/lib/python2.6/site-packages/numpy/f2py/crackfortran.py",
line 665, in markoutercomma
    assert not f,`f,line,l,cc`
AssertionError: (1, 'r_min, r_max, a, n, mesh@)@ bind@(@c', 'r_min@,@
r_max@,@ a@,@ n@,@ mesh@)@ bind@(@c', ')')





And it seems it's trying to use f2py. I don't want to use f2py, just
use regular gfortran to compile .f90 to .o, as shown above.

Does anybody know how to do it?

Thanks,
Ondrej


More information about the NumPy-Discussion mailing list