Skip to content

Commit ded2f28

Browse files
committed
Introduce Cmake support for SwiftFoundation
1 parent d73d7c1 commit ded2f28

File tree

11 files changed

+306
-3
lines changed

11 files changed

+306
-3
lines changed

CMakeLists.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
cmake_minimum_required(VERSION 3.22)
16+
17+
project(SwiftFoundation
18+
LANGUAGES C Swift)
19+
20+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
21+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
22+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
23+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
24+
25+
set(BUILD_TESTING NO)
26+
27+
# Make sure our dependencies exists
28+
find_package(SwiftCollections CONFIG REQUIRED)
29+
find_package(SwiftFoundationICU CONFIG REQUIRED)
30+
# In the future we will fetch these dependencies if none found
31+
32+
# Availability Macros
33+
set(_SwiftFoundation_availability_macros)
34+
list(APPEND _SwiftFoundation_availability_macros
35+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPreview 0.1:macOS 13.3, iOS 16.4, tvOS 16.4, watchOS 9.4\">"
36+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPreview 0.2:macOS 13.3, iOS 16.4, tvOS 16.4, watchOS 9.4\">"
37+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPreview 0.3:macOS 13.3, iOS 16.4, tvOS 16.4, watchOS 9.4\">"
38+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPreview 0.4:macOS 13.3, iOS 16.4, tvOS 16.4, watchOS 9.4\">"
39+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.1:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
40+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.2:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
41+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.3:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
42+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.4:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
43+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.1:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">"
44+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.2:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">"
45+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.3:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">"
46+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.4:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">")
47+
48+
add_subdirectory(Sources)
49+
add_subdirectory(cmake/modules)

Sources/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
add_subdirectory(_CShims)
16+
add_subdirectory(FoundationEssentials)
17+
add_subdirectory(FoundationInternationalization)
18+
add_subdirectory(Foundation)

Sources/Foundation/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
file(GLOB_RECURSE _FoundationPreviewSources "*.swift")
16+
add_library(FoundationPreview ${_FoundationPreviewSources})
17+
18+
# Dependencies
19+
find_package(SwiftFoundationICU CONFIG REQUIRED)
20+
add_dependencies(FoundationPreview FoundationEssentials)
21+
add_dependencies(FoundationPreview FoundationInternationalization)
22+
23+
target_link_libraries(FoundationPreview PRIVATE
24+
FoundationEssentials
25+
FoundationInternationalization
26+
SwiftFoundationICU::FoundationICU)
27+
28+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS FoundationPreview)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
file(GLOB_RECURSE _FoundationEssentialsSources "*.swift")
16+
add_library(FoundationEssentials ${_FoundationEssentialsSources})
17+
18+
# Configure FoundationMacros
19+
include(ExternalProject)
20+
ExternalProject_Add(FoundationMacros
21+
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../FoundationMacros"
22+
INSTALL_COMMAND "")
23+
ExternalProject_Get_Property(FoundationMacros BINARY_DIR)
24+
if(CMAKE_HOST_WIN32)
25+
set(FoundationMacrosPath "${BINARY_DIR}/FoundationMacros.exe#FoundationMacros")
26+
else()
27+
set(FoundationMacrosPath "${BINARY_DIR}/FoundationMacros#FoundationMacros")
28+
endif()
29+
30+
add_dependencies(FoundationEssentials _CShims)
31+
add_dependencies(FoundationEssentials FoundationMacros)
32+
33+
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
34+
target_compile_options(FoundationEssentials PRIVATE "-D_GNU_SOURCE")
35+
endif()
36+
37+
target_compile_options(FoundationEssentials PRIVATE
38+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend VariadicGenerics>"
39+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>")
40+
target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_availability_macros})
41+
target_compile_options(FoundationEssentials PRIVATE -load-plugin-executable "${FoundationMacrosPath}")
42+
target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation")
43+
44+
find_package(SwiftCollections CONFIG REQUIRED)
45+
46+
target_link_libraries(FoundationEssentials PUBLIC
47+
_CShims
48+
SwiftCollections::OrderedCollections
49+
SwiftCollections::_RopeModule)
50+
51+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS FoundationEssentials)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
file(GLOB_RECURSE _FoundationInternationalizationSources "*.swift")
16+
add_library(FoundationInternationalization ${_FoundationInternationalizationSources})
17+
18+
target_compile_options(FoundationInternationalization PRIVATE
19+
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>")
20+
target_compile_options(FoundationInternationalization PRIVATE ${_SwiftFoundation_availability_macros})
21+
target_compile_options(FoundationInternationalization PRIVATE -package-name "SwiftFoundation")
22+
23+
find_package(SwiftFoundationICU CONFIG REQUIRED)
24+
25+
# Dependencies
26+
add_dependencies(FoundationInternationalization _CShims)
27+
add_dependencies(FoundationInternationalization FoundationEssentials)
28+
29+
target_link_libraries(FoundationInternationalization PRIVATE
30+
_CShims
31+
FoundationEssentials
32+
SwiftFoundationICU::FoundationICU)
33+
34+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS FoundationInternationalization)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
cmake_minimum_required(VERSION 3.22)
16+
17+
if(POLICY CMP0157)
18+
cmake_policy(SET CMP0157 NEW)
19+
endif()
20+
21+
project(FoundationMacros
22+
LANGUAGES Swift)
23+
24+
find_package(SwiftSyntax CONFIG)
25+
if (NOT SwiftSyntax_FOUND)
26+
include(FetchContent)
27+
28+
FetchContent_Declare(SwiftSyntax
29+
GIT_REPOSITORY https://github.com/apple/swift-syntax.git
30+
GIT_TAG 84a4bedfd1294f6ee18e6dc9ad70df55fa6230f6)
31+
FetchContent_MakeAvailable(SwiftSyntax)
32+
endif()
33+
34+
file(GLOB_RECURSE _FoundationMacrosSources "*.swift")
35+
add_executable(FoundationMacros ${_FoundationMacrosSources})
36+
37+
# Link against SwiftSyntax
38+
target_compile_options(FoundationMacros PRIVATE -parse-as-library)
39+
target_link_libraries(FoundationMacros
40+
SwiftSyntax
41+
SwiftSyntaxMacros
42+
SwiftCompilerPlugin)

