Skip to content

Commit cb3f278

Browse files
committed
Test: add a script for building modules by using command line arguments emitted from dependency JSON
1 parent 429da7c commit cb3f278

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===--------------- BuildModulesFromGraph.swift --------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
import Foundation
13+
14+
let fileName = CommandLine.arguments[1]
15+
let swiftPath = CommandLine.arguments[2]
16+
let moduleName = CommandLine.arguments[3]
17+
let data = try! Data(contentsOf: URL(fileURLWithPath: fileName))
18+
19+
let decoder = JSONDecoder()
20+
let moduleDependencyGraph = try! decoder.decode(
21+
ModuleDependencyGraph.self, from: data)
22+
23+
func findModuleBuildingCommand(_ moduleName: String) -> [String]? {
24+
for (_, dep) in moduleDependencyGraph.modules {
25+
if dep.modulePath.hasSuffix(moduleName) {
26+
switch dep.details {
27+
case .swift(let details):
28+
return details.commandLine
29+
case .clang(let details):
30+
return details.commandLine
31+
}
32+
} else {
33+
continue
34+
}
35+
}
36+
return nil
37+
}
38+
39+
func run(command: String, arguments: [String] = []) -> Int32 {
40+
let process = Process()
41+
process.launchPath = command
42+
process.arguments = arguments
43+
let outputPipe = Pipe()
44+
process.standardOutput = outputPipe
45+
process.launch()
46+
process.waitUntilExit()
47+
return process.terminationStatus
48+
}
49+
50+
if let command = findModuleBuildingCommand(moduleName) {
51+
exit(run(command: swiftPath, arguments: command))
52+
} else {
53+
fatalError("cannot find module building commands for \(moduleName)")
54+
}

test/ScanDependencies/Inputs/ModuleDependencyGraph.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ struct SwiftModuleDetails: Codable {
6565

6666
/// The bridging header, if any.
6767
var bridgingHeader: BridgingHeader?
68+
69+
/// The Swift command line arguments that need to be passed through
70+
/// to the -compile-module-from-interface action to build this module.
71+
var commandLine: [String]?
6872
}
6973

7074
/// Details specific to Clang modules.

test/ScanDependencies/module_deps.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
// RUN: %target-codesign %t/main
1818
// RUN: %target-run %t/main %t/deps.json
1919

20+
// RUN: mkdir -p %t/BuildModules
21+
// RUN: cp %S/Inputs/BuildModulesFromGraph.swift %t/BuildModules/main.swift
22+
// RUN: %target-build-swift %S/Inputs/ModuleDependencyGraph.swift %t/BuildModules/main.swift -o %t/ModuleBuilder
23+
// RUN: %target-codesign %t/ModuleBuilder
24+
25+
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path A.pcm
26+
// RUN: ls %t/clang-module-cache/A-*.pcm
27+
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path B.pcm
28+
// RUN: ls %t/clang-module-cache/B-*.pcm
29+
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path C.pcm
30+
// RUN: ls %t/clang-module-cache/C-*.pcm
31+
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path A.swiftmodule
32+
// RUN: ls %t/clang-module-cache/A-*.swiftmodule
33+
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path E.swiftmodule
34+
// RUN: ls %t/clang-module-cache/E-*.swiftmodule
35+
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path F.swiftmodule
36+
// RUN: ls %t/clang-module-cache/F-*.swiftmodule
37+
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path G.swiftmodule
38+
// RUN: ls %t/clang-module-cache/G-*.swiftmodule
39+
2040
// REQUIRES: executable_test
2141
// REQUIRES: objc_interop
2242

test/lit.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ config.substitutions.append( ('%Benchmark_O', config.benchmark_o) )
409409
config.substitutions.append( ('%Benchmark_Driver', config.benchmark_driver) )
410410
config.substitutions.append( ('%llvm-strings', config.llvm_strings) )
411411
config.substitutions.append( ('%target-ptrauth', run_ptrauth ) )
412+
config.substitutions.append( ('%swift-path', config.swift) )
412413

413414
# This must come after all substitutions containing "%swift".
414415
config.substitutions.append(

0 commit comments

Comments
 (0)