Skip to content

Commit 997f0a0

Browse files
author
Harlan Haskins
authored
[Frontend] Default the prebuilt cache to <resourcedir>/<platform>/prebuilt-modules (#24320)
By default, prebuilt modules will live in <resource-dir>/<platform>/prebuilt-modules, so make this explicit if it wasn't passed in explicitly.
1 parent ecfcdbf commit 997f0a0

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

lib/Frontend/CompilerInvocation.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
4242
setRuntimeResourcePath(LibPath.str(), /*IsDefault=*/true);
4343
}
4444

45+
/// If we haven't explicitly passed -prebuilt-module-cache-path, set it to
46+
/// the default value of <resource-dir>/<platform>/prebuilt-modules.
47+
/// @note This should be called once, after search path options and frontend
48+
/// options have been parsed.
49+
static void setDefaultPrebuiltCacheIfNecessary(
50+
FrontendOptions &frontendOpts, const SearchPathOptions &searchPathOpts,
51+
const llvm::Triple &triple) {
52+
53+
if (!frontendOpts.PrebuiltModuleCachePath.empty())
54+
return;
55+
if (searchPathOpts.RuntimeResourcePath.empty())
56+
return;
57+
58+
SmallString<64> defaultPrebuiltPath{searchPathOpts.RuntimeResourcePath};
59+
StringRef platform = getPlatformNameForTriple(triple);
60+
llvm::sys::path::append(defaultPrebuiltPath, platform, "prebuilt-modules");
61+
frontendOpts.PrebuiltModuleCachePath = defaultPrebuiltPath.str();
62+
}
63+
4564
static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts,
4665
llvm::Triple &Triple) {
4766
llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath);
@@ -1300,6 +1319,8 @@ bool CompilerInvocation::parseArgs(
13001319
}
13011320

13021321
updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target);
1322+
setDefaultPrebuiltCacheIfNecessary(FrontendOpts, SearchPathOpts,
1323+
LangOpts.Target);
13031324

13041325
return false;
13051326
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 1. Create folders for a) our Swift module, b) the module cache, and c) a
2+
// fake resource dir with a default prebuilt module cache inside.
3+
// RUN: %empty-directory(%t/PrebuiltModule.swiftmodule)
4+
// RUN: %empty-directory(%t/ModuleCache)
5+
// RUN: %empty-directory(%t/ResourceDir/%target-sdk-name/prebuilt-modules/PrebuiltModule.swiftmodule)
6+
7+
// 2. Define some public API
8+
// RUN: echo 'public struct InPrebuiltModule {}' > %t/PrebuiltModule.swift
9+
10+
// 3. Compile this into a module and put it into the default prebuilt cache
11+
// location relative to the fake resource dir. Also drop an interface into
12+
// the build dir.
13+
// RUN: %target-swift-frontend -emit-module %t/PrebuiltModule.swift -o %t/ResourceDir/%target-sdk-name/prebuilt-modules/PrebuiltModule.swiftmodule/%target-cpu.swiftmodule -module-name PrebuiltModule -parse-stdlib -emit-module-interface-path %t/PrebuiltModule.swiftmodule/%target-cpu.swiftinterface
14+
15+
// 4. Import this prebuilt module, but DON'T pass in -prebuilt-module-cache-path, it should use the implicit one.
16+
// RUN: %target-swift-frontend -typecheck -resource-dir %t/ResourceDir -I %t %s -parse-stdlib -module-cache-path %t/ModuleCache -sdk %t
17+
18+
// 5. Make sure we installed a forwarding module in the module cache.
19+
// RUN: %{python} %S/ModuleCache/Inputs/check-is-forwarding-module.py %t/ModuleCache/PrebuiltModule-*.swiftmodule
20+
21+
import PrebuiltModule
22+
23+
func x<T>(_ x: T) {}
24+
25+
x(InPrebuiltModule.self)

test/lit.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,9 @@ else:
10081008
config.substitutions.append(('%module-target-triple',
10091009
target_specific_module_triple))
10101010

1011+
# Add 'target-sdk-name' as the name for platform-specific directories
1012+
config.substitutions.append(('%target-sdk-name', config.target_sdk_name))
1013+
10111014
# Different OS's require different prefixes for the environment variables to be
10121015
# propagated to the calling contexts.
10131016
# In order to make tests OS-agnostic, names of environment variables should be

0 commit comments

Comments
 (0)