Skip to content

Introduce Cmake support for SwiftFoundation #573

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
Jun 17, 2024
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
90 changes: 90 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

cmake_minimum_required(VERSION 3.24)

if(POLICY CMP0156)
# Deduplicate linked libraries where appropriate
cmake_policy(SET CMP0156 NEW)
endif()

if(POLICY CMP0157)
# New Swift build model: improved incremental build performance and LSP support
cmake_policy(SET CMP0157 NEW)
endif()

project(SwiftFoundation
LANGUAGES C Swift)

if(NOT SWIFT_SYSTEM_NAME)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(SWIFT_SYSTEM_NAME macosx)
else()
set(SWIFT_SYSTEM_NAME "$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>")
endif()
endif()

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)

set(BUILD_TESTING NO)

set(COLLECTIONS_SINGLE_MODULE YES)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm okay to leave them for now, but we should probably move these settings into a separate CMake cache file so that if folks need to cross-compile SwiftFoundation for use outside of the prebuilt SDK, they can do so in a way that is configurable.

set(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE YES)

# Make sure our dependencies exists
include(FetchContent)
if (_SwiftFoundationICU_SourceDIR)
FetchContent_Declare(SwiftFoundationICU
SOURCE_DIR ${_SwiftFoundationICU_SourceDIR})
else()
FetchContent_Declare(SwiftFoundationICU
GIT_REPOSITORY https://github.com/apple/swift-foundation-icu.git
GIT_TAG 0.0.8)
endif()

if (_SwiftCollections_SourceDIR)
FetchContent_Declare(SwiftCollections
SOURCE_DIR ${_SwiftCollections_SourceDIR})
else()
FetchContent_Declare(SwiftCollections
GIT_REPOSITORY https://github.com/apple/swift-collections.git
GIT_TAG 1.1.1)
endif()
FetchContent_MakeAvailable(SwiftFoundationICU SwiftCollections)

list(APPEND CMAKE_MODULE_PATH ${SwiftFoundation_SOURCE_DIR}/cmake/modules)

# Availability Macros (only applies to FoundationEssentials and FoundationInternationalization)
set(_SwiftFoundation_availability_macros)
list(APPEND _SwiftFoundation_availability_macros
"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\">"
"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\">"
"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\">"
"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\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.1:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.2:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.3:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicate 0.4:macOS 14, iOS 17, tvOS 17, watchOS 10\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.1:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.2:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.3:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend \"AvailabilityMacro=FoundationPredicateRegex 0.4:macOS 10000, iOS 10000, tvOS 10000, watchOS 10000\">")

include(SwiftFoundationSwiftSupport)

