Skip to content

Commit eecde02

Browse files
authored
Merge pull request #63132 from artemcm/LibSwiftScanTargetInfoV2
[libSwiftScan] Add V2 target info query API that takes compiler executable path as parameter
2 parents 1a597c5 + 1b4bc33 commit eecde02

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ swiftscan_compiler_supported_features_query();
303303
//=== Target-Info Functions -----------------------------------------------===//
304304
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
305305
swiftscan_compiler_target_info_query(swiftscan_scan_invocation_t invocation);
306+
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
307+
swiftscan_compiler_target_info_query_v2(swiftscan_scan_invocation_t invocation,
308+
const char *main_executable_path);
306309

307310
//=== Scanner Functions ---------------------------------------------------===//
308311

include/swift/DependencyScan/DependencyScanningTool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class DependencyScannerDiagnosticCollectingConsumer : public DiagnosticConsumer
4848

4949
/// Given a set of arguments to a print-target-info frontend tool query, produce the
5050
/// JSON target info.
51-
llvm::ErrorOr<swiftscan_string_ref_t> getTargetInfo(ArrayRef<const char *> Command);
51+
llvm::ErrorOr<swiftscan_string_ref_t> getTargetInfo(ArrayRef<const char *> Command,
52+
const char *main_executable_path);
5253

5354
/// The high-level implementation of the dependency scanner that runs on
5455
/// an individual worker thread.

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
namespace swift {
2727
namespace dependencies {
2828

29-
llvm::ErrorOr<swiftscan_string_ref_t> getTargetInfo(ArrayRef<const char *> Command) {
29+
llvm::ErrorOr<swiftscan_string_ref_t> getTargetInfo(ArrayRef<const char *> Command,
30+
const char *main_executable_path) {
3031
// We must reset option occurrences because we are handling an unrelated
3132
// command-line to those possibly parsed before using the same tool.
3233
// We must do so because LLVM options parsing is done using a managed
@@ -45,7 +46,7 @@ llvm::ErrorOr<swiftscan_string_ref_t> getTargetInfo(ArrayRef<const char *> Comma
4546
SourceManager dummySM;
4647
DiagnosticEngine DE(dummySM);
4748
CompilerInvocation Invocation;
48-
if (Invocation.parseArgs(Args, DE)) {
49+
if (Invocation.parseArgs(Args, DE, nullptr, {}, main_executable_path)) {
4950
return std::make_error_code(std::errc::invalid_argument);
5051
}
5152

@@ -235,7 +236,8 @@ DependencyScanningTool::initCompilerInstanceForScan(
235236
// We must do so because LLVM options parsing is done using a managed
236237
// static `GlobalParser`.
237238
llvm::cl::ResetAllOptionOccurrences();
238-
if (Invocation.parseArgs(CommandArgs, Instance->getDiags())) {
239+
if (Invocation.parseArgs(CommandArgs, Instance->getDiags(),
240+
nullptr, WorkingDirectory, "/tmp/foo")) {
239241
return std::make_error_code(std::errc::invalid_argument);
240242
}
241243

tools/libSwiftScan/libSwiftScan.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,18 @@ static void addFrontendFlagOption(llvm::opt::OptTable &table,
507507

508508
swiftscan_string_ref_t
509509
swiftscan_compiler_target_info_query(swiftscan_scan_invocation_t invocation) {
510+
return swiftscan_compiler_target_info_query_v2(invocation, nullptr);
511+
}
512+
513+
swiftscan_string_ref_t
514+
swiftscan_compiler_target_info_query_v2(swiftscan_scan_invocation_t invocation,
515+
const char *main_executable_path) {
510516
int argc = invocation->argv->count;
511517
std::vector<const char *> Compilation;
512518
for (int i = 0; i < argc; ++i)
513519
Compilation.push_back(swift::c_string_utils::get_C_string(invocation->argv->strings[i]));
514520

515-
auto TargetInfo = swift::dependencies::getTargetInfo(Compilation);
521+
auto TargetInfo = swift::dependencies::getTargetInfo(Compilation, main_executable_path);
516522
if (TargetInfo.getError())
517523
return swift::c_string_utils::create_null();
518524
return TargetInfo.get();

tools/libSwiftScan/libSwiftScan.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ swiftscan_scanner_dispose
5858
swiftscan_compiler_supported_arguments_query
5959
swiftscan_compiler_supported_features_query
6060
swiftscan_compiler_target_info_query
61+
swiftscan_compiler_target_info_query_v2
6162
swiftscan_scanner_cache_serialize
6263
swiftscan_scanner_cache_load
6364
swiftscan_scanner_cache_reset

unittests/DependencyScan/PrintTarget.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ TEST_F(ScanTest, TestTargetInfoQuery) {
3636
for (auto &str : CommandStrArr)
3737
Compilation.push_back(str.c_str());
3838

39-
auto targetInfo = swift::dependencies::getTargetInfo(Compilation);
39+
SmallString<128> pathRoot("base");
40+
SmallString<128> compilerPath(pathRoot);
41+
llvm::sys::path::append(compilerPath, "foo", "bar", "bin", "swift-frontend");
42+
SmallString<128> relativeLibPath(pathRoot);
43+
llvm::sys::path::append(relativeLibPath, "foo", "bar", "lib", "swift");;
44+
45+
auto targetInfo = swift::dependencies::getTargetInfo(Compilation, compilerPath.c_str());
4046
if (targetInfo.getError()) {
4147
llvm::errs() << "For compilation: ";
4248
for (auto &str : Compilation)
@@ -47,4 +53,15 @@ TEST_F(ScanTest, TestTargetInfoQuery) {
4753
auto targetInfoStr = std::string(swift::c_string_utils::get_C_string(targetInfo.get()));
4854
EXPECT_NE(targetInfoStr.find("\"triple\": \"x86_64-apple-macosx12.0\""), std::string::npos);
4955
EXPECT_NE(targetInfoStr.find("\"librariesRequireRPath\": false"), std::string::npos);
56+
57+
std::string expectedRuntimeResourcePath = "\"runtimeResourcePath\": \"" + relativeLibPath.str().str() + "\"";
58+
// On windows, need to normalize the path back to "\\" separators
59+
size_t start_pos = 0;
60+
while((start_pos = expectedRuntimeResourcePath.find("\\", start_pos)) != std::string::npos) {
61+
expectedRuntimeResourcePath.replace(start_pos, 1, "\\\\");
62+
start_pos += 2;
63+
}
64+
llvm::dbgs() << "Expected Runtime Resource Path: \n" << expectedRuntimeResourcePath << "\n";
65+
llvm::dbgs() << "Result Target Info: \n" << targetInfoStr << "\n";
66+
EXPECT_NE(targetInfoStr.find(expectedRuntimeResourcePath.c_str()), std::string::npos);
5067
}

0 commit comments

Comments
 (0)