Skip to content
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
1 change: 1 addition & 0 deletions stdlib/private/SwiftPrivate/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ add_swift_target_library(swiftSwiftPrivate ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
SWIFT_MODULE_DEPENDS_MACCATALYST ${swift_swiftprivate_darwin_depencencies}
SWIFT_MODULE_DEPENDS_FREESTANDING "${SWIFT_FREESTANDING_TEST_DEPENDENCIES}"
SWIFT_MODULE_DEPENDS_LINUX Glibc
SWIFT_MODULE_DEPENDS_LINUX_STATIC Musl
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
Expand Down
1 change: 1 addition & 0 deletions stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_swift_target_library(swiftSwiftPrivateLibcExtras ${SWIFT_STDLIB_LIBRARY_BUIL
SWIFT_MODULE_DEPENDS_MACCATALYST ${swift_private_libc_extras_darwin_depencencies}
SWIFT_MODULE_DEPENDS_FREESTANDING "${SWIFT_FREESTANDING_TEST_DEPENDENCIES}"
SWIFT_MODULE_DEPENDS_LINUX Glibc
SWIFT_MODULE_DEPENDS_LINUX_STATIC Musl
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
Expand Down
1 change: 1 addition & 0 deletions stdlib/private/SwiftPrivateThreadExtras/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_swift_target_library(swiftSwiftPrivateThreadExtras ${SWIFT_STDLIB_LIBRARY_BU
SWIFT_MODULE_DEPENDS_MACCATALYST ${swift_private_thread_extras_darwin_depencencies}
SWIFT_MODULE_DEPENDS_FREESTANDING "${SWIFT_FREESTANDING_TEST_DEPENDENCIES}"
SWIFT_MODULE_DEPENDS_LINUX Glibc
SWIFT_MODULE_DEPENDS_LINUX_STATIC Musl
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
Expand Down
9 changes: 8 additions & 1 deletion stdlib/private/SwiftReflectionTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ if (SWIFT_INCLUDE_TESTS AND SWIFT_BUILD_DYNAMIC_STDLIB)
SWIFT_MODULE_DEPENDS_WATCHOS ${swift_reflection_test_darwin_depencencies}
SWIFT_MODULE_DEPENDS_XROS ${swift_reflection_test_darwin_depencencies}
SWIFT_MODULE_DEPENDS_LINUX Glibc
SWIFT_MODULE_DEPENDS_LINUX_STATIC Musl
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
SWIFT_MODULE_DEPENDS_WINDOWS CRT
INSTALL_IN_COMPONENT stdlib-experimental
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}")
DARWIN_INSTALL_NAME_DIR "${SWIFT_DARWIN_STDLIB_PRIVATE_INSTALL_NAME_DIR}"

TARGET_SDKS NOT LINUX_STATIC)

foreach(SDK ${SWIFT_SDKS})
if ("${SDK}" STREQUAL "LINUX_STATIC")
continue()
endif()

foreach(ARCH ${SWIFT_SDK_${SDK}_ARCHITECTURES})
set(VARIANT_SUFFIX "-${SWIFT_SDK_${SDK}_LIB_SUBDIR}-${ARCH}")
add_dependencies("swiftSwiftReflectionTest${VARIANT_SUFFIX}"
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Backtracing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ add_swift_target_library(swift_Backtracing ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
INSTALL_IN_COMPONENT stdlib
MACCATALYST_BUILD_FLAVOR "zippered"

TARGET_SDKS OSX LINUX
TARGET_SDKS OSX LINUX LINUX_STATIC
)
4 changes: 2 additions & 2 deletions stdlib/public/Backtracing/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,10 @@ extension arm_gprs {
}
}

to[31] = mctx.sp
to[31] = UInt64(mctx.sp)
}
}
gprs.pc = mctx.pc
gprs.pc = UInt64(mctx.pc)
gprs.valid = 0x1ffffffff
}

Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/Cxx/cxxshim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ foreach(sdk ${SWIFT_SDKS})

