Adding new source types to distutils?

Devin Jeanpierre jeanpierreda at gmail.com
Sun Apr 28 13:55:47 EDT 2013


Last night I wrote a toy prototype module that lets one compile Rust
crates into extension modules for Python. The problem is, I don't know
the "right" way to do this. Ideally I'd just want to tell build_ext
that there's a new source type I want to handle (.rc and .rs), and
also tell distutils that it should handle it by running the code that
I specify (which can compile the .rs/.rc files, remove them from the
sources list, and add the resulting object files and such to the
linker arguments)

The problem is that, as I understand it, the way to do this is
subclassing and then replacing the build_ext command. At least, that's
what Cython does. The problem is, that's what Cython does, so if I do
that, it means you can't use Cython and Rust together -- that's bad,
because Cython would be useful for writing bindings to Rust crates,
too. So I don't want to write my own subclass. In place of that, I
don't know what the right approach is.

One possibility is that I subclass Cython's build_ext if it exists,
otherwise distutils'. This seems like it's a terrible thing to do,
since it locks out any Cython alternative that I may not be aware of,
and any other kind of extension to build_ext. I don't know what else I
can do.

(What I ended up doing, just so that I could actually write the code,
was write a new Extension type that compiles the rust code when the
extra_link_args attribute is accessed. This is, of course, absolutely
terrible, and only barely does the job. It's not as bad as what I had
to do to get the linker arguments from rustc, though...)

-- Devin



More information about the Python-list mailing list