clike compilers: Add default sysroot include directory

When building against a sysroot, meson currently fails to detect that
the /usr/include directory within that sysroot is a default search
directory. This causes the directory to be added with -isystem to the
compiler command-line which in turn causes include lookup failures.

A minimal test case to reproduce this behaviour is:

  $ echo -e "#include <iostream>\nint main() { return 0; }" > foo.cpp
  $ g++ --sysroot / -isystem /usr/include -o foo foo.cpp
  In file included from /usr/include/c++/11.1.0/ext/string_conversions.h:41,
                   from /usr/include/c++/11.1.0/bits/basic_string.h:6594,
                   from /usr/include/c++/11.1.0/string:55,
                   from /usr/include/c++/11.1.0/bits/locale_classes.h:40,
                   from /usr/include/c++/11.1.0/bits/ios_base.h:41,
                   from /usr/include/c++/11.1.0/ios:42,
                   from /usr/include/c++/11.1.0/ostream:38,
                   from /usr/include/c++/11.1.0/iostream:39,
                   from foo.cpp:1:
  /usr/include/c++/11.1.0/cstdlib:75:15: fatal error: stdlib.h: No such file or directory
     75 | #include_next <stdlib.h>
      |               ^~~~~~~~~~
  compilation terminated.

Omitting the -isystem /usr/include option avoids that error because it
doesn't mess up the include directory search order.

Fix this by adding the sysroot's /usr/include directory to the list of
default search directories when a sysroot is detected. This ensures that
any occurrences of it are filtered out and the include directory search
order is left alone.
pull/9993/head
Thierry Reding 3 years ago
parent 9e4feed91a
commit 291dbef659
  1. 18
      mesonbuild/compilers/mixins/clike.py

@ -90,6 +90,24 @@ class CLikeCompilerArgs(arglist.CompilerArgs):
new.insert(group_start, '-Wl,--start-group')
# Remove system/default include paths added with -isystem
default_dirs = self.compiler.get_default_include_dirs()
sys_root = None
for i, each in enumerate(new):
if each.startswith('--sysroot'):
if each == '--sysroot':
try:
sys_root = new[i + 1]
except IndexError:
mlog.warning('--sysroot passed to', self.compiler.language, 'compiler without argument')
break
if each.startswith('--sysroot='):
sys_root = each[10:]
break
if sys_root:
default_dirs.append(os.path.join(sys_root, 'usr', 'include'))
if default_dirs:
real_default_dirs = [self._cached_realpath(i) for i in default_dirs]
bad_idx_list: T.List[int] = []

Loading…
Cancel
Save