Skip to content

Simplify build system for WASI #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)

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


if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
if(HAS_LIBDISPATCH_API)
find_package(dispatch CONFIG REQUIRED)
endif()

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

if(CMAKE_SYSTEM_NAME STREQUAL WASI)
# BlocksRuntime is already in libdispatch so is only needed if libdispatch is
# NOT being used
if(NOT HAS_LIBDISPATCH_API)
add_subdirectory(Sources/BlocksRuntime)
endif()

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

if(CMAKE_SYSTEM_NAME STREQUAL WASI)
add_dependencies(CoreFoundation BlocksRuntime)
endif()

target_include_directories(CoreFoundation PRIVATE
${CMAKE_BINARY_DIR}/uuid-headers
${CMAKE_CURRENT_BINARY_DIR}/CoreFoundation.framework/Headers)
Expand All @@ -86,15 +87,21 @@ if(ENABLE_TESTING)
endif()

if(NOT BUILD_SHARED_LIBS)
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
CoreFoundation CFXMLInterface)

if(NOT HAS_LIBDISPATCH_API)
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
CoreFoundation CFXMLInterface CFURLSessionInterface)
install(TARGETS CoreFoundation CFXMLInterface CFURLSessionInterface
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
else()
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
CoreFoundation CFXMLInterface BlocksRuntime)
install(TARGETS CoreFoundation CFXMLInterface
BlocksRuntime)
endif()

install(TARGETS CoreFoundation CFXMLInterface
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)

if(BUILD_NETWORKING)
set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS
CFURLSessionInterface)
install(TARGETS CFURLSessionInterface
DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
endif()
endif()
Expand All @@ -110,6 +117,9 @@ install(DIRECTORY
DESTINATION
${swift_lib_dir}/CoreFoundation
FILES_MATCHING PATTERN "*.h")

# TODO(katei): Remove wasm32-unknown-wasi specific modulemap which is required to link icui18n
# This workaround can be removed after https://github.com/apple/swift/pull/35936
if(CMAKE_SYSTEM_NAME STREQUAL WASI)
install(FILES
CoreFoundation/Base.subproj/$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:static/>wasm32-unknown-wasi.modulemap
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// workaround: wasm32-unknown-wasi needs to link icui18n because it's not linked by default
// on the platform.
module CoreFoundation [extern_c] [system] {
umbrella header "CoreFoundation.h"
explicit module CFPlugInCOM { header "CFPlugInCOM.h" }

link "CoreFoundation"
link "uuid"

link "BlocksRuntime"
link "icui18n"
}
79 changes: 39 additions & 40 deletions CoreFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@ endif()
set(CMAKE_POSITION_INDEPENDENT_CODE YES)


if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
if(HAS_LIBDISPATCH_API)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG OFF)
find_package(Threads REQUIRED)
endif()

if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
find_package(LibXml2 REQUIRED)
find_package(LibXml2 REQUIRED)

if(BUILD_NETWORKING)
find_package(CURL CONFIG)
if(CURL_FOUND)
set(CURL_VERSION_STRING ${CURL_VERSION})
else()
find_package(CURL REQUIRED)
endif()
else()
include(libxml2)
endif()
find_package(ICU COMPONENTS uc i18n REQUIRED)
endif()
Expand Down Expand Up @@ -393,21 +392,27 @@ target_compile_definitions(CoreFoundation
target_include_directories(CoreFoundation
PRIVATE
${PROJECT_SOURCE_DIR})

if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)

target_link_libraries(CoreFoundation PRIVATE
${CMAKE_DL_LIBS}
BlocksRuntime)

if(HAS_LIBDISPATCH_API)
target_link_libraries(CoreFoundation PRIVATE
Threads::Threads
${CMAKE_DL_LIBS}
BlocksRuntime
dispatch)
else()
add_dependencies(CoreFoundation BlocksRuntime)
endif()

if(NOT BUILD_SHARED_LIBS)
set_property(TARGET CoreFoundation PROPERTY STATIC_LIBRARY_OPTIONS
$<TARGET_OBJECTS:BlocksRuntime>)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL WASI)
# Enable emulated mman and constant CFSTRINGS
target_compile_definitions(CoreFoundation PRIVATE _WASI_EMULATED_MMAN __CONSTANT_CFSTRINGS__)
target_include_directories(CoreFoundation PRIVATE
"${PROJECT_SOURCE_DIR}/../Sources/BlocksRuntime")
target_link_libraries(CoreFoundation PRIVATE
BlocksRuntime)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Android)
target_link_libraries(CoreFoundation PRIVATE
log)
Expand Down Expand Up @@ -446,7 +451,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
PRIVATE
CURL_STATICLIB)
endif()
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin AND BUILD_NETWORKING)
target_link_libraries(CFURLSessionInterface PRIVATE
CURL::libcurl)
endif()
Expand All @@ -464,17 +469,11 @@ add_framework(CFXMLInterface
SOURCES
Parsing.subproj/CFXMLInterface.c)
add_dependencies(CFXMLInterface CoreFoundation)
target_link_libraries(CFXMLInterface PRIVATE BlocksRuntime)

