Skip to content

Commit 8b8ec53

Browse files
committed
[sourcekit] Add global -module-cache-path to test executables
Add a global -module-cache-path option to `sourcekitd-test` and `complete-test` and have lit provide the default module cache in its substitutions. Previously many tests have explicitly provided the `%mcp_opt` option, but this is easy to forget when writing new tests. The module cache is inserted into the compiler arguments at the beginning so that it's still possible for a test to override it with a per-test cache if desired. rdar://58752842
1 parent 3322f6d commit 8b8ec53

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

test/SourceKit/lit.local.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ elif 'swift_evolve' in config.available_features:
1414
else:
1515
sk_path_sanitize = os.path.join(os.path.dirname(__file__), 'Inputs', 'sourcekitd_path_sanitize.py')
1616

17-
config.substitutions.append( ('%sourcekitd-test', config.sourcekitd_test) )
18-
config.substitutions.append( ('%complete-test', config.complete_test) )
17+
config.substitutions.append( ('%sourcekitd-test', '%s -module-cache-path %r' % (config.sourcekitd_test, config.clang_module_cache_path)) )
18+
config.substitutions.append( ('%complete-test', '%s -module-cache-path=%r' % (config.complete_test, config.clang_module_cache_path)) )
1919
config.substitutions.append( ('%swiftlib_dir', config.swiftlib_dir) )
2020
config.substitutions.append( ('%sed_clean', '%s %s' % (sys.executable, sk_path_sanitize) )
2121
)

test/lit.cfg

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,11 @@ config.swift_driver_test_options += ' -Xfrontend'
362362
config.swift_driver_test_options += ' -ignore-module-source-info'
363363
config.sil_test_options = os.environ.get('SIL_TEST_OPTIONS', '')
364364

365-
clang_module_cache_path = make_path(config.swift_test_results_dir, "clang-module-cache")
366-
shutil.rmtree(clang_module_cache_path, ignore_errors=True)
367-
mcp_opt = "-module-cache-path %r" % clang_module_cache_path
368-
clang_mcp_opt = "-fmodules-cache-path=%r" % clang_module_cache_path
369-
lit_config.note("Using Clang module cache: " + clang_module_cache_path)
365+
config.clang_module_cache_path = make_path(config.swift_test_results_dir, "clang-module-cache")
366+
shutil.rmtree(config.clang_module_cache_path, ignore_errors=True)
367+
mcp_opt = "-module-cache-path %r" % config.clang_module_cache_path
368+
clang_mcp_opt = "-fmodules-cache-path=%r" % config.clang_module_cache_path
369+
lit_config.note("Using Clang module cache: " + config.clang_module_cache_path)
370370
lit_config.note("Using test results dir: " + config.swift_test_results_dir)
371371

372372
completion_cache_path = make_path(config.swift_test_results_dir, "completion-cache")

tools/SourceKit/tools/complete-test/complete-test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ struct TestOptions {
6565
Optional<unsigned> fuzzyWeight;
6666
Optional<unsigned> popularityBonus;
6767
StringRef filterRulesJSON;
68+
std::string moduleCachePath;
6869
bool rawOutput = false;
6970
bool structureOutput = false;
7071
ArrayRef<const char *> compilerArgs;
@@ -251,6 +252,8 @@ static bool parseOptions(ArrayRef<const char *> args, TestOptions &options,
251252
return false;
252253
}
253254
options.showTopNonLiteral = uval;
255+
} else if (opt == "module-cache-path") {
256+
options.moduleCachePath = value;
254257
}
255258
}
256259

@@ -673,6 +676,10 @@ static bool codeCompleteRequest(sourcekitd_uid_t requestUID, const char *name,
673676
sourcekitd_request_array_set_string(args, SOURCEKITD_ARRAY_APPEND,"-sdk");
674677
sourcekitd_request_array_set_string(args, SOURCEKITD_ARRAY_APPEND, sdk);
675678
}
679+
if (!options.moduleCachePath.empty()) {
680+
sourcekitd_request_array_set_string(args, SOURCEKITD_ARRAY_APPEND, "-module-cache-path");
681+
sourcekitd_request_array_set_string(args, SOURCEKITD_ARRAY_APPEND, options.moduleCachePath.c_str());
682+
}
676683
// Add -- options.
677684
for (const char *arg : options.compilerArgs)
678685
sourcekitd_request_array_set_string(args, SOURCEKITD_ARRAY_APPEND, arg);