add_subdirectory(Sources)
add_subdirectory(cmake/modules)
33 changes: 31 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ let package = Package(
from: "1.1.0"),
.package(
url: "https://github.com/apple/swift-foundation-icu",
exact: "0.0.7"),
exact: "0.0.8"),
.package(
url: "https://github.com/apple/swift-syntax.git",
from: "510.0.0")
from: "600.0.0-latest")
],
targets: [
// Foundation (umbrella)
Expand Down Expand Up @@ -91,6 +91,24 @@ let package = Package(
.product(name: "_RopeModule", package: "swift-collections"),
.product(name: "OrderedCollections", package: "swift-collections"),
],
exclude: [
"Formatting/CMakeLists.txt",
"PropertyList/CMakeLists.txt",
"Decimal/CMakeLists.txt",
"String/CMakeLists.txt",
"Error/CMakeLists.txt",
"Locale/CMakeLists.txt",
"Data/CMakeLists.txt",
"TimeZone/CMakeLists.txt",
"JSON/CMakeLists.txt",
"AttributedString/CMakeLists.txt",
"Calendar/CMakeLists.txt",
"Predicate/CMakeLists.txt",
"CMakeLists.txt",
"ProcessInfo/CMakeLists.txt",
"FileManager/CMakeLists.txt",
"URL/CMakeLists.txt"
],
cSettings: [
.define("_GNU_SOURCE", .when(platforms: [.linux]))
],
Expand Down Expand Up @@ -119,6 +137,16 @@ let package = Package(
.target(name: "_CShims"),
.product(name: "_FoundationICU", package: "swift-foundation-icu")
],
exclude: [
"String/CMakeLists.txt",
"TimeZone/CMakeLists.txt",
"ICU/CMakeLists.txt",
"Formatting/CMakeLists.txt",
"Locale/CMakeLists.txt",
"Calendar/CMakeLists.txt",
"CMakeLists.txt",
"Predicate/CMakeLists.txt"
],
swiftSettings: [
.enableExperimentalFeature("AccessLevelOnImport")
] + availabilityMacros + concurrencyChecking
Expand All @@ -135,6 +163,7 @@ let package = Package(
.product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
],
exclude: ["CMakeLists.txt"],
swiftSettings: [
.enableExperimentalFeature("AccessLevelOnImport")
] + availabilityMacros + concurrencyChecking
Expand Down
18 changes: 18 additions & 0 deletions Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

add_subdirectory(_CShims)
add_subdirectory(FoundationMacros)
add_subdirectory(FoundationEssentials)
add_subdirectory(FoundationInternationalization)
37 changes: 37 additions & 0 deletions Sources/FoundationEssentials/AttributedString/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
target_sources(FoundationEssentials PRIVATE
AttributeContainer.swift
AttributeScope.swift
AttributedString+AttributeTransformation.swift
AttributedString+CharacterView.swift
AttributedString+Guts.swift
AttributedString+Runs+AttributeSlices.swift
AttributedString+Runs+Run.swift
AttributedString+Runs.swift
AttributedString+UnicodeScalarView.swift
AttributedString+_InternalRun.swift
AttributedString+_InternalRuns.swift
AttributedString+_InternalRunsSlice.swift
AttributedString.swift
AttributedStringAttribute.swift
AttributedStringAttributeConstrainingBehavior.swift
AttributedStringAttributeStorage.swift
AttributedStringCodable.swift
AttributedStringProtocol.swift
AttributedSubstring.swift
Collection\ Stdlib\ Defaults.swift
Conversion.swift
FoundationAttributes.swift
String.Index+ABI.swift)
77 changes: 77 additions & 0 deletions Sources/FoundationEssentials/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

add_library(FoundationEssentials
_ThreadLocal.swift
Bundle+Stub.swift
Codable.swift
CodableUtilities.swift
CodableWithConfiguration.swift
ComparisonResult.swift
Date.swift
DateInterval.swift
FoundationEssentials.swift
IndexPath.swift
LockedState.swift
Logging.swift
OutputBuffer.swift
Platform.swift
SortComparator.swift
UUID_Wrappers.swift
UUID.swift
WinSDK+Extensions.swift)

add_subdirectory(AttributedString)
add_subdirectory(Calendar)
add_subdirectory(Data)
add_subdirectory(Decimal)
add_subdirectory(Error)
add_subdirectory(FileManager)
add_subdirectory(Formatting)
add_subdirectory(JSON)
add_subdirectory(Locale)
add_subdirectory(Predicate)
add_subdirectory(ProcessInfo)
add_subdirectory(PropertyList)
add_subdirectory(String)
add_subdirectory(TimeZone)
add_subdirectory(URL)

# Depend on FoundationMacros
add_dependencies(FoundationEssentials FoundationMacros)
target_compile_options(FoundationEssentials PRIVATE -plugin-path ${CMAKE_BINARY_DIR}/lib)

if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
target_compile_options(FoundationEssentials PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -Xcc -Xfrontend -D_GNU_SOURCE>")
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
endif()

target_compile_options(FoundationEssentials PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend VariadicGenerics>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend AccessLevelOnImport>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-experimental-feature -Xfrontend StrictConcurrency>"
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -enable-upcoming-feature -Xfrontend InferSendableFromCaptures>")
target_compile_options(FoundationEssentials PRIVATE ${_SwiftFoundation_availability_macros})
target_compile_options(FoundationEssentials PRIVATE -package-name "SwiftFoundation")

target_link_libraries(FoundationEssentials PUBLIC
_CShims
_FoundationCollections)

set_target_properties(FoundationEssentials PROPERTIES
INSTALL_RPATH "$ORIGIN")

set_property(GLOBAL APPEND PROPERTY SWIFT_FOUNDATION_EXPORTS FoundationEssentials)
_swift_foundation_install_target(FoundationEssentials)
26 changes: 26 additions & 0 deletions Sources/FoundationEssentials/Calendar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

target_sources(FoundationEssentials PRIVATE
Calendar.swift
Calendar_Autoupdating.swift
Calendar_Cache.swift
Calendar_Enumerate.swift
Calendar_Gregorian.swift
Calendar_Protocol.swift
Calendar_Recurrence.swift
Date+FormatStyle.swift
Date+Locale.swift
DateComponents.swift
RecurrenceRule.swift)
25 changes: 25 additions & 0 deletions Sources/FoundationEssentials/Data/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

target_sources(FoundationEssentials PRIVATE
Collections+DataProtocol.swift
ContiguousBytes.swift
Data+Base64.swift
Data+Error.swift
Data+Reading.swift
Data+Stub.swift
Data+Writing.swift
Data.swift
DataProtocol.swift
Pointers+DataProtocol.swift)
16 changes: 16 additions & 0 deletions Sources/FoundationEssentials/Decimal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
target_sources(FoundationEssentials PRIVATE
Decimal.swift
Decimal+Conformances.swift)
19 changes: 19 additions & 0 deletions Sources/FoundationEssentials/Error/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift open source project
##
## Copyright (c) 2024 Apple Inc. and the Swift project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.md for the list of Swift project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
target_sources(FoundationEssentials PRIVATE
CocoaError+FilePath.swift
CocoaError.swift
ErrorCodes+POSIX.swift
ErrorCodes+Win32.swift
ErrorCodes.swift)
Loading