Skip to content

Commit 767a222

Browse files
committed
[Build] Fix regression in Xcc handling.
- Noticed by Chris Bailey (see #507). - This also adds at least some test coverage of -Xcc and -Xswiftc. - This also fixes it so we don't quite the extra C flags when passing them to Clang (which would still work, because Clang ignores -Xcc, but was incorrect and produced a warning).
1 parent d76d276 commit 767a222

File tree

7 files changed

+42
-8
lines changed

7 files changed

+42
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import PackageDescription
2+
3+
let package = Package(
4+
name: "ExtraCommandLineFlags",
5+
targets: [
6+
Target(name: "SwiftExec", dependencies: ["CLib"])])
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include "CLib.h"
2+
3+
void foo(void) { }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This is to check a -Xcc arg.
2+
#if !defined(EXTRA_C_DEFINE) || EXTRA_C_DEFINE != 2
3+
#error "unexpected compiler flags"
4+
#endif
5+
6+
void foo(void);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import CLib
2+
3+
// This is expected to be set with -Xswiftc.
4+
#if !EXTRA_SWIFTC_DEFINE
5+
doesNotCompile()
6+
#endif
7+
8+
foo()
9+

Sources/Build/describe().swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ public func describe(_ prefix: AbsolutePath, _ conf: Configuration, _ graph: Pac
2727
throw Error.onlyCModule(name: module.name)
2828
}
2929

30-
let Xcc = flags.cCompilerFlags.flatMap{ ["-Xcc", $0] }
3130
let Xld = flags.linkerFlags.flatMap{ ["-Xlinker", $0] }
3231
let prefix = prefix.appending(component: conf.dirname)
3332
try makeDirectories(prefix)
34-
let swiftcArgs = flags.cCompilerFlags + flags.swiftCompilerFlags + verbosity.ccArgs
33+
let swiftcArgs = flags.cCompilerFlags.flatMap{ ["-Xcc", $0] } + flags.swiftCompilerFlags + verbosity.ccArgs
3534

3635
let SWIFT_EXEC = toolchain.SWIFT_EXEC
3736
let CC = getenv("CC") ?? "clang"
@@ -52,7 +51,7 @@ public func describe(_ prefix: AbsolutePath, _ conf: Configuration, _ graph: Pac
5251
if module.isTest { continue }
5352
#endif
5453
// FIXME: Find a way to eliminate `externalModules` from here.
55-
let compile = try Command.compile(clangModule: module, externalModules: graph.externalModules, configuration: conf, prefix: prefix, CC: CC, otherArgs: Xcc + toolchain.platformArgsClang)
54+
let compile = try Command.compile(clangModule: module, externalModules: graph.externalModules, configuration: conf, prefix: prefix, CC: CC, otherArgs: flags.cCompilerFlags + toolchain.platformArgsClang)
5655
commands += compile
5756
targets.append(compile, for: module)
5857

Tests/Functional/Utilities.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,17 @@ extension SwiftPMProduct {
182182
}
183183

184184
@discardableResult
185-
func executeSwiftBuild(_ chdir: AbsolutePath, configuration: Configuration = .Debug, printIfError: Bool = false, Xld: [String] = [], env: [String: String] = [:]) throws -> String {
185+
func executeSwiftBuild(_ chdir: AbsolutePath, configuration: Configuration = .Debug, printIfError: Bool = false, Xcc: [String] = [], Xld: [String] = [], Xswiftc: [String] = [], env: [String: String] = [:]) throws -> String {
186186
var args = ["--configuration"]
187187
switch configuration {
188188
case .Debug:
189189
args.append("debug")
190190
case .Release:
191191
args.append("release")
192192
}
193+
args += Xcc.flatMap{ ["-Xcc", $0] }
193194
args += Xld.flatMap{ ["-Xlinker", $0] }
195+
args += Xswiftc.flatMap{ ["-Xswiftc", $0] }
194196

195197
let swiftBuild = SwiftPMProduct.SwiftBuild
196198
var env = env
@@ -211,11 +213,11 @@ func mktmpdir(function: StaticString = #function, file: StaticString = #file, li
211213
}
212214
}
213215

214-
func XCTAssertBuilds(_ path: AbsolutePath, configurations: Set<Configuration> = [.Debug, .Release], file: StaticString = #file, line: UInt = #line, Xld: [String] = [], env: [String: String] = [:]) {
216+
func XCTAssertBuilds(_ path: AbsolutePath, configurations: Set<Configuration> = [.Debug, .Release], file: StaticString = #file, line: UInt = #line, Xcc: [String] = [], Xld: [String] = [], Xswiftc: [String] = [], env: [String: String] = [:]) {
215217
for conf in configurations {
216218
do {
217219
print(" Building \(conf)")
218-
_ = try executeSwiftBuild(path, configuration: conf, printIfError: true, Xld: Xld, env: env)
220+
_ = try executeSwiftBuild(path, configuration: conf, printIfError: true, Xcc: Xcc, Xld: Xld, Xswiftc: Xswiftc, env: env)
219221
} catch {
220222
XCTFail("`swift build -c \(conf)' failed:\n\n\(error)\n", file: file, line: line)
221223
}
@@ -230,9 +232,9 @@ func XCTAssertSwiftTest(_ path: AbsolutePath, file: StaticString = #file, line:
230232
}
231233
}
232234

233-
func XCTAssertBuildFails(_ path: AbsolutePath, file: StaticString = #file, line: UInt = #line) {
235+
func XCTAssertBuildFails(_ path: AbsolutePath, file: StaticString = #file, line: UInt = #line, Xcc: [String] = [], Xld: [String] = [], Xswiftc: [String] = [], env: [String: String] = [:]) {
234236
do {
235-
_ = try executeSwiftBuild(path)
237+
_ = try executeSwiftBuild(path, Xcc: Xcc, Xld: Xld, Xswiftc: Xswiftc)
236238

237239
XCTFail("`swift build' succeeded but should have failed", file: file, line: line)
238240

Tests/Functional/ValidLayoutTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ class ValidLayoutsTestCase: XCTestCase {
101101
}
102102
}
103103

104+
func testExtraCommandLineFlags() {
105+
fixture(name: "ValidLayouts/ExtraCommandLineFlags") { prefix in
106+
// This project is expected to require Xcc and Xswiftc overrides.
107+
XCTAssertBuildFails(prefix)
108+
XCTAssertBuildFails(prefix, Xcc: ["-DEXTRA_C_DEFINE=2"])
109+
XCTAssertBuilds(prefix, Xcc: ["-DEXTRA_C_DEFINE=2"], Xswiftc: ["-DEXTRA_SWIFTC_DEFINE"])
110+
}
111+
}
112+
104113
static var allTests = [
105114
("testSingleModuleLibrary", testSingleModuleLibrary),
106115
("testSingleModuleExecutable", testSingleModuleExecutable),

0 commit comments

Comments
 (0)