From f3d3cb191759de31f09117e6d561d65c08766dc1 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Thu, 8 Aug 2024 15:39:44 -0700 Subject: [PATCH 1/3] Bring back defines and flags Several definitions and flags were dropped from the build when moving to swift-froundation. This patch puts back the ones that we need in order to build against musl. Since the musl builds are static, we also need to pick up the link to libRT or dispatch will fail to link. --- CMakeLists.txt | 21 +++++++++++++++++++++ cmake/modules/FindLibRT.cmake | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30d960cb07..e99cfc989f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,9 +77,29 @@ endif() FetchContent_MakeAvailable(SwiftFoundationICU SwiftFoundation) include(CheckLinkerFlag) +include(CheckSymbolExists) check_linker_flag(C "LINKER:--build-id=sha1" LINKER_SUPPORTS_BUILD_ID) +check_symbol_exists("strlcat" "string.h" HAVE_STRLCAT) +check_symbol_exists("strlcpy" "string.h" HAVE_STRLCPY) +check_symbol_exists("issetugid" "unistd.h" HAVE_ISSETUGID) +add_compile_definitions( + $<$,$>:HAVE_STRLCAT> + $<$,$>:HAVE_STRLCPY> + $<$,$>:HAVE_ISSETUGID>) + +if(CMAKE_SYSTEM_NAME STREQUAL Linux OR ANDROID) + add_compile_definitions($<$:_GNU_SOURCE>) +endif() + +if(CMAKE_SYSTEM_NAME STREQUAL Linux) + check_symbol_exists(sched_getaffinity "sched.h" HAVE_SCHED_GETAFFINITY) + add_compile_definitions($<$:HAVE_SCHED_GETAFFINITY>) +endif() + +add_compile_options($<$:-fblocks>) + # Precompute module triple for installation if(NOT SwiftFoundation_MODULE_TRIPLE) set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info) @@ -93,6 +113,7 @@ if(NOT SwiftFoundation_MODULE_TRIPLE) endif() # System dependencies +find_package(LibRT) find_package(dispatch CONFIG) if(NOT dispatch_FOUND) if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") diff --git a/cmake/modules/FindLibRT.cmake b/cmake/modules/FindLibRT.cmake index 0a9f0d80e5..caee828517 100644 --- a/cmake/modules/FindLibRT.cmake +++ b/cmake/modules/FindLibRT.cmake @@ -4,7 +4,7 @@ # # Find librt library and headers. # -# The mdoule defines the following variables: +# The module defines the following variables: # # :: # From bd3e56d9776302c0433ed951c26d245e5344a5ca Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Thu, 8 Aug 2024 15:41:38 -0700 Subject: [PATCH 2/3] Fix the musl build in Port.swift This fixes how we pick up SOCK_STREAM and IPPROTO_TCP from musl. We weren't before so it was just failing to build. --- Sources/Foundation/Port.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/Foundation/Port.swift b/Sources/Foundation/Port.swift index c1f78c14a4..c4ed8282bf 100644 --- a/Sources/Foundation/Port.swift +++ b/Sources/Foundation/Port.swift @@ -113,6 +113,12 @@ fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue) fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP) #endif +#if canImport(Musl) +import Musl +fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM) +fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP) +#endif + #if canImport(Glibc) && os(Android) || os(OpenBSD) import Glibc fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM) From d00e2a6f002ddc34184eb8b3d4f21df03db3f9a4 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Tue, 13 Aug 2024 14:47:48 -0700 Subject: [PATCH 3/3] Add comment on symbol searching Add comment describing why we need to check for the availability of strlcat/strlcpy and issetguid. Also removed setting the _GNU_SOURCES compile-definition and -fblocks flag as it's already added through one of the global variables. --- CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e99cfc989f..dbc303dc4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,12 @@ include(CheckSymbolExists) check_linker_flag(C "LINKER:--build-id=sha1" LINKER_SUPPORTS_BUILD_ID) +# Detect if the system libc defines symbols for these functions. +# If it is not availble, swift-corelibs-foundation has its own implementations +# that will be used. If it is available, it should not redefine them. +# Note: SwiftPM does not have the ability to introspect the contents of the SDK +# and therefore will always include these functions in the build and will +# cause build failures on platforms that define these functions. check_symbol_exists("strlcat" "string.h" HAVE_STRLCAT) check_symbol_exists("strlcpy" "string.h" HAVE_STRLCPY) check_symbol_exists("issetugid" "unistd.h" HAVE_ISSETUGID) @@ -89,17 +95,11 @@ add_compile_definitions( $<$,$>:HAVE_STRLCPY> $<$,$>:HAVE_ISSETUGID>) -if(CMAKE_SYSTEM_NAME STREQUAL Linux OR ANDROID) - add_compile_definitions($<$:_GNU_SOURCE>) -endif() - if(CMAKE_SYSTEM_NAME STREQUAL Linux) check_symbol_exists(sched_getaffinity "sched.h" HAVE_SCHED_GETAFFINITY) add_compile_definitions($<$:HAVE_SCHED_GETAFFINITY>) endif() -add_compile_options($<$:-fblocks>) - # Precompute module triple for installation if(NOT SwiftFoundation_MODULE_TRIPLE) set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info)