Skip to content

Commit

Permalink
Add Single Module Build Mode to CMake for Foundation (#385)
Browse files Browse the repository at this point in the history
* Add new single-module and foundation-toolchain-module modes to CMake build

* Fix allowable client flag syntax
  • Loading branch information
jmschonfeld committed Jun 3, 2024
1 parent 8253d6d commit ee97538
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 62 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ if(CMAKE_SYSTEM_NAME STREQUAL Windows OR CMAKE_SYSTEM_NAME STREQUAL Darwin)
option(BUILD_SHARED_LIBS "Build shared libraries by default" YES)
endif()

option(COLLECTIONS_SINGLE_MODULE "Build as a single module" NO)
option(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE "Build a module for Foundation in the toolchain" NO)

if(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE)
set(COLLECTIONS_SINGLE_MODULE YES)
endif()

include(CTest)
include(SwiftSupport)

Expand Down
20 changes: 13 additions & 7 deletions Sources/BitCollections/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(BitCollections
list(APPEND COLLECTIONS_BIT_SOURCES
"BitArray/BitArray+BitwiseOperations.swift"
"BitArray/BitArray+ChunkedBitsIterators.swift"
"BitArray/BitArray+Codable.swift"
Expand Down Expand Up @@ -65,10 +65,16 @@ add_library(BitCollections
"Shared/UInt+Tricks.swift"
"Shared/_Word.swift"
)
target_link_libraries(BitCollections PRIVATE
InternalCollectionsUtilities)
set_target_properties(BitCollections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY COLLECTIONS_BIT_SOURCES ${COLLECTIONS_BIT_SOURCES})

_install_target(BitCollections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS BitCollections)
if(NOT COLLECTIONS_SINGLE_MODULE)
add_library(BitCollections
${COLLECTIONS_BIT_SOURCES})
target_link_libraries(BitCollections PRIVATE
InternalCollectionsUtilities)
set_target_properties(BitCollections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(BitCollections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS BitCollections)
endif()
4 changes: 3 additions & 1 deletion Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ See https://swift.org/LICENSE.txt for license information
#]]

add_subdirectory(BitCollections)
add_subdirectory(Collections)
add_subdirectory(DequeModule)
add_subdirectory(HashTreeCollections)
add_subdirectory(HeapModule)
add_subdirectory(OrderedCollections)
add_subdirectory(RopeModule)
add_subdirectory(InternalCollectionsUtilities)

# Add Collections last so it can find global properties set by sub directories
add_subdirectory(Collections)
93 changes: 79 additions & 14 deletions Sources/Collections/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,82 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(Collections
"Collections.swift")
target_link_libraries(Collections PRIVATE
BitCollections
DequeModule
HeapModule
OrderedCollections
HashTreeCollections
)
set_target_properties(Collections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(Collections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS Collections)
if(COLLECTIONS_SINGLE_MODULE)
if (COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE)
set(COLLECTIONS_MODULE_NAME _FoundationCollections)
set(COLLECTIONS_MODULE_TYPE STATIC)
else()
set(COLLECTIONS_MODULE_NAME Collections)
set(COLLECTIONS_MODULE_TYPE)
endif()

get_property(COLLECTIONS_UTILITIES_SOURCES GLOBAL PROPERTY COLLECTIONS_UTILITIES_SOURCES)
list(TRANSFORM COLLECTIONS_UTILITIES_SOURCES PREPEND "../InternalCollectionsUtilities/")
get_property(COLLECTIONS_BIT_SOURCES GLOBAL PROPERTY COLLECTIONS_BIT_SOURCES)
list(TRANSFORM COLLECTIONS_BIT_SOURCES PREPEND "../BitCollections/")
get_property(COLLECTIONS_DEQUE_SOURCES GLOBAL PROPERTY COLLECTIONS_DEQUE_SOURCES)
list(TRANSFORM COLLECTIONS_DEQUE_SOURCES PREPEND "../DequeModule/")
get_property(COLLECTIONS_HEAP_SOURCES GLOBAL PROPERTY COLLECTIONS_HEAP_SOURCES)
list(TRANSFORM COLLECTIONS_HEAP_SOURCES PREPEND "../HeapModule/")
get_property(COLLECTIONS_ORDERED_SOURCES GLOBAL PROPERTY COLLECTIONS_ORDERED_SOURCES)
list(TRANSFORM COLLECTIONS_ORDERED_SOURCES PREPEND "../OrderedCollections/")
get_property(COLLECTIONS_HASHTREE_SOURCES GLOBAL PROPERTY COLLECTIONS_HASHTREE_SOURCES)
list(TRANSFORM COLLECTIONS_HASHTREE_SOURCES PREPEND "../HashTreeCollections/")

add_library(${COLLECTIONS_MODULE_NAME} ${COLLECTIONS_MODULE_TYPE}
"Collections.swift"
${COLLECTIONS_UTILITIES_SOURCES}
${COLLECTIONS_BIT_SOURCES}
${COLLECTIONS_DEQUE_SOURCES}
${COLLECTIONS_HEAP_SOURCES}
${COLLECTIONS_ORDERED_SOURCES}
${COLLECTIONS_HASHTREE_SOURCES})

if(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE)
get_property(COLLECTIONS_ROPE_SOURCES GLOBAL PROPERTY COLLECTIONS_ROPE_SOURCES)
list(TRANSFORM COLLECTIONS_ROPE_SOURCES PREPEND "../RopeModule/")
target_sources(${COLLECTIONS_MODULE_NAME} PRIVATE
${COLLECTIONS_ROPE_SOURCES})
target_compile_options(${COLLECTIONS_MODULE_NAME} PRIVATE
"SHELL:$<$<COMPILE_LANGUAGE:Swift>:-Xfrontend -allowable-client -Xfrontend FoundationEssentials>")
endif()

target_compile_definitions(${COLLECTIONS_MODULE_NAME} PRIVATE COLLECTIONS_SINGLE_MODULE)
set_target_properties(${COLLECTIONS_MODULE_NAME} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

if(COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE)
# Install only the swift module and not the library
if(BUILD_SHARED_LIBS)
set(swift swift)
else()
set(swift swift_static)
endif()
get_swift_host_os(swift_os)
get_swift_host_arch(swift_arch)
install(FILES $<TARGET_PROPERTY:${COLLECTIONS_MODULE_NAME},Swift_MODULE_DIRECTORY>/${COLLECTIONS_MODULE_NAME}.swiftdoc
DESTINATION lib/${swift}/${swift_os}/${COLLECTIONS_MODULE_NAME}.swiftmodule
RENAME ${swift_arch}.swiftdoc)
install(FILES $<TARGET_PROPERTY:${COLLECTIONS_MODULE_NAME},Swift_MODULE_DIRECTORY>/${COLLECTIONS_MODULE_NAME}.swiftmodule
DESTINATION lib/${swift}/${swift_os}/${COLLECTIONS_MODULE_NAME}.swiftmodule
RENAME ${swift_arch}.swiftmodule)
else()
_install_target(${COLLECTIONS_MODULE_NAME})
endif()
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS ${COLLECTIONS_MODULE_NAME})
else()
add_library(Collections
"Collections.swift")
target_link_libraries(Collections PRIVATE
BitCollections
DequeModule
HeapModule
OrderedCollections
HashTreeCollections
)
set_target_properties(Collections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(Collections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS Collections)
endif()
20 changes: 13 additions & 7 deletions Sources/DequeModule/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(DequeModule
list(APPEND COLLECTIONS_DEQUE_SOURCES
"Deque+Codable.swift"
"Deque+Collection.swift"
"Deque+CustomReflectable.swift"
Expand All @@ -26,10 +26,16 @@ add_library(DequeModule
"_DequeSlot.swift"
"_UnsafeWrappedBuffer.swift"
)
target_link_libraries(DequeModule PRIVATE
InternalCollectionsUtilities)
set_target_properties(DequeModule PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY COLLECTIONS_DEQUE_SOURCES ${COLLECTIONS_DEQUE_SOURCES})

_install_target(DequeModule)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS DequeModule)
if(NOT COLLECTIONS_SINGLE_MODULE)
add_library(DequeModule
${COLLECTIONS_DEQUE_SOURCES})
target_link_libraries(DequeModule PRIVATE
InternalCollectionsUtilities)
set_target_properties(DequeModule PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(DequeModule)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS DequeModule)
endif()
20 changes: 13 additions & 7 deletions Sources/HashTreeCollections/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(HashTreeCollections
list(APPEND COLLECTIONS_HASHTREE_SOURCES
"HashNode/_AncestorHashSlots.swift"
"HashNode/_Bitmap.swift"
"HashNode/_Bucket.swift"
Expand Down Expand Up @@ -94,10 +94,16 @@ add_library(HashTreeCollections
"TreeSet/TreeSet+SetAlgebra union.swift"
"TreeSet/TreeSet.swift"
)
target_link_libraries(HashTreeCollections PRIVATE
InternalCollectionsUtilities)
set_target_properties(HashTreeCollections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY COLLECTIONS_HASHTREE_SOURCES ${COLLECTIONS_HASHTREE_SOURCES})

_install_target(HashTreeCollections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS HashTreeCollections)
if(NOT COLLECTIONS_SINGLE_MODULE)
add_library(HashTreeCollections
${COLLECTIONS_HASHTREE_SOURCES})
target_link_libraries(HashTreeCollections PRIVATE
InternalCollectionsUtilities)
set_target_properties(HashTreeCollections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(HashTreeCollections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS HashTreeCollections)
endif()
20 changes: 13 additions & 7 deletions Sources/HeapModule/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(HeapModule
list(APPEND COLLECTIONS_HEAP_SOURCES
"_HeapNode.swift"
"Heap.swift"
"Heap+Descriptions.swift"
"Heap+ExpressibleByArrayLiteral.swift"
"Heap+Invariants.swift"
"Heap+UnsafeHandle.swift")
target_link_libraries(HeapModule PRIVATE
InternalCollectionsUtilities)
set_target_properties(HeapModule PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY COLLECTIONS_HEAP_SOURCES ${COLLECTIONS_HEAP_SOURCES})

_install_target(HeapModule)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS HeapModule)
if(NOT COLLECTIONS_SINGLE_MODULE)
add_library(HeapModule
${COLLECTIONS_HEAP_SOURCES})
target_link_libraries(HeapModule PRIVATE
InternalCollectionsUtilities)
set_target_properties(HeapModule PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(HeapModule)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS HeapModule)
endif()
16 changes: 11 additions & 5 deletions Sources/InternalCollectionsUtilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(InternalCollectionsUtilities
list(APPEND COLLECTIONS_UTILITIES_SOURCES
"autogenerated/Debugging.swift"
"autogenerated/Descriptions.swift"
"autogenerated/RandomAccessCollection+Offsets.swift"
Expand All @@ -27,8 +27,14 @@ add_library(InternalCollectionsUtilities
"_SortedCollection.swift"
"_UniqueCollection.swift"
)
set_target_properties(InternalCollectionsUtilities PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY COLLECTIONS_UTILITIES_SOURCES ${COLLECTIONS_UTILITIES_SOURCES})

_install_target(InternalCollectionsUtilities)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS InternalCollectionsUtilities)
if(NOT COLLECTIONS_SINGLE_MODULE)
add_library(InternalCollectionsUtilities
${COLLECTIONS_UTILITIES_SOURCES})
set_target_properties(InternalCollectionsUtilities PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(InternalCollectionsUtilities)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS InternalCollectionsUtilities)
endif()
20 changes: 13 additions & 7 deletions Sources/OrderedCollections/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(OrderedCollections
list(APPEND COLLECTIONS_ORDERED_SOURCES
"HashTable/_HashTable.swift"
"HashTable/_HashTable+Bucket.swift"
"HashTable/_HashTable+BucketIterator.swift"
Expand Down Expand Up @@ -71,10 +71,16 @@ add_library(OrderedCollections
"OrderedSet/OrderedSet.swift"
"Utilities/_UnsafeBitset.swift"
)
target_link_libraries(OrderedCollections PRIVATE
InternalCollectionsUtilities)
set_target_properties(OrderedCollections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY COLLECTIONS_ORDERED_SOURCES ${COLLECTIONS_ORDERED_SOURCES})

_install_target(OrderedCollections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS OrderedCollections)
if(NOT COLLECTIONS_SINGLE_MODULE)
add_library(OrderedCollections
${COLLECTIONS_ORDERED_SOURCES})
target_link_libraries(OrderedCollections PRIVATE
InternalCollectionsUtilities)
set_target_properties(OrderedCollections PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(OrderedCollections)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS OrderedCollections)
endif()
20 changes: 13 additions & 7 deletions Sources/RopeModule/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
#]]

add_library(_RopeModule
list(APPEND COLLECTIONS_ROPE_SOURCES
"BigString/Basics/BigString+Metrics.swift"
"BigString/Basics/BigString+Index.swift"
"BigString/Basics/BigString+Summary.swift"
Expand Down Expand Up @@ -85,10 +85,16 @@ add_library(_RopeModule
"Utilities/String.Index+ABI.swift"
"Utilities/Optional Utilities.swift"
)
target_link_libraries(_RopeModule PRIVATE
InternalCollectionsUtilities)
set_target_properties(_RopeModule PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY COLLECTIONS_ROPE_SOURCES ${COLLECTIONS_ROPE_SOURCES})

_install_target(_RopeModule)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS _RopeModule)
if(NOT COLLECTIONS_SINGLE_MODULE)
add_library(_RopeModule
${COLLECTIONS_ROPE_SOURCES})
target_link_libraries(_RopeModule PRIVATE
InternalCollectionsUtilities)
set_target_properties(_RopeModule PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

_install_target(_RopeModule)
set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS _RopeModule)
endif()

0 comments on commit ee97538

Please sign in to comment.