Skip to content

Commit ccf8e31

Browse files
committed
Introduce Cmake support for SwiftCorelibsFoundation
1 parent 0f1a45b commit ccf8e31

File tree

20 files changed

+323
-4
lines changed

20 files changed

+323
-4
lines changed

CMakeLists.txt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.22)
16+
17+
if(POLICY CMP0156)
18+
# Deduplicate linked libraries where appropriate
19+
cmake_policy(SET CMP0156 NEW)
20+
endif()
21+
22+
if(POLICY CMP0157)
23+
# New Swift build model: improved incremental build performance and LSP support
24+
cmake_policy(SET CMP0157 NEW)
25+
endif()
26+
27+
set(CMAKE_C_COMPILER "/usr/bin/clang")
28+
29+
project(Foundation
30+
LANGUAGES C Swift)
31+
32+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
33+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
34+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
35+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
36+
37+
# Fetchable dependcies
38+
include(FetchContent)
39+
if (_SwiftFoundationICU_SourceDIR)
40+
FetchContent_Declare(SwiftFoundationICU
41+
SOURCE_DIR ${_SwiftFoundationICU_SourceDIR})
42+
else()
43+
FetchContent_Declare(SwiftFoundationICU
44+
GIT_REPOSITORY https://github.com/iCharlesHu/swift-foundation-icu.git
45+
GIT_TAG charles/cmake-support)
46+
endif()
47+
FetchContent_MakeAvailable(SwiftFoundationICU)
48+
49+
if (_SwiftFoundation_SourceDIR)
50+
FetchContent_Declare(SwiftFoundation
51+
SOURCE_DIR ${_SwiftFoundation_SourceDIR})
52+
else()
53+
FetchContent_Declare(SwiftFoundation
54+
GIT_REPOSITORY https://github.com/iCharlesHu/swift-foundation.git
55+
GIT_TAG charles/cmake-support)
56+
endif()
57+
FetchContent_MakeAvailable(SwiftFoundation)
58+
59+
# System dependencies (fail fast if dependencies are missing)
60+
find_package(LibXml2 REQUIRED)
61+
find_package(CURL REQUIRED)
62+
63+
# Common build flags (_CFURLSessionInterface, _CFXMLInterface, CoreFoundation)
64+
set(_Foundation_common_build_flags)
65+
list(APPEND _Foundation_common_build_flags
66+
"-DDEPLOYMENT_RUNTIME_SWIFT"
67+
"-DCF_BUILDING_CF"
68+
"-DDEPLOYMENT_ENABLE_LIBDISPATCH"
69+
"-DHAVE_STRUCT_TIMESPEC"
70+
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS"
71+
"-Wno-shorten-64-to-32"
72+
"-Wno-deprecated-declarations"
73+
"-Wno-unreachable-code"
74+
"-Wno-conditional-uninitialized"
75+
"-Wno-unused-variable"
76+
"-Wno-unused-function"
77+
"-Wno-microsoft-enum-forward-reference"
78+
"-fconstant-cfstrings"
79+
"-fexceptions" # TODO: not on OpenBSD
80+
"-fdollars-in-identifiers"
81+
"-fno-common"
82+
"-fcf-runtime-abi=swift"
83+
"-fblocks")
84+
85+
if(CMAKE_BUILD_TYPE STREQUAL Debug)
86+
list(APPEND _Foundation_common_build_flags
87+
"-DDEBUG")
88+
endif()
89+
90+
# Swift build flags (Foundation, FoundationNetworking, FoundationXML)
91+
set(_Foundation_swift_build_flags)
92+
list(APPEND _Foundation_swift_build_flags
93+
"-DDEPLOYMENT_RUNTIME_SWIFT"
94+
"-DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")
95+
96+
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
97+
list(APPEND _Foundation_common_build_flags
98+
"-D_GNU_SOURCE"
99+
"-I/usr/lib/swift") # dispatch
100+
endif()
101+
102+
add_subdirectory(Sources)

