@@ -1144,6 +1144,14 @@ static std::string GetPluginServer(llvm::StringRef plugin_library_path) {
1144
1144
return {};
1145
1145
}
1146
1146
1147
+ static std::string GetPluginServerForSDK (llvm::StringRef sdk_path) {
1148
+ XcodeSDK sdk (std::string (llvm::sys::path::filename (sdk_path)));
1149
+ auto server_or_err = HostInfo::FindSDKTool (sdk, " swift-plugin-server" );
1150
+ if (!server_or_err)
1151
+ return " " ;
1152
+ return server_or_err->str ();
1153
+ }
1154
+
1147
1155
// / Retrieve the serialized AST data blobs and initialize the compiler
1148
1156
// / invocation with the concatenated search paths from the blobs.
1149
1157
// / \returns true if an error was encountered.
@@ -1165,6 +1173,37 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1165
1173
return false ;
1166
1174
1167
1175
auto &search_path_options = invocation.getSearchPathOptions ();
1176
+ auto get_override_server = [&](llvm::StringRef plugin_path) -> std::string {
1177
+ // If the user manually specified an override plugin server for a
1178
+ // specific path prefix, return it.
1179
+ Args plugin_servers =
1180
+ Target::GetGlobalProperties ().GetSwiftPluginServerForPath ();
1181
+ for (auto &arg: plugin_servers) {
1182
+ auto key_value = arg.ref ().split (' =' );
1183
+ llvm::SmallString<0 > ignore (plugin_path);
1184
+ if (llvm::sys::path::replace_path_prefix (ignore, key_value.first , {}))
1185
+ return key_value.second .str ();
1186
+ }
1187
+ return {};
1188
+ };
1189
+ auto get_plugin_server = [&](llvm::StringRef plugin,
1190
+ std::function<std::string (void )> fallback) {
1191
+ // Search for a manual override first, then try fallback.
1192
+ std::string server = get_override_server (plugin);
1193
+ if (server.empty ())
1194
+ server = fallback ();
1195
+ if (server.empty ()) {
1196
+ HEALTH_LOG_PRINTF (" Could not find swift-plugin-server for %s" ,
1197
+ plugin.str ().c_str ());
1198
+ return std::string ();
1199
+ }
1200
+ if (!FileSystem::Instance ().Exists (server)) {
1201
+ HEALTH_LOG_PRINTF (" Swift plugin server does not exist: %s" ,
1202
+ server.c_str ());
1203
+ server.clear ();
1204
+ }
1205
+ return server;
1206
+ };
1168
1207
1169
1208
#define INIT_SEARCH_PATH_SET (TYPE, ACCESSOR, NAME, KEY ) \
1170
1209
std::vector<TYPE> NAME; \
@@ -1265,54 +1304,73 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1265
1304
// Rewrite them to go through an ABI-compatible swift-plugin-server.
1266
1305
if (known_plugin_search_paths.insert (path).second ) {
1267
1306
if (known_external_plugin_search_paths.insert (path).second ) {
1268
- std::string server = GetPluginServer (path);
1269
- if (server.empty ()) {
1270
- HEALTH_LOG_PRINTF (" Could not find swift-plugin-server for %s" ,
1271
- path.str ().c_str ());
1307
+ std::string server = get_plugin_server (
1308
+ path, [&]() { return GetPluginServer (path); });
1309
+ if (server.empty ())
1272
1310
continue ;
1273
- }
1274
1311
if (exists (path))
1275
1312
external_plugin_search_paths.push_back ({path.str (), server});
1276
1313
}
1277
1314
}
1278
- for (auto path :
1279
- extended_validation_info.getExternalPluginSearchPaths ()) {
1280
- // Sandboxed system plugins shipping with some compiler.
1281
- // Keep the original plugin server path, it needs to be ABI
1282
- // compatible with the version of SwiftSyntax used by the plugin.
1283
- auto plugin_server = path.split (' #' );
1284
- llvm::StringRef plugin = plugin_server.first ;
1285
- llvm::StringRef server = plugin_server.second ;
1286
- if (known_external_plugin_search_paths.insert (plugin).second )
1287
- if (exists (plugin) && exists (server))
1288
- external_plugin_search_paths.push_back (
1289
- {plugin.str (), server.str ()});
1290
- }
1315
+ }
1316
+ for (auto path :
1317
+ extended_validation_info.getExternalPluginSearchPaths ()) {
1318
+ // Sandboxed system plugins shipping with some compiler.
1319
+ // Keep the original plugin server path, it needs to be ABI
1320
+ // compatible with the version of SwiftSyntax used by the plugin.
1321
+ auto plugin_server = path.split (' #' );
1322
+ llvm::StringRef plugin = plugin_server.first ;
1323
+ std::string server = get_plugin_server (
1324
+ plugin, [&]() { return plugin_server.second .str (); });
1325
+ if (server.empty ())
1326
+ continue ;
1327
+ if (known_external_plugin_search_paths.insert (plugin).second )
1328
+ if (exists (plugin))
1329
+ external_plugin_search_paths.push_back ({plugin.str (), server});
1330
+ }
1291
1331
1292
- for (auto path :
1293
- extended_validation_info.getCompilerPluginLibraryPaths ()) {
1294
- // Compiler plugin libraries.
1295
- if (known_compiler_plugin_library_paths.insert (path).second )
1296
- if (exists (path))
1297
- compiler_plugin_library_paths.push_back (path.str ());
1298
- }
1332
+ for (auto dylib :
1333
+ extended_validation_info.getCompilerPluginLibraryPaths ()) {
1334
+ // Compiler plugin libraries.
1335
+ if (known_compiler_plugin_library_paths.insert (dylib).second )
1336
+ if (exists (dylib)) {
1337
+ // We never want to directly load any plugins, since a crash in
1338
+ // the plugin would bring down LLDB. Here, we assume that the
1339
+ // correct plugin server for a direct compiler plugin is the one
1340
+ // from the SDK the compiler was building for. This is just a
1341
+ // heuristic.
1342
+ llvm::SmallString<0 > dir (dylib);
1343
+ llvm::sys::path::remove_filename (dir);
1344
+ std::string server = get_plugin_server (dir, [&]() {
1345
+ return GetPluginServerForSDK (invocation.getSDKPath ());
1346
+ });
1347
+ if (server.empty ())
1348
+ continue ;
1299
1349
1300
- for (auto path :
1301
- extended_validation_info.getCompilerPluginExecutablePaths ()) {
1302
- // Compiler plugin executables.
1303
- auto plugin_modules = path.split (' #' );
1304
- llvm::StringRef plugin = plugin_modules.first ;
1305
- llvm::StringRef modules_list = plugin_modules.second ;
1306
- llvm::SmallVector<llvm::StringRef, 0 > modules;
1307
- modules_list.split (modules, " ," );
1308
- std::vector<std::string> modules_vec;
1309
- for (auto m : modules)
1310
- modules_vec.push_back (m.str ());
1311
- if (known_compiler_plugin_executable_paths.insert (path).second )
1312
- if (exists (plugin))
1313
- compiler_plugin_executable_paths.push_back (
1314
- {plugin.str (), modules_vec});
1315
- }
1350
+ // FIXME: The Swift compiler expects external plugins
1351
+ // to be named libModuleName.[dylib|so|dll]. This
1352
+ // means this our translation attempts only work for
1353
+ // macro libraries following this convention. cf.
1354
+ // PluginLoader::lookupExternalLibraryPluginByModuleName().
1355
+ external_plugin_search_paths.push_back ({dir.str ().str (), server});
1356
+ }
1357
+ }
1358
+
1359
+ for (auto path :
1360
+ extended_validation_info.getCompilerPluginExecutablePaths ()) {
1361
+ // Compiler plugin executables.
1362
+ auto plugin_modules = path.split (' #' );
1363
+ llvm::StringRef plugin = plugin_modules.first ;
1364
+ llvm::StringRef modules_list = plugin_modules.second ;
1365
+ llvm::SmallVector<llvm::StringRef, 0 > modules;
1366
+ modules_list.split (modules, " ," );
1367
+ std::vector<std::string> modules_vec;
1368
+ for (auto m : modules)
1369
+ modules_vec.push_back (m.str ());
1370
+ if (known_compiler_plugin_executable_paths.insert (path).second )
1371
+ if (exists (plugin))
1372
+ compiler_plugin_executable_paths.push_back (
1373
+ {plugin.str (), modules_vec});
1316
1374
}
1317
1375
return true ;
1318
1376
};
@@ -1945,6 +2003,10 @@ static void
1945
2003
ProcessModule (ModuleSP module_sp, std::string m_description,
1946
2004
bool discover_implicit_search_paths, bool use_all_compiler_flags,
1947
2005
Target &target, llvm::Triple triple,
2006
+ std::vector<swift::ExternalPluginSearchPathAndServerPath>
2007
+ &external_plugin_search_paths,
2008
+ std::vector<swift::PluginExecutablePathAndModuleNames>
2009
+ &compiler_plugin_executable_paths,
1948
2010
std::vector<std::string> &module_search_paths,
1949
2011
std::vector<std::pair<std::string, bool >> &framework_search_paths,
1950
2012
std::vector<std::string> &extra_clang_args) {
@@ -2063,7 +2125,15 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
2063
2125
// collected here and surfaced.
2064
2126
}
2065
2127
2128
+ // Copy the interesting deserialized flags to the out parameters.
2066
2129
const auto &opts = invocation.getSearchPathOptions ();
2130
+ external_plugin_search_paths.insert (external_plugin_search_paths.end (),
2131
+ opts.ExternalPluginSearchPaths .begin (),
2132
+ opts.ExternalPluginSearchPaths .end ());
2133
+ compiler_plugin_executable_paths.insert (
2134
+ compiler_plugin_executable_paths.end (),
2135
+ opts.getCompilerPluginExecutablePaths ().begin (),
2136
+ opts.getCompilerPluginExecutablePaths ().end ());
2067
2137
module_search_paths.insert (module_search_paths.end (),
2068
2138
opts.getImportSearchPaths ().begin (),
2069
2139
opts.getImportSearchPaths ().end ());
@@ -2086,6 +2156,10 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
2086
2156
2087
2157
LLDB_SCOPED_TIMER ();
2088
2158
std::string m_description = " SwiftASTContextForExpressions" ;
2159
+ std::vector<swift::ExternalPluginSearchPathAndServerPath>
2160
+ external_plugin_search_paths;
2161
+ std::vector<swift::PluginExecutablePathAndModuleNames>
2162
+ compiler_plugin_executable_paths;
2089
2163
std::vector<std::string> module_search_paths;
2090
2164
std::vector<std::pair<std::string, bool >> framework_search_paths;
2091
2165
TargetSP target_sp = typeref_typesystem.GetTargetWP ().lock ();
@@ -2259,8 +2333,9 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
2259
2333
std::vector<std::string> extra_clang_args;
2260
2334
ProcessModule (target.GetImages ().GetModuleAtIndex (mi), m_description,
2261
2335
discover_implicit_search_paths, use_all_compiler_flags,
2262
- target, triple, module_search_paths, framework_search_paths,
2263
- extra_clang_args);
2336
+ target, triple, external_plugin_search_paths,
2337
+ compiler_plugin_executable_paths, module_search_paths,
2338
+ framework_search_paths, extra_clang_args);
2264
2339
swift_ast_sp->AddExtraClangArgs (extra_clang_args);
2265
2340
}
2266
2341
@@ -2305,6 +2380,15 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
2305
2380
return {};
2306
2381
}
2307
2382
2383
+ // Initialize the compiler plugin search paths.
2384
+ auto &opts = swift_ast_sp->GetSearchPathOptions ();
2385
+ opts.ExternalPluginSearchPaths .insert (opts.ExternalPluginSearchPaths .end (),
2386
+ external_plugin_search_paths.begin (),
2387
+ external_plugin_search_paths.end ());
2388
+ assert (opts.getCompilerPluginExecutablePaths ().empty ());
2389
+ opts.setCompilerPluginExecutablePaths (
2390
+ std::move (compiler_plugin_executable_paths));
2391
+
2308
2392
for (size_t mi = 0 ; mi != num_images; ++mi) {
2309
2393
std::vector<std::string> module_names;
2310
2394
auto module_sp = target.GetImages ().GetModuleAtIndex (mi);
@@ -4539,16 +4623,23 @@ void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) {
4539
4623
bool use_all_compiler_flags = target_sp->GetUseAllCompilerFlags ();
4540
4624
unsigned num_images = module_list.GetSize ();
4541
4625
for (size_t mi = 0 ; mi != num_images; ++mi) {
4626
+ std::vector<swift::ExternalPluginSearchPathAndServerPath>
4627
+ external_plugin_search_paths;
4628
+ std::vector<swift::PluginExecutablePathAndModuleNames>
4629
+ compiler_plugin_executable_paths;
4542
4630
std::vector<std::string> module_search_paths;
4543
4631
std::vector<std::pair<std::string, bool >> framework_search_paths;
4544
4632
std::vector<std::string> extra_clang_args;
4545
4633
lldb::ModuleSP module_sp = module_list.GetModuleAtIndex (mi);
4546
4634
ProcessModule (module_sp, m_description, discover_implicit_search_paths,
4547
4635
use_all_compiler_flags, *target_sp, GetTriple (),
4548
- module_search_paths, framework_search_paths,
4549
- extra_clang_args);
4550
- // If the use-all-compiler-flags setting is enabled, the expression
4551
- // context is supposed to merge all search paths from all dylibs.
4636
+ external_plugin_search_paths,
4637
+ compiler_plugin_executable_paths, module_search_paths,
4638
+ framework_search_paths, extra_clang_args);
4639
+ // If the use-all-compiler-flags setting is enabled, the
4640
+ // expression context is supposed to merge all search paths
4641
+ // from all dylibs.
4642
+ // TODO: Maybe we should also do this for compiler plugins?
4552
4643
if (use_all_compiler_flags && !extra_clang_args.empty ()) {
4553
4644
// We cannot reconfigure ClangImporter after its creation.
4554
4645
// Instead poison the SwiftASTContext so it gets recreated.
@@ -4579,16 +4670,20 @@ void SwiftASTContext::LogConfiguration() {
4579
4670
HEALTH_LOG_PRINTF (" (no AST context)" );
4580
4671
return ;
4581
4672
}
4673
+ HEALTH_LOG_PRINTF (" Swift/C++ interop : %s" ,
4674
+ m_ast_context_ap->LangOpts .EnableCXXInterop ? " on" : " off" );
4675
+ HEALTH_LOG_PRINTF (" Swift/Objective-C interop : %s" ,
4676
+ m_ast_context_ap->LangOpts .EnableObjCInterop ? " on" : " off" );
4582
4677
4583
- HEALTH_LOG_PRINTF (" Architecture : %s" ,
4678
+ HEALTH_LOG_PRINTF (" Architecture : %s" ,
4584
4679
m_ast_context_ap->LangOpts .Target .getTriple ().c_str ());
4585
4680
HEALTH_LOG_PRINTF (
4586
- " SDK path : %s" ,
4681
+ " SDK path : %s" ,
4587
4682
m_ast_context_ap->SearchPathOpts .getSDKPath ().str ().c_str ());
4588
4683
HEALTH_LOG_PRINTF (
4589
- " Runtime resource path : %s" ,
4684
+ " Runtime resource path : %s" ,
4590
4685
m_ast_context_ap->SearchPathOpts .RuntimeResourcePath .c_str ());
4591
- HEALTH_LOG_PRINTF (" Runtime library paths : (%llu items)" ,
4686
+ HEALTH_LOG_PRINTF (" Runtime library paths : (%llu items)" ,
4592
4687
(unsigned long long )m_ast_context_ap->SearchPathOpts
4593
4688
.RuntimeLibraryPaths .size ());
4594
4689
@@ -4597,7 +4692,7 @@ void SwiftASTContext::LogConfiguration() {
4597
4692
HEALTH_LOG_PRINTF (" %s" , runtime_library_path.c_str ());
4598
4693
}
4599
4694
4600
- HEALTH_LOG_PRINTF (" Runtime library import paths : (%llu items)" ,
4695
+ HEALTH_LOG_PRINTF (" Runtime library import paths : (%llu items)" ,
4601
4696
(unsigned long long )m_ast_context_ap->SearchPathOpts
4602
4697
.getRuntimeLibraryImportPaths ()
4603
4698
.size ());
@@ -4607,7 +4702,7 @@ void SwiftASTContext::LogConfiguration() {
4607
4702
HEALTH_LOG_PRINTF (" %s" , runtime_import_path.c_str ());
4608
4703
}
4609
4704
4610
- HEALTH_LOG_PRINTF (" Framework search paths : (%llu items)" ,
4705
+ HEALTH_LOG_PRINTF (" Framework search paths : (%llu items)" ,
4611
4706
(unsigned long long )m_ast_context_ap->SearchPathOpts
4612
4707
.getFrameworkSearchPaths ()
4613
4708
.size ());
@@ -4616,7 +4711,7 @@ void SwiftASTContext::LogConfiguration() {
4616
4711
HEALTH_LOG_PRINTF (" %s" , framework_search_path.Path .c_str ());
4617
4712
}
4618
4713
4619
- HEALTH_LOG_PRINTF (" Import search paths : (%llu items)" ,
4714
+ HEALTH_LOG_PRINTF (" Import search paths : (%llu items)" ,
4620
4715
(unsigned long long )m_ast_context_ap->SearchPathOpts
4621
4716
.getImportSearchPaths ()
4622
4717
.size ());
@@ -4629,13 +4724,39 @@ void SwiftASTContext::LogConfiguration() {
4629
4724
GetClangImporterOptions ();
4630
4725
4631
4726
HEALTH_LOG_PRINTF (
4632
- " Extra clang arguments : (%llu items)" ,
4727
+ " Extra clang arguments : (%llu items)" ,
4633
4728
(unsigned long long )clang_importer_options.ExtraArgs .size ());
4634
4729
for (std::string &extra_arg : clang_importer_options.ExtraArgs ) {
4635
4730
HEALTH_LOG_PRINTF (" %s" , extra_arg.c_str ());
4636
4731
}
4637
- HEALTH_LOG_PRINTF (" Swift/C++ interop mode: %s" ,
4638
- m_ast_context_ap->LangOpts .EnableCXXInterop ? " on" : " off" );
4732
+
4733
+ #define PRINT_PLUGIN_PATHS (ACCESSOR, NAME, TEMPLATE, ...) \
4734
+ { \
4735
+ auto paths = m_ast_context_ap->SearchPathOpts .ACCESSOR ; \
4736
+ HEALTH_LOG_PRINTF (" %s: (%llu items)" , NAME, \
4737
+ (unsigned long long )paths.size ()); \
4738
+ for (auto &path : paths) { \
4739
+ HEALTH_LOG_PRINTF (" " TEMPLATE, ##__VA_ARGS__); \
4740
+ } \
4741
+ }
4742
+ PRINT_PLUGIN_PATHS (getCompilerPluginLibraryPaths (),
4743
+ " Compiler Plugin Library Paths " ,
4744
+ " %s" , path.c_str ());
4745
+ PRINT_PLUGIN_PATHS (getCompilerPluginExecutablePaths (),
4746
+ " Compiler Plugin Executable Paths " , " %s: [%s]" ,
4747
+ path.ExecutablePath .c_str (),
4748
+ [](auto path_names) -> std::string {
4749
+ std::string s;
4750
+ llvm::raw_string_ostream os (s);
4751
+ llvm::interleaveComma (path_names, os);
4752
+ return os.str ();
4753
+ }(path.ModuleNames )
4754
+ .c_str ());
4755
+ PRINT_PLUGIN_PATHS (PluginSearchPaths, " Plugin search paths " ,
4756
+ " %s" , path.c_str ());
4757
+ PRINT_PLUGIN_PATHS (ExternalPluginSearchPaths,
4758
+ " External plugin search paths " , " %s (server: %s)" ,
4759
+ path.SearchPath .c_str (), path.ServerPath .c_str ());
4639
4760
}
4640
4761
4641
4762
bool SwiftASTContext::HasTarget () {
0 commit comments