Skip to content

Commit da9efe6

Browse files
committed
Add some feature flags to control use of optional features in the build
This will allow building llbuild with a Swift Static SDK using Musl libc, using SwiftPM.
1 parent fafb264 commit da9efe6

File tree

3 files changed

+75
-35
lines changed

3 files changed

+75
-35
lines changed

Package.swift

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66
import PackageDescription
77
import class Foundation.ProcessInfo
88

9+
let isStaticBuild = ProcessInfo.processInfo.environment["LLBUILD_STATIC_LINK"] != nil
10+
let useEmbeddedSqlite = isStaticBuild || ProcessInfo.processInfo.environment["LLBUILD_USE_EMBEDDED_SQLITE"] != nil
11+
let useTerminfo = !isStaticBuild && ProcessInfo.processInfo.environment["LLBUILD_NO_TERMINFO"] == nil
12+
13+
let embeddedSqliteCondition: TargetDependencyCondition? = {
14+
if useEmbeddedSqlite {
15+
return nil
16+
}
17+
return .when(platforms: [.windows])
18+
}()
19+
20+
let externalSqliteLibraries: [LinkerSetting] = {
21+
if useEmbeddedSqlite {
22+
return []
23+
}
24+
return [.linkedLibrary("sqlite3", .when(platforms: [.macOS, .iOS, .tvOS, .watchOS, .visionOS, .macCatalyst, .linux, .android]))]
25+
}()
26+
27+
let terminfoLibraries: [LinkerSetting] = {
28+
if !useTerminfo {
29+
return []
30+
}
31+
return [.linkedLibrary("ncurses", .when(platforms: [.linux, .macOS, .android]))]
32+
}()
33+
934
let package = Package(
1035
name: "llbuild",
1136
platforms: [
@@ -22,10 +47,6 @@ let package = Package(
2247
.library(
2348
name: "llbuildSwift",
2449
targets: ["llbuildSwift"]),
25-
.library(
26-
name: "llbuildSwiftDynamic",
27-
type: .dynamic,
28-
targets: ["llbuildSwift"]),
2950
.library(
3051
name: "llbuildAnalysis",
3152
targets: ["llbuildAnalysis"]),
@@ -83,12 +104,10 @@ let package = Package(
83104
name: "llbuildCore",
84105
dependencies: [
85106
"llbuildBasic",
86-
.product(name: "SwiftToolchainCSQLite", package: "swift-toolchain-sqlite", condition: .when(platforms: [.windows])),
107+
.product(name: "SwiftToolchainCSQLite", package: "swift-toolchain-sqlite", condition: embeddedSqliteCondition),
87108
],
88109
path: "lib/Core",
89-
linkerSettings: [
90-
.linkedLibrary("sqlite3", .when(platforms: [.macOS, .iOS, .tvOS, .watchOS, .visionOS, .macCatalyst, .linux, .android]))
91-
]
110+
linkerSettings: externalSqliteLibraries
92111
),
93112
.target(
94113
name: "llbuildBuildSystem",
@@ -129,15 +148,20 @@ let package = Package(
129148
.linkedLibrary("pthread", .when(platforms: [.linux]))]),
130149
.target(
131150
name: "llbuildCoreTests",
132-
dependencies: ["llbuildCore", "gmocklib"],
151+
dependencies: [
152+
"llbuildCore",
153+
"gmocklib",
154+
.product(name: "SwiftToolchainCSQLite", package: "swift-toolchain-sqlite", condition: embeddedSqliteCondition),
155+
],
133156
path: "unittests/Core",
134157
cxxSettings: [
135158
.headerSearchPath("../../utils/unittest/googlemock/include"),
136159
.headerSearchPath("../../utils/unittest/googletest/include"),
137160
],
138161
linkerSettings: [
139162
.linkedLibrary("dl", .when(platforms: [.linux])),
140-
.linkedLibrary("pthread", .when(platforms: [.linux]))]),
163+
.linkedLibrary("pthread", .when(platforms: [.linux])),
164+
] + externalSqliteLibraries),
141165
.target(
142166
name: "llbuildBuildSystemTests",
143167
dependencies: ["llbuildBuildSystem", "gmocklib"],
@@ -231,12 +255,21 @@ let package = Package(
231255
path: "lib/llvm/Support",
232256
linkerSettings: [
233257
.linkedLibrary("m", .when(platforms: [.linux])),
234-
.linkedLibrary("ncurses", .when(platforms: [.linux, .macOS, .android]))]
258+
] + terminfoLibraries
235259
),
236260
],
237261
cxxLanguageStandard: .cxx14
238262
)
239263

