Skip to content

Commit 77f5139

Browse files
authored
bpo-32059: setup.py now also searches the sysroot paths (GH-4452)
detect_modules() in setup.py now also searches the sysroot paths when cross-compiling.
1 parent cfa797c commit 77f5139

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``detect_modules()`` in ``setup.py`` now also searches the sysroot paths
2+
when cross-compiling.

setup.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
6388
def 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

Comments
 (0)