Skip to content

[6.0][ClangImporter] Make sure the -resource-dir is checked before the -sdk, as done everywhere else in the compiler #76199

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 2 commits into from
Dec 2, 2024
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
15 changes: 14 additions & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ std::optional<std::vector<std::string>> ClangImporter::getClangCC1Arguments(
llvm::interleave(
invocationArgs, [](StringRef arg) { llvm::errs() << arg; },
[] { llvm::errs() << "' '"; });
llvm::errs() << "'\n";
llvm::errs() << "'\n\n";
}

clang::CreateInvocationOptions CIOpts;
Expand Down Expand Up @@ -1260,11 +1260,24 @@ ClangImporter::create(ASTContext &ctx,
// Wrap Swift's FS to allow Clang to override the working directory
VFS = llvm::vfs::RedirectingFileSystem::create(
fileMapping.redirectedFiles, true, *ctx.SourceMgr.getFileSystem());
if (importerOpts.DumpClangDiagnostics) {
llvm::errs() << "clang importer redirected file mappings:\n";
for (const auto &mapping : fileMapping.redirectedFiles) {
llvm::errs() << " mapping real file '" << mapping.second
<< "' to virtual file '" << mapping.first << "'\n";
}
llvm::errs() << "\n";
}

if (!fileMapping.overridenFiles.empty()) {
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> overridenVFS =
new llvm::vfs::InMemoryFileSystem();
for (const auto &file : fileMapping.overridenFiles) {
if (importerOpts.DumpClangDiagnostics) {
llvm::errs() << "clang importer overriding file '" << file.first
<< "' with the following contents:\n";
llvm::errs() << file.second << "\n";
}
auto contents = ctx.Allocate<char>(file.second.size() + 1);
std::copy(file.second.begin(), file.second.end(), contents.begin());
// null terminate the buffer.
Expand Down
14 changes: 7 additions & 7 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ static std::optional<Path> getActualModuleMapPath(

Path result;

StringRef SDKPath = Opts.getSDKPath();
if (!SDKPath.empty()) {
result.append(SDKPath.begin(), SDKPath.end());
llvm::sys::path::append(result, "usr", "lib", "swift");
if (!Opts.RuntimeResourcePath.empty()) {
result.append(Opts.RuntimeResourcePath.begin(),
Opts.RuntimeResourcePath.end());
llvm::sys::path::append(result, platform);
if (isArchSpecific) {
llvm::sys::path::append(result, arch);
Expand All @@ -51,10 +50,11 @@ static std::optional<Path> getActualModuleMapPath(
return result;
}

if (!Opts.RuntimeResourcePath.empty()) {
StringRef SDKPath = Opts.getSDKPath();
if (!SDKPath.empty()) {
result.clear();
result.append(Opts.RuntimeResourcePath.begin(),
Opts.RuntimeResourcePath.end());
result.append(SDKPath.begin(), SDKPath.end());
llvm::sys::path::append(result, "usr", "lib", "swift");
llvm::sys::path::append(result, platform);
if (isArchSpecific) {
llvm::sys::path::append(result, arch);
Expand Down
39 changes: 39 additions & 0 deletions test/ClangImporter/print-module-map-paths.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %empty-directory(%t)
// RUN: mkdir -p %t/resources/linux/armv7 %t/sdk/usr/include %t/sdk/usr/lib/swift/linux/armv7
// RUN: touch %t/sdk/usr/include/{inttypes,stdint,unistd}.h

// RUN: touch %t/resources/linux/armv7/{SwiftGlibc.h,glibc.modulemap}
// RUN: touch %t/sdk/usr/lib/swift/linux/armv7/{SwiftGlibc.h,glibc.modulemap}
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target armv7-unknown-linux-gnueabihf -sdk %t/sdk -resource-dir %t/resources 2>&1 | %FileCheck -check-prefix=CHECK-LINUX %s

// RUN: cp %S/../../stdlib/public/Cxx/{cxxshim/libcxxshim.modulemap,libstdcxx/libstdcxx.h,libstdcxx/libstdcxx.modulemap} %t/resources/linux
// RUN: %target-swift-frontend %s -typecheck -parse-stdlib -dump-clang-diagnostics -resource-dir %t/resources -cxx-interoperability-mode=default 2>&1 | %FileCheck -check-prefix=CHECK-CXX -check-prefix=CHECK-%target-os-CXX %s

// RUN: mkdir -p %t/resources/android/aarch64 %t/sdk/usr/lib/swift/android/aarch64
// RUN: cp %S/../../stdlib/public/Platform/{SwiftAndroidNDK.h,SwiftBionic.h,android.modulemap} %t/resources/android/aarch64
// RUN: cp %S/../../stdlib/public/Platform/{SwiftAndroidNDK.h,SwiftBionic.h,android.modulemap} %t/sdk/usr/lib/swift/android/aarch64
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target aarch64-unknown-linux-android -sdk %t/sdk -resource-dir %t/resources 2>&1 | %FileCheck -check-prefix=CHECK-ANDROID %s

// RUN: cp %S/../../stdlib/public/Cxx/cxxshim/libcxxshim.modulemap %t/resources/android
// RUN: cp %S/../../stdlib/public/Cxx/cxxshim/libcxxshim.modulemap %t/sdk/usr/lib/swift/android
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target aarch64-unknown-linux-android -sdk %t/sdk -resource-dir %t/resources -cxx-interoperability-mode=default 2>&1 | %FileCheck -check-prefix=CHECK-ANDROID-CXX %s

// CHECK-LINUX: clang importer redirected file mappings:
// CHECK-LINUX-NEXT: mapping real file '{{.*}}{{/|\\}}resources{{/|\\}}linux{{/|\\}}armv7{{/|\\}}glibc.modulemap' to virtual file '{{.*}}{{/|\\}}sdk{{/|\\}}usr{{/|\\}}include{{/|\\}}module.modulemap'
// CHECK-LINUX-NEXT: mapping real file '{{.*}}{{/|\\}}resources{{/|\\}}linux{{/|\\}}armv7{{/|\\}}SwiftGlibc.h' to virtual file '{{.*}}{{/|\\}}sdk{{/|\\}}usr{{/|\\}}include{{/|\\}}SwiftGlibc.h'

// CHECK-CXX: clang importer redirected file mappings:
// CHECK-linux-gnu-CXX: mapping real file '{{.*}}/resources/linux/libstdcxx.h' to virtual file '{{.*}}/usr/include/c++/{{.*}}/libstdcxx.h'
// CHECK-linux-gnu-CXX: clang importer overriding file '{{.*}}/usr/include/c++/{{.*}}/module.modulemap' with the following contents:
// CHECK-linux-gnu-CXX-NEXT: --- libstdcxx.modulemap ---
// CHECK-linux-gnu-CXX: Currently libstdc++ does not have a module map. To work around
// CHECK-linux-gnu-CXX-NEXT: this, Swift provides its own module map for libstdc++.
// CHECK-linux-gnu-CXX: header "libstdcxx.h"
// CHECK-linux-gnu-CXX: clang importer driver args: {{.*}}'-fmodule-map-file={{.*}}resources/linux/libcxxshim.modulemap'

// CHECK-ANDROID: clang importer redirected file mappings:
// CHECK-ANDROID-NEXT: mapping real file '{{.*}}{{/|\\}}resources{{/|\\}}android{{/|\\}}aarch64{{/|\\}}android.modulemap' to virtual file '{{.*}}{{/|\\}}sdk{{/|\\}}usr{{/|\\}}include{{/|\\}}module.modulemap'
// CHECK-ANDROID-NEXT: mapping real file '{{.*}}{{/|\\}}resources{{/|\\}}android{{/|\\}}aarch64{{/|\\}}SwiftAndroidNDK.h' to virtual file '{{.*}}{{/|\\}}sdk{{/|\\}}usr{{/|\\}}include{{/|\\}}SwiftAndroidNDK.h'
// CHECK-ANDROID-NEXT: mapping real file '{{.*}}{{/|\\}}resources{{/|\\}}android{{/|\\}}aarch64{{/|\\}}SwiftBionic.h' to virtual file '{{.*}}{{/|\\}}sdk{{/|\\}}usr{{/|\\}}include{{/|\\}}SwiftBionic.h'

// CHECK-ANDROID-CXX: clang importer driver args: {{.*}}'-fmodule-map-file={{.*}}resources{{/|\\}}android{{/|\\}}libcxxshim.modulemap'
4 changes: 2 additions & 2 deletions test/Frontend/embed-bitcode.ll
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
; MARKER-CMD-NEXT: 00

; CHECK-COMPILER-NOT: argument unused
; CHECK-COMPILER: clang
; CHECK-COMPILER: bin{{/|\\}}clang
; CHECK-COMPILER-SAME: -fembed-bitcode
; CHECK-COMPILER-SAME: -target
; CHECK-COMPILER-NOT: argument unused
; CHECK-COMPILER: Fast Register Allocator

; CHECK-COMPILER-OPT-NOT: argument unused
; CHECK-COMPILER-OPT: clang
; CHECK-COMPILER-OPT: bin{{/|\\}}clang
; CHECK-COMPILER-OPT-SAME: -fembed-bitcode
; CHECK-COMPILER-OPT-SAME: -target
; CHECK-COMPILER-OPT-SAME: -Os
Expand Down