Skip to content

Commit d8e304e

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 5f0ef4c commit d8e304e

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
@@ -210,7 +210,17 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
210210
case llvm::Triple::WatchOS:
211211
return getPlatformNameForDarwin(getDarwinPlatformKind(triple));
212212
case llvm::Triple::Linux:
213-
return triple.isAndroid() ? "android" : "linux";
213+
if (triple.isAndroid())
214+
return "android";
215+
else if (triple.isMusl()) {
216+
// The triple for linux-static is <arch>-swift-linux-musl, to distinguish
217+
// it from a "normal" musl set-up (ala Alpine).
218+
if (triple.getVendor() == llvm::Triple::Swift)
219+
return "linux-static";
220+
else
221+
return "musl";
222+
} else
223+
return "linux";
214224
case llvm::Triple::FreeBSD:
215225
return "freebsd";
216226
case llvm::Triple::OpenBSD:

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ importer::getNormalInvocationArguments(
625625
// using Glibc or a libc that respects that flag. This will cause some
626626
// source breakage however (specifically with strerror_r()) on Linux
627627
// without a workaround.
628-
if (triple.isOSFuchsia() || triple.isAndroid()) {
628+
if (triple.isOSFuchsia() || triple.isAndroid() || triple.isMusl()) {
629629
// Many of the modern libc features are hidden behind feature macros like
630630
// _GNU_SOURCE or _XOPEN_SOURCE.
631631
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()) {
@@ -279,7 +281,8 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
279281
}
280282

281283
SmallString<128> SharedResourceDirPath;
282-
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
284+
getResourceDirPath(SharedResourceDirPath, context.Args,
285+
/*Shared=*/!(staticExecutable || staticStdlib));
283286

284287
SmallString<128> swiftrtPath = SharedResourceDirPath;
285288
llvm::sys::path::append(swiftrtPath,

0 commit comments

Comments
 (0)