From acdc7e138ff67f02c261bb6372f755c2fdb6bc7b Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Sat, 30 Mar 2024 10:28:40 +0000 Subject: [PATCH 1/3] build: Repair the build on WASI platform Cherry picked from https://github.com/apple/swift-corelibs-foundation/pull/4934 (cherry picked from commit 51ee6906be0556eb63cd35a16ad4167f69f16a63) --- Package.swift | 20 ++++++++++++++++++-- Sources/CoreFoundation/CFBundle.c | 8 +++++++- Sources/CoreFoundation/CFString.c | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index 4ead01c3ee..8aa40e7965 100644 --- a/Package.swift +++ b/Package.swift @@ -3,6 +3,16 @@ import PackageDescription +let platformsWithThreads: [Platform] = [ + .iOS, + .macOS, + .tvOS, + .watchOS, + .macCatalyst, + .driverKit, + .android, + .linux, +] var dispatchIncludeFlags: [CSetting] if let environmentPath = Context.environment["DISPATCH_INCLUDE_PATH"] { dispatchIncludeFlags = [.unsafeFlags([ @@ -31,8 +41,11 @@ let coreFoundationBuildSettings: [CSetting] = [ .define("DEPLOYMENT_ENABLE_LIBDISPATCH"), .define("DEPLOYMENT_RUNTIME_SWIFT"), .define("HAVE_STRUCT_TIMESPEC"), - .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"), + .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)), .define("_GNU_SOURCE", .when(platforms: [.linux, .android])), + .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), + .define("HAVE_STRLCPY", .when(platforms: [.wasi])), + .define("HAVE_STRLCAT", .when(platforms: [.wasi])), .unsafeFlags([ "-Wno-shorten-64-to-32", "-Wno-deprecated-declarations", @@ -61,8 +74,11 @@ let interfaceBuildSettings: [CSetting] = [ .define("CF_BUILDING_CF"), .define("DEPLOYMENT_ENABLE_LIBDISPATCH"), .define("HAVE_STRUCT_TIMESPEC"), - .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS"), + .define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)), .define("_GNU_SOURCE", .when(platforms: [.linux, .android])), + .define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])), + .define("HAVE_STRLCPY", .when(platforms: [.wasi])), + .define("HAVE_STRLCAT", .when(platforms: [.wasi])), .unsafeFlags([ "-Wno-shorten-64-to-32", "-Wno-deprecated-declarations", diff --git a/Sources/CoreFoundation/CFBundle.c b/Sources/CoreFoundation/CFBundle.c index 8026a26256..05afe9888e 100644 --- a/Sources/CoreFoundation/CFBundle.c +++ b/Sources/CoreFoundation/CFBundle.c @@ -596,7 +596,13 @@ static CFBundleRef _CFBundleGetBundleWithIdentifier(CFStringRef bundleID, void * CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) { // Use the frame that called this as a hint - return _CFBundleGetBundleWithIdentifier(bundleID, __builtin_return_address(0)); + void *hint; +#if TARGET_OS_WASI + hint = NULL; +#else + hint = __builtin_frame_address(0); +#endif + return _CFBundleGetBundleWithIdentifier(bundleID, hint); } CFBundleRef _CFBundleGetBundleWithIdentifierWithHint(CFStringRef bundleID, void *pointer) { diff --git a/Sources/CoreFoundation/CFString.c b/Sources/CoreFoundation/CFString.c index 1de46dac05..94a6c86d15 100644 --- a/Sources/CoreFoundation/CFString.c +++ b/Sources/CoreFoundation/CFString.c @@ -28,7 +28,7 @@ #include "CFRuntime_Internal.h" #include #include <_foundation_unicode/uchar.h> -#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD +#if TARGET_OS_MAC || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI #include "CFConstantKeys.h" #include "CFStringLocalizedFormattingInternal.h" #endif From 8873088ed6903231235bf8f338358a68157794a8 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 5 Aug 2024 20:28:46 +0100 Subject: [PATCH 2/3] Reflect `Package.swift` WASI changes in `CMakeLists.txt` --- CMakeLists.txt | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30d960cb07..edb6cf06f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -115,7 +115,6 @@ list(APPEND _Foundation_common_build_flags "-DCF_BUILDING_CF" "-DDEPLOYMENT_ENABLE_LIBDISPATCH" "-DHAVE_STRUCT_TIMESPEC" - "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" "-Wno-shorten-64-to-32" "-Wno-deprecated-declarations" "-Wno-unreachable-code" @@ -127,6 +126,18 @@ list(APPEND _Foundation_common_build_flags "-Wno-switch" "-fblocks") +if(CMAKE_SYSTEM_NAME STREQUAL "WASI") + list(APPEND _Foundation_common_build_flags + "-D_WASI_EMULATED_SIGNAL" + "-DHAVE_STRLCPY" + "-DHAVE_STRLCAT" + ) +else() + list(APPEND _Foundation_common_build_flags + "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" + ) +endif() + if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC") list(APPEND _Foundation_common_build_flags "-fconstant-cfstrings" @@ -154,10 +165,21 @@ set(_Foundation_swift_build_flags) list(APPEND _Foundation_swift_build_flags "-swift-version 6" "-DDEPLOYMENT_RUNTIME_SWIFT" - "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" "-Xfrontend" "-require-explicit-sendable") +if(CMAKE_SYSTEM_NAME STREQUAL "WASI") + list(APPEND _Foundation_swift_build_flags + "-D_WASI_EMULATED_SIGNAL" + "-DHAVE_STRLCPY" + "-DHAVE_STRLCAT" + ) +else() + list(APPEND _Foundation_swift_build_flags + "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" + ) +endif() + if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") list(APPEND _Foundation_common_build_flags "-D_GNU_SOURCE") From df11f82c875eb658c472bfe5e79cb2fe3331a05d Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 5 Aug 2024 20:16:22 +0100 Subject: [PATCH 3/3] Add `.windows` to `platformsWithThreads` in `Package.swift` (cherry picked from commit bc0dd484adbabdb978e68cf6905e3b44ca5117bd) --- Package.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Package.swift b/Package.swift index 8aa40e7965..2fa74e7085 100644 --- a/Package.swift +++ b/Package.swift @@ -12,6 +12,7 @@ let platformsWithThreads: [Platform] = [ .driverKit, .android, .linux, + .windows, ] var dispatchIncludeFlags: [CSetting] if let environmentPath = Context.environment["DISPATCH_INCLUDE_PATH"] {