Skip to content
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

Monomodule support #266

Merged
merged 12 commits into from
Mar 23, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ xcuserdata/
/Benchmarks/.swiftpm
/Benchmarks/.build
.docc-build
__pycache__
289 changes: 202 additions & 87 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,145 +45,260 @@ var defines: [String] = [
// Enables randomized testing of some data structure implementations.
"COLLECTIONS_RANDOMIZED_TESTING",

// Enable modules that aren't source stable yet, and aren't ready for general use.
// "COLLECTIONS_ENABLE_UNSTABLE_MODULES",
// Enable this to build the sources as a single, large module.
// This removes the distinct modules for each data structure, instead
// putting them all directly into the `Collections` module.
// Note: This is a source-incompatible variation of the default configuration.
// "COLLECTIONS_SINGLE_MODULE",
]

var _modules: [String] = []
var _products: [Product] = []
var _targets: [Target] = []
var _settings: [SwiftSetting] = defines.map { .define($0) }

func registerTargets(_ targets: [Target]) {
_targets.append(contentsOf: targets)
struct CustomTarget {
enum Kind {
case exported
case hidden
case test
case testSupport
}

var kind: Kind
var name: String
var dependencies: [Target.Dependency]
var directory: String
var exclude: [String]
}
func registerTargets(_ targets: Target...) { registerTargets(targets) }

func registerLibrary(_ module: String, _ targets: [Target]) {
_modules.append(module)
_products.append(.library(name: module, targets: [module]))
registerTargets(targets)
extension CustomTarget.Kind {
func path(for name: String) -> String {
switch self {
case .exported, .hidden: return "Sources/\(name)"
case .test, .testSupport: return "Tests/\(name)"
}
}

var isTest: Bool {
switch self {
case .exported, .hidden: return false
case .test, .testSupport: return true
}
}
}
func registerLibrary(_ module: String, _ targets: Target...) {
registerLibrary(module, targets)

extension CustomTarget {
static func target(
kind: Kind,
name: String,
dependencies: [Target.Dependency] = [],
directory: String? = nil,
exclude: [String] = []
) -> CustomTarget {
CustomTarget(
kind: kind,
name: name,
dependencies: dependencies,
directory: directory ?? name,
exclude: exclude)
}

func toTarget() -> Target {
var linkerSettings: [LinkerSetting] = []
if kind == .testSupport {
linkerSettings.append(
.linkedFramework("XCTest", .when(platforms: [.macOS, .iOS, .watchOS, .tvOS])))
}
switch kind {
case .exported, .hidden, .testSupport:
return Target.target(
name: name,
dependencies: dependencies,
path: kind.path(for: directory),
exclude: exclude,
swiftSettings: _settings,
linkerSettings: linkerSettings)
case .test:
return Target.testTarget(
name: name,
dependencies: dependencies,
path: kind.path(for: directory),
exclude: exclude,
swiftSettings: _settings,
linkerSettings: linkerSettings)
}
}
}

func registerUnstableLibrary(_ module: String, _ targets: Target...) {
if defines.contains("COLLECTIONS_ENABLE_UNSTABLE_MODULES") {
registerLibrary(module, targets)
extension Array where Element == CustomTarget {
func toMonolithicTarget(
name: String,
linkerSettings: [LinkerSetting] = []
) -> Target {
let targets = self.filter { !$0.kind.isTest }
return Target.target(
name: name,
path: "Sources",
exclude: [
"CMakeLists.txt",
"BitCollections/BitCollections.docc",
"Collections/Collections.docc",
"DequeModule/DequeModule.docc",
"HashTreeCollections/HashTreeCollections.docc",
"HeapModule/HeapModule.docc",
"OrderedCollections/OrderedCollections.docc",
] + targets.flatMap { t in
t.exclude.map { "\(t.name)/\($0)" }
},
sources: targets.map { "\($0.directory)" },
swiftSettings: _settings,
linkerSettings: linkerSettings)
}

func toMonolithicTestTarget(
name: String,
dependencies: [Target.Dependency] = [],
linkerSettings: [LinkerSetting] = []
) -> Target {
let targets = self.filter { $0.kind.isTest }
return Target.testTarget(
name: name,
dependencies: dependencies,
path: "Tests",
exclude: targets.flatMap { t in
t.exclude.map { "\(t.name)/\($0)" }
},
sources: targets.map { "\($0.name)" },
swiftSettings: _settings,
linkerSettings: linkerSettings)
}
}

registerTargets(
let targets: [CustomTarget] = [
.target(
kind: .testSupport,
name: "_CollectionsTestSupport",
dependencies: ["_CollectionsUtilities"],
swiftSettings: _settings,
linkerSettings: [
.linkedFramework(
"XCTest",
.when(platforms: [.macOS, .iOS, .watchOS, .tvOS])),
]
),
.testTarget(
dependencies: ["_CollectionsUtilities"]),
.target(
kind: .test,
name: "CollectionsTestSupportTests",
dependencies: ["_CollectionsTestSupport"],
swiftSettings: _settings),

dependencies: ["_CollectionsTestSupport"]),
.target(
kind: .hidden,
name: "_CollectionsUtilities",
exclude: ["CMakeLists.txt"],
swiftSettings: _settings)
)
exclude: [
"CMakeLists.txt",
"Compatibility/Array+WithContiguousStorage Compatibility.swift.gyb",
"Compatibility/UnsafeMutableBufferPointer+SE-0370.swift.gyb",
"Compatibility/UnsafeMutablePointer+SE-0370.swift.gyb",
"Compatibility/UnsafeRawPointer extensions.swift.gyb",
"Debugging.swift.gyb",
"Descriptions.swift.gyb",
"IntegerTricks/FixedWidthInteger+roundUpToPowerOfTwo.swift.gyb",
"IntegerTricks/Integer rank.swift.gyb",
"IntegerTricks/UInt+first and last set bit.swift.gyb",
"IntegerTricks/UInt+reversed.swift.gyb",
"RandomAccessCollection+Offsets.swift.gyb",
"UnsafeBitSet/_UnsafeBitSet+Index.swift.gyb",
"UnsafeBitSet/_UnsafeBitSet+_Word.swift.gyb",
"UnsafeBitSet/_UnsafeBitSet.swift.gyb",
"UnsafeBufferPointer+Extras.swift.gyb",
"UnsafeMutableBufferPointer+Extras.swift.gyb",
]),

registerLibrary(
"BitCollections",
.target(
kind: .exported,
name: "BitCollections",
dependencies: ["_CollectionsUtilities"],
exclude: ["CMakeLists.txt"],
swiftSettings: _settings),
.testTarget(
exclude: ["CMakeLists.txt"]),
.target(
kind: .test,
name: "BitCollectionsTests",
dependencies: [
"BitCollections", "_CollectionsTestSupport", "OrderedCollections"
],
swiftSettings: _settings)
)
]),

registerLibrary(
"DequeModule",
.target(
kind: .exported,
name: "DequeModule",
dependencies: ["_CollectionsUtilities"],
exclude: ["CMakeLists.txt"],
swiftSettings: _settings),
.testTarget(
exclude: ["CMakeLists.txt"]),
.target(
kind: .test,
name: "DequeTests",
dependencies: ["DequeModule", "_CollectionsTestSupport"],
swiftSettings: _settings)
)
dependencies: ["DequeModule", "_CollectionsTestSupport"]),

registerLibrary(
"HashTreeCollections",
.target(
kind: .exported,
name: "HashTreeCollections",
dependencies: ["_CollectionsUtilities"],
exclude: ["CMakeLists.txt"],
swiftSettings: _settings),
.testTarget(
exclude: ["CMakeLists.txt"]),
.target(
kind: .test,
name: "HashTreeCollectionsTests",
dependencies: ["HashTreeCollections", "_CollectionsTestSupport"],
swiftSettings: _settings)
)
dependencies: ["HashTreeCollections", "_CollectionsTestSupport"]),

