Skip to content

[Clang] Prioritise built-in headers, even on musl. #8862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/lib/Basic/Targets/OSTargets.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public OSTargetInfo<Target> {
} else {
Builder.defineMacro("__gnu_linux__");
}

if (Triple.isMusl())
Builder.defineMacro("__musl__", "1");
if (Opts.POSIXThreads)
Builder.defineMacro("_REENTRANT");
if (Opts.CPlusPlus)
Expand Down
24 changes: 14 additions & 10 deletions clang/lib/Driver/ToolChains/Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,21 @@ void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,

// Add 'include' in the resource directory, which is similar to
// GCC_INCLUDE_DIR (private headers) in GCC. Note: the include directory
// contains some files conflicting with system /usr/include. musl systems
// prefer the /usr/include copies which are more relevant.
SmallString<128> ResourceDirInclude(D.ResourceDir);
llvm::sys::path::append(ResourceDirInclude, "include");
if (!DriverArgs.hasArg(options::OPT_nobuiltininc) &&
(!getTriple().isMusl() || DriverArgs.hasArg(options::OPT_nostdlibinc)))
// contains some files conflicting with system /usr/include.
//
// Note: the include order used to be different on musl systems because
// of the expectation that that users of that C library would use the
// C library's copies of the "built-in" headers. This was a mistake;
// it's better to adapt the built-in headers to fall through in that case
// (using #include_next), since otherwise if they get pulled in through
// some other mechanism, __has_include_next(<header>) will fail and then
// they'll try to redefine things, which causes errors.
if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
SmallString<128> ResourceDirInclude(D.ResourceDir);
llvm::sys::path::append(ResourceDirInclude, "include");
addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);

}

if (DriverArgs.hasArg(options::OPT_nostdlibinc))
return;

Expand Down Expand Up @@ -677,9 +684,6 @@ void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/include"));

addExternCSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/include"));

if (!DriverArgs.hasArg(options::OPT_nobuiltininc) && getTriple().isMusl())
addSystemInclude(DriverArgs, CC1Args, ResourceDirInclude);
}

void Linux::addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
Expand Down
10 changes: 3 additions & 7 deletions clang/lib/Headers/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
#include_next <float.h>
#else

/* If we're on MinGW, fall back to the system's float.h, which might have
* additional definitions provided for Windows.
* For more details see http://msdn.microsoft.com/en-us/library/y0ybw9fy.aspx
*
* Also fall back on Darwin and AIX to allow additional definitions and
* implementation-defined values.
/* On various platforms, fall back to the system's float.h, which might have
* additional definitions and/or implementation-defined values.
*/
#if (defined(__APPLE__) || defined(__MINGW32__) || defined(_MSC_VER) || \
defined(_AIX)) && \
defined(_AIX) || defined(__musl__)) && \
__STDC_HOSTED__ && __has_include_next(<float.h>)

/* Prior to Apple's 10.7 SDK, float.h SDK header used to apply an extra level
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Headers/stddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
#undef __need_wint_t
#include_next <stddef.h>

#elif defined(__musl__)

// On musl systems, use the system header
#include_next <stddef.h>

#else

#if !defined(__need_ptrdiff_t) && !defined(__need_size_t) && \
Expand Down Expand Up @@ -119,4 +124,4 @@ __WINT_TYPE__ directly; accommodate both by requiring __need_wint_t */
#undef __need_wint_t
#endif /* __need_wint_t */

#endif /* __MVS__ */
#endif /* !defined(__MVS__) && !defined(__musl__) */
39 changes: 0 additions & 39 deletions clang/test/Driver/linux-musl-header-search.cpp

This file was deleted.