add_custom_command(OUTPUT ${module_dir}
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${module_dir}")
if(SWIFT_BUILD_STATIC_STDLIB)
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
add_custom_command(OUTPUT ${module_dir_static}
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${module_dir_static}")
endif()
Expand All @@ -18,7 +18,7 @@ foreach(sdk ${SWIFT_SDKS})
COMMENT "Copying ${source} to ${module_dir}")
list(APPEND outputs "${module_dir}/${source}")

if(SWIFT_BUILD_STATIC_STDLIB)
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
add_custom_command(OUTPUT ${module_dir_static}/${source}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${source}
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/${source}" "${module_dir_static}/${source}"
Expand All @@ -27,7 +27,7 @@ foreach(sdk ${SWIFT_SDKS})
endif()
endforeach()
list(APPEND outputs ${module_dir})
if(SWIFT_BUILD_STATIC_STDLIB)
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
list(APPEND outputs ${module_dir_static})
endif()

Expand All @@ -40,7 +40,7 @@ foreach(sdk ${SWIFT_SDKS})
swift_install_in_component(FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
DESTINATION "lib/swift/${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
COMPONENT compiler)
if(SWIFT_BUILD_STATIC_STDLIB)
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
swift_install_in_component(FILES libcxxshim.modulemap libcxxshim.h libcxxstdlibshim.h
DESTINATION "lib/swift_static/${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
COMPONENT compiler)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Cxx/libstdcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ foreach(sdk ${SWIFT_SDKS})
list(APPEND libstdcxx_modulemap_target_list ${copy_libstdcxx_header})
add_dependencies(swift-stdlib-${SWIFT_SDK_${sdk}_LIB_SUBDIR} ${copy_libstdcxx_header})

if(SWIFT_BUILD_STATIC_STDLIB)
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
add_custom_command_target(
copy_libstdcxx_modulemap_static
COMMAND
Expand Down Expand Up @@ -73,7 +73,7 @@ foreach(sdk ${SWIFT_SDKS})
DESTINATION "lib/swift/${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
COMPONENT sdk-overlay)

if(SWIFT_BUILD_STATIC_STDLIB)
if(SWIFT_BUILD_STATIC_STDLIB OR SWIFT_SDK_${sdk}_STATIC_ONLY)
swift_install_in_component(FILES "${libstdcxx_modulemap_out_static}"
DESTINATION "lib/swift_static/${SWIFT_SDK_${sdk}_LIB_SUBDIR}"
COMPONENT sdk-overlay)
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/Differentiation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE
SWIFT_MODULE_DEPENDS_WATCHOS ${swiftDifferentiationDarwinDependencies}
SWIFT_MODULE_DEPENDS_XROS ${swiftDifferentiationDarwinDependencies}
SWIFT_MODULE_DEPENDS_LINUX Glibc
SWIFT_MODULE_DEPENDS_LINUX_STATIC Musl
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
SWIFT_MODULE_DEPENDS_OPENBSD Glibc
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
Expand Down
4 changes: 3 additions & 1 deletion stdlib/public/SwiftRemoteMirror/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ add_swift_target_library(swiftRemoteMirror
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
DARWIN_INSTALL_NAME_DIR "${SWIFTLIB_DARWIN_INSTALL_NAME_DIR}"
INSTALL_IN_COMPONENT
swift-remote-mirror)
swift-remote-mirror

TARGET_SDKS NOT LINUX_STATIC)
88 changes: 39 additions & 49 deletions stdlib/public/SwiftShims/swift/shims/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ add_custom_command(
OUTPUT "${output_dir}"
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${output_dir}")

if(SWIFT_BUILD_STATIC_STDLIB)
add_custom_command(
OUTPUT "${output_dir_static}"
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${output_dir_static}")
endif()
add_custom_command(
OUTPUT "${output_dir_static}"
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${output_dir_static}")

set(outputs)
foreach(input ${sources})
Expand All @@ -52,17 +50,15 @@ foreach(input ${sources})
COMMENT "Copying ${input} to ${output_dir}")
list(APPEND outputs "${output_dir}/${input}")

if(SWIFT_BUILD_STATIC_STDLIB)
add_custom_command(
OUTPUT "${output_dir_static}/${input}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${input}"
COMMAND
"${CMAKE_COMMAND}" "-E" "copy_if_different"
"${CMAKE_CURRENT_SOURCE_DIR}/${input}"
"${output_dir_static}/${input}"
COMMENT "Copying ${input} to ${output_dir_static}")
list(APPEND outputs "${output_dir_static}/${input}")
endif()
add_custom_command(
OUTPUT "${output_dir_static}/${input}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${input}"
COMMAND
"${CMAKE_COMMAND}" "-E" "copy_if_different"
"${CMAKE_CURRENT_SOURCE_DIR}/${input}"
"${output_dir_static}/${input}"
COMMENT "Copying ${input} to ${output_dir_static}")
list(APPEND outputs "${output_dir_static}/${input}")
endforeach()
if (SWIFT_STDLIB_INSTALL_PARENT_MODULE_FOR_SHIMS)
# Copy the module map into the parent directory. Using "parent.modulemap"
Expand Down Expand Up @@ -143,21 +139,19 @@ add_custom_command_target(unused_var

add_dependencies(copy_shim_headers symlink_clang_headers)

if(SWIFT_BUILD_STATIC_STDLIB)
add_custom_command_target(unused_var
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" "${SWIFTSTATICLIB_DIR}"
COMMAND
"${CMAKE_COMMAND}" "-E" "${SWIFT_COPY_OR_SYMLINK_DIR}"
"${clang_headers_location}"
"${SWIFTSTATICLIB_DIR}/clang"
add_custom_command_target(unused_var
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" "${SWIFTSTATICLIB_DIR}"
COMMAND
"${CMAKE_COMMAND}" "-E" "${SWIFT_COPY_OR_SYMLINK_DIR}"
"${clang_headers_location}"
"${SWIFTSTATICLIB_DIR}/clang"

CUSTOM_TARGET_NAME "symlink_clang_headers_static"
OUTPUT "${SWIFTSTATICLIB_DIR}/clang"
COMMENT "Symlinking Clang resource headers into ${SWIFTSTATICLIB_DIR}/clang")
CUSTOM_TARGET_NAME "symlink_clang_headers_static"
OUTPUT "${SWIFTSTATICLIB_DIR}/clang"
COMMENT "Symlinking Clang resource headers into ${SWIFTSTATICLIB_DIR}/clang")

add_dependencies(copy_shim_headers symlink_clang_headers_static)
endif()
add_dependencies(copy_shim_headers symlink_clang_headers_static)

if(NOT SWIFT_BUILT_STANDALONE)
if(TARGET clang-resource-headers) # LLVM > 8
Expand Down Expand Up @@ -227,15 +221,15 @@ if (SWIFT_STDLIB_INSTALL_PARENT_MODULE_FOR_SHIMS)
COMPONENT stdlib)
endif()

if(SWIFT_BUILD_STATIC_STDLIB)
swift_install_in_component(FILES ${sources}
DESTINATION "lib/swift_static/shims"
if (SWIFT_BUILD_STATIC_STDLIB)
swift_install_in_component(FILES ${sources}
DESTINATION "lib/swift_static/shims"
COMPONENT stdlib)
if (SWIFT_STDLIB_INSTALL_PARENT_MODULE_FOR_SHIMS)
swift_install_in_component(FILES "${output_dir_static}/../module.modulemap"
DESTINATION "lib/swift_static"
COMPONENT stdlib)
if (SWIFT_STDLIB_INSTALL_PARENT_MODULE_FOR_SHIMS)
swift_install_in_component(FILES "${output_dir_static}/../module.modulemap"
DESTINATION "lib/swift_static"
COMPONENT stdlib)
endif()
endif()
endif()

# Install Clang headers under the Swift library so that an installed Swift's
Expand All @@ -245,12 +239,10 @@ swift_install_in_component(DIRECTORY "${clang_headers_location}/"
COMPONENT clang-builtin-headers
PATTERN "*.h")

if(SWIFT_BUILD_STATIC_STDLIB)
swift_install_in_component(DIRECTORY "${clang_headers_location}/"
DESTINATION "lib/swift_static/clang"
COMPONENT clang-builtin-headers
PATTERN "*.h")
endif()
swift_install_in_component(DIRECTORY "${clang_headers_location}/"
DESTINATION "lib/swift_static/clang"
COMPONENT clang-builtin-headers
PATTERN "*.h")

if(SWIFT_BUILD_RUNTIME_WITH_HOST_COMPILER OR SWIFT_PREBUILT_CLANG)
# This will still link against the Swift-forked clang headers if the Swift
Expand All @@ -268,12 +260,10 @@ swift_install_symlink_component(clang-resource-dir-symlink
TARGET ${SWIFT_CLANG_RESOURCE_DIR_SYMLINK_INSTALL_TARGET}
DESTINATION "lib/swift")

if(SWIFT_BUILD_STATIC_STDLIB)
swift_install_symlink_component(clang-resource-dir-symlink
LINK_NAME clang
TARGET ${SWIFT_CLANG_RESOURCE_DIR_SYMLINK_INSTALL_TARGET}
DESTINATION "lib/swift_static")
endif()
swift_install_symlink_component(clang-resource-dir-symlink
LINK_NAME clang
TARGET ${SWIFT_CLANG_RESOURCE_DIR_SYMLINK_INSTALL_TARGET}
DESTINATION "lib/swift_static")

# Possibly install Clang headers under Clang's resource directory in case we
# need to use a different version of the headers than the installed Clang. This
Expand Down
Loading