From 1bd55748072b2627389a04177518cb98069fce2a 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 1d9c684d94..9a5c765cc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,9 +95,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) @@ -114,6 +134,7 @@ endif() # We know libdispatch is always unavailable on WASI if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI) + 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 16071dfb7894497484b84f15b059baa497e5f3a1 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 b22db59d5b4aac08614151081ed0786a944e4000 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 9a5c765cc3..abecd29bd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,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) @@ -107,17 +113,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)