Skip to content

Commit da779ca

Browse files
authored
Merge pull request #71844 from al45tair/eng/PR-123506306
Add support to the compiler for musl and the LINUX_STATIC SDK.
2 parents 64aee7e + 978169f commit da779ca

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed

lib/Basic/Platform.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,17 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
231231
case llvm::Triple::XROS:
232232
return getPlatformNameForDarwin(getDarwinPlatformKind(triple));
233233
case llvm::Triple::Linux:
234-
return triple.isAndroid() ? "android" : "linux";
234+
if (triple.isAndroid())
235+
return "android";
236+
else if (triple.isMusl()) {
237+
// The triple for linux-static is <arch>-swift-linux-musl, to distinguish
238+
// it from a "normal" musl set-up (ala Alpine).
239+
if (triple.getVendor() == llvm::Triple::Swift)
240+
return "linux-static";
241+
else
242+
return "musl";
243+
} else
244+
return "linux";
235245
case llvm::Triple::FreeBSD:
236246
return "freebsd";
237247
case llvm::Triple::OpenBSD:

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ void importer::getNormalInvocationArguments(
646646
// using Glibc or a libc that respects that flag. This will cause some
647647
// source breakage however (specifically with strerror_r()) on Linux
648648
// without a workaround.
649-
if (triple.isOSFuchsia() || triple.isAndroid()) {
649+
if (triple.isOSFuchsia() || triple.isAndroid() || triple.isMusl()) {
650650
// Many of the modern libc features are hidden behind feature macros like
651651
// _GNU_SOURCE or _XOPEN_SOURCE.
652652
invocationArgStrs.insert(invocationArgStrs.end(), {

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ createClangArgs(const ASTContext &ctx, clang::driver::Driver &clangDriver) {
180180

181181
static bool shouldInjectLibcModulemap(const llvm::Triple &triple) {
182182
return triple.isOSGlibc() || triple.isOSOpenBSD() || triple.isOSFreeBSD() ||
183-
triple.isAndroid() || triple.isOSWASI();
183+
triple.isAndroid() || triple.isMusl() || triple.isOSWASI();
184184
}
185185

186186
static SmallVector<std::pair<std::string, std::string>, 2>
@@ -254,8 +254,9 @@ static void getLibStdCxxFileMapping(
254254
// We currently only need this when building for Linux.
255255
if (!triple.isOSLinux())
256256
return;
257-
// Android uses libc++.
258-
if (triple.isAndroid())
257+
// Android uses libc++, as does our fully static Linux config.
258+
if (triple.isAndroid()
259+
|| (triple.isMusl() && triple.getVendor() == llvm::Triple::Swift))
259260
return;
260261

261262
// Extract the libstdc++ installation path from Clang driver.
@@ -529,30 +530,44 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
529530

530531
const llvm::Triple &triple = ctx.LangOpts.Target;
531532

533+
// For modulemaps that have all the C standard library headers together in
534+
// a single module, we end up with module cycles with the clang _Builtin_
535+
// modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
536+
// clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
537+
// platform headers are in the SwiftLibc module, there's a module cycle
538+
// SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
539+
// stdint.h (builtin) -> stdint.h (platform)).
540+
//
541+
// Until these modulemaps can be fixed, the builtin headers need to join
542+
// the system modules to avoid the cycle.
543+
//
544+
// Note that this does nothing to fix the same problem with C++ headers,
545+
// and that this is generally a fragile solution.
546+
//
547+
// We start by assuming we do *not* need to do this, then enable it for
548+
// affected modulemaps.
549+
result.requiresBuiltinHeadersInSystemModules = false;
550+
532551
SmallVector<std::pair<std::string, std::string>, 2> libcFileMapping;
533552
if (triple.isOSWASI()) {
534553
// WASI Mappings
535554
libcFileMapping =
536555
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
556+
557+
// WASI's module map needs fixing
558+
result.requiresBuiltinHeadersInSystemModules = true;
559+
} else if (triple.isMusl()) {
560+
libcFileMapping =
561+
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs);
537562
} else {
538563
// Android/BSD/Linux Mappings
539564
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
540565
StringRef("SwiftGlibc.h"), vfs);
566+
567+
// glibc.modulemap needs fixing
568+
result.requiresBuiltinHeadersInSystemModules = true;
541569
}
542570
result.redirectedFiles.append(libcFileMapping);
543-
// Both libc module maps have the C standard library headers all together in a
544-
// SwiftLibc module. That leads to module cycles with the clang _Builtin_
545-
// modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
546-
// clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
547-
// platform headers are in the SwiftLibc module, there's a module cycle
548-
// SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
549-
// stdint.h (builtin) -> stdint.h (platform)). Until this can be fixed in
550-
// these module maps, the clang builtin headers need to join the "system"
551-
// modules (SwiftLibc). i.e. when the clang builtin stdint.h is in the
552-
// SwiftLibc module too, the cycle goes away. Note that
553-
// -fbuiltin-headers-in-system-modules does nothing to fix the same problem
554-
// with C++ headers, and is generally fragile.
555-
result.requiresBuiltinHeadersInSystemModules = !libcFileMapping.empty();
556571

557572
if (ctx.LangOpts.EnableCXXInterop)
558573
getLibStdCxxFileMapping(result, ctx, vfs);

lib/Driver/UnixToolChains.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ bool isAmazonLinux2023Host() {
127127
}
128128

129129
std::string toolchains::GenericUnix::getDefaultLinker() const {
130-
if (getTriple().isAndroid() || isAmazonLinux2023Host())
130+
if (getTriple().isAndroid() || isAmazonLinux2023Host()
131+
|| (getTriple().isMusl()
132+
&& getTriple().getVendor() == llvm::Triple::Swift))
131133
return "lld";
132134

133135
switch (getTriple().getArch()) {
@@ -285,7 +287,8 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
285287
}
286288

287289
SmallString<128> SharedResourceDirPath;
288-
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
290+
getResourceDirPath(SharedResourceDirPath, context.Args,
291+
/*Shared=*/!(staticExecutable || staticStdlib));
289292

290293
SmallString<128> swiftrtPath = SharedResourceDirPath;
291294
llvm::sys::path::append(swiftrtPath,

0 commit comments

Comments
 (0)