[clang] [MinGW] Avoid adding <base>/include and <base>/lib when cross compiling
authorMartin Storsjö <martin@martin.st>
Tue, 22 Nov 2022 14:12:39 +0000 (16:12 +0200)
committerMartin Storsjö <martin@martin.st>
Mon, 16 Jan 2023 22:39:12 +0000 (00:39 +0200)
commitfd15cb935d7aae25ad62bfe06fe9f17cea585978
tree3fd62aa4a58001c9ac892a1099484fc0a88e3323
parenta288d7f937708cf67d960962bfa22ffae37ddbf4
[clang] [MinGW] Avoid adding <base>/include and <base>/lib when cross compiling

The MinGW compiler driver first tries to deduce the root of
the toolchain installation (either clang itself or a separate
cross mingw gcc installation). On top of this root, a number
of include and lib paths are added (some added unconditionally,
some only if they exist):
- <base>/x86_64-w64-mingw32/include
- <base>/include
- <base>/include/x86_64-w64-windows-gnu
(Some more are also added for libstdc++ and/or libc++.)

The first one is the one commonly used for MinGW targets so
far. For LLVM runtimes installed with the
LLVM_ENABLE_PER_TARGET_RUNTIME_DIR option, the latter two are
used though (this is currently not the default, not yet at least).

For cross compiling, if base is a separate dedicated directory,
this is fine, but when using the sysroots of a distro-installed
cross mingw toolchain, base is /usr - and having /usr/include
in the include path for cross compilation is a potential
source for problems; see
https://github.com/llvm/llvm-project/issues/59871.

If not cross compiling though, <base>/include needs to be included
too. E.g. in the case of msys2, most headers are in e.g.
/mingw64/include while the compiler is /mingw64/bin/clang.

When cross compiling, if the sysroot has been explicitly set
by the user, keep <base>/include too. (In the case of a distro
provided cross gcc toolchain in /usr, the sysroot needs to be set
to /usr and not /usr/x86_64-w64-mingw32 though, to be able to find
libgcc files under /usr/lib/gcc/x86_64-w64-mingw32. So with such a
toolchain, setting the sysroot explicitly does retain the problem.)

All in all - this avoids adding /usr/include and /usr/lib to the
include/lib paths when doing mingw cross compilation with a
distro-provided sysroot in /usr/x86_64-w64-mingw32.

Test that the include directory is omitted in the mingw-sysroot.cpp
tests, when cross compiling. That test is only ever executed on
non-Windows hosts, since it uses symlinks to set up fake environments
with colocated compilers and header/lib directories. There aren't
really any current corresponding tests for the same implicit behaviours
when actually running on Windows.

Differential Revision: https://reviews.llvm.org/D141206
clang/lib/Driver/ToolChains/MinGW.cpp
clang/test/Driver/mingw-sysroot.cpp