Swig and distutils

Оlе Ѕtrеісhеr ole-usenet-spam at gmx.net
Tue Aug 29 02:56:18 EDT 2017


Hi,

I want to write a setup.py for a module from a swig source. The
recommended way [1] does not work because it does not process the
generated Python file. Searching on Stackoverflow gives an (ugly)
workaround and the hint "this may be considered as a bug" [2].

However, I couldn't find a distutils bug or a discussion why this is not
changed (the problems seems to be already >5 years old). Is there a
rationale behind the current behauviour? And shouldn't that documented
in the Python distutils documentation? [1] is very misleading here then.

I have another problem with the current setup: I am using a directory
structure where setup.py resides outside of the current sources (since
it is generated by a CMakeFile) in the build directory. That means that
I have to use the "package_dir" argument of setup.py. This however has
two drawbacks:

1. It does not apply to ext_modules: how can I do that?

2. The Python code generated by swig will/should be placed in the build
dir (since it is generated), so they are not found when the package_dir
is set to the source directory. When I set package_dir to the build
directory, then the other Python source files are not found. How can I
specify both directories here?

One workaround would be to just call the setup function twice: First
with the existing Python source and the swig C sources, and then with
the Python sources generated from swig:

---------------------8<--------------------------------
from distutils.core import setup, Extension

setup(name="my", version="1.0",
      package_dir={"my": "../../src"}
      py_modules=["my"],
      ext_modules=[
          Extension("_my_c", ["my_c.i"]) # specify source dir of my_c.i here?
      ])

setup(name="my", version="1.0", py_modules=["my_c"])
---------------------8<--------------------------------

Although I didn't find any documentation that forbids calling the setup
function twice, I also did not find an example where this is done. Would
that work?

Best regards

Ole

[1] https://docs.python.org/3.6/distutils/setupscript.html#extension-source-files
[2] https://stackoverflow.com/questions/17666018/using-distutils-where-swig-interface-file-is-in-src-folder?rq=1



More information about the Python-list mailing list