Skip to content

Commit 689c485

Browse files
authored
build: add a CMake based build system (apple#42)
Add a CMake based build system to enable bootstrapping the toolchain with the ability to use Swift Collections in SPM.
1 parent fcb788c commit 689c485

File tree

6 files changed

+265
-0
lines changed

6 files changed

+265
-0
lines changed

CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#[[
2+
This source file is part of the Swift Collections Open Source Project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
cmake_minimum_required(VERSION 3.16)
11+
project(swift-collections
12+
LANGUAGES C Swift)
13+
14+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
15+
16+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
17+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
18+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
19+
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
20+
21+
if(CMAKE_SYSTEM_NAME STREQUAL Windows OR CMAKE_SYSTEM_NAME STREQUAL Darwin)
22+
option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
23+
endif()
24+
25+
include(CTest)
26+
include(SwiftSupport)
27+
28+
add_subdirectory(Sources)
29+
# if(BUILD_TESTING)
30+
# add_subdirectory(Tests)
31+
# endif()
32+
33+
get_property(SWIFT_COLLECTIONS_EXPORTS GLOBAL PROPERTY SWIFT_COLLECTIONS_EXPORTS)
34+
export(TARGETS ${SWIFT_COLLECTIONS_EXPORTS}
35+
NAMESPACE SwiftCollections::
36+
FILE swift-collections-config.cmake
37+
EXPORT_LINK_INTERFACE_LIBRARIES)

Sources/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[[
2+
This source file is part of the Swift Collections Open Source Project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_subdirectory(Collections)
11+
add_subdirectory(DequeModule)
12+
add_subdirectory(OrderedCollections)

Sources/Collections/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[[
2+
This source file is part of the Swift Collections Open Source Project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(Collections
11+
Collections.swift)
12+
target_link_libraries(Collections PRIVATE
13+
DequeModule
14+
OrderedCollections)
15+
set_target_properties(Collections PROPERTIES
16+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
17+
18+
_install_target(Collections)
19+
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS Collections)

Sources/DequeModule/CMakeLists.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#[[
2+
This source file is part of the Swift Collections Open Source Project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(DequeModule
11+
_DequeBuffer.swift
12+
_DequeBufferHeader.swift
13+
_DequeSlot.swift
14+
_UnsafeWrappedBuffer.swift
15+
Deque._Storage.swift
16+
Deque._UnsafeHandle.swift
17+
Deque.swift
18+
Deque+Codable.swift
19+
Deque+CustomDebugStringConvertible.swift
20+
Deque+CustomReflectable.swift
21+
Deque+CustomStringConvertible.swift
22+
Deque+Equatable.swift
23+
Deque+ExpressibleByArrayLiteral.swift
24+
Deque+Extras.swift
25+
Deque+Hashable.swift
26+
Deque+MutableCollection.swift
27+
Deque+RandomAccessCollection.swift
28+
Deque+RangeReplaceableCollection.swift
29+
Deque+Sequence.swift
30+
Deque+Testing.swift
31+
UnsafeMutableBufferPointer+Utilities.swift)
32+
set_target_properties(DequeModule PROPERTIES
33+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
34+
35+
_install_target(DequeModule)
36+
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS DequeModule)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#[[
2+
This source file is part of the Swift Collections Open Source Project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
add_library(OrderedCollections
11+
"HashTable/_HashTable+Bucket.swift"
12+
"HashTable/_HashTable+BucketIterator.swift"
13+
"HashTable/_HashTable+Constants.swift"
14+
"HashTable/_HashTable+CustomStringConvertible.swift"
15+
"HashTable/_Hashtable+Header.swift"
16+
"HashTable/_HashTable+Testing.swift"
17+
"HashTable/_HashTable+UnsafeHandle.swift"
18+
"HashTable/_HashTable.swift"
19+
20+
"OrderedDictionary/OrderedDictionary+Codable.swift"
21+
"OrderedDictionary/OrderedDictionary+CustomDebugStringConvertible.swift"
22+
"OrderedDictionary/OrderedDictionary+CustomReflectable.swift"
23+
"OrderedDictionary/OrderedDictionary+CustomStringConvertible.swift"
24+
"OrderedDictionary/OrderedDictionary+Elements+SubSequence.swift"
25+
"OrderedDictionary/OrderedDictionary+Elements.swift"
26+
"OrderedDictionary/OrderedDictionary+Equatable.swift"
27+
"OrderedDictionary/OrderedDictionary+ExpressibleByDictionaryLiteral.swift"
28+
"OrderedDictionary/OrderedDictionary+Hashable.swift"
29+
"OrderedDictionary/OrderedDictionary+Initializers.swift"
30+
"OrderedDictionary/OrderedDictionary+Invariants.swift"
31+
"OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift"
32+
"OrderedDictionary/OrderedDictionary+Partial RangeReplaceableCollection.swift"
33+
"OrderedDictionary/OrderedDictionary+Sequence.swift"
34+
"OrderedDictionary/OrderedDictionary+Values.swift"
35+
"OrderedDictionary/OrderedDictionary.swift"
36+
37+
"OrderedSet/OrderedSet+Codable.swift"
38+
"OrderedSet/OrderedSet+CustomDebugStringConvertible.swift"
39+
"OrderedSet/OrderedSet+CustomReflectable.swift"
40+
"OrderedSet/OrderedSet+CustomStringConvertible.swift"
41+
"OrderedSet/OrderedSet+Diffing.swift"
42+
"OrderedSet/OrderedSet+Equatable.swift"
43+
"OrderedSet/OrderedSet+ExpressibleByArrayLiteral.swift"
44+
"OrderedSet/OrderedSet+Hashable.swift"
45+
"OrderedSet/OrderedSet+Initializers.swift"
46+
"OrderedSet/OrderedSet+Insertions.swift"
47+
"OrderedSet/OrderedSet+Invariants.swift"
48+
"OrderedSet/OrderedSet+Partial MutableCollection.swift"
49+
"OrderedSet/OrderedSet+Partial RangeReplaceableCollection.swift"
50+
"OrderedSet/OrderedSet+Partial SetAlgebra+Basics.swift"
51+
"OrderedSet/OrderedSet+Partial SetAlgebra+Operations.swift"
52+
"OrderedSet/OrderedSet+Partial SetAlgebra+Predicates.swift"
53+
"OrderedSet/OrderedSet+RandomAccessCollection.swift"
54+
"OrderedSet/OrderedSet+ReserveCapacity.swift"
55+
"OrderedSet/OrderedSet+SubSequence.swift"
56+
"OrderedSet/OrderedSet+Testing.swift"
57+
"OrderedSet/OrderedSet+UnorderedView.swift"
58+
"OrderedSet/OrderedSet+UnstableInternals.swift"
59+
"OrderedSet/OrderedSet.swift"
60+
61+
"Utilities/_UnsafeBitset.swift"
62+
"Utilities/RandomAccessCollection+Offsets.swift")
63+
set_target_properties(OrderedCollections PROPERTIES
64+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
65+
66+
_install_target(OrderedCollections)
67+
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS OrderedCollections)

cmake/modules/SwiftSupport.cmake

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#[[
2+
This source file is part of the Swift Collections Open Source Project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See https://swift.org/LICENSE.txt for license information
8+
#]]
9+
10+
# Returns the architecture name in a variable
11+
#
12+
# Usage:
13+
# get_swift_host_arch(result_var_name)
14+
#
15+
# Sets ${result_var_name} with the converted architecture name derived from
16+
# CMAKE_SYSTEM_PROCESSOR.
17+
function(get_swift_host_arch result_var_name)
18+
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
19+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
20+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
21+
set("${result_var_name}" "aarch64" PARENT_SCOPE)
22+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
23+
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
24+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
25+
set("${result_var_name}" "powerpc64le" PARENT_SCOPE)
26+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "s390x")
27+
set("${result_var_name}" "s390x" PARENT_SCOPE)
28+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv6l")
29+
set("${result_var_name}" "armv6" PARENT_SCOPE)
30+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7l")
31+
set("${result_var_name}" "armv7" PARENT_SCOPE)
32+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "armv7-a")
33+
set("${result_var_name}" "armv7" PARENT_SCOPE)
34+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64")
35+
set("${result_var_name}" "x86_64" PARENT_SCOPE)
36+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "IA64")
37+
set("${result_var_name}" "itanium" PARENT_SCOPE)
38+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
39+
set("${result_var_name}" "i686" PARENT_SCOPE)
40+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
41+
set("${result_var_name}" "i686" PARENT_SCOPE)
42+
else()
43+
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
44+
endif()
45+
endfunction()
46+
47+
# Returns the os name in a variable
48+
#
49+
# Usage:
50+
# get_swift_host_os(result_var_name)
51+
#
52+
#
53+
# Sets ${result_var_name} with the converted OS name derived from
54+
# CMAKE_SYSTEM_NAME.
55+
function(get_swift_host_os result_var_name)
56+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
57+
set(${result_var_name} macosx PARENT_SCOPE)
58+
else()
59+
string(TOLOWER ${CMAKE_SYSTEM_NAME} cmake_system_name_lc)
60+
set(${result_var_name} ${cmake_system_name_lc} PARENT_SCOPE)
61+
endif()
62+
endfunction()
63+
64+
function(_install_target module)
65+
get_swift_host_os(swift_os)
66+
get_target_property(type ${module} TYPE)
67+
68+
if(type STREQUAL STATIC_LIBRARY)
69+
set(swift swift_static)
70+
else()
71+
set(swift swift)
72+
endif()
73+
74+
install(TARGETS ${module}
75+
ARCHIVE DESTINATION lib/${swift}/${swift_os}
76+
LIBRARY DESTINATION lib/${swift}/${swift_os}
77+
RUNTIME DESTINATION bin)
78+
if(type STREQUAL EXECUTABLE)
79+
return()
80+
endif()
81+
82+
get_swift_host_arch(swift_arch)
83+
get_target_property(module_name ${module} Swift_MODULE_NAME)
84+
if(NOT module_name)
85+
set(module_name ${module})
86+
endif()
87+
88+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftdoc
89+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
90+
RENAME ${swift_arch}.swiftdoc)
91+
install(FILES $<TARGET_PROPERTY:${module},Swift_MODULE_DIRECTORY>/${module_name}.swiftmodule
92+
DESTINATION lib/${swift}/${swift_os}/${module_name}.swiftmodule
93+
RENAME ${swift_arch}.swiftmodule)
94+
endfunction()

0 commit comments

Comments
 (0)