Skip to content

Commit 9b20c9d

Browse files
authored
Merge pull request #427 from artemcm/DependencyScanLib
Rely on `libSwiftScan` for dependency scanning actions.
2 parents 2f0094e + 5848895 commit 9b20c9d

28 files changed

+1392
-171
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ if(CMAKE_VERSION VERSION_LESS 3.16)
1919
set(CMAKE_LINK_LIBRARY_FLAG "-l")
2020
endif()
2121

22-
# ensure Swift compiler can find _CSwiftDriver
23-
add_compile_options($<$<COMPILE_LANGUAGE:Swift>:-I$<SEMICOLON>${CMAKE_CURRENT_SOURCE_DIR}/Sources/_CSwiftDriver/include>)
22+
# ensure Swift compiler can find _CSwiftScan
23+
add_compile_options($<$<COMPILE_LANGUAGE:Swift>:-I$<SEMICOLON>${CMAKE_CURRENT_SOURCE_DIR}/Sources/CSwiftScan/include>)
2424

2525
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
2626

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ let package = Package(
3636
targets: ["SwiftDriverExecution"]),
3737
],
3838
targets: [
39-
.target(name: "_CSwiftDriver"),
39+
40+
/// C modules wrapper for _InternalLibSwiftScan.
41+
.target(name: "CSwiftScan"),
4042

4143
/// The driver library.
4244
.target(
4345
name: "SwiftDriver",
44-
dependencies: ["SwiftOptions", "SwiftToolsSupport-auto", "Yams", "_CSwiftDriver"]),
46+
dependencies: ["SwiftOptions", "SwiftToolsSupport-auto",
47+
"CSwiftScan", "Yams"]),
4548

