Skip to content

Commit 4f37d62

Browse files
authored
Merge pull request #26237 from gottesmm/pr-be50c5dbe2eb5925715fc34f31f6d6b8c1c9fb97
[benchmark] Refactor out how we specify benchmarks in Package.swift s…
2 parents 505f9ba + dcb495d commit 4f37d62

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

benchmark/Package.swift

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:4.2
1+
// swift-tools-version:5.0
22

33
import PackageDescription
44
import Foundation
@@ -9,10 +9,15 @@ unsupportedTests.insert("ObjectiveCBridging")
99
unsupportedTests.insert("ObjectiveCBridgingStubs")
1010
#endif
1111

12-
// This is a stop gap hack so we can edit benchmarks in Xcode.
13-
let singleSourceLibraries: [String] = {
12+
//===---
13+
// Single Source Libraries
14+
//
15+
16+
/// Return the source files in subDirectory that we will translate into
17+
/// libraries. Each source library will be compiled as its own module.
18+
func getSingleSourceLibraries(subDirectory: String) -> [String] {
1419
let f = FileManager.`default`
15-
let dirURL = URL(fileURLWithPath: "single-source").absoluteURL
20+
let dirURL = URL(fileURLWithPath: subDirectory)
1621
let fileURLs = try! f.contentsOfDirectory(at: dirURL,
1722
includingPropertiesForKeys: nil)
1823
return fileURLs.compactMap { (path: URL) -> String? in
@@ -25,27 +30,45 @@ let singleSourceLibraries: [String] = {
2530
return nil
2631
}
2732

28-
let s = String(c[0])
33+
let name = String(c[0])
2934

3035
// We do not support this test.
31-
if unsupportedTests.contains(s) {
36+
if unsupportedTests.contains(name) {
3237
return nil
3338
}
3439

35-
assert(s != "PrimsSplit")
36-
return s
40+
return name
3741
}
38-
}()
42+
}
43+
44+
var singleSourceLibraryDirs: [String] = []
45+
singleSourceLibraryDirs.append("single-source")
46+
47+
var singleSourceLibraries: [String] = singleSourceLibraryDirs.flatMap {
48+
getSingleSourceLibraries(subDirectory: $0)
49+
}
3950

40-
let multiSourceLibraries: [String] = {
51+
//===---
52+
// Multi Source Libraries
53+
//
54+
55+
func getMultiSourceLibraries(subDirectory: String) -> [(String, String)] {
4156
let f = FileManager.`default`
42-
let dirURL = URL(fileURLWithPath: "multi-source").absoluteURL
43-
let fileURLs = try! f.contentsOfDirectory(at: dirURL,
44-
includingPropertiesForKeys: nil)
45-
return fileURLs.map { (path: URL) -> String in
46-
return path.lastPathComponent
47-
}
48-
}()
57+
let dirURL = URL(string: subDirectory)!
58+
let subDirs = try! f.contentsOfDirectory(at: dirURL, includingPropertiesForKeys: nil)
59+
return subDirs.map { (subDirectory, $0.lastPathComponent) }
60+
}
61+
62+
var multiSourceLibraryDirs: [String] = []
63+
multiSourceLibraryDirs.append("multi-source")
64+
65+
var multiSourceLibraries: [(parentSubDir: String, name: String)] = multiSourceLibraryDirs.flatMap {
66+
getMultiSourceLibraries(subDirectory: $0)
67+
}
68+
69+
//===---
70+
// Products
71+
//
4972

5073
var products: [Product] = []
5174
products.append(.library(name: "TestsUtils", type: .static, targets: ["TestsUtils"]))
@@ -54,9 +77,15 @@ products.append(.library(name: "DriverUtils", type: .static, targets: ["DriverUt
5477
products.append(.library(name: "ObjectiveCTests", type: .static, targets: ["ObjectiveCTests"]))
5578
#endif
5679
products.append(.executable(name: "SwiftBench", targets: ["SwiftBench"]))
57-
products.append(.library(name: "PrimsSplit", type: .static, targets: ["PrimsSplit"]))
80+
5881
products += singleSourceLibraries.map { .library(name: $0, type: .static, targets: [$0]) }
59-
products += multiSourceLibraries.map { .library(name: $0, type: .static, targets: [$0]) }
82+
products += multiSourceLibraries.map {
83+
return .library(name: $0.name, type: .static, targets: [$0.name])
84+
}
85+
86+
//===---
87+
// Targets
88+
//
6089

6190
var targets: [Target] = []
6291
targets.append(.target(name: "TestsUtils", path: "utils", sources: ["TestsUtils.swift"]))
@@ -73,7 +102,7 @@ swiftBenchDeps.append(.target(name: "ObjectiveCTests"))
73102
#endif
74103
swiftBenchDeps.append(.target(name: "DriverUtils"))
75104
swiftBenchDeps += singleSourceLibraries.map { .target(name: $0) }
76-
swiftBenchDeps += multiSourceLibraries.map { .target(name: $0) }
105+
swiftBenchDeps += multiSourceLibraries.map { .target(name: $0.name) }
77106

78107
targets.append(
79108
.target(name: "SwiftBench",
@@ -92,20 +121,27 @@ var singleSourceDeps: [Target.Dependency] = [.target(name: "TestsUtils")]
92121
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
93122
singleSourceDeps.append(.target(name: "ObjectiveCTests"))
94123
#endif
95-
targets += singleSourceLibraries.map { x in
96-
return .target(name: x,
124+
125+
targets += singleSourceLibraries.map { name in
126+
return .target(name: name,
97127
dependencies: singleSourceDeps,
98128
path: "single-source",
99-
sources: ["\(x).swift"])
129+
sources: ["\(name).swift"])
100130
}
101-
targets += multiSourceLibraries.map { x in
102-
return .target(name: x,
131+
132+
targets += multiSourceLibraries.map { lib in
133+
return .target(
134+
name: lib.name,
103135
dependencies: [
104136
.target(name: "TestsUtils")
105137
],
106-
path: "multi-source/\(x)")
138+
path: lib.parentSubDir)
107139
}
108140

141+
//===---
142+
// Top Level Definition
143+
//
144+
109145
let p = Package(
110146
name: "swiftbench",
111147
products: products,

0 commit comments

Comments
 (0)