Skip to content

Commit d25f8d0

Browse files
Merge pull request swiftlang#267 from swiftwasm/katei/simplify-build-system
Simplify build system for WASI
2 parents 7403b04 + ed9c432 commit d25f8d0

File tree

9 files changed

+92
-124
lines changed

9 files changed

+92
-124
lines changed

CMakeLists.txt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
3838
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
3939

4040
option(BUILD_SHARED_LIBS "build shared libraries" ON)
41+
option(HAS_LIBDISPATCH_API "has libdispatch API" ON)
42+
option(BUILD_NETWORKING "build FoundationNetworking module" ON)
43+
option(BUILD_TOOLS "build tools" ON)
4144
option(NS_CURL_ASSUME_FEATURES_MISSING "Assume that optional libcurl features are missing rather than test the library's version, for build debugging" NO)
4245

4346

44-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
47+
if(HAS_LIBDISPATCH_API)
4548
find_package(dispatch CONFIG REQUIRED)
4649
endif()
4750

@@ -54,7 +57,9 @@ set(CF_DEPLOYMENT_SWIFT YES CACHE BOOL "Build for Swift" FORCE)
5457
set(SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
5558
set(BUILD_SHARED_LIBS NO)
5659

57-
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
60+
# BlocksRuntime is already in libdispatch so is only needed if libdispatch is
61+
# NOT being used
62+
if(NOT HAS_LIBDISPATCH_API)
5863
add_subdirectory(Sources/BlocksRuntime)
5964
endif()
6065

@@ -71,10 +76,6 @@ add_custom_target(uuid-headers
7176
DEPENDS ${CMAKE_BINARY_DIR}/uuid-headers/uuid/uuid.h)
7277
add_dependencies(CoreFoundation uuid-headers)
7378

74-
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
75-
add_dependencies(CoreFoundation BlocksRuntime)
76-
endif()
77-
7879
target_include_directories(CoreFoundation PRIVATE
7980
${CMAKE_BINARY_DIR}/uuid-headers
8081
${CMAKE_CURRENT_BINARY_DIR}/CoreFoundation.framework/Headers)
@@ -86,15 +87,21 @@ if(ENABLE_TESTING)
8687
endif()
8788

8889
if(NOT BUILD_SHARED_LIBS)
89-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
90+
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
91+
CoreFoundation CFXMLInterface)
92+
93+
if(NOT HAS_LIBDISPATCH_API)
9094
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
91-
CoreFoundation CFXMLInterface CFURLSessionInterface)
92-
install(TARGETS CoreFoundation CFXMLInterface CFURLSessionInterface
93-
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
94-
else()
95-
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
96-
CoreFoundation CFXMLInterface BlocksRuntime)
97-
install(TARGETS CoreFoundation CFXMLInterface
95+
BlocksRuntime)
96+
endif()
97+
98+
install(TARGETS CoreFoundation CFXMLInterface
99+
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
100+
101+
if(BUILD_NETWORKING)
102+
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
103+
CFURLSessionInterface)
104+
install(TARGETS CFURLSessionInterface
98105
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
99106
endif()
100107
endif()
@@ -110,6 +117,9 @@ install(DIRECTORY
110117
DESTINATION
111118
${swift_lib_dir}/CoreFoundation
112119
FILES_MATCHING PATTERN "*.h")
120+
121+
# TODO(katei): Remove wasm32-unknown-wasi specific modulemap which is required to link icui18n
122+
# This workaround can be removed after https://github.com/apple/swift/pull/35936
113123
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
114124
install(FILES
115125
CoreFoundation/Base.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>wasm32-unknown-wasi.modulemap
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
// workaround: wasm32-unknown-wasi needs to link icui18n because it's not linked by default
2+
// on the platform.
13
module CoreFoundation [extern_c] [system] {
24
umbrella header "CoreFoundation.h"
35
explicit module CFPlugInCOM { header "CFPlugInCOM.h" }
46

5-
link "CoreFoundation"
6-
link "uuid"
7-
8-
link "BlocksRuntime"
97
link "icui18n"
108
}

CoreFoundation/CMakeLists.txt

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,22 @@ endif()
2121
set(CMAKE_POSITION_INDEPENDENT_CODE YES)
2222

2323

24-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
24+
if(HAS_LIBDISPATCH_API)
2525
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
2626
set(THREADS_PREFER_PTHREAD_FLAG OFF)
2727
find_package(Threads REQUIRED)
2828
endif()
2929

3030
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
31-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
32-
find_package(LibXml2 REQUIRED)
31+
find_package(LibXml2 REQUIRED)
32+
33+
if(BUILD_NETWORKING)
3334
find_package(CURL CONFIG)
3435
if(CURL_FOUND)
3536
set(CURL_VERSION_STRING ${CURL_VERSION})
3637
else()
3738
find_package(CURL REQUIRED)
3839
endif()
39-
else()
40-
include(libxml2)
4140
endif()
4241
find_package(ICU COMPONENTS uc i18n REQUIRED)
4342
endif()
@@ -393,21 +392,27 @@ target_compile_definitions(CoreFoundation
393392
target_include_directories(CoreFoundation
394393
PRIVATE
395394
${PROJECT_SOURCE_DIR})
396-
397-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
395+
396+
target_link_libraries(CoreFoundation PRIVATE
397+
${CMAKE_DL_LIBS}
398+
BlocksRuntime)
399+
400+
if(HAS_LIBDISPATCH_API)
398401
target_link_libraries(CoreFoundation PRIVATE
399402
Threads::Threads
400-
${CMAKE_DL_LIBS}
401-
BlocksRuntime
402403
dispatch)
403-
else()
404-
add_dependencies(CoreFoundation BlocksRuntime)
404+
endif()
405+
406+
if(NOT BUILD_SHARED_LIBS)
407+
set_property(TARGET CoreFoundation PROPERTY STATIC_LIBRARY_OPTIONS
408+
$<TARGET_OBJECTS:BlocksRuntime>)
409+
endif()
410+
411+
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
412+
# Enable emulated mman and constant CFSTRINGS
405413
target_compile_definitions(CoreFoundation PRIVATE _WASI_EMULATED_MMAN __CONSTANT_CFSTRINGS__)
406-
target_include_directories(CoreFoundation PRIVATE
407-
"${PROJECT_SOURCE_DIR}/../Sources/BlocksRuntime")
408-
target_link_libraries(CoreFoundation PRIVATE
409-
BlocksRuntime)
410414
endif()
415+
411416
if(CMAKE_SYSTEM_NAME STREQUAL Android)
412417
target_link_libraries(CoreFoundation PRIVATE
413418
log)
@@ -446,7 +451,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
446451
PRIVATE
447452
CURL_STATICLIB)
448453
endif()
449-
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
454+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin AND BUILD_NETWORKING)
450455
target_link_libraries(CFURLSessionInterface PRIVATE
451456
CURL::libcurl)
452457
endif()
@@ -464,17 +469,11 @@ add_framework(CFXMLInterface
464469
SOURCES
465470
Parsing.subproj/CFXMLInterface.c)
466471
add_dependencies(CFXMLInterface CoreFoundation)
472+
target_link_libraries(CFXMLInterface PRIVATE BlocksRuntime)
467473

468-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
469-
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
470-
target_link_libraries(CFXMLInterface PRIVATE
471-
LibXml2::LibXml2)
472-
endif()
473-
else()
474-
target_include_directories(CFXMLInterface PRIVATE
475-
"${PROJECT_SOURCE_DIR}/../Sources/BlocksRuntime"
476-
"${LIBXML2_INCLUDE_DIRS}")
477-
target_link_libraries(CFXMLInterface PRIVATE ${LIBXML2_LIBRARIES})
474+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
475+
target_link_libraries(CFXMLInterface PRIVATE
476+
LibXml2::LibXml2)
478477
endif()
479478

480479
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
@@ -515,9 +514,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
515514
User32
516515
mincore)
517516
endif()
518-
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows
519-
AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin
520-
AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
517+
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
521518
target_link_libraries(CoreFoundation
522519
PRIVATE
523520
m)
@@ -529,7 +526,7 @@ AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
529526
m)
530527
endif()
531528