4649
/// The execution library.
4750
.target(

Sources/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +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-
add_subdirectory(_CSwiftDriver)
9+
add_subdirectory(CSwiftScan)
1010
add_subdirectory(SwiftOptions)
1111
add_subdirectory(SwiftDriver)
1212
add_subdirectory(SwiftDriverExecution)

Sources/_CSwiftDriver/CMakeLists.txt renamed to Sources/CSwiftScan/CMakeLists.txt

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

9-
add_library(CSwiftDriver STATIC
10-
_CSwiftDriverImpl.c)
9+
add_library(CSwiftScan STATIC
10+
CSwiftScanImpl.c)

Sources/CSwiftScan/CSwiftScanImpl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is here to prevent the package manager from warning about a
2+
// target with no sources.
3+
#include "include/swiftscan_header.h"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module CSwiftScan {
2+
header "swiftscan_header.h"
3+
export *
4+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
//===-- swiftscan_header.h - C API for Swift Dependency Scanning --*- C -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 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+
13+
#ifndef SWIFT_C_DEPENDENCY_SCAN_H
14+
#define SWIFT_C_DEPENDENCY_SCAN_H
15+
16+
#include <stdbool.h>
17+
#include <stddef.h>
18+
#include <stdint.h>
19+
20+
#define SWIFTSCAN_VERSION_MAJOR 0
21+
#define SWIFTSCAN_VERSION_MINOR 1
22+
23+
//=== Public Scanner Data Types -------------------------------------------===//
24+
25+
typedef struct {
26+
const void *data;
27+
size_t length;
28+
} swiftscan_string_ref_t;
29+
30+
typedef struct {
31+
swiftscan_string_ref_t *strings;
32+
size_t count;
33+
} swiftscan_string_set_t;
34+
35+
typedef enum {
36+
SWIFTSCAN_DEPENDENCY_INFO_SWIFT_TEXTUAL = 0,
37+
SWIFTSCAN_DEPENDENCY_INFO_SWIFT_BINARY = 1,
38+
SWIFTSCAN_DEPENDENCY_INFO_SWIFT_PLACEHOLDER = 2,
39+
SWIFTSCAN_DEPENDENCY_INFO_CLANG = 3
40+
} swiftscan_dependency_info_kind_t;
41+
42+
typedef struct swiftscan_module_details_s *swiftscan_module_details_t;
43+
typedef struct swiftscan_dependency_info_s *swiftscan_dependency_info_t;
44+
typedef struct swiftscan_dependency_graph_s *swiftscan_dependency_graph_t;
45+
typedef struct swiftscan_import_set_s *swiftscan_import_set_t;
46+
typedef struct {
47+
swiftscan_dependency_info_t *modules;
48+
size_t count;
49+
} swiftscan_dependency_set_t;
50+
51+
//=== Batch Scan Input Specification --------------------------------------===//
52+
53+
typedef struct swiftscan_batch_scan_entry_s *swiftscan_batch_scan_entry_t;
54+
typedef struct {
55+
swiftscan_batch_scan_entry_t *modules;
56+
size_t count;
57+
} swiftscan_batch_scan_input_t;
58+
typedef struct {
59+
swiftscan_dependency_graph_t *results;
60+
size_t count;
61+
} swiftscan_batch_scan_result_t;
62+
63+
//=== Scanner Invocation Specification ------------------------------------===//
64+
65+
typedef struct swiftscan_scan_invocation_s *swiftscan_scan_invocation_t;
66+
typedef void *swiftscan_scanner_t;
67+
68+
//=== libSwiftScan Functions ------------------------------------------------===//
69+
70+
typedef struct {
71+
72+
//=== Dependency Result Functions -----------------------------------------===//
73+
swiftscan_string_ref_t
74+
(*swiftscan_dependency_graph_get_main_module_name)(swiftscan_dependency_graph_t);
75+
swiftscan_dependency_set_t *
76+
(*swiftscan_dependency_graph_get_dependencies)(swiftscan_dependency_graph_t);
77+
78+
//=== Dependency Module Info Functions ------------------------------------===//
79+
swiftscan_string_ref_t
80+
(*swiftscan_module_info_get_module_name)(swiftscan_dependency_info_t);
81+
swiftscan_string_ref_t
82+
(*swiftscan_module_info_get_module_path)(swiftscan_dependency_info_t);
83+
swiftscan_string_set_t *
84+
(*swiftscan_module_info_get_source_files)(swiftscan_dependency_info_t);
85+
swiftscan_string_set_t *
86+
(*swiftscan_module_info_get_direct_dependencies)(swiftscan_dependency_info_t);
87+
swiftscan_module_details_t
88+
(*swiftscan_module_info_get_details)(swiftscan_dependency_info_t);
89+
90+
//=== Dependency Module Info Details Functions ----------------------------===//
91+
swiftscan_dependency_info_kind_t
92+
(*swiftscan_module_detail_get_kind)(swiftscan_module_details_t);
93+
94+
//=== Swift Textual Module Details query APIs -----------------------------===//
95+
swiftscan_string_ref_t
96+
(*swiftscan_swift_textual_detail_get_module_interface_path)(swiftscan_module_details_t);
97+
swiftscan_string_set_t *
98+
(*swiftscan_swift_textual_detail_get_compiled_module_candidates)(swiftscan_module_details_t);
99+
swiftscan_string_ref_t
100+
(*swiftscan_swift_textual_detail_get_bridging_header_path)(swiftscan_module_details_t);
101+
swiftscan_string_set_t *
102+
(*swiftscan_swift_textual_detail_get_bridging_source_files)(swiftscan_module_details_t);
103+
swiftscan_string_set_t *
104+
(*swiftscan_swift_textual_detail_get_bridging_module_dependencies)(swiftscan_module_details_t);
105+
swiftscan_string_set_t *
106+
(*swiftscan_swift_textual_detail_get_command_line)(swiftscan_module_details_t);
107+
swiftscan_string_set_t *
108+
(*swiftscan_swift_textual_detail_get_extra_pcm_args)(swiftscan_module_details_t);
109+
swiftscan_string_ref_t
110+
(*swiftscan_swift_textual_detail_get_context_hash)(swiftscan_module_details_t);
111+
bool
112+
(*swiftscan_swift_textual_detail_get_is_framework)(swiftscan_module_details_t);
113+
114+
//=== Swift Binary Module Details query APIs ------------------------------===//
115+
swiftscan_string_ref_t
116+
(*swiftscan_swift_binary_detail_get_compiled_module_path)(swiftscan_module_details_t);
117+
swiftscan_string_ref_t
118+
(*swiftscan_swift_binary_detail_get_module_doc_path)(swiftscan_module_details_t);
119+
swiftscan_string_ref_t
120+
(*swiftscan_swift_binary_detail_get_module_source_info_path)(swiftscan_module_details_t);
121+
122+
//=== Swift Placeholder Module Details query APIs -------------------------===//
123+
swiftscan_string_ref_t
124+
(*swiftscan_swift_placeholder_detail_get_compiled_module_path)(swiftscan_module_details_t);
125+
swiftscan_string_ref_t
126+
(*swiftscan_swift_placeholder_detail_get_module_doc_path)(swiftscan_module_details_t);
127+
swiftscan_string_ref_t
128+
(*swiftscan_swift_placeholder_detail_get_module_source_info_path)(swiftscan_module_details_t);
129+
130+
//=== Clang Module Details query APIs -------------------------------------===//
131+
swiftscan_string_ref_t
132+
(*swiftscan_clang_detail_get_module_map_path)(swiftscan_module_details_t);
133+
swiftscan_string_ref_t
134+
(*swiftscan_clang_detail_get_context_hash)(swiftscan_module_details_t);
135+
swiftscan_string_set_t *
136+
(*swiftscan_clang_detail_get_command_line)(swiftscan_module_details_t);
137+
138+
//=== Batch Scan Input Functions ------------------------------------------===//
139+
swiftscan_batch_scan_input_t *
140+
(*swiftscan_batch_scan_input_create)(void);
141+
void
142+
(*swiftscan_batch_scan_input_set_modules)(swiftscan_batch_scan_input_t *, int, swiftscan_batch_scan_entry_t *);
143+
144+
//=== Batch Scan Entry Functions ------------------------------------------===//
145+
swiftscan_batch_scan_entry_t
146+
(*swiftscan_batch_scan_entry_create)(void);
147+
void
148+
(*swiftscan_batch_scan_entry_set_module_name)(swiftscan_batch_scan_entry_t, const char *);
149+
void
150+
(*swiftscan_batch_scan_entry_set_arguments)(swiftscan_batch_scan_entry_t, const char *);
151+
void
152+
(*swiftscan_batch_scan_entry_set_is_swift)(swiftscan_batch_scan_entry_t, bool);
153+
swiftscan_string_ref_t
154+
(*swiftscan_batch_scan_entry_get_module_name)(swiftscan_batch_scan_entry_t);
155+
swiftscan_string_ref_t
156+
(*swiftscan_batch_scan_entry_get_arguments)(swiftscan_batch_scan_entry_t);
157+
bool
158+
(*swiftscan_batch_scan_entry_get_is_swift)(swiftscan_batch_scan_entry_t);
159+
160+
//=== Prescan Result Functions --------------------------------------------===//
161+
swiftscan_string_set_t *
162+
(*swiftscan_import_set_get_imports)(swiftscan_import_set_t);
163+
164+
//=== Scanner Invocation Functions ----------------------------------------===//
165+
swiftscan_scan_invocation_t
166+
(*swiftscan_scan_invocation_create)();
167+
void
168+
(*swiftscan_scan_invocation_set_working_directory)(swiftscan_scan_invocation_t, const char *);
169+
void
170+
(*swiftscan_scan_invocation_set_argv)(swiftscan_scan_invocation_t, int, const char **);
171+
swiftscan_string_ref_t
172+
(*swiftscan_scan_invocation_get_working_directory)(swiftscan_scan_invocation_t);
173+
int
174+
(*swiftscan_scan_invocation_get_argc)(swiftscan_scan_invocation_t);
175+
swiftscan_string_set_t *
176+
(*swiftscan_scan_invocation_get_argv)(swiftscan_scan_invocation_t);
177+
178+
//=== Cleanup Functions ---------------------------------------------------===//
179+
void
180+
(*swiftscan_dependency_graph_dispose)(swiftscan_dependency_graph_t);
181+
void
182+
(*swiftscan_import_set_dispose)(swiftscan_import_set_t);
183+
void
184+
(*swiftscan_batch_scan_entry_dispose)(swiftscan_batch_scan_entry_t);
185+
void
186+
(*swiftscan_batch_scan_input_dispose)(swiftscan_batch_scan_input_t *);
187+
void
188+
(*swiftscan_batch_scan_result_dispose)(swiftscan_batch_scan_result_t *);
189+
void
190+
(*swiftscan_scan_invocation_dispose)(swiftscan_scan_invocation_t);
191+
192+
//=== Scanner Functions ---------------------------------------------------===//
193+
swiftscan_scanner_t (*swiftscan_scanner_create)(void);
194+
void (*swiftscan_scanner_dispose)(swiftscan_scanner_t);
195+
196+
swiftscan_dependency_graph_t
197+
(*swiftscan_dependency_graph_create)(swiftscan_scanner_t, swiftscan_scan_invocation_t);
198+
199+
swiftscan_batch_scan_result_t *
200+
(*swiftscan_batch_scan_result_create)(swiftscan_scanner_t,
201+
swiftscan_batch_scan_input_t *,
202+
swiftscan_scan_invocation_t);
203+
204+
swiftscan_import_set_t
205+
(*swiftscan_import_set_create)(swiftscan_scanner_t, swiftscan_scan_invocation_t);
206+
} swiftscan_functions_t;
207+
208+
#endif // SWIFT_C_DEPENDENCY_SCAN_H

Sources/SwiftDriver/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ add_library(SwiftDriver
1616
"ExplicitModuleBuilds/InterModuleDependencies/InterModuleDependencyGraph.swift"
1717
"ExplicitModuleBuilds/InterModuleDependencies/InterModuleDependencyOracle.swift"
1818

19+
SwiftScan/DependencyGraphBuilder.swift
20+
SwiftScan/SwiftScan.swift
21+
1922
Driver/CompilerMode.swift
2023
Driver/DebugInfo.swift
2124
Driver/Driver.swift
@@ -101,7 +104,7 @@ target_link_libraries(SwiftDriver PUBLIC
101104
target_link_libraries(SwiftDriver PRIVATE
102105
CYaml
103106
Yams
104-
CSwiftDriver)
107+
CSwiftScan)
105108

106109
set_property(GLOBAL APPEND PROPERTY SWIFTDRIVER_EXPORTS SwiftDriver)
107110

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import TSCBasic
1313
import TSCUtility
1414
import Foundation
1515
import SwiftOptions
16-
@_implementationOnly import _CSwiftDriver
1716

1817
/// The Swift driver.
1918
public struct Driver {
@@ -125,6 +124,9 @@ public struct Driver {
125124
/// The target triple.
126125
@_spi(Testing) public var targetTriple: Triple { frontendTargetInfo.target.triple }
127126

127+
/// The host environment triple.
128+
@_spi(Testing) public let hostTriple: Triple
129+
128130
/// The variant target triple.
129131
var targetVariantTriple: Triple? {
130132
frontendTargetInfo.targetVariant?.triple
@@ -402,6 +404,12 @@ public struct Driver {
402404
executor: self.executor, fileSystem: fileSystem,
403405
useStaticResourceDir: self.useStaticResourceDir)
404406

407+
// Compute the host machine's triple
408+
self.hostTriple =
409+
try Self.computeHostTriple(toolchain: self.toolchain,
410+
executor: self.executor,
411+
swiftCompilerPrefixArgs: self.swiftCompilerPrefixArgs)
412+
405413
// Classify and collect all of the input files.
406414
let inputFiles = try Self.collectInputFiles(&self.parsedOptions)
407415
self.inputFiles = inputFiles
@@ -412,23 +420,6 @@ public struct Driver {
412420
return ($0, modTime)
413421
})
414422

415-
// Create an instance of an inter-module dependency oracle, if the driver's
416-
// client did not provide one. The clients are expected to provide an oracle
417-
// when they wish to share module dependency information across targets.
418-
if let dependencyOracle = interModuleDependencyOracle {
419-
self.interModuleDependencyOracle = dependencyOracle
420-
} else {
421-
self.interModuleDependencyOracle = InterModuleDependencyOracle()
422-
423-
// This is a shim for backwards-compatibility with ModuleInfoMap-based API
424-
// used by SwiftPM
425-
if let externalArtifacts = externalBuildArtifacts {
426-
if !externalArtifacts.1.isEmpty {
427-
try self.interModuleDependencyOracle.mergeModules(from: externalArtifacts.1)
428-
}
429-
}
430-
}
431-
432423
do {
433424
let outputFileMap: OutputFileMap?
434425
// Initialize an empty output file map, which will be populated when we start creating jobs.
@@ -450,6 +441,23 @@ public struct Driver {
450441
}
451442
}
452443

444+
// Create an instance of an inter-module dependency oracle, if the driver's
445+
// client did not provide one. The clients are expected to provide an oracle
446+
// when they wish to share module dependency information across targets.
447+
if let dependencyOracle = interModuleDependencyOracle {
448+
self.interModuleDependencyOracle = dependencyOracle
449+
} else {
450+
self.interModuleDependencyOracle = InterModuleDependencyOracle()
451+
452+
// This is a shim for backwards-compatibility with ModuleInfoMap-based API
453+
// used by SwiftPM
454+
if let externalArtifacts = externalBuildArtifacts {
455+
if !externalArtifacts.1.isEmpty {
456+
try self.interModuleDependencyOracle.mergeModules(from: externalArtifacts.1)
457+
}
458+
}
459+
}
460+
453461
self.fileListThreshold = try Self.computeFileListThreshold(&self.parsedOptions, diagnosticsEngine: diagnosticsEngine)
454462
self.shouldUseInputFileList = inputFiles.count > fileListThreshold
455463
if shouldUseInputFileList {
@@ -2099,6 +2107,17 @@ extension Driver {
20992107
static let defaultToolchainType: Toolchain.Type = GenericUnixToolchain.self
21002108
#endif
21012109

2110+
static func computeHostTriple(toolchain: Toolchain,
2111+
executor: DriverExecutor,
2112+
swiftCompilerPrefixArgs: [String]) throws -> Triple {
2113+
return try executor.execute(
2114+
job: toolchain.printTargetInfoJob(target: nil, targetVariant: nil,
2115+
swiftCompilerPrefixArgs: swiftCompilerPrefixArgs),
2116+
capturingJSONOutputAs: FrontendTargetInfo.self,
2117+
forceResponseFiles: false,
2118+
recordedInputModificationDates: [:]).target.triple
2119+
}
2120+
21022121
static func computeToolchain(
21032122
_ parsedOptions: inout ParsedOptions,
21042123
diagnosticsEngine: DiagnosticsEngine,

0 commit comments

Comments
 (0)