@@ -1083,6 +1083,14 @@ static std::string GetPluginServer(llvm::StringRef plugin_library_path) {
1083
1083
return {};
1084
1084
}
1085
1085
1086
+ static std::string GetPluginServerForSDK (llvm::StringRef sdk_path) {
1087
+ XcodeSDK sdk (std::string (llvm::sys::path::filename (sdk_path)));
1088
+ auto server_or_err = HostInfo::FindSDKTool (sdk, " swift-plugin-server" );
1089
+ if (!server_or_err)
1090
+ return " " ;
1091
+ return server_or_err->str ();
1092
+ }
1093
+
1086
1094
// / Retrieve the serialized AST data blobs and initialize the compiler
1087
1095
// / invocation with the concatenated search paths from the blobs.
1088
1096
// / \returns true if an error was encountered.
@@ -1104,6 +1112,37 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1104
1112
return false ;
1105
1113
1106
1114
auto &search_path_options = invocation.getSearchPathOptions ();
1115
+ auto get_override_server = [&](llvm::StringRef plugin_path) -> std::string {
1116
+ // If the user manually specified an override plugin server for a
1117
+ // specific path prefix, return it.
1118
+ Args plugin_servers =
1119
+ Target::GetGlobalProperties ().GetSwiftPluginServerForPath ();
1120
+ for (auto &arg: plugin_servers) {
1121
+ auto key_value = arg.ref ().split (' =' );
1122
+ llvm::SmallString<0 > ignore (plugin_path);
1123
+ if (llvm::sys::path::replace_path_prefix (ignore, key_value.first , {}))
1124
+ return key_value.second .str ();
1125
+ }
1126
+ return {};
1127
+ };
1128
+ auto get_plugin_server = [&](llvm::StringRef plugin,
1129
+ std::function<std::string (void )> fallback) {
1130
+ // Search for a manual override first, then try fallback.
1131
+ std::string server = get_override_server (plugin);
1132
+ if (server.empty ())
1133
+ server = fallback ();
1134
+ if (server.empty ()) {
1135
+ HEALTH_LOG_PRINTF (" Could not find swift-plugin-server for %s" ,
1136
+ plugin.str ().c_str ());
1137
+ return std::string ();
1138
+ }
1139
+ if (!FileSystem::Instance ().Exists (server)) {
1140
+ HEALTH_LOG_PRINTF (" Swift plugin server does not exist: %s" ,
1141
+ server.c_str ());
1142
+ server.clear ();
1143
+ }
1144
+ return server;
1145
+ };
1107
1146
1108
1147
#define INIT_SEARCH_PATH_SET (TYPE, ACCESSOR, NAME, KEY ) \
1109
1148
std::vector<TYPE> NAME; \
@@ -1204,54 +1243,73 @@ static bool DeserializeAllCompilerFlags(swift::CompilerInvocation &invocation,
1204
1243
// Rewrite them to go through an ABI-compatible swift-plugin-server.
1205
1244
if (known_plugin_search_paths.insert (path).second ) {
1206
1245
if (known_external_plugin_search_paths.insert (path).second ) {
1207
- std::string server = GetPluginServer (path);
1208
- if (server.empty ()) {
1209
- HEALTH_LOG_PRINTF (" Could not find swift-plugin-server for %s" ,
1210
- path.str ().c_str ());
1246
+ std::string server = get_plugin_server (
1247
+ path, [&]() { return GetPluginServer (path); });
1248
+ if (server.empty ())
1211
1249
continue ;
1212
- }
1213
1250
if (exists (path))
1214
1251
external_plugin_search_paths.push_back ({path.str (), server});
1215
1252
}
1216
1253
}
1217
- for (auto path :
1218
- extended_validation_info.getExternalPluginSearchPaths ()) {
1219
- // Sandboxed system plugins shipping with some compiler.
1220
- // Keep the original plugin server path, it needs to be ABI
1221
- // compatible with the version of SwiftSyntax used by the plugin.
1222
- auto plugin_server = path.split (' #' );
1223
- llvm::StringRef plugin = plugin_server.first ;
1224
- llvm::StringRef server = plugin_server.second ;
1225
- if (known_external_plugin_search_paths.insert (plugin).second )
1226
- if (exists (plugin) && exists (server))
1227
- external_plugin_search_paths.push_back (
1228
- {plugin.str (), server.str ()});
1229
- }
1254
+ }
1255
+ for (auto path :
1256
+ extended_validation_info.getExternalPluginSearchPaths ()) {
1257
+ // Sandboxed system plugins shipping with some compiler.
1258
+ // Keep the original plugin server path, it needs to be ABI
1259
+ // compatible with the version of SwiftSyntax used by the plugin.
1260
+ auto plugin_server = path.split (' #' );
1261
+ llvm::StringRef plugin = plugin_server.first ;
1262
+ std::string server = get_plugin_server (
1263
+ plugin, [&]() { return plugin_server.second .str (); });
1264
+ if (server.empty ())
1265
+ continue ;
1266
+ if (known_external_plugin_search_paths.insert (plugin).second )
1267
+ if (exists (plugin))
1268
+ external_plugin_search_paths.push_back ({plugin.str (), server});
1269
+ }
1230
1270
1231
- for (auto path :
1232
- extended_validation_info.getCompilerPluginLibraryPaths ()) {
1233
- // Compiler plugin libraries.
1234
- if (known_compiler_plugin_library_paths.insert (path).second )
1235
- if (exists (path))
1236
- compiler_plugin_library_paths.push_back (path.str ());
1237
- }
1271
+ for (auto dylib :
1272
+ extended_validation_info.getCompilerPluginLibraryPaths ()) {
1273
+ // Compiler plugin libraries.
1274
+ if (known_compiler_plugin_library_paths.insert (dylib).second )
1275
+ if (exists (dylib)) {
1276
+ // We never want to directly load any plugins, since a crash in
1277
+ // the plugin would bring down LLDB. Here, we assume that the
1278
+ // correct plugin server for a direct compiler plugin is the one
1279
+ // from the SDK the compiler was building for. This is just a
1280
+ // heuristic.
1281
+ llvm::SmallString<0 > dir (dylib);
1282
+ llvm::sys::path::remove_filename (dir);
1283
+ std::string server = get_plugin_server (dir, [&]() {
1284
+ return GetPluginServerForSDK (invocation.getSDKPath ());
1285
+ });
1286
+ if (server.empty ())
1287
+ continue ;
1238
1288
1239
- for (auto path :
1240
- extended_validation_info.getCompilerPluginExecutablePaths ()) {
1241
- // Compiler plugin executables.
1242
- auto plugin_modules = path.split (' #' );
1243
- llvm::StringRef plugin = plugin_modules.first ;
1244
- llvm::StringRef modules_list = plugin_modules.second ;
1245
- llvm::SmallVector<llvm::StringRef, 0 > modules;
1246
- modules_list.split (modules, " ," );
1247
- std::vector<std::string> modules_vec;
1248
- for (auto m : modules)
1249
- modules_vec.push_back (m.str ());
1250
- if (known_compiler_plugin_executable_paths.insert (path).second )
1251
- if (exists (plugin))
1252
- compiler_plugin_executable_paths.push_back (
1253
- {plugin.str (), modules_vec});
1254
- }
1289
+ // FIXME: The Swift compiler expects external plugins
1290
+ // to be named libModuleName.[dylib|so|dll]. This
1291
+ // means this our translation attempts only work for
1292
+ // macro libraries following this convention. cf.
1293
+ // PluginLoader::lookupExternalLibraryPluginByModuleName().
1294
+ external_plugin_search_paths.push_back ({dir.str ().str (), server});
1295
+ }
1296
+ }
1297
+
1298
+ for (auto path :
1299
+ extended_validation_info.getCompilerPluginExecutablePaths ()) {
1300
+ // Compiler plugin executables.
1301
+ auto plugin_modules = path.split (' #' );
1302
+ llvm::StringRef plugin = plugin_modules.first ;
1303
+ llvm::StringRef modules_list = plugin_modules.second ;
1304
+ llvm::SmallVector<llvm::StringRef, 0 > modules;
1305
+ modules_list.split (modules, " ," );
1306
+ std::vector<std::string> modules_vec;
1307
+ for (auto m : modules)
1308
+ modules_vec.push_back (m.str ());
1309
+ if (known_compiler_plugin_executable_paths.insert (path).second )
1310
+ if (exists (plugin))
1311
+ compiler_plugin_executable_paths.push_back (
1312
+ {plugin.str (), modules_vec});
1255
1313
}
1256
1314
return true ;
1257
1315
};
@@ -1884,6 +1942,10 @@ static void
1884
1942
ProcessModule (ModuleSP module_sp, std::string m_description,
1885
1943
bool discover_implicit_search_paths, bool use_all_compiler_flags,
1886
1944
Target &target, llvm::Triple triple,
1945
+ std::vector<swift::ExternalPluginSearchPathAndServerPath>
1946
+ &external_plugin_search_paths,
1947
+ std::vector<swift::PluginExecutablePathAndModuleNames>
1948
+ &compiler_plugin_executable_paths,
1887
1949
std::vector<std::string> &module_search_paths,
1888
1950
std::vector<std::pair<std::string, bool >> &framework_search_paths,
1889
1951
std::vector<std::string> &extra_clang_args) {
@@ -2002,7 +2064,15 @@ ProcessModule(ModuleSP module_sp, std::string m_description,
2002
2064
// collected here and surfaced.
2003
2065
}
2004
2066
2067
+ // Copy the interesting deserialized flags to the out parameters.
2005
2068
const auto &opts = invocation.getSearchPathOptions ();
2069
+ external_plugin_search_paths.insert (external_plugin_search_paths.end (),
2070
+ opts.ExternalPluginSearchPaths .begin (),
2071
+ opts.ExternalPluginSearchPaths .end ());
2072
+ compiler_plugin_executable_paths.insert (
2073
+ compiler_plugin_executable_paths.end (),
2074
+ opts.getCompilerPluginExecutablePaths ().begin (),
2075
+ opts.getCompilerPluginExecutablePaths ().end ());
2006
2076
module_search_paths.insert (module_search_paths.end (),
2007
2077
opts.getImportSearchPaths ().begin (),
2008
2078
opts.getImportSearchPaths ().end ());
@@ -2025,6 +2095,10 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
2025
2095
2026
2096
LLDB_SCOPED_TIMER ();
2027
2097
std::string m_description = " SwiftASTContextForExpressions" ;
2098
+ std::vector<swift::ExternalPluginSearchPathAndServerPath>
2099
+ external_plugin_search_paths;
2100
+ std::vector<swift::PluginExecutablePathAndModuleNames>
2101
+ compiler_plugin_executable_paths;
2028
2102
std::vector<std::string> module_search_paths;
2029
2103
std::vector<std::pair<std::string, bool >> framework_search_paths;
2030
2104
TargetSP target_sp = typeref_typesystem.GetTargetWP ().lock ();
@@ -2198,8 +2272,9 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
2198
2272
std::vector<std::string> extra_clang_args;
2199
2273
ProcessModule (target.GetImages ().GetModuleAtIndex (mi), m_description,
2200
2274
discover_implicit_search_paths, use_all_compiler_flags,
2201
- target, triple, module_search_paths, framework_search_paths,
2202
- extra_clang_args);
2275
+ target, triple, external_plugin_search_paths,
2276
+ compiler_plugin_executable_paths, module_search_paths,
2277
+ framework_search_paths, extra_clang_args);
2203
2278
swift_ast_sp->AddExtraClangArgs (extra_clang_args);
2204
2279
}
2205
2280
@@ -2244,6 +2319,15 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(
2244
2319
return {};
2245
2320
}
2246
2321
2322
+ // Initialize the compiler plugin search paths.
2323
+ auto &opts = swift_ast_sp->GetSearchPathOptions ();
2324
+ opts.ExternalPluginSearchPaths .insert (opts.ExternalPluginSearchPaths .end (),
2325
+ external_plugin_search_paths.begin (),
2326
+ external_plugin_search_paths.end ());
2327
+ assert (opts.getCompilerPluginExecutablePaths ().empty ());
2328
+ opts.setCompilerPluginExecutablePaths (
2329
+ std::move (compiler_plugin_executable_paths));
2330
+
2247
2331
for (size_t mi = 0 ; mi != num_images; ++mi) {
2248
2332
std::vector<std::string> module_names;
2249
2333
auto module_sp = target.GetImages ().GetModuleAtIndex (mi);
@@ -4854,16 +4938,23 @@ void SwiftASTContextForExpressions::ModulesDidLoad(ModuleList &module_list) {
4854
4938
bool use_all_compiler_flags = target_sp->GetUseAllCompilerFlags ();
4855
4939
unsigned num_images = module_list.GetSize ();
4856
4940
for (size_t mi = 0 ; mi != num_images; ++mi) {
4941
+ std::vector<swift::ExternalPluginSearchPathAndServerPath>
4942
+ external_plugin_search_paths;
4943
+ std::vector<swift::PluginExecutablePathAndModuleNames>
4944
+ compiler_plugin_executable_paths;
4857
4945
std::vector<std::string> module_search_paths;
4858
4946
std::vector<std::pair<std::string, bool >> framework_search_paths;
4859
4947
std::vector<std::string> extra_clang_args;
4860
4948
lldb::ModuleSP module_sp = module_list.GetModuleAtIndex (mi);
4861
4949
ProcessModule (module_sp, m_description, discover_implicit_search_paths,
4862
4950
use_all_compiler_flags, *target_sp, GetTriple (),
4863
- module_search_paths, framework_search_paths,
4864
- extra_clang_args);
4865
- // If the use-all-compiler-flags setting is enabled, the expression
4866
- // context is supposed to merge all search paths from all dylibs.
4951
+ external_plugin_search_paths,
4952
+ compiler_plugin_executable_paths, module_search_paths,
4953
+ framework_search_paths, extra_clang_args);
4954
+ // If the use-all-compiler-flags setting is enabled, the
4955
+ // expression context is supposed to merge all search paths
4956
+ // from all dylibs.
4957
+ // TODO: Maybe we should also do this for compiler plugins?
4867
4958
if (use_all_compiler_flags && !extra_clang_args.empty ()) {
4868
4959
// We cannot reconfigure ClangImporter after its creation.
4869
4960
// Instead poison the SwiftASTContext so it gets recreated.
@@ -4894,16 +4985,20 @@ void SwiftASTContext::LogConfiguration() {
4894
4985
HEALTH_LOG_PRINTF (" (no AST context)" );
4895
4986
return ;
4896
4987
}
4988
+ HEALTH_LOG_PRINTF (" Swift/C++ interop : %s" ,
4989
+ m_ast_context_ap->LangOpts .EnableCXXInterop ? " on" : " off" );
4990
+ HEALTH_LOG_PRINTF (" Swift/Objective-C interop : %s" ,
4991
+ m_ast_context_ap->LangOpts .EnableObjCInterop ? " on" : " off" );
4897
4992
4898
- HEALTH_LOG_PRINTF (" Architecture : %s" ,
4993
+ HEALTH_LOG_PRINTF (" Architecture : %s" ,
4899
4994
m_ast_context_ap->LangOpts .Target .getTriple ().c_str ());
4900
4995
HEALTH_LOG_PRINTF (
4901
- " SDK path : %s" ,
4996
+ " SDK path : %s" ,
4902
4997
m_ast_context_ap->SearchPathOpts .getSDKPath ().str ().c_str ());
4903
4998
HEALTH_LOG_PRINTF (
4904
- " Runtime resource path : %s" ,
4999
+ " Runtime resource path : %s" ,
4905
5000
m_ast_context_ap->SearchPathOpts .RuntimeResourcePath .c_str ());
4906
- HEALTH_LOG_PRINTF (" Runtime library paths : (%llu items)" ,
5001
+ HEALTH_LOG_PRINTF (" Runtime library paths : (%llu items)" ,
4907
5002
(unsigned long long )m_ast_context_ap->SearchPathOpts
4908
5003
.RuntimeLibraryPaths .size ());
4909
5004
@@ -4912,7 +5007,7 @@ void SwiftASTContext::LogConfiguration() {
4912
5007
HEALTH_LOG_PRINTF (" %s" , runtime_library_path.c_str ());
4913
5008
}
4914
5009
4915
- HEALTH_LOG_PRINTF (" Runtime library import paths : (%llu items)" ,
5010
+ HEALTH_LOG_PRINTF (" Runtime library import paths : (%llu items)" ,
4916
5011
(unsigned long long )m_ast_context_ap->SearchPathOpts
4917
5012
.getRuntimeLibraryImportPaths ()
4918
5013
.size ());
@@ -4922,7 +5017,7 @@ void SwiftASTContext::LogConfiguration() {
4922
5017
HEALTH_LOG_PRINTF (" %s" , runtime_import_path.c_str ());
4923
5018
}
4924
5019
4925
- HEALTH_LOG_PRINTF (" Framework search paths : (%llu items)" ,
5020
+ HEALTH_LOG_PRINTF (" Framework search paths : (%llu items)" ,
4926
5021
(unsigned long long )m_ast_context_ap->SearchPathOpts
4927
5022
.getFrameworkSearchPaths ()
4928
5023
.size ());
@@ -4931,7 +5026,7 @@ void SwiftASTContext::LogConfiguration() {
4931
5026
HEALTH_LOG_PRINTF (" %s" , framework_search_path.Path .c_str ());
4932
5027
}
4933
5028
4934
- HEALTH_LOG_PRINTF (" Import search paths : (%llu items)" ,
5029
+ HEALTH_LOG_PRINTF (" Import search paths : (%llu items)" ,
4935
5030
(unsigned long long )m_ast_context_ap->SearchPathOpts
4936
5031
.getImportSearchPaths ()
4937
5032
.size ());
@@ -4944,13 +5039,39 @@ void SwiftASTContext::LogConfiguration() {
4944
5039
GetClangImporterOptions ();
4945
5040
4946
5041
HEALTH_LOG_PRINTF (
4947
- " Extra clang arguments : (%llu items)" ,
5042
+ " Extra clang arguments : (%llu items)" ,
4948
5043
(unsigned long long )clang_importer_options.ExtraArgs .size ());
4949
5044
for (std::string &extra_arg : clang_importer_options.ExtraArgs ) {
4950
5045
HEALTH_LOG_PRINTF (" %s" , extra_arg.c_str ());
4951
5046
}
4952
- HEALTH_LOG_PRINTF (" Swift/C++ interop mode: %s" ,
4953
- m_ast_context_ap->LangOpts .EnableCXXInterop ? " on" : " off" );
5047
+
5048
+ #define PRINT_PLUGIN_PATHS (ACCESSOR, NAME, TEMPLATE, ...) \
5049
+ { \
5050
+ auto paths = m_ast_context_ap->SearchPathOpts .ACCESSOR ; \
5051
+ HEALTH_LOG_PRINTF (" %s: (%llu items)" , NAME, \
5052
+ (unsigned long long )paths.size ()); \
5053
+ for (auto &path : paths) { \
5054
+ HEALTH_LOG_PRINTF (" " TEMPLATE, ##__VA_ARGS__); \
5055
+ } \
5056
+ }
5057
+ PRINT_PLUGIN_PATHS (getCompilerPluginLibraryPaths (),
5058
+ " Compiler Plugin Library Paths " ,
5059
+ " %s" , path.c_str ());
5060
+ PRINT_PLUGIN_PATHS (getCompilerPluginExecutablePaths (),
5061
+ " Compiler Plugin Executable Paths " , " %s: [%s]" ,
5062
+ path.ExecutablePath .c_str (),
5063
+ [](auto path_names) -> std::string {
5064
+ std::string s;
5065
+ llvm::raw_string_ostream os (s);
5066
+ llvm::interleaveComma (path_names, os);
5067
+ return os.str ();
5068
+ }(path.ModuleNames )
5069
+ .c_str ());
5070
+ PRINT_PLUGIN_PATHS (PluginSearchPaths, " Plugin search paths " ,
5071
+ " %s" , path.c_str ());
5072
+ PRINT_PLUGIN_PATHS (ExternalPluginSearchPaths,
5073
+ " External plugin search paths " , " %s (server: %s)" ,
5074
+ path.SearchPath .c_str (), path.ServerPath .c_str ());
4954
5075
}
4955
5076
4956
5077
bool SwiftASTContext::HasTarget () {
0 commit comments