Sources/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
add_subdirectory(CoreFoundation)
16+
add_subdirectory(_CFXMLInterface)
17+
add_subdirectory(_CFURLSessionInterface)
18+
add_subdirectory(Foundation)
19+
add_subdirectory(FoundationXML)
20+
add_subdirectory(FoundationNetworking)

Sources/CoreFoundation/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
file(GLOB_RECURSE _CoreFoundationSources "*.c")
16+
add_library(_CoreFoundation STATIC ${_CoreFoundationSources})
17+
18+
target_include_directories(_CoreFoundation
19+
PUBLIC
20+
include
21+
PRIVATE
22+
internalInclude)
23+
24+
set(_CoreFoundation_build_flags ${_Foundation_common_build_flags})
25+
list(APPEND _CoreFoundation_build_flags
26+
"-DCF_CHARACTERSET_UNICODE_DATA_L=\"${CMAKE_SOURCE_DIR}/Sources/CoreFoundation/CFUnicodeData-L.mapping\""
27+
"-DCF_CHARACTERSET_UNICODE_DATA_B=\"${CMAKE_SOURCE_DIR}/Sources/CoreFoundation/CFUnicodeData-B.mapping\""
28+
"-DCF_CHARACTERSET_UNICHAR_DB=\"${CMAKE_SOURCE_DIR}/Sources/CoreFoundation/CFUniCharPropertyDatabase.data\""
29+
"-DCF_CHARACTERSET_BITMAP=\"${CMAKE_SOURCE_DIR}/Sources/CoreFoundation/CFCharacterSetBitmaps.bitmap\"")
30+
31+
target_compile_options(_CoreFoundation PRIVATE
32+
"SHELL:$<$<COMPILE_LANGUAGE:C>:${_CoreFoundation_build_flags}>")
33+
34+
target_precompile_headers(_CoreFoundation PRIVATE internalInclude/CoreFoundation_Prefix.h)
35+
36+
target_link_libraries(_CoreFoundation PUBLIC
37+
FoundationICU)
38+
39+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS _CoreFoundation)

Sources/CoreFoundation/include/CoreFoundation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777
#include "ForSwiftFoundationOnly.h"
7878

79-
#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32
79+
#if TARGET_OS_OSX || TARGET_OS_IPHONE || TARGET_OS_WIN32 || TARGET_OS_LINUX
8080
#include "CFMessagePort.h"
8181
#include "CFPlugIn.h"
8282
#include "CFRunLoop.h"

