Skip to content

Commit 04af8ac

Browse files
miss-islingtonxdegaye
authored andcommitted
bpo-32059: setup.py now also searches the sysroot paths (GH-4452) (#4562)
detect_modules() in setup.py now also searches the sysroot paths when cross-compiling. (cherry picked from commit 77f5139)
1 parent 6bce8ac commit 04af8ac

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed
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,
@@ -544,18 +569,23 @@ def detect_modules(self):
544569
add_dir_to_list(self.compiler.include_dirs,
545570
sysconfig.get_config_var("INCLUDEDIR"))
546571

572+
system_lib_dirs = ['/lib64', '/usr/lib64', '/lib', '/usr/lib']
573+
system_include_dirs = ['/usr/include']
547574
# lib_dirs and inc_dirs are used to search for files;
548575
# if a file is found in one of those directories, it can
549576
# be assumed that no additional -I,-L directives are needed.
550577
if not cross_compiling:
551-
lib_dirs = self.compiler.library_dirs + [
552-
'/lib64', '/usr/lib64',
553-
'/lib', '/usr/lib',
554-
]
555-
inc_dirs = self.compiler.include_dirs + ['/usr/include']
578+
lib_dirs = self.compiler.library_dirs + system_lib_dirs
579+
inc_dirs = self.compiler.include_dirs + system_include_dirs
556580
else:
557-
lib_dirs = self.compiler.library_dirs[:]
558-
inc_dirs = self.compiler.include_dirs[:]
581+
# Add the sysroot paths. 'sysroot' is a compiler option used to
582+
# set the logical path of the standard system headers and
583+
# libraries.
584+
lib_dirs = (self.compiler.library_dirs +
585+
sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
586+
inc_dirs = (self.compiler.include_dirs +
587+
sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
588+
system_include_dirs))
559589
exts = []
560590
missing = []
561591

0 commit comments

Comments
 (0)