264+
if !isStaticBuild {
265+
package.products += [
266+
.library(
267+
name: "llbuildSwiftDynamic",
268+
type: .dynamic,
269+
targets: ["llbuildSwift"]),
270+
]
271+
}
272+
240273
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
241274
package.dependencies += [
242275
.package(url: "https://github.com/swiftlang/swift-toolchain-sqlite", from: "1.0.0"),
@@ -247,40 +280,45 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
247280
]
248281
}
249282

250-
// FIXME: Conditionalize these flags since SwiftPM 5.3 and earlier will crash for platforms they don't know about.
251-
#if os(Windows)
252-
253-
do {
254-
let llvmTargets: Set<String> = [
255-
"libllbuild",
256-
"llbuildCore",
283+
let llvmTargets: Set<String> = [
284+
"libllbuild",
285+
"llbuildCore",
257286

258-
"llvmDemangle",
259-
"llvmSupport",
287+
"llvmDemangle",
288+
"llvmSupport",
260289

261-
"llbuild",
262-
"llbuildBasic",
263-
"llbuildBuildSystem",
264-
"llbuildCommands",
265-
"llbuildNinja",
290+
"llbuild",
291+
"llbuildBasic",
292+
"llbuildBuildSystem",
293+
"llbuildCommands",
294+
"llbuildNinja",
266295

267-
"llbuildBasicTests",
268-
"llbuildBuildSystemTests",
269-
"llbuildCoreTests",
270-
"llbuildNinjaTests",
296+
"llbuildBasicTests",
297+
"llbuildBuildSystemTests",
298+
"llbuildCoreTests",
299+
"llbuildNinjaTests",
271300

272-
"swift-build-tool",
273-
]
301+
"swift-build-tool",
302+
]
274303

304+
if !useTerminfo {
275305
package.targets.filter({ llvmTargets.contains($0.name) }).forEach { target in
276306
target.cxxSettings = (target.cxxSettings ?? []) + [
277-
.define("LLVM_ON_WIN32", .when(platforms: [.windows])),
278-
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])),
279-
.define("_CRT_NONSTDC_NO_WARNINGS", .when(platforms: [.windows])),
307+
.define("LLBUILD_NO_TERMINFO"),
280308
]
281309
}
282310
}
283311

312+
// FIXME: Conditionalize these flags since SwiftPM 5.3 and earlier will crash for platforms they don't know about.
313+
#if os(Windows)
314+
package.targets.filter({ llvmTargets.contains($0.name) }).forEach { target in
315+
target.cxxSettings = (target.cxxSettings ?? []) + [
316+
.define("LLVM_ON_WIN32", .when(platforms: [.windows])),
317+
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])),
318+
.define("_CRT_NONSTDC_NO_WARNINGS", .when(platforms: [.windows])),
319+
]
320+
}
321+
284322
package.targets.first { $0.name == "llbuildBasic" }?.linkerSettings = [
285323
.linkedLibrary("ShLwApi", .when(platforms: [.windows]))
286324
]

include/llvm/Config/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
#define HAVE_SYS_TYPES_H 1
268268

269269
/* Define if the setupterm() function is supported this platform. */
270-
#if defined(__APPLE__) && TARGET_OS_IPHONE
270+
#if (defined(__APPLE__) && TARGET_OS_IPHONE) || defined(LLBUILD_NO_TERMINFO)
271271
#undef HAVE_TERMINFO
272272
#else
273273
#define HAVE_TERMINFO 1

unittests/TestSupport/XCTestCase+Extensions.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// See http://swift.org/LICENSE.txt for license information
77
// See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9+
#if canImport(XCTest)
910
import XCTest
1011

1112
@available(macOS 10.15, *)
@@ -56,3 +57,4 @@ public extension XCTestCase {
5657
return filePath
5758
}
5859
}
60+
#endif

0 commit comments

Comments
 (0)