Skip to content

Add support to the compiler for musl and the LINUX_STATIC SDK. #71844

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 3 commits into from
May 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
12 changes: 11 additions & 1 deletion lib/Basic/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,17 @@ StringRef swift::getPlatformNameForTriple(const llvm::Triple &triple) {
case llvm::Triple::XROS:
return getPlatformNameForDarwin(getDarwinPlatformKind(triple));
case llvm::Triple::Linux:
return triple.isAndroid() ? "android" : "linux";
if (triple.isAndroid())
return "android";
else if (triple.isMusl()) {
// The triple for linux-static is <arch>-swift-linux-musl, to distinguish
// it from a "normal" musl set-up (ala Alpine).
if (triple.getVendor() == llvm::Triple::Swift)
return "linux-static";
else
return "musl";
} else
return "linux";
case llvm::Triple::FreeBSD:
return "freebsd";
case llvm::Triple::OpenBSD:
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ void importer::getNormalInvocationArguments(
// using Glibc or a libc that respects that flag. This will cause some
// source breakage however (specifically with strerror_r()) on Linux
// without a workaround.
if (triple.isOSFuchsia() || triple.isAndroid()) {
if (triple.isOSFuchsia() || triple.isAndroid() || triple.isMusl()) {
// Many of the modern libc features are hidden behind feature macros like
// _GNU_SOURCE or _XOPEN_SOURCE.
invocationArgStrs.insert(invocationArgStrs.end(), {
Expand Down
47 changes: 31 additions & 16 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ createClangArgs(const ASTContext &ctx, clang::driver::Driver &clangDriver) {

static bool shouldInjectLibcModulemap(const llvm::Triple &triple) {
return triple.isOSGlibc() || triple.isOSOpenBSD() || triple.isOSFreeBSD() ||
triple.isAndroid() || triple.isOSWASI();
triple.isAndroid() || triple.isMusl() || triple.isOSWASI();
}

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

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

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

// For modulemaps that have all the C standard library headers together in
// a single module, we end up with module cycles with the clang _Builtin_
// modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
// clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
// platform headers are in the SwiftLibc module, there's a module cycle
// SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
// stdint.h (builtin) -> stdint.h (platform)).
//
// Until these modulemaps can be fixed, the builtin headers need to join
// the system modules to avoid the cycle.
//
// Note that this does nothing to fix the same problem with C++ headers,
// and that this is generally a fragile solution.
//
// We start by assuming we do *not* need to do this, then enable it for
// affected modulemaps.
result.requiresBuiltinHeadersInSystemModules = false;

SmallVector<std::pair<std::string, std::string>, 2> libcFileMapping;
if (triple.isOSWASI()) {
// WASI Mappings
libcFileMapping =
getLibcFileMapping(ctx, "wasi-libc.modulemap", std::nullopt, vfs);

// WASI's module map needs fixing
result.requiresBuiltinHeadersInSystemModules = true;
} else if (triple.isMusl()) {
libcFileMapping =
getLibcFileMapping(ctx, "musl.modulemap", StringRef("SwiftMusl.h"), vfs);
} else {
// Android/BSD/Linux Mappings
libcFileMapping = getLibcFileMapping(ctx, "glibc.modulemap",
StringRef("SwiftGlibc.h"), vfs);

// glibc.modulemap needs fixing
result.requiresBuiltinHeadersInSystemModules = true;
}
result.redirectedFiles.append(libcFileMapping);
// Both libc module maps have the C standard library headers all together in a
// SwiftLibc module. That leads to module cycles with the clang _Builtin_
// modules. e.g. <inttypes.h> includes <stdint.h> on these platforms. The
// clang builtin <stdint.h> include-nexts <stdint.h>. When both of those
// platform headers are in the SwiftLibc module, there's a module cycle
// SwiftLibc -> _Builtin_stdint -> SwiftLibc (i.e. inttypes.h (platform) ->
// stdint.h (builtin) -> stdint.h (platform)). Until this can be fixed in
// these module maps, the clang builtin headers need to join the "system"
// modules (SwiftLibc). i.e. when the clang builtin stdint.h is in the
// SwiftLibc module too, the cycle goes away. Note that
// -fbuiltin-headers-in-system-modules does nothing to fix the same problem
// with C++ headers, and is generally fragile.
result.requiresBuiltinHeadersInSystemModules = !libcFileMapping.empty();

if (ctx.LangOpts.EnableCXXInterop)
getLibStdCxxFileMapping(result, ctx, vfs);
Expand Down
7 changes: 5 additions & 2 deletions lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ bool isAmazonLinux2023Host() {
}

std::string toolchains::GenericUnix::getDefaultLinker() const {
if (getTriple().isAndroid() || isAmazonLinux2023Host())
if (getTriple().isAndroid() || isAmazonLinux2023Host()
|| (getTriple().isMusl()
&& getTriple().getVendor() == llvm::Triple::Swift))
return "lld";

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

SmallString<128> SharedResourceDirPath;
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/true);
getResourceDirPath(SharedResourceDirPath, context.Args,
/*Shared=*/!(staticExecutable || staticStdlib));

SmallString<128> swiftrtPath = SharedResourceDirPath;
llvm::sys::path::append(swiftrtPath,
Expand Down
84 changes: 51 additions & 33 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,39 +114,35 @@ set(swift_runtime_library_compile_flags ${swift_runtime_compile_flags})
list(APPEND swift_runtime_library_compile_flags -DswiftCore_EXPORTS)
list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/stdlib/include/llvm/Support -I${SWIFT_SOURCE_DIR}/include)

if(SWIFT_BUILD_STATIC_STDLIB)
set(static_binary_lnk_file_list)
set(static_binary_lnk_file_list)

foreach(sdk ${SWIFT_SDKS})
if(NOT "${sdk}" STREQUAL "LINUX" AND NOT "${sdk}" STREQUAL "WASI")
continue()
endif()
foreach(sdk ${SWIFT_SDKS})
if(NOT SWIFT_SDK_${sdk}_STATIC_LINKING_SUPPORTED)
continue()
endif()

string(TOLOWER "${sdk}" lowercase_sdk)
set(static_binary_lnk_src "${SWIFT_SOURCE_DIR}/stdlib/public/Resources/${lowercase_sdk}/static-executable-args.lnk")
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
set(lib_dir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
set(static_binary_lnk_src "${SWIFT_SOURCE_DIR}/stdlib/public/Resources/${lib_dir}/static-executable-args.lnk")

# Generate the static-executable-args.lnk file used for ELF systems (eg linux)
set(linkfile "${lowercase_sdk}/static-executable-args.lnk")
set(linkfile "${lib_dir}/static-executable-args.lnk")
add_custom_command_target(swift_static_binary_${sdk}_args
COMMAND
"${CMAKE_COMMAND}" -E copy
"${static_binary_lnk_src}"
"${SWIFTSTATICLIB_DIR}/${linkfile}"
"${CMAKE_COMMAND}" -E copy
"${static_binary_lnk_src}"
"${SWIFTSTATICLIB_DIR}/${linkfile}"
OUTPUT
"${SWIFTSTATICLIB_DIR}/${linkfile}"
"${SWIFTSTATICLIB_DIR}/${linkfile}"
DEPENDS
"${static_binary_lnk_src}")
"${static_binary_lnk_src}")

list(APPEND static_binary_lnk_file_list ${swift_static_binary_${sdk}_args})
swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
DESTINATION "lib/swift_static/${lowercase_sdk}"
COMPONENT stdlib)
endforeach()
if(static_binary_lnk_file_list)
add_dependencies(stdlib ${static_binary_lnk_file_list})
add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list})
DESTINATION "lib/swift_static/${lib_dir}"
COMPONENT stdlib)
endif()
endif()
endforeach()

add_swift_target_library(swiftRuntime OBJECT_LIBRARY
${swift_runtime_sources}
Expand Down Expand Up @@ -248,15 +244,15 @@ foreach(sdk ${SWIFT_SDKS})
"${static_runtime_registrar}"
DEPENDS
"${swiftrtObject}")
if(SWIFT_BUILD_DYNAMIC_STDLIB)
if(SWIFT_BUILD_DYNAMIC_STDLIB AND NOT SWIFT_SDK_${sdk}_STATIC_ONLY)
swift_install_in_component(FILES
"${shared_runtime_registrar}"
DESTINATION
"lib/swift/${arch_subdir}"
COMPONENT
stdlib)
endif()
if(SWIFT_BUILD_STATIC_STDLIB)
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
swift_install_in_component(FILES
"${static_runtime_registrar}"
DESTINATION
Expand All @@ -273,10 +269,26 @@ foreach(sdk ${SWIFT_SDKS})
add_dependencies(stdlib swift-stdlib-${arch_suffix} swiftImageRegistration-${arch_suffix})
endif()

# Generate the static-stdlib-args.lnk file used by -static-stdlib option for
# 'GenericUnix' (eg linux)
if(SWIFT_SDK_${sdk}_OBJECT_FORMAT STREQUAL "ELF")
string(TOLOWER "${sdk}" lowercase_sdk)
endforeach()

# Generate the static-stdlib-args.lnk file used by -static-stdlib option for
# 'GenericUnix' (eg linux)
if(SWIFT_SDK_${sdk}_OBJECT_FORMAT STREQUAL "ELF")
set(lib_dir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}")
set(static_stdlib_lnk_src "${SWIFT_SOURCE_DIR}/stdlib/public/Resources/${lib_dir}/static-stdlib-args.lnk")
set(linkfile ${lib_dir}/static-stdlib-args.lnk)
if(EXISTS "${static_stdlib_lnk_src}")
add_custom_command_target(swift_static_stdlib_${sdk}_args
COMMAND
"${CMAKE_COMMAND}" -E copy
"${static_stdlib_lnk_src}"
"${SWIFTSTATICLIB_DIR}/${linkfile}"
OUTPUT
"${SWIFTSTATICLIB_DIR}/${linkfile}"
DEPENDS
"${static_stdlib_lnk_src}")
list(APPEND static_binary_lnk_file_list ${swift_static_stdlib_${sdk}_args})
else()
set(libpthread -lpthread)
set(concurrency_libs)
set(android_libraries)
Expand All @@ -287,7 +299,6 @@ foreach(sdk ${SWIFT_SDKS})
set(concurrency_libs "-ldispatch -lBlocksRuntime")
endif()

set(linkfile ${lowercase_sdk}/static-stdlib-args.lnk)
file(WRITE "${SWIFTSTATICLIB_DIR}/${linkfile}" "
-ldl
${libpthread}
Expand All @@ -299,10 +310,17 @@ ${concurrency_libs}
-Xlinker -export-dynamic
-Xlinker --exclude-libs
-Xlinker ALL")

swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
DESTINATION "lib/swift_static/${lowercase_sdk}"
COMPONENT stdlib)
endif()
endforeach()

swift_install_in_component(FILES "${SWIFTSTATICLIB_DIR}/${linkfile}"
DESTINATION "lib/swift_static/${lib_dir}"
COMPONENT stdlib)
endif()

endforeach()

if(static_binary_lnk_file_list)
add_dependencies(stdlib ${static_binary_lnk_file_list})
add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list})
endif()