Sources/CoreFoundation/module.modulemap renamed to Sources/CoreFoundation/include/module.modulemap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
framework module _CoreFoundation [extern_c] [system] {
1+
module _CoreFoundation {
22
umbrella header "CoreFoundation.h"
33
explicit module CFPlugInCOM { header "CFPlugInCOM.h" }
44

Sources/Foundation/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
file(GLOB_RECURSE _FoundationSources "*.swift")
16+
add_library(Foundation SHARED ${_FoundationSources})
17+
18+
target_compile_options(Foundation PRIVATE
19+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:${_Foundation_swift_build_flags}>")
20+
21+
target_link_libraries(Foundation PRIVATE
22+
FoundationEssentials
23+
FoundationInternationalization
24+
_CoreFoundation)
25+
26+
install(TARGETS Foundation
27+
ARCHIVE DESTINATION lib
28+
LIBRARY DESTINATION lib
29+
RUNTIME DESTINATION bin)
30+
31+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS Foundation)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
file(GLOB_RECURSE _FoundationNetworkingSources "*.swift")
16+
add_library(FoundationNetworking SHARED ${_FoundationNetworkingSources})
17+
18+
target_compile_options(FoundationNetworking PRIVATE
19+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:${_Foundation_swift_build_flags}>")
20+
21+
target_link_libraries(FoundationNetworking PRIVATE
22+
FoundationEssentials
23+
Foundation
24+
_CoreFoundation
25+
_CFURLSessionInterface)
26+
27+
install(TARGETS FoundationNetworking
28+
ARCHIVE DESTINATION lib
29+
LIBRARY DESTINATION lib
30+
RUNTIME DESTINATION bin)
31+
32+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS FoundationNetworking)

Sources/FoundationXML/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
file(GLOB_RECURSE _FoundationXMLSources "*.swift")
16+
add_library(FoundationXML SHARED ${_FoundationXMLSources})
17+
18+
target_compile_options(FoundationXML PRIVATE
19+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:${_Foundation_swift_build_flags}>")
20+
21+
target_link_libraries(FoundationXML PRIVATE
22+
FoundationEssentials
23+
Foundation
24+
_CoreFoundation
25+
_CFXMLInterface)
26+
27+
install(TARGETS FoundationXML
28+
ARCHIVE DESTINATION lib
29+
LIBRARY DESTINATION lib
30+
RUNTIME DESTINATION bin)
31+
32+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS FoundationXML)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
file(GLOB_RECURSE _CFURLSessionInterfaceSources "*.c")
16+
add_library(_CFURLSessionInterface STATIC ${_CFURLSessionInterfaceSources})
17+
18+
target_include_directories(_CFURLSessionInterface
19+
PUBLIC
20+
include
21+
PRIVATE
22+
../CoreFoundation/internalInclude
23+
../CoreFoundation/include)
24+
25+
target_precompile_headers(_CFURLSessionInterface PRIVATE ${CMAKE_SOURCE_DIR}/Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h)
26+
27+
target_compile_options(_CFURLSessionInterface PRIVATE
28+
"SHELL:$<$<COMPILE_LANGUAGE:C>:${_Foundation_common_build_flags}>")
29+
30+
target_link_libraries(_CFURLSessionInterface PRIVATE
31+
_CoreFoundation)

Sources/_CFURLSessionInterface/include/CFURLSessionInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "CFTargetConditionals.h"
3131
#include "CFBase.h"
3232
#include <stdio.h>
33+
#include <stdbool.h>
3334
#if defined(_WIN32)
3435
#include <winsock2.h>
3536
#endif

Sources/_CFURLSessionInterface/module.modulemap renamed to Sources/_CFURLSessionInterface/include/module.modulemap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
framework module _CFURLSessionInterface [extern_c] [system] {
1+
module _CFURLSessionInterface {
22
umbrella header "CFURLSessionInterface.h"
33

44
export *
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
file(GLOB_RECURSE _CFXMLInterfaceSources "*.c")
16+
add_library(_CFXMLInterface STATIC ${_CFXMLInterfaceSources})
17+
18+
target_include_directories(_CFXMLInterface
19+
PUBLIC
20+
include
21+
PRIVATE
22+
../CoreFoundation/internalInclude
23+
../CoreFoundation/include
24+
/usr/include/libxml2/)
25+
26+
target_compile_options(_CFXMLInterface PRIVATE
27+
"SHELL:$<$<COMPILE_LANGUAGE:C>:${_Foundation_common_build_flags}>")
28+
29+
target_link_libraries(_CFXMLInterface PRIVATE
30+
_CoreFoundation)

Sources/_CFXMLInterface/include/CFXMLInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "CFTargetConditionals.h"
1818
#include "CFBase.h"
19+
#include "CFError.h"
1920
#include <stdint.h>
2021
#include <sys/types.h>
2122
#include <stdbool.h>

Sources/_CFXMLInterface/module.modulemap renamed to Sources/_CFXMLInterface/include/module.modulemap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
framework module _CFXMLInterface [extern_c] [system] {
1+
module _CFXMLInterface {
22
umbrella header "CFXMLInterface.h"
33

44
export *

0 commit comments

Comments
 (0)