|
18 | 18 | #include "clang/Driver/Driver.h"
|
19 | 19 | #include "clang/Driver/ToolChain.h"
|
20 | 20 | #include "clang/Frontend/CompilerInstance.h"
|
| 21 | +#include "llvm/WindowsDriver/MSVCPaths.h" |
21 | 22 |
|
22 | 23 | using namespace swift;
|
23 | 24 |
|
@@ -296,14 +297,119 @@ getLibStdCxxFileMapping(ASTContext &ctx) {
|
296 | 297 | };
|
297 | 298 | }
|
298 | 299 |
|
| 300 | +namespace { |
| 301 | +std::string |
| 302 | +GetWindowsAuxiliaryFile(StringRef modulemap, const SearchPathOptions &Options) { |
| 303 | + StringRef SDKPath = Options.getSDKPath(); |
| 304 | + if (!SDKPath.empty()) { |
| 305 | + llvm::SmallString<261> path{SDKPath}; |
| 306 | + llvm::sys::path::append(path, "usr", "share", modulemap); |
| 307 | + if (llvm::sys::fs::exists(path)) |
| 308 | + return path.str().str(); |
| 309 | + } |
| 310 | + |
| 311 | + if (!Options.RuntimeResourcePath.empty()) { |
| 312 | + llvm::SmallString<261> path{Options.RuntimeResourcePath}; |
| 313 | + llvm::sys::path::append(path, "windows", modulemap); |
| 314 | + if (llvm::sys::fs::exists(path)) |
| 315 | + return path.str().str(); |
| 316 | + } |
| 317 | + |
| 318 | + return ""; |
| 319 | +} |
| 320 | + |
| 321 | +SmallVector<std::pair<std::string, std::string>, 2> |
| 322 | +GetWindowsFileMappings(ASTContext &Context) { |
| 323 | + const llvm::Triple &Triple = Context.LangOpts.Target; |
| 324 | + SmallVector<std::pair<std::string, std::string>, 2> Mappings; |
| 325 | + std::string AuxiliaryFile; |
| 326 | + |
| 327 | + if (!Triple.isWindowsMSVCEnvironment()) |
| 328 | + return Mappings; |
| 329 | + |
| 330 | + clang::driver::Driver Driver = createClangDriver(Context); |
| 331 | + const llvm::opt::InputArgList Args = createClangArgs(Context, Driver); |
| 332 | + const clang::driver::ToolChain &ToolChain = Driver.getToolChain(Args, Triple); |
| 333 | + llvm::vfs::FileSystem &VFS = ToolChain.getVFS(); |
| 334 | + |
| 335 | + struct { |
| 336 | + std::string Path; |
| 337 | + std::string IncludeVersion; |
| 338 | + std::string LibraryVersion; |
| 339 | + int MajorVersion; |
| 340 | + } WindowsSDK; |
| 341 | + if (llvm::getWindowsSDKDir(VFS, {}, {}, {}, |
| 342 | + WindowsSDK.Path, WindowsSDK.MajorVersion, |
| 343 | + WindowsSDK.IncludeVersion, |
| 344 | + WindowsSDK.LibraryVersion)) { |
| 345 | + llvm::SmallString<261> WinSDKInjection{WindowsSDK.Path}; |
| 346 | + llvm::sys::path::append(WinSDKInjection, "Include"); |
| 347 | + if (WindowsSDK.MajorVersion > 8) |
| 348 | + llvm::sys::path::append(WinSDKInjection, WindowsSDK.IncludeVersion, "um"); |
| 349 | + llvm::sys::path::append(WinSDKInjection, "module.modulemap"); |
| 350 | + |
| 351 | + AuxiliaryFile = |
| 352 | + GetWindowsAuxiliaryFile("winsdk.modulemap", Context.SearchPathOpts); |
| 353 | + if (!AuxiliaryFile.empty()) |
| 354 | + Mappings.emplace_back(std::string(WinSDKInjection), AuxiliaryFile); |
| 355 | + } |
| 356 | + |
| 357 | + struct { |
| 358 | + std::string Path; |
| 359 | + std::string Version; |
| 360 | + } UCRTSDK; |
| 361 | + if (llvm::getUniversalCRTSdkDir(VFS, {}, {}, {}, UCRTSDK.Path, |
| 362 | + UCRTSDK.Version)) { |
| 363 | + llvm::SmallString<261> UCRTInjection{UCRTSDK.Path}; |
| 364 | + llvm::sys::path::append(UCRTInjection, "Include", UCRTSDK.Version, "ucrt"); |
| 365 | + llvm::sys::path::append(UCRTInjection, "module.modulemap"); |
| 366 | + |
| 367 | + AuxiliaryFile = |
| 368 | + GetWindowsAuxiliaryFile("ucrt.modulemap", Context.SearchPathOpts); |
| 369 | + if (!AuxiliaryFile.empty()) |
| 370 | + Mappings.emplace_back(std::string(UCRTInjection), AuxiliaryFile); |
| 371 | + } |
| 372 | + |
| 373 | + struct { |
| 374 | + std::string Path; |
| 375 | + llvm::ToolsetLayout Layout; |
| 376 | + } VCTools; |
| 377 | + if (llvm::findVCToolChainViaCommandLine(VFS, {}, {}, {}, VCTools.Path, VCTools.Layout) || |
| 378 | + llvm::findVCToolChainViaEnvironment(VFS, VCTools.Path, VCTools.Layout) || |
| 379 | + llvm::findVCToolChainViaSetupConfig(VFS, VCTools.Path, VCTools.Layout) || |
| 380 | + llvm::findVCToolChainViaRegistry(VCTools.Path, VCTools.Layout)) { |
| 381 | + llvm::SmallString<261> VCToolsInjection{VCTools.Path}; |
| 382 | + llvm::sys::path::append(VCToolsInjection, "include"); |
| 383 | + |
| 384 | + llvm::sys::path::append(VCToolsInjection, "module.modulemap"); |
| 385 | + AuxiliaryFile = |
| 386 | + GetWindowsAuxiliaryFile("vcruntime.modulemap", Context.SearchPathOpts); |
| 387 | + if (!AuxiliaryFile.empty()) |
| 388 | + Mappings.emplace_back(std::string(VCToolsInjection), AuxiliaryFile); |
| 389 | + |
| 390 | + llvm::sys::path::remove_filename(VCToolsInjection); |
| 391 | + llvm::sys::path::append(VCToolsInjection, "vcruntime.apinotes"); |
| 392 | + AuxiliaryFile = |
| 393 | + GetWindowsAuxiliaryFile("vcruntime.apinotes", Context.SearchPathOpts); |
| 394 | + if (!AuxiliaryFile.empty()) |
| 395 | + Mappings.emplace_back(std::string(VCToolsInjection), AuxiliaryFile); |
| 396 | + } |
| 397 | + |
| 398 | + return Mappings; |
| 399 | +} |
| 400 | +} |
| 401 | + |
299 | 402 | SmallVector<std::pair<std::string, std::string>, 2>
|
300 | 403 | swift::getClangInvocationFileMapping(ASTContext &ctx) {
|
301 | 404 | SmallVector<std::pair<std::string, std::string>, 2> result;
|
302 | 405 |
|
| 406 | + // Linux Mappings |
303 | 407 | result.append(getGlibcFileMapping(ctx));
|
304 |
| - |
305 |
| - if (ctx.LangOpts.EnableCXXInterop) { |
| 408 | + if (ctx.LangOpts.EnableCXXInterop) |
306 | 409 | result.append(getLibStdCxxFileMapping(ctx));
|
307 |
| - } |
| 410 | + |
| 411 | + // Windows Mappings |
| 412 | + result.append(GetWindowsFileMappings(ctx)); |
| 413 | + |
308 | 414 | return result;
|
309 | 415 | }
|
0 commit comments