Friizzle
Friizzle

Reputation: 19

How to conditionally change setuptools compiler?

I am building a C binding using the CFFI package. My project is being packaged for pypi so need to cover different operating systems. By default, setuptools uses the MSVC compiler for windows and the gnu gcc compiler for Linux. I would like to conditionally change the compiler to mingw32 if the operating system is windows. This is because parts of the C code base uses VLAs which MSVC does not support, and I would prefer to just change compiler instead of the whole codebase .

The only way I have been able to successfully change the compiler is with a setup.cfg file that looks like this:

[build]
compiler=mingw32

[build_ext]
compiler=mingw32

However I don't want to use setup.cfg because it is outdated, and I need to change the compiler option depending on the operating system which you can't do in a cfg file

Does anyone know how I can move this to setup.py so I can make it conditional?

Upvotes: 1

Views: 471

Answers (1)

Zenthm
Zenthm

Reputation: 372

You can set the compiler in the command-line using the option --compiler (i.e., python setup.py build --compiler).

To do this conditionally, you can override the build command class in the argument cmdclass of setuptools.setup() to set the compiler to use, like this:

from setuptools.command.build import build as _build

class build(_build):
    def finalize_options(self):
        super().finalize_options()

        if (...):
            self.compiler = "mingw32"

The setup would be:

setup(
    ...
    cmdclass={"build": build},
    ...
)

You would do the same for build_ext.

Upvotes: 2

Related Questions