8000 Probe for compiler flags required to compile C99 source code · pysam-developers/pysam@313b5fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 313b5fd

Browse files
committed
Probe for compiler flags required to compile C99 source code
Add a configure-style test to probe for -std=c99 etc flags that elderly compilers may need to enable compilation of C99 code. If such a flag is needed, add it to the compiler executables used to build extension modules. Together with the SIMD intrinsics configure backport, this fixes #1327.
1 parent f0ed4ec commit 313b5fd

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

pysam/conftest_cstd.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <string.h>
2+
#include <unistd.h>
3+
4+
// C++-style comment; for-decl; optind
5+
6+
int main(int argc, char **argv) {
7+
int sum = 0;
8+
for (int i = optind; i < argc; i++)
9+
sum += strlen(argv[i]);
10+
return sum;
11+
}

setup.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
from setuptools.extension import Extension
2929

3030
try:
31-
from setuptools.errors import LinkError
31+
from setuptools.errors import CompileError, LinkError
3232
except ImportError:
33-
from distutils.errors import LinkError
33+
from distutils.errors import CompileError, LinkError
3434

3535
try:
3636
from Cython.Distutils import build_ext
@@ -272,6 +272,22 @@ def check_ext_symbol_conflicts(self):
272272

273273
if errors > 0: raise LinkError("symbols defined in multiple extensions")
274274

275+
def c99_compile_args(self):
276+
"""Determines whether any compiler flags are needed to ensure C99 compilation."""
277+
compiler = getattr(self.compiler, "compiler", "C compiler")
278+
if isinstance(compiler, list): compiler = compiler[0]
279+
log.info("checking for %s option to enable C99 features...", compiler)
280+
for flags in [None, ["-std=c99"], ["-std=gnu99"]]:
281+
try:
282+
self.compiler.compile(["pysam/conftest_cstd.c"], output_dir=self.build_temp, extra_preargs=flags)
283+
log.info("%s option to enable C99 features: %s", compiler, " ".join(flags) if flags else "none needed")
284+
return flags
285+
except CompileError:
286+
log.info("(ignoring errors from test probes)")
287+
288+
log.error("%s cannot compile C99 source code", compiler)
289+
return None
290+
275291
def run(self):
276292
if sys.platform == 'darwin':
277293
ldshared = os.environ.get('LDSHARED', sysconfig.get_config_var('LDSHARED'))
@@ -286,6 +302,19 @@ def run(self):
286302
except subprocess.CalledProcessError:
287303
log.warning("skipping symbol collision check (invoking nm failed)")
288304

305+
def build_extensions(self):
306+
c99_flags = self.c99_compile_args()
307+
if c99_flags:
308+
executables = {}
309+
for executable in ["compiler", "compiler_so"]:
310+
command = getattr(self.compiler, executable, None)
311+
if command:
312+
if isinstance(command, list): executables[executable] = command + c99_flags
313+
elif isinstance(command, str): executables[executable] = f"{command} {' '.join(c99_flags)}"
314+
self.compiler.set_executables(**executables)
315+
316+
super().build_extensions()
317+
289318
def build_extension(self, ext):
290319

291320
if isinstance(ext, CyExtension) and ext._init_func:

0 commit comments

Comments
 (0)
0