@@ -60,6 +60,31 @@ def add_dir_to_list(dirlist, dir):
6060 return
6161 dirlist .insert (0 , dir )
6262
63+ def sysroot_paths (make_vars , subdirs ):
64+ """Get the paths of sysroot sub-directories.
65+
66+ * make_vars: a sequence of names of variables of the Makefile where
67+ sysroot may be set.
68+ * subdirs: a sequence of names of subdirectories used as the location for
69+ headers or libraries.
70+ """
71+
72+ dirs = []
73+ for var_name in make_vars :
74+ var = sysconfig .get_config_var (var_name )
75+ if var is not None :
76+ m = re .search (r'--sysroot=([^"]\S*|"[^"]+")' , var )
77+ if m is not None :
78+ sysroot = m .group (1 ).strip ('"' )
79+ for subdir in subdirs :
80+ if os .path .isabs (subdir ):
81+ subdir = subdir [1 :]
82+ path = os .path .join (sysroot , subdir )
83+ if os .path .isdir (path ):
84+ dirs .append (path )
85+ break
86+ return dirs
87+
6388def macosx_sdk_root ():
6489 """
6590 Return the directory of the current OSX SDK,
@@ -559,18 +584,23 @@ def detect_modules(self):
559584 add_dir_to_list (self .compiler .include_dirs ,
560585 sysconfig .get_config_var ("INCLUDEDIR" ))
561586
587+ system_lib_dirs = ['/lib64' , '/usr/lib64' , '/lib' , '/usr/lib' ]
588+ system_include_dirs = ['/usr/include' ]
562589 # lib_dirs and inc_dirs are used to search for files;
563590 # if a file is found in one of those directories, it can
564591 # be assumed that no additional -I,-L directives are needed.
565592 if not cross_compiling :
566- lib_dirs = self .compiler .library_dirs + [
567- '/lib64' , '/usr/lib64' ,
568- '/lib' , '/usr/lib' ,
569- ]
570- inc_dirs = self .compiler .include_dirs + ['/usr/include' ]
593+ lib_dirs = self .compiler .library_dirs + system_lib_dirs
594+ inc_dirs = self .compiler .include_dirs + system_include_dirs
571595 else :
572- lib_dirs = self .compiler .library_dirs [:]
573- inc_dirs = self .compiler .include_dirs [:]
596+ # Add the sysroot paths. 'sysroot' is a compiler option used to
597+ # set the logical path of the standard system headers and
598+ # libraries.
599+ lib_dirs = (self .compiler .library_dirs +
600+ sysroot_paths (('LDFLAGS' , 'CC' ), system_lib_dirs ))
601+ inc_dirs = (self .compiler .include_dirs +
602+ sysroot_paths (('CPPFLAGS' , 'CFLAGS' , 'CC' ),
603+ system_include_dirs ))
574604 exts = []
575605 missing = []
576606
0 commit comments