532-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
529+
if(HAS_LIBDISPATCH_API)
533530
target_link_libraries(CoreFoundation
534531
PRIVATE
535532
dispatch)
@@ -540,6 +537,7 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
540537
PRIVATE
541538
dispatch)
542539
endif()
540+
543541
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
544542
target_link_libraries(CoreFoundation
545543
PRIVATE
@@ -555,17 +553,18 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
555553
-Xlinker;-alias_list;-Xlinker;Base.subproj/DarwinSymbolAliases;-twolevel_namespace;-sectcreate;__UNICODE;__csbitmaps;CharacterSets/CFCharacterSetBitmaps.bitmap;-sectcreate;__UNICODE;__properties;CharacterSets/CFUniCharPropertyDatabase.data;-sectcreate;__UNICODE;__data;CharacterSets/CFUnicodeData-L.mapping;-segprot;__UNICODE;r;r)
556554
endif()
557555

558-
set(WASI_UNAVAILABLE_TARGETS)
559-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
560-
list(APPEND WASI_UNAVAILABLE_TARGETS CFURLSessionInterface)
561-
endif()
562-
563556
install(TARGETS
564-
CoreFoundation
565-
CFXMLInterface
566-
${WASI_UNAVAILABLE_TARGETS}
567-
DESTINATION
568-
"${CMAKE_INSTALL_FULL_LIBDIR}/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
557+
CoreFoundation
558+
CFXMLInterface
559+
DESTINATION
560+
"${CMAKE_INSTALL_FULL_LIBDIR}/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
561+
562+
if(BUILD_NETWORKING)
563+
install(TARGETS
564+
CFURLSessionInterface
565+
DESTINATION
566+
"${CMAKE_INSTALL_FULL_LIBDIR}/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
567+
endif()
569568

570569
# Needed to avoid double slash "//" when CMAKE_INSTALL_PREFIX set to "/" and DESTDIR used to relocate whole installation.
571570
# Double slash raise CMake error "file called with network path DESTINATION //System/Library/Frameworks".

Sources/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ add_subdirectory(UUID)
22
add_subdirectory(Foundation)
33
add_subdirectory(FoundationXML)
44

5-
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
5+
if(BUILD_NETWORKING)
66
add_subdirectory(FoundationNetworking)
7+
endif()
8+
9+
if(BUILD_TOOLS)
710
add_subdirectory(Tools)
8-
endif()
11+
endif()

Sources/Foundation/CMakeLists.txt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ set_target_properties(Foundation PROPERTIES
163163
Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift
164164
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/swift)
165165

166+
if(NOT BUILD_SHARED_LIBS)
167+
add_dependencies(Foundation CoreFoundation uuid)
168+
# TODO(katei): Comment out after swift-frontend implementation
169+
# https://github.com/apple/swift/pull/35936
170+
# target_compile_options(Foundation
171+
# PRIVATE
172+
# "SHELL:-public-autolink-library icui18n")
173+
set_property(TARGET Foundation PROPERTY STATIC_LIBRARY_OPTIONS
174+
$<TARGET_OBJECTS:CoreFoundation>
175+
$<TARGET_OBJECTS:uuid>
176+
$<TARGET_OBJECTS:BlocksRuntime>)
177+
endif()
178+
166179
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
167180
# NOTE: workaround for CMake which doesn't link in OBJECT libraries properly
168181
add_dependencies(Foundation CoreFoundationResources)
@@ -172,13 +185,6 @@ elseif(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
172185
target_link_options(Foundation PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
173186
endif()
174187

175-
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
176-
target_compile_options(Foundation
177-
PRIVATE
178-
-sdk ${CMAKE_SYSROOT}
179-
-target wasm32-unknown-wasi
180-
)
181-
endif()
182188

183189
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS Foundation)
184190
_install_target(Foundation)

Sources/Foundation/Data.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
@usableFromInline let memset = Glibc.memset
2020
@usableFromInline let memcpy = Glibc.memcpy
2121
@usableFromInline let memcmp = Glibc.memcmp
22+
#elseif canImport(WASILibc)
23+
@usableFromInline let calloc = WASILibc.calloc
24+
@usableFromInline let malloc = WASILibc.malloc
25+
@usableFromInline let free = WASILibc.free
26+
@usableFromInline let memset = WASILibc.memset
27+
@usableFromInline let memcpy = WASILibc.memcpy
28+
@usableFromInline let memcmp = WASILibc.memcmp
2229
#endif
2330

2431
#if !canImport(Darwin)
@@ -32,6 +39,8 @@ internal func malloc_good_size(_ size: Int) -> Int {
3239

3340
#if canImport(Glibc)
3441
import Glibc
42+
#elseif canImport(WASILibc)
43+
import WASILibc
3544
#endif
3645

3746
internal func __NSDataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) {

Sources/FoundationXML/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,5 @@ if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
2626
endif()
2727

2828

29-
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
30-
target_compile_options(FoundationXML
31-
PRIVATE
32-
-sdk ${CMAKE_SYSROOT}
33-
-target wasm32-unknown-wasi
34-
)
35-
endif()
36-
3729
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS FoundationXML)
3830
_install_target(FoundationXML)

Sources/UUID/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,3 @@ if(NOT BUILD_SHARED_LIBS)
3232
LIBRARY DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>
3333
RUNTIME DESTINATION bin)
3434
endif()
35-
36-
install(TARGETS uuid
37-
ARCHIVE DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>
38-
LIBRARY DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)

cmake/modules/libxml2.cmake

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)