tools/SourceKit/tools/sourcekitd-test/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ def optimize_for_ide : Joined<["-"], "for-ide=">,
146146
def suppress_config_request : Flag<["-"], "suppress-config-request">,
147147
HelpText<"Suppress the default global configuration request, that is otherwise sent before any other request (except for the global-config request itself)">;
148148

149+
def module_cache_path: Separate<["-"], "module-cache-path">, HelpText<"module cache path">;
150+
def module_cache_path_EQ : Joined<["-"], "module-cache-path=">, Alias<module_cache_path>;
151+
149152
def help : Flag<["-", "--"], "help">,
150153
HelpText<"Display available options">;
151154

tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
385385
SuppressDefaultConfigRequest = true;
386386
break;
387387

388+
case OPT_module_cache_path:
389+
ModuleCachePath = InputArg->getValue();
390+
break;
391+
388392
case OPT_UNKNOWN:
389393
llvm::errs() << "error: unknown argument: "
390394
<< InputArg->getAsString(ParsedArgs) << '\n'

tools/SourceKit/tools/sourcekitd-test/TestOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct TestOptions {
9494
std::string CachePath;
9595
llvm::SmallVector<std::string, 4> RequestOptions;
9696
llvm::ArrayRef<const char *> CompilerArgs;
97+
std::string ModuleCachePath;
9798
bool UsingSwiftArgs;
9899
std::string USR;
99100
std::string SwiftName;

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
403403
if (Opts.parseArgs(Args.slice(0, Optargc)))
404404
return 1;
405405

406+
if (!Opts.ModuleCachePath.empty())
407+
InitOpts.ModuleCachePath = Opts.ModuleCachePath;
408+
406409
if (Optargc < Args.size())
407410
Opts.CompilerArgs = Args.slice(Optargc+1);
408411

@@ -515,6 +518,8 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
515518
ByteOffset;
516519
}
517520

521+
bool compilerArgsAreClang = false;
522+
518523
sourcekitd_object_t Req = sourcekitd_request_dictionary_create(nullptr,
519524
nullptr, 0);
520525
ActiveRequest = Opts.Request;
@@ -883,6 +888,8 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
883888
} else {
884889
if (Opts.UsingSwiftArgs)
885890
sourcekitd_request_dictionary_set_int64(Req, KeyUsingSwiftArgs, true);
891+
else
892+
compilerArgsAreClang = true;
886893
sourcekitd_request_dictionary_set_uid(Req, KeyRequest,
887894
RequestEditorOpenHeaderInterface);
888895
}
@@ -978,6 +985,15 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
978985

979986
if (!Opts.CompilerArgs.empty()) {
980987
sourcekitd_object_t Args = sourcekitd_request_array_create(nullptr, 0);
988+
if (!Opts.ModuleCachePath.empty()) {
989+
if (compilerArgsAreClang) {
990+
std::string opt = "-fmodules-cache-path=" + Opts.ModuleCachePath;
991+
sourcekitd_request_array_set_string(Args, SOURCEKITD_ARRAY_APPEND, opt.c_str());
992+
} else {
993+
sourcekitd_request_array_set_string(Args, SOURCEKITD_ARRAY_APPEND, "-module-cache-path");
994+
sourcekitd_request_array_set_string(Args, SOURCEKITD_ARRAY_APPEND, Opts.ModuleCachePath.c_str());
995+
}
996+
}
981997
for (auto Arg : Opts.CompilerArgs)
982998
sourcekitd_request_array_set_string(Args, SOURCEKITD_ARRAY_APPEND, Arg);
983999
sourcekitd_request_dictionary_set_value(Req, KeyCompilerArgs, Args);

0 commit comments

Comments
 (0)