From 1ec900e2a00e9905e9b49e894d057c38b76bd720 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Thu, 15 Jun 2023 17:45:48 -0700 Subject: [PATCH] [Macros] Add default plugin paths for Darwin SDKs and platforms. Corresponding to https://github.com/apple/swift-driver/pull/1377, this adds some default plugin paths for Darwin SDKs and platforms. Fixes rdar://110819604. --- include/swift/Driver/ToolChain.h | 5 +++ lib/Driver/DarwinToolChains.cpp | 45 +++++++++++++++++++ lib/Driver/ToolChains.cpp | 11 +++++ lib/Driver/ToolChains.h | 6 +++ test/Driver/compiler_plugin_path_macosx.swift | 24 ++++++++++ test/Driver/driver-compile.swift | 2 +- test/Driver/merge-module.swift | 7 +-- test/Driver/sdk.swift | 2 +- 8 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 test/Driver/compiler_plugin_path_macosx.swift diff --git a/include/swift/Driver/ToolChain.h b/include/swift/Driver/ToolChain.h index d76b0c6c99036..9655eebe6ae71 100644 --- a/include/swift/Driver/ToolChain.h +++ b/include/swift/Driver/ToolChain.h @@ -143,6 +143,11 @@ class ToolChain { const llvm::opt::ArgList &inputArgs, llvm::opt::ArgStringList &arguments) const; + virtual void addPlatformSpecificPluginFrontendArgs( + const OutputInfo &OI, + const CommandOutput &output, + const llvm::opt::ArgList &inputArgs, + llvm::opt::ArgStringList &arguments) const; virtual InvocationInfo constructInvocation(const CompileJobAction &job, const JobContext &context) const; virtual InvocationInfo constructInvocation(const InterpretJobAction &job, diff --git a/lib/Driver/DarwinToolChains.cpp b/lib/Driver/DarwinToolChains.cpp index 02024e99257a5..7ec1a9a1b7eb8 100644 --- a/lib/Driver/DarwinToolChains.cpp +++ b/lib/Driver/DarwinToolChains.cpp @@ -620,6 +620,51 @@ void toolchains::Darwin::addCommonFrontendArgs( } } +/// Add the frontend arguments needed to find external plugins in standard +/// locations based on the base path. +static void addExternalPluginFrontendArgs( + StringRef basePath, const llvm::opt::ArgList &inputArgs, + llvm::opt::ArgStringList &arguments) { + // Plugin server: $BASE/usr/bin/swift-plugin-server + SmallString<128> pluginServer; + llvm::sys::path::append( + pluginServer, basePath, "usr", "bin", "swift-plugin-server"); + + SmallString<128> pluginDir; + llvm::sys::path::append(pluginDir, basePath, "usr", "lib"); + llvm::sys::path::append(pluginDir, "swift", "host", "plugins"); + arguments.push_back("-external-plugin-path"); + arguments.push_back(inputArgs.MakeArgString(pluginDir + "#" + pluginServer)); + + pluginDir.clear(); + llvm::sys::path::append(pluginDir, basePath, "usr", "local", "lib"); + llvm::sys::path::append(pluginDir, "swift", "host", "plugins"); + arguments.push_back("-external-plugin-path"); + arguments.push_back(inputArgs.MakeArgString(pluginDir + "#" + pluginServer)); +} + +void toolchains::Darwin::addPlatformSpecificPluginFrontendArgs( + const OutputInfo &OI, + const CommandOutput &output, + const llvm::opt::ArgList &inputArgs, + llvm::opt::ArgStringList &arguments) const { + // Add SDK-relative directories for plugins. + if (!OI.SDKPath.empty()) { + addExternalPluginFrontendArgs(OI.SDKPath, inputArgs, arguments); + } + + // Add platform-relative directories for plugins. + if (!OI.SDKPath.empty()) { + SmallString<128> platformPath; + llvm::sys::path::append(platformPath, OI.SDKPath); + llvm::sys::path::remove_filename(platformPath); // specific SDK + llvm::sys::path::remove_filename(platformPath); // SDKs + llvm::sys::path::remove_filename(platformPath); // Developer + llvm::sys::path::append(platformPath, "Developer"); + addExternalPluginFrontendArgs(platformPath, inputArgs, arguments); + } +} + ToolChain::InvocationInfo toolchains::Darwin::constructInvocation(const DynamicLinkJobAction &job, const JobContext &context) const { diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index c17b17f808b2d..eb1c8a0ade532 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -375,6 +375,9 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI, // Specify default plugin search path options after explicitly specified // options. inputArgs.AddAllArgs(arguments, options::OPT_plugin_search_Group); + addPlatformSpecificPluginFrontendArgs(OI, output, inputArgs, arguments); + + // Toolchain-relative plugin paths { SmallString<64> pluginPath; auto programPath = getDriver().getSwiftProgramPath(); @@ -403,6 +406,14 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI, inputArgs.AddAllArgs(arguments, options::OPT_Xcc); } +void ToolChain::addPlatformSpecificPluginFrontendArgs( + const OutputInfo &OI, + const CommandOutput &output, + const llvm::opt::ArgList &inputArgs, + llvm::opt::ArgStringList &arguments) const { + // Overridden where necessary. +} + static void addRuntimeLibraryFlags(const OutputInfo &OI, ArgStringList &Arguments) { if (!OI.RuntimeVariant) diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index c30b04809e4f1..a91f3dd78cc29 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -53,6 +53,12 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain { const llvm::opt::ArgList &inputArgs, llvm::opt::ArgStringList &arguments) const override; + void addPlatformSpecificPluginFrontendArgs( + const OutputInfo &OI, + const CommandOutput &output, + const llvm::opt::ArgList &inputArgs, + llvm::opt::ArgStringList &arguments) const override; + InvocationInfo constructInvocation(const InterpretJobAction &job, const JobContext &context) const override; InvocationInfo constructInvocation(const DynamicLinkJobAction &job, diff --git a/test/Driver/compiler_plugin_path_macosx.swift b/test/Driver/compiler_plugin_path_macosx.swift new file mode 100644 index 0000000000000..556dcf21579a6 --- /dev/null +++ b/test/Driver/compiler_plugin_path_macosx.swift @@ -0,0 +1,24 @@ +// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s 2>^1 | %FileCheck %s +// REQUIRES: OS=macosx + +// CHECK: -external-plugin-path +// CHECK-SAME: .sdk/usr/lib/swift/host/plugins# +// CHECK-SAME: .sdk/usr/bin/swift-plugin-server + +// CHECK-SAME: -external-plugin-path +// CHECK-SAME: .sdk/usr/local/lib/swift/host/plugins# +// CHECK-SAME: .sdk/usr/bin/swift-plugin-server + +// CHECK-SAME: -external-plugin-path +// CHECK-SAME: MacOSX.platform/Developer/usr/lib/swift/host/plugins# +// CHECK-SAME: MacOSX.platform/Developer/usr/bin/swift-plugin-server + +// CHECK-SAME: -external-plugin-path +// CHECK-SAME: MacOSX.platform/Developer/usr/local/lib/swift/host/plugins# +// CHECK-SAME: MacOSX.platform/Developer/usr/bin/swift-plugin-server + +// CHECK-SAME: -plugin-path +// CHECK-SAME: {{(/|\\\\)}}lib{{(/|\\\\)}}swift{{(/|\\\\)}}host{{(/|\\\\)}}plugins + +// CHECK-SAME: -plugin-path +// CHECK-SAME: {{(/|\\\\)}}local{{(/|\\\\)}}lib{{(/|\\\\)}}swift{{(/|\\\\)}}host{{(/|\\\\)}}plugins diff --git a/test/Driver/driver-compile.swift b/test/Driver/driver-compile.swift index 920bd127e199c..5910b4f1b794e 100644 --- a/test/Driver/driver-compile.swift +++ b/test/Driver/driver-compile.swift @@ -75,7 +75,7 @@ // COMPLEX: bin{{/|\\\\}}swift // COMPLEX: -c // COMPLEX: Driver{{/|\\\\}}driver-compile.swift -// COMPLEX-DAG: -sdk {{.*}}/Inputs/clang-importer-sdk +// COMPLEX-DAG: -sdk // COMPLEX-DAG: -foo -bar // COMPLEX-DAG: -Xllvm -baz // COMPLEX-DAG: -Xcc -garply diff --git a/test/Driver/merge-module.swift b/test/Driver/merge-module.swift index b8cf77ba0ee13..9a2e934661bcc 100644 --- a/test/Driver/merge-module.swift +++ b/test/Driver/merge-module.swift @@ -36,15 +36,16 @@ // SIMPLE: -o main.swiftmodule -// COMPLEX: {{bin(/|\\\\)swift(-frontend|c)?(\.exe)?"?}} -frontend +// COMPLEX: -frontend // COMPLEX: -emit-module // COMPLEX-DAG: -emit-module-doc-path {{[^ ]*[/\\]}}merge-module-{{[^ ]*}}.swiftdoc -// COMPLEX-DAG: -sdk {{.*}}/Inputs/clang-importer-sdk +// COMPLEX-DAG: -sdk +// COMPLEX-DAG /Inputs/clang-importer-sdk // COMPLEX-DAG: -foo -bar // COMPLEX-DAG: -F /path/to/frameworks -F /path/to/more/frameworks // COMPLEX-DAG: -I /path/to/headers -I path/to/more/headers // COMPLEX-DAG: -module-cache-path /tmp/modules -// COMPLEX: {{bin(/|\\\\)swift(-frontend|c)?(\.exe)?"?}} -frontend +// COMPLEX-DAG: -frontend // COMPLEX: -emit-module // COMPLEX-DAG: -F /path/to/frameworks -F /path/to/more/frameworks // COMPLEX-DAG: -I /path/to/headers -I path/to/more/headers diff --git a/test/Driver/sdk.swift b/test/Driver/sdk.swift index 8aba6e15f51f3..6ec138bd8fbc1 100644 --- a/test/Driver/sdk.swift +++ b/test/Driver/sdk.swift @@ -12,7 +12,7 @@ // OSX: bin{{/|\\\\}}swift // OSX: Driver{{/|\\\\}}sdk.swift // OSX: -sdk {{.*}}/Inputs/clang-importer-sdk -// OSX-NEXT: bin{{/|\\\\}}swift +// OSX: bin{{/|\\\\}}swift // OSX: -sdk {{.*}}/Inputs/clang-importer-sdk // OSX: {{.*}}.o{{[ "]}} // OSX: {{-syslibroot|--sysroot}} {{[^ ]*}}/Inputs/clang-importer-sdk