|
| 1 | +from setuptools import setup, Extension |
| 2 | +from setuptools.command.build_ext import build_ext |
| 3 | +import sys |
| 4 | +import setuptools |
| 5 | + |
| 6 | + |
| 7 | +# As of Python 3.6, CCompiler has a `has_flag` method. |
| 8 | +# cf http://bugs.python.org/issue26689 |
| 9 | +def has_flag(compiler, flagname): |
| 10 | + """Return a boolean indicating whether a flag name is supported on |
| 11 | + the specified compiler. |
| 12 | + """ |
| 13 | + import tempfile |
| 14 | + |
| 15 | + with tempfile.NamedTemporaryFile("w", suffix=".cpp") as f: |
| 16 | + f.write("int main (int argc, char **argv) { return 0; }") |
| 17 | + try: |
| 18 | + compiler.compile([f.name], extra_postargs=[flagname]) |
| 19 | + except setuptools.distutils.errors.CompileError: |
| 20 | + return False |
| 21 | + return True |
| 22 | + |
| 23 | + |
| 24 | +def cpp_flag(compiler): |
| 25 | + """Return the -std=c++[11/14/17] compiler flag. |
| 26 | + The newer version is prefered over c++11 (when it is available). |
| 27 | + """ |
| 28 | + flags = ["-std=c++17", "-std=c++14", "-std=c++11"] |
| 29 | + |
| 30 | + for flag in flags: |
| 31 | + if has_flag(compiler, flag): |
| 32 | + return flag |
| 33 | + |
| 34 | + raise RuntimeError("Unsupported compiler -- at least C++11 support is needed!") |
| 35 | + |
| 36 | + |
| 37 | +class BuildExt(build_ext): |
| 38 | + """A custom build extension for adding compiler-specific options.""" |
| 39 | + |
| 40 | + c_opts = {"msvc": ["/EHsc"], "unix": []} |
| 41 | + l_opts = {"msvc": [], "unix": []} |
| 42 | + |
| 43 | + if sys.platform == "darwin": |
| 44 | + darwin_opts = ["-stdlib=libc++", "-mmacosx-version-min=10.7"] |
| 45 | + c_opts["unix"] += darwin_opts |
| 46 | + l_opts["unix"] += darwin_opts |
| 47 | + |
| 48 | + def build_extensions(self): |
| 49 | + ct = self.compiler.compiler_type |
| 50 | + opts = self.c_opts.get(ct, []) |
| 51 | + link_opts = self.l_opts.get(ct, []) |
| 52 | + if ct == "unix": |
| 53 | + opts.append("-s") # strip |
| 54 | + opts.append("-g0") # remove debug symbols |
| 55 | + opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version()) |
| 56 | + opts.append(cpp_flag(self.compiler)) |
| 57 | + if has_flag(self.compiler, "-fvisibility=hidden"): |
| 58 | + opts.append("-fvisibility=hidden") |
| 59 | + elif ct == "msvc": |
| 60 | + opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version()) |
| 61 | + for ext in self.extensions: |
| 62 | + ext.extra_compile_args = opts |
| 63 | + ext.extra_link_args = link_opts |
| 64 | + |
| 65 | + # self._gather_global_includes() |
| 66 | + |
| 67 | + build_ext.build_extensions(self) |
| 68 | + |
| 69 | + def run(self): |
| 70 | + |
| 71 | + # files need to be generated before building can occur |
| 72 | + self.run_command("build_gen") |
| 73 | + |
| 74 | + build_ext.run(self) |
| 75 | + |
| 76 | + |
| 77 | +# ext_modules = [ |
| 78 | +# Extension( |
| 79 | +# "python_example", |
| 80 | +# ["src/main.cpp"], |
| 81 | +# include_dirs=[ |
| 82 | +# # include dirs: iterate robotpy-build entrypoints, retrieve |
| 83 | +# ], |
| 84 | +# # library_dirs= |
| 85 | +# # libraries= |
| 86 | +# language="c++", |
| 87 | +# ) |
| 88 | +# ] |
| 89 | + |
0 commit comments