Skip to content

[libSwiftScan] Add V2 target info query API that takes compiler executable path as parameter #63132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift-c/DependencyScan/DependencyScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ swiftscan_compiler_supported_features_query();
//=== Target-Info Functions -----------------------------------------------===//
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
swiftscan_compiler_target_info_query(swiftscan_scan_invocation_t invocation);
SWIFTSCAN_PUBLIC swiftscan_string_ref_t
swiftscan_compiler_target_info_query_v2(swiftscan_scan_invocation_t invocation,
const char *main_executable_path);

//=== Scanner Functions ---------------------------------------------------===//

Expand Down
3 changes: 2 additions & 1 deletion include/swift/DependencyScan/DependencyScanningTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class DependencyScannerDiagnosticCollectingConsumer : public DiagnosticConsumer

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

/// The high-level implementation of the dependency scanner that runs on
/// an individual worker thread.
Expand Down
8 changes: 5 additions & 3 deletions lib/DependencyScan/DependencyScanningTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
namespace swift {
namespace dependencies {

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

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

Expand Down
8 changes: 7 additions & 1 deletion tools/libSwiftScan/libSwiftScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,18 @@ static void addFrontendFlagOption(llvm::opt::OptTable &table,

swiftscan_string_ref_t
swiftscan_compiler_target_info_query(swiftscan_scan_invocation_t invocation) {
return swiftscan_compiler_target_info_query_v2(invocation, nullptr);
}

swiftscan_string_ref_t
swiftscan_compiler_target_info_query_v2(swiftscan_scan_invocation_t invocation,
const char *main_executable_path) {
int argc = invocation->argv->count;
std::vector<const char *> Compilation;
for (int i = 0; i < argc; ++i)
Compilation.push_back(swift::c_string_utils::get_C_string(invocation->argv->strings[i]));

auto TargetInfo = swift::dependencies::getTargetInfo(Compilation);
auto TargetInfo = swift::dependencies::getTargetInfo(Compilation, main_executable_path);
if (TargetInfo.getError())
return swift::c_string_utils::create_null();
return TargetInfo.get();
Expand Down
1 change: 1 addition & 0 deletions tools/libSwiftScan/libSwiftScan.exports
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ swiftscan_scanner_dispose
swiftscan_compiler_supported_arguments_query
swiftscan_compiler_supported_features_query
swiftscan_compiler_target_info_query
swiftscan_compiler_target_info_query_v2
swiftscan_scanner_cache_serialize
swiftscan_scanner_cache_load
swiftscan_scanner_cache_reset
Expand Down
19 changes: 18 additions & 1 deletion unittests/DependencyScan/PrintTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ TEST_F(ScanTest, TestTargetInfoQuery) {
for (auto &str : CommandStrArr)
Compilation.push_back(str.c_str());

auto targetInfo = swift::dependencies::getTargetInfo(Compilation);
SmallString<128> pathRoot("base");
SmallString<128> compilerPath(pathRoot);
llvm::sys::path::append(compilerPath, "foo", "bar", "bin", "swift-frontend");
SmallString<128> relativeLibPath(pathRoot);
llvm::sys::path::append(relativeLibPath, "foo", "bar", "lib", "swift");;

auto targetInfo = swift::dependencies::getTargetInfo(Compilation, compilerPath.c_str());
if (targetInfo.getError()) {
llvm::errs() << "For compilation: ";
for (auto &str : Compilation)
Expand All @@ -47,4 +53,15 @@ TEST_F(ScanTest, TestTargetInfoQuery) {
auto targetInfoStr = std::string(swift::c_string_utils::get_C_string(targetInfo.get()));
EXPECT_NE(targetInfoStr.find("\"triple\": \"x86_64-apple-macosx12.0\""), std::string::npos);
EXPECT_NE(targetInfoStr.find("\"librariesRequireRPath\": false"), std::string::npos);

std::string expectedRuntimeResourcePath = "\"runtimeResourcePath\": \"" + relativeLibPath.str().str() + "\"";
// On windows, need to normalize the path back to "\\" separators
size_t start_pos = 0;
while((start_pos = expectedRuntimeResourcePath.find("\\", start_pos)) != std::string::npos) {
expectedRuntimeResourcePath.replace(start_pos, 1, "\\\\");
start_pos += 2;
}
llvm::dbgs() << "Expected Runtime Resource Path: \n" << expectedRuntimeResourcePath << "\n";
llvm::dbgs() << "Result Target Info: \n" << targetInfoStr << "\n";
EXPECT_NE(targetInfoStr.find(expectedRuntimeResourcePath.c_str()), std::string::npos);
}