registerLibrary(
"HeapModule",
.target(
kind: .exported,
name: "HeapModule",
dependencies: ["_CollectionsUtilities"],
exclude: ["CMakeLists.txt"],
swiftSettings: _settings),
.testTarget(
exclude: ["CMakeLists.txt"]),
.target(
kind: .test,
name: "HeapTests",
dependencies: ["HeapModule", "_CollectionsTestSupport"],
swiftSettings: _settings)
)
dependencies: ["HeapModule", "_CollectionsTestSupport"]),

registerLibrary(
"OrderedCollections",
.target(
kind: .exported,
name: "OrderedCollections",
dependencies: ["_CollectionsUtilities"],
exclude: ["CMakeLists.txt"],
swiftSettings: _settings),
.testTarget(
exclude: ["CMakeLists.txt"]),
.target(
kind: .test,
name: "OrderedCollectionsTests",
dependencies: ["OrderedCollections", "_CollectionsTestSupport"],
swiftSettings: _settings)
)
dependencies: ["OrderedCollections", "_CollectionsTestSupport"]),

registerLibrary(
"_RopeModule",
.target(
kind: .exported,
name: "_RopeModule",
dependencies: ["_CollectionsUtilities"],
path: "Sources/RopeModule",
swiftSettings: _settings),
.testTarget(
directory: "RopeModule"),
.target(
kind: .test,
name: "RopeModuleTests",
dependencies: ["_RopeModule", "_CollectionsTestSupport"],
swiftSettings: _settings)
)
dependencies: ["_RopeModule", "_CollectionsTestSupport"]),

