Skip to content

Commit 38a998a

Browse files
committed
Fix direct clang cc1 emit-pcm commands with vfs overlay on Windows
Explicit module builds currently fail on Windows because direct-clang-cc1-module-build emit-pcm commands take overlaid system module map files as inputs but miss the clang VFS overlay. This change adds the overlay and fixes explicit module builds on Windows.
1 parent 73fd67b commit 38a998a

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ class CompilerInstance {
736736
/// Retrieve the printing path for bridging header.
737737
std::string getBridgingHeaderPath() const;
738738

739+
void setUpVFSForDirectClangCC1EmitPCM();
740+
739741
private:
740742
/// Set up the file system by loading and validating all VFS overlay YAML
741743
/// files. If the process of validating VFS files failed, or the overlay

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,14 @@ ModuleDependencyInfo ModuleDependencyScanner::bridgeClangModuleDependency(
19921992
}
19931993
}
19941994

1995+
// Pass the -sdk flag to make the system header VFS overlay finable
1996+
// for the -direct-clang-cc1-module-build emit-pcm command on Windows.
1997+
StringRef SDKPath = ScanASTContext.SearchPathOpts.getSDKPath();
1998+
if (!SDKPath.empty()) {
1999+
swiftArgs.push_back("-sdk");
2000+
swiftArgs.push_back(SDKPath.str());
2001+
}
2002+
19952003
// Add args reported by the scanner.
19962004
auto clangArgs = invocation.getCC1CommandLine();
19972005
llvm::for_each(clangArgs, addClangArg);

lib/Frontend/Frontend.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,23 @@ void CompilerInstance::setupCachingDiagnosticsProcessorIfNeeded() {
559559
CDP->startDiagnosticCapture();
560560
}
561561

562+
void CompilerInstance::setUpVFSForDirectClangCC1EmitPCM() {
563+
// Set up the VFS overlay so that the -direct-clang-cc1-module-build
564+
// emit-pcm command can find a system modulemap file (like
565+
// vcruntime.modulemap) as an input file.
566+
if (Invocation.getClangImporterOptions().DirectClangCC1ModuleBuild &&
567+
Invocation.getFrontendOptions().RequestedAction ==
568+
FrontendOptions::ActionType::EmitPCM) {
569+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
570+
SourceMgr.getFileSystem();
571+
ClangInvocationFileMapping FileMapping = applyClangInvocationMapping(
572+
*Context, nullptr, VFS, /*suppressDiagnostic=*/false);
573+
if (!FileMapping.redirectedFiles.empty()) {
574+
SourceMgr.setFileSystem(std::move(VFS));
575+
}
576+
}
577+
}
578+
562579
bool CompilerInstance::setup(const CompilerInvocation &Invoke,
563580
std::string &Error, ArrayRef<const char *> Args) {
564581
Invocation = Invoke;
@@ -582,13 +599,17 @@ bool CompilerInstance::setup(const CompilerInvocation &Invoke,
582599

583600
assert(Lexer::isIdentifier(Invocation.getModuleName()));
584601

585-
if (setUpInputs()) {
586-
Error = "Setting up inputs failed";
602+
if (setUpASTContextIfNeeded()) {
603+
Error = "Setting up ASTContext failed";
587604
return true;
588605
}
589606

590-
if (setUpASTContextIfNeeded()) {
591-
Error = "Setting up ASTContext failed";
607+
// ASTContext is needed to set up the VFS overlay, which is needed
608+
// to allow inputs from the VFS overlay are set up.
609+
setUpVFSForDirectClangCC1EmitPCM();
610+
611+
if (setUpInputs()) {
612+
Error = "Setting up inputs failed";
592613
return true;
593614
}
594615

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module SAL [system] {
2+
header "sal.h"
3+
export *
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// REQUIRES: OS=windows-msvc
2+
3+
// RUN: %empty-directory(%t)
4+
5+
// Test that the -direct-clang-cc1-module-build is able to create a module from the VC runtime with an overlaid modulemap file
6+
// RUN: %swift_frontend_plain -frontend -emit-pcm -module-name SAL -o %t/SAL.pcm -direct-clang-cc1-module-build "%vctoolsinstalldir\include\module.modulemap" -sdk %S/Inputs/WinSDK -Xcc -fsystem-module -Xcc -emit-module -Xcc -isystem -Xcc "%vctoolsinstalldir\include" -Xcc -cc1 -Xcc -fmodules -Xcc -fmodules-cache-path=%t/module-cache

test/lit.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,10 @@ config.substitutions.append( ('%clang',
857857

858858
config.substitutions.append(('%existing-swift-features', shlex.quote(json.dumps(list(config.existing_swift_features)))))
859859

860+
if kIsWindows:
861+
VCToolsInstallDir = os.path.normpath(os.getenv("VCToolsInstallDir"))
862+
config.substitutions.append(('%vctoolsinstalldir', VCToolsInstallDir))
863+
860864
###
861865

862866
def disallow(execName):

0 commit comments

Comments
 (0)