if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
target_link_libraries(CFXMLInterface PRIVATE
LibXml2::LibXml2)
endif()
else()
target_include_directories(CFXMLInterface PRIVATE
"${PROJECT_SOURCE_DIR}/../Sources/BlocksRuntime"
"${LIBXML2_INCLUDE_DIRS}")
target_link_libraries(CFXMLInterface PRIVATE ${LIBXML2_LIBRARIES})
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
target_link_libraries(CFXMLInterface PRIVATE
LibXml2::LibXml2)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
Expand Down Expand Up @@ -515,9 +514,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows)
User32
mincore)
endif()
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows
AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin
AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
target_link_libraries(CoreFoundation
PRIVATE
m)
Expand All @@ -529,7 +526,7 @@ AND NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
m)
endif()

if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
if(HAS_LIBDISPATCH_API)
target_link_libraries(CoreFoundation
PRIVATE
dispatch)
Expand All @@ -540,6 +537,7 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
PRIVATE
dispatch)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
target_link_libraries(CoreFoundation
PRIVATE
Expand All @@ -555,17 +553,18 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
-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)
endif()

set(WASI_UNAVAILABLE_TARGETS)
if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
list(APPEND WASI_UNAVAILABLE_TARGETS CFURLSessionInterface)
endif()

install(TARGETS
CoreFoundation
CFXMLInterface
${WASI_UNAVAILABLE_TARGETS}
DESTINATION
"${CMAKE_INSTALL_FULL_LIBDIR}/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
CoreFoundation
CFXMLInterface
DESTINATION
"${CMAKE_INSTALL_FULL_LIBDIR}/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")

if(BUILD_NETWORKING)
install(TARGETS
CFURLSessionInterface
DESTINATION
"${CMAKE_INSTALL_FULL_LIBDIR}/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
endif()

# Needed to avoid double slash "//" when CMAKE_INSTALL_PREFIX set to "/" and DESTDIR used to relocate whole installation.
# Double slash raise CMake error "file called with network path DESTINATION //System/Library/Frameworks".
Expand Down
7 changes: 5 additions & 2 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ add_subdirectory(UUID)
add_subdirectory(Foundation)
add_subdirectory(FoundationXML)

if(NOT CMAKE_SYSTEM_NAME STREQUAL WASI)
if(BUILD_NETWORKING)
add_subdirectory(FoundationNetworking)
endif()

if(BUILD_TOOLS)
add_subdirectory(Tools)
endif()
endif()
20 changes: 13 additions & 7 deletions Sources/Foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ set_target_properties(Foundation PROPERTIES
Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/swift)

if(NOT BUILD_SHARED_LIBS)
add_dependencies(Foundation CoreFoundation uuid)
# TODO(katei): Comment out after swift-frontend implementation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify what implementation are you referring to here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added 👍

# https://github.com/apple/swift/pull/35936
# target_compile_options(Foundation
# PRIVATE
# "SHELL:-public-autolink-library icui18n")
set_property(TARGET Foundation PROPERTY STATIC_LIBRARY_OPTIONS
$<TARGET_OBJECTS:CoreFoundation>
$<TARGET_OBJECTS:uuid>
$<TARGET_OBJECTS:BlocksRuntime>)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
# NOTE: workaround for CMake which doesn't link in OBJECT libraries properly
add_dependencies(Foundation CoreFoundationResources)
Expand All @@ -172,13 +185,6 @@ elseif(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
target_link_options(Foundation PRIVATE "SHELL:-no-toolchain-stdlib-rpath")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL WASI)
target_compile_options(Foundation
PRIVATE
-sdk ${CMAKE_SYSROOT}
-target wasm32-unknown-wasi
)
endif()

set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS Foundation)
_install_target(Foundation)
9 changes: 9 additions & 0 deletions Sources/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
@usableFromInline let memset = Glibc.memset
@usableFromInline let memcpy = Glibc.memcpy
@usableFromInline let memcmp = Glibc.memcmp
#elseif canImport(WASILibc)
@usableFromInline let calloc = WASILibc.calloc
@usableFromInline let malloc = WASILibc.malloc
@usableFromInline let free = WASILibc.free
@usableFromInline let memset = WASILibc.memset
@usableFromInline let memcpy = WASILibc.memcpy
@usableFromInline let memcmp = WASILibc.memcmp
#endif

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

#if canImport(Glibc)
import Glibc
#elseif canImport(WASILibc)
import WASILibc
#endif

internal func __NSDataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) {
Expand Down
8 changes: 0 additions & 8 deletions Sources/FoundationXML/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,5 @@ if(NOT CMAKE_SYSTEM_NAME MATCHES "Darwin|Windows")
endif()


if(CMAKE_SYSTEM_NAME STREQUAL WASI)
target_compile_options(FoundationXML
PRIVATE
-sdk ${CMAKE_SYSROOT}
-target wasm32-unknown-wasi
)
endif()

set_property(GLOBAL APPEND PROPERTY Foundation_EXPORTS FoundationXML)
_install_target(FoundationXML)
4 changes: 0 additions & 4 deletions Sources/UUID/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,3 @@ if(NOT BUILD_SHARED_LIBS)
LIBRARY DESTINATION lib/swift_static/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>
RUNTIME DESTINATION bin)
endif()

install(TARGETS uuid
ARCHIVE DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>
LIBRARY DESTINATION lib/swift$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:_static>/$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>)
45 changes: 0 additions & 45 deletions cmake/modules/libxml2.cmake

This file was deleted.