1-
2- cmake_minimum_required (VERSION 3.15.1)
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+ cmake_minimum_required (VERSION 3.24)
316
417list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR} /cmake/modules)
518
6- # NOTE(compnerd) enable CMP0091 - select MSVC runtime based on
7- # CMAKE_MSVC_RUNTIME_LIBRARY. Requires CMake 3.15 or newer
8- if (POLICY CMP0091)
9- cmake_policy (SET CMP0091 NEW)
19+ if (POLICY CMP0156)
20+ # Deduplicate linked libraries where appropriate
21+ cmake_policy (SET CMP0156 NEW)
22+ endif ()
23+
24+ if (POLICY CMP0157)
25+ # New Swift build model: improved incremental build performance and LSP support
26+ cmake_policy (SET CMP0157 NEW)
27+ endif ()
28+
29+ if (NOT DEFINED CMAKE_C_COMPILER)
30+ set (CMAKE_C_COMPILER clang)
1031endif ()
1132
1233project (Foundation
13- LANGUAGES C Swift)
14- enable_testing ()
34+ LANGUAGES C Swift)
1535
1636if (NOT SWIFT_SYSTEM_NAME)
1737 if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
@@ -21,143 +41,115 @@ if(NOT SWIFT_SYSTEM_NAME)
2141 endif ()
2242endif ()
2343
24- # NOTE(compnerd) default to /MD or /MDd by default based on the configuration.
25- # Cache the variable to allow the user to alter the configuration.
26- set (CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL" CACHE
27- STRING "MSVC Runtime Library" )
28-
29- if (CMAKE_VERSION VERSION_LESS 3.16.0)
30- if (NOT (CMAKE_SYSTEM_NAME STREQUAL Windows OR CMAKE_SYSTEM_NAME STREQUAL Darwin))
31- set (CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG "-Xlinker -rpath -Xlinker " )
32- set (CMAKE_SHARED_LIBRARY_RUNTIME_Swift_FLAG_SEP ":" )
33- endif ()
34- # Workaround for CMake 3.15 which doesn't link libraries properly on Windows
35- set (CMAKE_LINK_LIBRARY_FLAG "-l" )
36- endif ()
37-
38- if (CMAKE_VERSION VERSION_LESS 3.16 AND CMAKE_SYSTEM_NAME STREQUAL Windows)
39- set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin)
40- set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin)
41- else ()
42- set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /lib)
43- set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /lib)
44- endif ()
44+ set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /lib)
45+ set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /lib)
4546set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} /bin)
4647set (CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR} /swift)
4748
4849option (BUILD_SHARED_LIBS "build shared libraries" ON )
49- option (BUILD_FULLY_STATIC "build fully static" NO )
50- option (HAS_LIBDISPATCH_API "has libdispatch API" ON )
51- option (FOUNDATION_ENABLE_FOUNDATION_NETWORKING "build FoundationNetworking module" ON )
52- option (FOUNDATION_BUILD_TOOLS "build tools" ON )
53- option (NS_CURL_ASSUME_FEATURES_MISSING "Assume that optional libcurl features are missing rather than test the library's version, for build debugging" NO )
54-
55- if (HAS_LIBDISPATCH_API)
56- find_package (dispatch CONFIG REQUIRED)
57- endif ()
5850
59- find_package (ICU COMPONENTS uc i18n REQUIRED OPTIONAL_COMPONENTS data)
60-
61- # This is needed if we're statically linking, otherwise we can't pull in Dispatch
62- # because we won't have RT::rt as a CMake target.
63- if (NOT CMAKE_SYSTEM_NAME STREQUAL Android)
64- find_package (LibRT)
51+ # Optionally build tools (on by default) but only when building shared libraries
52+ if (BUILD_SHARED_LIBS )
53+ option (FOUNDATION_BUILD_TOOLS "build tools" ON )
6554endif ()
6655
67- include (SwiftSupport)
68- include (GNUInstallDirs)
69- include (XCTest)
70-
71- set (CF_DEPLOYMENT_SWIFT YES CACHE BOOL "Build for Swift" FORCE)
56+ set (CMAKE_POSITION_INDEPENDENT_CODE YES )
7257
73- set (CMAKE_THREAD_PREFER_PTHREAD TRUE )
74- set (THREADS_PREFER_PTHREAD_FLAG OFF )
75- if (HAS_LIBDISPATCH_API)
76- find_package (Threads REQUIRED)
58+ # Fetchable dependcies
59+ include (FetchContent)
60+ if (_SwiftFoundationICU_SourceDIR)
61+ FetchContent_Declare(SwiftFoundationICU
62+ SOURCE_DIR ${_SwiftFoundationICU_SourceDIR} )
63+ else ()
64+ FetchContent_Declare(SwiftFoundationICU
65+ GIT_REPOSITORY https://github.com/apple /swift-foundation-icu.git
66+ GIT_TAG 0.0.8)
7767endif ()
7868
79- set (SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS} )
80- set (BUILD_SHARED_LIBS NO )
81- add_subdirectory (CoreFoundation EXCLUDE_FROM_ALL )
82- set (BUILD_SHARED_LIBS ${SAVED_BUILD_SHARED_LIBS} )
83-
84- # BlocksRuntime is already in libdispatch so it is only needed if libdispatch is
85- # NOT being used
86- if (NOT HAS_LIBDISPATCH_API)
87- add_subdirectory (Sources /BlocksRuntime)
69+ if (_SwiftFoundation_SourceDIR)
70+ FetchContent_Declare(SwiftFoundation
71+ SOURCE_DIR ${_SwiftFoundation_SourceDIR} )
72+ else ()
73+ FetchContent_Declare(SwiftFoundation
74+ GIT_REPOSITORY https://github.com/apple /swift-foundation.git
75+ GIT_TAG main)
8876endif ()
89-
90- # Setup include paths for uuid/uuid.h
91- add_custom_command (OUTPUT ${CMAKE_BINARY_DIR} /uuid-headers/uuid/uuid.h
92- COMMAND
93- ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR} /uuid-headers/uuid
94- COMMAND
95- ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR} /Sources /UUID/uuid.h ${CMAKE_BINARY_DIR} /uuid-headers/uuid/uuid.h)
96- add_custom_target (uuid-headers
97- DEPENDS ${CMAKE_BINARY_DIR} /uuid-headers/uuid/uuid.h)
98- add_dependencies (CoreFoundation uuid-headers)
99- target_include_directories (CoreFoundation PRIVATE
100- ${CMAKE_BINARY_DIR} /uuid-headers
101- ${CMAKE_CURRENT_BINARY_DIR} /CoreFoundation.framework /Headers)
102-
103- add_subdirectory (Sources )
104- if (ENABLE_TESTING)
105- find_package (XCTest CONFIG REQUIRED)
106- add_subdirectory (Tests)
77+ FetchContent_MakeAvailable(SwiftFoundationICU SwiftFoundation)
78+
79+ include (CheckLinkerFlag)
80+
81+ check_linker_flag(C "LINKER:--build-id=sha1" LINKER_SUPPORTS_BUILD_ID)
82+
83+ # Precompute module triple for installation
84+ if (NOT SwiftFoundation_MODULE_TRIPLE)
85+ set (module_triple_command "${CMAKE_Swift_COMPILER} " -print-target -info)
86+ if (CMAKE_Swift_COMPILER_TARGET)
87+ list (APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET} )
88+ endif ()
89+ execute_process (COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json)
90+ string (JSON module_triple GET "${target_info_json} " "target" "moduleTriple" )
91+ set (SwiftFoundation_MODULE_TRIPLE "${module_triple} " CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files" )
92+ mark_as_advanced (SwiftFoundation_MODULE_TRIPLE)
10793endif ()
10894
109- if (NOT BUILD_SHARED_LIBS )
110- set_property (GLOBAL APPEND PROPERTY Foundation_EXPORTS
111- CoreFoundation CFXMLInterface)
112-
113- if (NOT HAS_LIBDISPATCH_API)
114- set_property (GLOBAL APPEND PROPERTY Foundation_EXPORTS
115- BlocksRuntime)
116- endif ()
117-
118- install (TARGETS CoreFoundation CFXMLInterface
119- DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} )
95+ # System dependencies (fail fast if dependencies are missing)
96+ find_package (LibXml2 REQUIRED)
97+ find_package (CURL REQUIRED)
98+ find_package (dispatch CONFIG REQUIRED)
99+
100+ # Common build flags (_CFURLSessionInterface, _CFXMLInterface, CoreFoundation)
101+ list (APPEND _Foundation_common_build_flags
102+ "-DDEPLOYMENT_RUNTIME_SWIFT"
103+ "-DCF_BUILDING_CF"
104+ "-DDEPLOYMENT_ENABLE_LIBDISPATCH"
105+ "-DHAVE_STRUCT_TIMESPEC"
106+ "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
107+ "-Wno-shorten-64-to-32"
108+ "-Wno-deprecated-declarations"
109+ "-Wno-unreachable-code"
110+ "-Wno-conditional-uninitialized"
111+ "-Wno-unused-variable"
112+ "-Wno-unused-function"
113+ "-Wno-microsoft-enum-forward-reference"
114+ "-Wno-int-conversion"
115+ "-fblocks" )
116+
117+ if (NOT "${CMAKE_C_SIMULATE_ID} " STREQUAL "MSVC" )
118+ list (APPEND _Foundation_common_build_flags
119+ "-fconstant-cfstrings"
120+ "-fdollars-in-identifiers"
121+ "-fno-common"
122+ "-fcf-runtime-abi=swift" )
123+
124+ if (NOT CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
125+ list (APPEND _Foundation_common_build_flags
126+ "-fexceptions" )
127+ endif ()
128+ else ()
129+ list (APPEND _Foundation_common_build_flags
130+ "/EHsc"
131+ "/clang:-fcf-runtime-abi=swift" )
132+ endif ()
120133
121- if (FOUNDATION_ENABLE_FOUNDATION_NETWORKING)
122- set_property (GLOBAL APPEND PROPERTY Foundation_EXPORTS
123- CFURLSessionInterface)
124- install (TARGETS CFURLSessionInterface
125- DESTINATION lib/swift_static/${SWIFT_SYSTEM_NAME} )
126- endif ()
134+ if (CMAKE_BUILD_TYPE STREQUAL Debug)
135+ list (APPEND _Foundation_common_build_flags
136+ "-DDEBUG" )
127137endif ()
128138
129- set (swift_lib_dir "lib/swift" )
130- if (NOT BUILD_SHARED_LIBS )
131- set (swift_lib_dir "lib/swift_static" )
139+ # Swift build flags (Foundation, FoundationNetworking, FoundationXML)
140+ set (_Foundation_swift_build_flags)
141+ list (APPEND _Foundation_swift_build_flags
142+ "-DDEPLOYMENT_RUNTIME_SWIFT"
143+ "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" )
144+
145+ if (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android" )
146+ list (APPEND _Foundation_common_build_flags
147+ "-D_GNU_SOURCE"
148+ "-I/usr/lib/swift" ) # dispatch
132149endif ()
133150
134- # TODO(compnerd) install as a Framework as that is how swift actually is built
135- install (DIRECTORY
136- ${CMAKE_CURRENT_BINARY_DIR} /CoreFoundation.framework /Headers/
137- DESTINATION
138- ${swift_lib_dir} /CoreFoundation
139- FILES_MATCHING PATTERN "*.h" )
140- install (FILES
141- CoreFoundation/Base.subproj/$<$<NOT :$<BOOL :${BUILD_SHARED_LIBS} >>:static />module.map
142- DESTINATION
143- ${swift_lib_dir} /CoreFoundation)
144- install (DIRECTORY
145- ${CMAKE_CURRENT_BINARY_DIR} /CFURLSessionInterface.framework /Headers/
146- DESTINATION
147- ${swift_lib_dir} /CFURLSessionInterface
148- FILES_MATCHING PATTERN "*.h" )
149- install (FILES
150- CoreFoundation/URL.subproj/$<$<NOT :$<BOOL :${BUILD_SHARED_LIBS} >>:static />module.map
151- DESTINATION
152- ${swift_lib_dir} /CFURLSessionInterface)
153- install (DIRECTORY
154- ${CMAKE_CURRENT_BINARY_DIR} /CFXMLInterface.framework /Headers/
155- DESTINATION
156- ${swift_lib_dir} /CFXMLInterface
157- FILES_MATCHING PATTERN "*.h" )
158- install (FILES
159- CoreFoundation/Parsing.subproj/$<$<NOT :$<BOOL :${BUILD_SHARED_LIBS} >>:static />module.map
160- DESTINATION
161- ${swift_lib_dir} /CFXMLInterface)
151+ include (GNUInstallDirs)
152+ include (FoundationSwiftSupport)
162153
154+ add_subdirectory (Sources )
163155add_subdirectory (cmake/modules)
0 commit comments