Skip to content

Commit 1b8d41a

Browse files
committed
Add support to the compiler for musl and the LINUX_STATIC SDK.
This is really just about setting appropriate defaults (such as making sure that the static Linux triple causes us to use lld). rdar://123506306
1 parent 4799767 commit 1b8d41a

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
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
@@ -643,7 +643,7 @@ importer::getNormalInvocationArguments(
643643
// using Glibc or a libc that respects that flag. This will cause some
644644
// source breakage however (specifically with strerror_r()) on Linux
645645
// without a workaround.
646-
if (triple.isOSFuchsia() || triple.isAndroid()) {
646+
if (triple.isOSFuchsia() || triple.isAndroid() || triple.isMusl()) {
647647
// Many of the modern libc features are hidden behind feature macros like
648648
// _GNU_SOURCE or _XOPEN_SOURCE.
649649
invocationArgStrs.insert(invocationArgStrs.end(), {

lib/ClangImporter/ClangIncludePaths.cpp

Lines changed: 7 additions & 3 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.
@@ -534,6 +535,9 @@ ClangInvocationFileMapping swift::getClangInvocationFileMapping(
534535
// WASI Mappings
535536
libcFileMapping =
536537
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);
538+
} else if (triple.isMusl()) {
539+
libcFileMapping =
540+
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs);
537541
} else {
538542
// Android/BSD/Linux Mappings
539543
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",

lib/Driver/UnixToolChains.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ ToolChain::InvocationInfo toolchains::GenericUnix::constructInvocation(
110110
}
111111

112112
std::string toolchains::GenericUnix::getDefaultLinker() const {
113-
if (getTriple().isAndroid())
113+
if (getTriple().isAndroid()
114+
|| (getTriple().isMusl()
115+
&& getTriple().getVendor() == llvm::Triple::Swift))
114116
return "lld";
115117

116118
switch (getTriple().getArch()) {
@@ -268,7 +270,8 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
268270
}
269271

270272
SmallString<128> SharedResourceDirPath;
271-
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
273+
getResourceDirPath(SharedResourceDirPath, context.Args,
274+
/*Shared=*/!(staticExecutable || staticStdlib));
272275

273276
SmallString<128> swiftrtPath = SharedResourceDirPath;
274277
llvm::sys::path::append(swiftrtPath,

0 commit comments

Comments
 (0)