registerLibrary(
"Collections",
.target(
kind: .exported,
name: "Collections",
dependencies: _modules.map { .target(name: $0) },
exclude: ["CMakeLists.txt"],
swiftSettings: _settings)
)
dependencies: [
"BitCollections",
"DequeModule",
"HashTreeCollections",
"HeapModule",
"OrderedCollections",
"_RopeModule",
],
exclude: ["CMakeLists.txt"])
]

var _products: [Product] = []
var _targets: [Target] = []
if defines.contains("COLLECTIONS_SINGLE_MODULE") {
_products = [
.library(name: "Collections", targets: ["Collections"]),
]
_targets = [
targets.toMonolithicTarget(name: "Collections"),
targets.toMonolithicTestTarget(
name: "CollectionsTests",
dependencies: ["Collections"]),
]
} else {
_products = targets.compactMap { t in
guard t.kind == .exported else { return nil }
return .library(name: t.name, targets: [t.name])
}
_targets = targets.map { $0.toTarget() }
}

let package = Package(
name: "swift-collections",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//
//===----------------------------------------------------------------------===//

#if !COLLECTIONS_SINGLE_MODULE
import _CollectionsUtilities
#endif

internal struct _ChunkedBitsForwardIterator {
internal typealias _BitPosition = _UnsafeBitSet.Index
Expand Down
2 changes: 2 additions & 0 deletions Sources/BitCollections/BitArray/BitArray+Copy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//
//===----------------------------------------------------------------------===//

#if !COLLECTIONS_SINGLE_MODULE
import _CollectionsUtilities
#endif

extension BitArray {
internal mutating func _copy(
Expand Down
2 changes: 2 additions & 0 deletions Sources/BitCollections/BitArray/BitArray+Invariants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//
//===----------------------------------------------------------------------===//

#if !COLLECTIONS_SINGLE_MODULE
import _CollectionsUtilities
#endif

extension BitArray {
/// True if consistency checking is enabled in the implementation of this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//
//===----------------------------------------------------------------------===//

#if !COLLECTIONS_SINGLE_MODULE
import _CollectionsUtilities
#endif

extension BitArray {
/// An unsafe-unowned bitarray view over `UInt` storage, providing bit array
Expand Down
Loading