Python package: how to know if compiler will support argument?

  cython, python, setuptools

Suppose I am building a python package which has a Cython extension that needs compilation, and this package is to be installed using setuptools. I can usually add extra compilation arguments by sub-classing Cython’s build_ext and overriding the build_extension method – example

from setuptools import setup
from setuptools.extension import Extension
from Cython.Distutils import build_ext

class build_ext_subclass(build_ext):
    def build_extensions(self):
        for ext in self.extensions:


    cmdclass = {'build_ext': build_ext_subclass}

And this works fine, provided that the compiler supports the new -my-arg argument.

But suppose now that I want to add something non-standard – for example, I’d like to add either -march=native or -mcpu=native, depending on which one is supported by the compiler.

How could I programatically determine if the compiler will support a given argument? Is it good practice or can something bad happen if for example the script has more than one call to setup in a try/except or if build_extension is called multiple times in a try/except block?

Source: Python Questions