@@ -364,6 +364,57 @@ def find_module_file(module, dirlist):
364364 return os .path .join (dirs [0 ], module )
365365
366366
367+ def parse_cflags (flags ):
368+ """Parse a string with compiler flags (-I, -D, -U, extra)
369+
370+ Distutils appends extra args to the compiler arguments. Some flags like
371+ -I must appear earlier. Otherwise the pre-processor picks up files
372+ from system inclue directories.
373+ """
374+ include_dirs = []
375+ define_macros = []
376+ undef_macros = []
377+ extra_compile_args = []
378+ if flags is not None :
379+ # shlex.split(None) reads from stdin
380+ for token in shlex .split (flags ):
381+ switch = token [0 :2 ]
382+ value = token [2 :]
383+ if switch == '-I' :
384+ include_dirs .append (value )
385+ elif switch == '-D' :
386+ key , _ , val = value .partition ("=" )
387+ if not val :
388+ val = None
389+ define_macros .append ((key , val ))
390+ elif switch == '-U' :
391+ undef_macros .append (value )
392+ else :
393+ extra_compile_args .append (token )
394+
395+ return include_dirs , define_macros , undef_macros , extra_compile_args
396+
397+
398+ def parse_ldflags (flags ):
399+ """Parse a string with linker flags (-L, -l, extra)"""
400+ library_dirs = []
401+ libraries = []
402+ extra_link_args = []
403+ if flags is not None :
404+ # shlex.split(None) reads from stdin
405+ for token in shlex .split (flags ):
406+ switch = token [0 :2 ]
407+ value = token [2 :]
408+ if switch == '-L' :
409+ library_dirs .append (value )
410+ elif switch == '-l' :
411+ libraries .append (value )
412+ else :
413+ extra_link_args .append (token )
414+
415+ return library_dirs , libraries , extra_link_args
416+
417+
367418class PyBuildExt (build_ext ):
368419
369420 def __init__ (self , dist ):
@@ -1469,30 +1520,39 @@ def detect_expat_elementtree(self):
14691520 #
14701521 # More information on Expat can be found at www.libexpat.org.
14711522 #
1472- cflags = sysconfig .get_config_var ("EXPAT_CFLAGS" )
1473- extra_compile_args = shlex . split ( cflags ) if cflags else None
1523+ cflags = parse_cflags ( sysconfig .get_config_var ("EXPAT_CFLAGS" ) )
1524+ include_dirs , define_macros , undef_macros , extra_compile_args = cflags
14741525 # ldflags includes either system libexpat or full path to
14751526 # our static libexpat.a.
1476- ldflags = sysconfig .get_config_var ("EXPAT_LDFLAGS" )
1477- extra_link_args = shlex . split ( ldflags ) if ldflags else None
1527+ ldflags = parse_ldflags ( sysconfig .get_config_var ("EXPAT_LDFLAGS" ) )
1528+ library_dirs , libraries , extra_link_args = ldflags
14781529
14791530 expat_depends = []
14801531 libexpat_a = sysconfig .get_config_var ("LIBEXPAT_A" )
14811532 if libexpat_a :
14821533 expat_depends .append (libexpat_a )
14831534
14841535 self .add (Extension ('pyexpat' ,
1536+ include_dirs = include_dirs ,
1537+ define_macros = define_macros ,
1538+ undef_macros = undef_macros ,
14851539 extra_compile_args = extra_compile_args ,
1540+ library_dirs = library_dirs ,
1541+ libraries = libraries ,
14861542 extra_link_args = extra_link_args ,
14871543 sources = ['pyexpat.c' ],
14881544 depends = expat_depends ))
14891545
14901546 # Fredrik Lundh's cElementTree module. Note that this also
14911547 # uses expat (via the CAPI hook in pyexpat).
14921548 self .add (Extension ('_elementtree' ,
1493- extra_compile_args = extra_compile_args ,
1494- sources = ['_elementtree.c' ],
1495- depends = ['pyexpat.c' , * expat_depends ]))
1549+ include_dirs = include_dirs ,
1550+ define_macros = define_macros ,
1551+ undef_macros = undef_macros ,
1552+ extra_compile_args = extra_compile_args ,
1553+ # no EXPAT_LDFLAGS
1554+ sources = ['_elementtree.c' ],
1555+ depends = ['pyexpat.c' , * expat_depends ]))
14961556
14971557 def detect_multibytecodecs (self ):
14981558 # Hye-Shik Chang's CJKCodecs modules.
@@ -1990,24 +2050,27 @@ def detect_decimal(self):
19902050 # Stefan Krah's _decimal module
19912051 sources = ['_decimal/_decimal.c' ]
19922052 depends = ['_decimal/docstrings.h' ]
1993- define_macros = []
1994-
1995- cflags = sysconfig .get_config_var ("DECIMAL_CFLAGS" )
1996- extra_compile_args = shlex .split (cflags ) if cflags else None
2053+
2054+ cflags = parse_cflags (sysconfig .get_config_var ("DECIMAL_CFLAGS" ))
2055+ include_dirs , define_macros , undef_macros , extra_compile_args = cflags
19972056 # ldflags includes either system libmpdec or full path to
19982057 # our static libmpdec.a.
1999- ldflags = sysconfig .get_config_var ("DECIMAL_LDFLAGS" )
2000- extra_link_args = shlex . split ( ldflags ) if ldflags else None
2001-
2058+ ldflags = parse_ldflags ( sysconfig .get_config_var ("DECIMAL_LDFLAGS" ) )
2059+ library_dirs , libraries , extra_link_args = ldflags
2060+
20022061 libmpdec_a = sysconfig .get_config_var ("LIBMPDEC_A" )
20032062 if libmpdec_a :
20042063 depends .append (libmpdec_a )
20052064
20062065 # Uncomment for extra functionality:
20072066 #define_macros.append(('EXTRA_FUNCTIONALITY', 1))
20082067 self .add (Extension ('_decimal' ,
2068+ include_dirs = include_dirs ,
20092069 define_macros = define_macros ,
2070+ undef_macros = undef_macros ,
20102071 extra_compile_args = extra_compile_args ,
2072+ library_dirs = library_dirs ,
2073+ libraries = libraries ,
20112074 extra_link_args = extra_link_args ,
20122075 sources = sources ,
20132076 depends = depends ))
0 commit comments