|
| 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) |
| 16 | + |
| 17 | +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) |
| 18 | + |
| 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) |
| 31 | +endif() |
| 32 | + |
| 33 | +project(Foundation |
| 34 | + LANGUAGES C Swift) |
| 35 | + |
| 36 | +if(NOT SWIFT_SYSTEM_NAME) |
| 37 | + if(CMAKE_SYSTEM_NAME STREQUAL Darwin) |
| 38 | + set(SWIFT_SYSTEM_NAME macosx) |
| 39 | + else() |
| 40 | + set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>") |
| 41 | + endif() |
| 42 | +endif() |
| 43 | + |
| 44 | +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 45 | +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 46 | +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) |
| 47 | +set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) |
| 48 | + |
| 49 | +# Fetchable dependcies |
| 50 | +include(FetchContent) |
| 51 | +if (_SwiftFoundationICU_SourceDIR) |
| 52 | + FetchContent_Declare(SwiftFoundationICU |
| 53 | + SOURCE_DIR ${_SwiftFoundationICU_SourceDIR}) |
| 54 | +else() |
| 55 | + FetchContent_Declare(SwiftFoundationICU |
| 56 | + GIT_REPOSITORY https://github.com/apple/swift-foundation-icu.git |
| 57 | + GIT_TAG 0.0.8) |
| 58 | +endif() |
| 59 | + |
| 60 | +if (_SwiftFoundation_SourceDIR) |
| 61 | + FetchContent_Declare(SwiftFoundation |
| 62 | + SOURCE_DIR ${_SwiftFoundation_SourceDIR}) |
| 63 | +else() |
| 64 | + FetchContent_Declare(SwiftFoundation |
| 65 | + GIT_REPOSITORY https://github.com/apple/swift-foundation.git |
| 66 | + GIT_TAG main) |
| 67 | +endif() |
| 68 | +FetchContent_MakeAvailable(SwiftFoundationICU SwiftFoundation) |
| 69 | + |
| 70 | +# Precompute module triple for installation |
| 71 | +if(NOT SwiftFoundation_MODULE_TRIPLE) |
| 72 | + set(module_triple_command "${CMAKE_Swift_COMPILER}" -print-target-info) |
| 73 | + if(CMAKE_Swift_COMPILER_TARGET) |
| 74 | + list(APPEND module_triple_command -target ${CMAKE_Swift_COMPILER_TARGET}) |
| 75 | + endif() |
| 76 | + execute_process(COMMAND ${module_triple_command} OUTPUT_VARIABLE target_info_json) |
| 77 | + string(JSON module_triple GET "${target_info_json}" "target" "moduleTriple") |
| 78 | + set(SwiftFoundation_MODULE_TRIPLE "${module_triple}" CACHE STRING "swift module triple used for installed swiftmodule and swiftinterface files") |
| 79 | + mark_as_advanced(SwiftFoundation_MODULE_TRIPLE) |
| 80 | +endif() |
| 81 | + |
| 82 | +# System dependencies (fail fast if dependencies are missing) |
| 83 | +find_package(LibXml2 REQUIRED) |
| 84 | +find_package(CURL REQUIRED) |
| 85 | +find_package(dispatch CONFIG REQUIRED) |
| 86 | + |
| 87 | +# Common build flags (_CFURLSessionInterface, _CFXMLInterface, CoreFoundation) |
| 88 | +list(APPEND _Foundation_common_build_flags |
| 89 | + "-DDEPLOYMENT_RUNTIME_SWIFT" |
| 90 | + "-DCF_BUILDING_CF" |
| 91 | + "-DDEPLOYMENT_ENABLE_LIBDISPATCH" |
| 92 | + "-DHAVE_STRUCT_TIMESPEC" |
| 93 | + "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS" |
| 94 | + "-Wno-shorten-64-to-32" |
| 95 | + "-Wno-deprecated-declarations" |
| 96 | + "-Wno-unreachable-code" |
| 97 | + "-Wno-conditional-uninitialized" |
| 98 | + "-Wno-unused-variable" |
| 99 | + "-Wno-unused-function" |
| 100 | + "-Wno-microsoft-enum-forward-reference" |
| 101 | + "-fconstant-cfstrings" |
| 102 | + "-fexceptions" # TODO: not on OpenBSD |
| 103 | + "-fdollars-in-identifiers" |
| 104 | + "-fno-common" |
| 105 | + "-fcf-runtime-abi=swift" |
| 106 | + "-fblocks") |
| 107 | + |
| 108 | +if(CMAKE_BUILD_TYPE STREQUAL Debug) |
| 109 | + list(APPEND _Foundation_common_build_flags |
| 110 | + "-DDEBUG") |
| 111 | +endif() |
| 112 | + |
| 113 | +# Swift build flags (Foundation, FoundationNetworking, FoundationXML) |
| 114 | +set(_Foundation_swift_build_flags) |
| 115 | +list(APPEND _Foundation_swift_build_flags |
| 116 | + "-DDEPLOYMENT_RUNTIME_SWIFT" |
| 117 | + "-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS") |
| 118 | + |
| 119 | +if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") |
| 120 | + list(APPEND _Foundation_common_build_flags |
| 121 | + "-D_GNU_SOURCE" |
| 122 | + "-I/usr/lib/swift") # dispatch |
| 123 | +endif() |
| 124 | + |
| 125 | +include(FoundationSwiftSupport) |
| 126 | + |
| 127 | +add_subdirectory(Sources) |
| 128 | +add_subdirectory(cmake/modules) |
0 commit comments