Skip to content

Commit 6450f42

Browse files
Merge pull request swiftlang#5057 from kateinoigakukun/yt/repair-wasi-build
build: Repair the build on WASI platform (take 3)
2 parents 7ccef0d + 3b8485a commit 6450f42

File tree

10 files changed

+103
-45
lines changed

10 files changed

+103
-45
lines changed

Diff for: CMakeLists.txt

+29-31
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,22 @@ if(NOT SwiftFoundation_MODULE_TRIPLE)
9393
endif()
9494

9595
# System dependencies
96-
find_package(dispatch CONFIG)
97-
if(NOT dispatch_FOUND)
98-
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
99-
set(DEFAULT_DISPATCH_INCLUDE_PATH "/usr/lib/swift")
100-
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
101-
set(DEFAULT_DISPATCH_INCLUDE_PATH "$ENV{SDKROOT}usr/include")
96+
97+
# We know libdispatch is always unavailable on WASI
98+
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
99+
find_package(dispatch CONFIG)
100+
if(NOT dispatch_FOUND)
101+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
102+
set(DEFAULT_DISPATCH_INCLUDE_PATH "/usr/lib/swift")
103+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
104+
set(DEFAULT_DISPATCH_INCLUDE_PATH "$ENV{SDKROOT}usr/include")
105+
endif()
106+
set(DISPATCH_INCLUDE_PATH "${DEFAULT_DISPATCH_INCLUDE_PATH}" CACHE STRING "A path to where you can find libdispatch headers")
107+
message("-- dispatch_DIR not found, using dispatch from SDK at ${DISPATCH_INCLUDE_PATH}")
108+
list(APPEND _Foundation_common_build_flags
109+
"-I${DISPATCH_INCLUDE_PATH}"
110+
"-I${DISPATCH_INCLUDE_PATH}/Block")
102111
endif()
103-
set(DISPATCH_INCLUDE_PATH "${DEFAULT_DISPATCH_INCLUDE_PATH}" CACHE STRING "A path to where you can find libdispatch headers")
104-
message("-- dispatch_DIR not found, using dispatch from SDK at ${DISPATCH_INCLUDE_PATH}")
105-
list(APPEND _Foundation_common_build_flags
106-
"-I${DISPATCH_INCLUDE_PATH}"
107-
"-I${DISPATCH_INCLUDE_PATH}/Block")
108112
endif()
109113
find_package(LibXml2 REQUIRED)
110114
find_package(CURL REQUIRED)
@@ -113,7 +117,6 @@ find_package(CURL REQUIRED)
113117
list(APPEND _Foundation_common_build_flags
114118
"-DDEPLOYMENT_RUNTIME_SWIFT"
115119
"-DCF_BUILDING_CF"
116-
"-DDEPLOYMENT_ENABLE_LIBDISPATCH"
117120
"-DHAVE_STRUCT_TIMESPEC"
118121
"-Wno-shorten-64-to-32"
119122
"-Wno-deprecated-declarations"
@@ -126,16 +129,10 @@ list(APPEND _Foundation_common_build_flags
126129
"-Wno-switch"
127130
"-fblocks")
128131

129-
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
130-
list(APPEND _Foundation_common_build_flags
131-
"-D_WASI_EMULATED_SIGNAL"
132-
"-DHAVE_STRLCPY"
133-
"-DHAVE_STRLCAT"
134-
)
135-
else()
136-
list(APPEND _Foundation_common_build_flags
137-
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
138-
)
132+
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
133+
list(APPEND _Foundation_common_build_flags
134+
"-DDEPLOYMENT_ENABLE_LIBDISPATCH"
135+
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")
139136
endif()
140137

141138
if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
@@ -168,16 +165,17 @@ list(APPEND _Foundation_swift_build_flags
168165
"-Xfrontend"
169166
"-require-explicit-sendable")
170167

171-
if(CMAKE_SYSTEM_NAME STREQUAL "WASI")
172-
list(APPEND _Foundation_swift_build_flags
173-
"-D_WASI_EMULATED_SIGNAL"
174-
"-DHAVE_STRLCPY"
175-
"-DHAVE_STRLCAT"
176-
)
168+
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
169+
# Enable wasi-libc emulation features
170+
set(WASI_EMULATION_DEFS _WASI_EMULATED_MMAN _WASI_EMULATED_SIGNAL _WASI_EMULATED_PROCESS_CLOCKS)
171+
foreach(def ${WASI_EMULATION_DEFS})
172+
list(APPEND _Foundation_swift_build_flags "SHELL:-Xcc -D${def}")
173+
list(APPEND _Foundation_common_build_flags "-D${def}")
174+
endforeach()
177175
else()
178-
list(APPEND _Foundation_swift_build_flags
179-
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
180-
)
176+
# Assume we have threads on other platforms
177+
list(APPEND _Foundation_swift_build_flags
178+
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")
181179
endif()
182180

183181
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")

Diff for: Package.swift

+15-6
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ let coreFoundationBuildSettings: [CSetting] = [
3939
.headerSearchPath("internalInclude"),
4040
.define("DEBUG", .when(configuration: .debug)),
4141
.define("CF_BUILDING_CF"),
42-
.define("DEPLOYMENT_ENABLE_LIBDISPATCH"),
42+
.define("DEPLOYMENT_ENABLE_LIBDISPATCH", .when(platforms: platformsWithThreads)),
4343
.define("DEPLOYMENT_RUNTIME_SWIFT"),
4444
.define("HAVE_STRUCT_TIMESPEC"),
4545
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)),
4646
.define("_GNU_SOURCE", .when(platforms: [.linux, .android])),
4747
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
48-
.define("HAVE_STRLCPY", .when(platforms: [.wasi])),
49-
.define("HAVE_STRLCAT", .when(platforms: [.wasi])),
5048
.unsafeFlags([
5149
"-Wno-shorten-64-to-32",
5250
"-Wno-deprecated-declarations",
@@ -78,8 +76,6 @@ let interfaceBuildSettings: [CSetting] = [
7876
.define("SWIFT_CORELIBS_FOUNDATION_HAS_THREADS", .when(platforms: platformsWithThreads)),
7977
.define("_GNU_SOURCE", .when(platforms: [.linux, .android])),
8078
.define("_WASI_EMULATED_SIGNAL", .when(platforms: [.wasi])),
81-
.define("HAVE_STRLCPY", .when(platforms: [.wasi])),
82-
.define("HAVE_STRLCAT", .when(platforms: [.wasi])),
8379
.unsafeFlags([
8480
"-Wno-shorten-64-to-32",
8581
"-Wno-deprecated-declarations",
@@ -169,7 +165,8 @@ let package = Package(
169165
.product(name: "FoundationEssentials", package: "swift-foundation"),
170166
"Foundation",
171167
"CoreFoundation",
172-
"_CFXMLInterface"
168+
"_CFXMLInterface",
169+
.target(name: "BlocksRuntime", condition: .when(platforms: [.wasi])),
173170
],
174171
path: "Sources/FoundationXML",
175172
exclude: [
@@ -195,6 +192,7 @@ let package = Package(
195192
name: "CoreFoundation",
196193
dependencies: [
197194
.product(name: "_FoundationICU", package: "swift-foundation-icu"),
195+
.target(name: "BlocksRuntime", condition: .when(platforms: [.wasi])),
198196
],
199197
path: "Sources/CoreFoundation",
200198
exclude: [
@@ -203,6 +201,17 @@ let package = Package(
203201
],
204202
cSettings: coreFoundationBuildSettings
205203
),
204+
.target(
205+
name: "BlocksRuntime",
206+
path: "Sources/CoreFoundation/BlockRuntime",
207+
exclude: [
208+
"CMakeLists.txt"
209+
],
210+
cSettings: [
211+
// For CFTargetConditionals.h
212+
.headerSearchPath("../include"),
213+
]
214+
),
206215
.target(
207216
name: "_CFXMLInterface",
208217
dependencies: [

Diff for: Sources/CMakeLists.txt

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
add_subdirectory(CoreFoundation)
1616
add_subdirectory(_CFXMLInterface)
17-
add_subdirectory(_CFURLSessionInterface)
17+
if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI")
18+
add_subdirectory(_CFURLSessionInterface)
19+
endif()
1820
add_subdirectory(Foundation)
1921
add_subdirectory(FoundationXML)
20-
add_subdirectory(FoundationNetworking)
22+
if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI")
23+
add_subdirectory(FoundationNetworking)
24+
endif()
2125
if(FOUNDATION_BUILD_TOOLS)
2226
add_subdirectory(plutil)
2327
endif()

Diff for: Sources/CoreFoundation/BlockRuntime/CMakeLists.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
## Licensed under Apache License v2.0
7+
##
8+
## See LICENSE.txt for license information
9+
## See CONTRIBUTORS.md for the list of Swift project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
# Build the vendored version of the BlocksRuntime library, which is used by
16+
# platforms that don't support libdispatch.
17+
18+
# Build the BlocksRuntime as an object library, shipped as a part
19+
# of libCoreFoundation.
20+
add_library(BlocksRuntime OBJECT
21+
data.c
22+
runtime.c)
23+
24+
target_include_directories(BlocksRuntime PUBLIC
25+
${CMAKE_CURRENT_SOURCE_DIR}/include
26+
# For CFTargetConditionals.h
27+
${CMAKE_CURRENT_SOURCE_DIR}/../include)
28+
29+
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS BlocksRuntime)

Diff for: Sources/CoreFoundation/CFBundle.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) {
600600
#if TARGET_OS_WASI
601601
hint = NULL;
602602
#else
603-
hint = __builtin_frame_address(0);
603+
hint = __builtin_return_address(0);
604604
#endif
605605
return _CFBundleGetBundleWithIdentifier(bundleID, hint);
606606
}

Diff for: Sources/CoreFoundation/CFString.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <stdarg.h>
3636
#include <stdio.h>
3737
#include <string.h>
38-
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
38+
#if TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD || TARGET_OS_WASI
3939
#include <unistd.h>
4040
#endif
4141
#if TARGET_OS_WASI

Diff for: Sources/CoreFoundation/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ target_link_libraries(CoreFoundation
118118
_FoundationICU
119119
dispatch)
120120

121+
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
122+
# On WASI, we use vendored BlocksRuntime instead of the one from libdispatch
123+
add_subdirectory(BlockRuntime)
124+
# Add BlocksRuntime object library to CoreFoundation static archive
125+
target_link_libraries(CoreFoundation PRIVATE BlocksRuntime)
126+
endif()
127+
121128
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS CoreFoundation)
122129

123130
# Copy Headers to known directory for direct client (XCTest) test builds

Diff for: Sources/CoreFoundation/include/CoreFoundation.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@
5858
#include "CFLocale.h"
5959
#include "CFNumber.h"
6060
#include "CFNumberFormatter.h"
61-
#if !TARGET_OS_WASI
6261
#include "CFPreferences.h"
63-
#endif
6462
#include "CFPropertyList.h"
6563
#include "CFSet.h"
6664
#include "CFString.h"
@@ -76,13 +74,17 @@
7674

7775
#include "ForSwiftFoundationOnly.h"
7876

79-
#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX
77+
#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX || TARGET_OS_WASI
78+
# if !TARGET_OS_WASI
8079
#include "CFMessagePort.h"
8180
#include "CFPlugIn.h"
81+
# endif
8282
#include "CFRunLoop.h"
8383
#include "CFStream.h"
84+
# if !TARGET_OS_WASI
8485
#include "CFSocket.h"
8586
#include "CFMachPort.h"
87+
# endif
8688

8789
#include "CFAttributedString.h"
8890
#include "CFNotificationCenter.h"

Diff for: Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h

+5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ typedef char * Class;
109109
#include <pthread.h>
110110
#endif
111111

112+
#if TARGET_OS_WASI
113+
#define HAVE_STRLCPY 1
114+
#define HAVE_STRLCAT 1
115+
#endif
116+
112117
#if TARGET_OS_WIN32
113118
#define BOOL WINDOWS_BOOL
114119

Diff for: Sources/_CFXMLInterface/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ target_include_directories(_CFXMLInterface
2020
../CoreFoundation/include
2121
PRIVATE
2222
../CoreFoundation/internalInclude
23-
/usr/include/libxml2/)
23+
${LIBXML2_INCLUDE_DIR})
2424

2525
target_compile_options(_CFXMLInterface INTERFACE
2626
"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xcc -fmodule-map-file=${CMAKE_CURRENT_SOURCE_DIR}/../CoreFoundation/include/module.modulemap>"
@@ -33,6 +33,10 @@ target_link_libraries(_CFXMLInterface PRIVATE
3333
dispatch
3434
LibXml2::LibXml2)
3535

36+
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
37+
target_link_libraries(_CFXMLInterface PRIVATE BlocksRuntime)
38+
endif()
39+
3640
if(NOT BUILD_SHARED_LIBS)
3741
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS _CFXMLInterface)
3842
install(TARGETS _CFXMLInterface

0 commit comments

Comments
 (0)