Sources/_CShims/CMakeLists.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
file(GLOB_RECURSE _CShimsSources "*.c")
16+
add_library(_CShims STATIC ${_CShimsSources})
17+
18+
target_include_directories(_CShims PUBLIC include)
19+
20+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
21+
target_compile_options(_CShims PUBLIC
22+
"$<$<COMPILE_LANGUAGE:C,CXX>:SHELL:-D_CRT_SECURE_NO_WARNINGS>")
23+
endif()
24+
25+
set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS _CShims)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module _CShims {
2+
header "_CShimsMacros.h"
3+
header "_CShimsTargetConditionals.h"
4+
header "_CStdlib.h"
5+
header "bplist_shims.h"
6+
header "CFUniCharBitmapData.h"
7+
header "CFUniCharBitmapData.inc.h"
8+
header "filemanager_shims.h"
9+
header "io_shims.h"
10+
header "platform_shims.h"
11+
header "string_shims.h"
12+
header "uuid.h"
13+
14+
export *
15+
}

Sources/_CShims/include/uuid.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* %End-Header%
3131
*/
3232

33-
#ifndef _UUID_UUID_H
34-
#define _UUID_UUID_H
33+
#ifndef _CSHIMS_UUID_UUID_H
34+
#define _CSHIMS_UUID_UUID_H
3535

3636
#include "_CShimsTargetConditionals.h"
3737
#include "_CShimsMacros.h"
@@ -78,4 +78,4 @@ INTERNAL void _foundation_uuid_unparse_upper(const uuid_t uu, uuid_string_t out)
7878
}
7979
#endif
8080

81-
#endif /* _UUID_UUID_H */
81+
#endif /* _CSHIMS_UUID_UUID_H */

cmake/modules/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
set(SWIFT_FOUNDATION_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/SwiftFoundationExports.cmake)
16+
17+
configure_file(SwiftFoundationConfig.cmake.in
18+
${CMAKE_CURRENT_BINARY_DIR}/SwiftFoundationConfig.cmake)
19+
20+
get_property(SWIFT_FOUNDATION_EXPORTS GLOBAL PROPERTY SWIFT_FOUNDATION_EXPORTS)
21+
export(TARGETS ${SWIFT_FOUNDATION_EXPORTS}
22+
NAMESPACE SwiftFoundation::
23+
FILE ${SWIFT_FOUNDATION_EXPORTS_FILE}
24+
EXPORT_LINK_INTERFACE_LIBRARIES)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the SwiftFoundation open source project
4+
##
5+
## Copyright (c) 2024 Apple Inc. and the SwiftFoundation 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 SwiftFoundation project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
if(NOT TARGET SwiftFoundation)
16+
include(@SWIFT_FOUNDATION_EXPORTS_FILE@)
17+
endif()

0 commit comments

Comments
 (0)