Skip to content

[Macros] In-process plugin server library tied to compiler host, not target #74785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

drodriguez
Copy link
Contributor

PR #73725 introduced the in-process plugin server library, but the selection of the library depends on the selected toolchain, which depends on the compiler target, not the host. When cross-compiling (for example from macOS to a embedded Unix target), the compiler will incorrectly chose the .so file, not find it, and fail to compile things like the @debugDescription macro.

Move the in-process plugin server library code from the platform toolchains into the parent type, and code it so it uses the right name depending on the compiler host at compilation time. This discards the target and only relies on the compiler host for selecting the right library.

@drodriguez drodriguez requested a review from rintaro June 27, 2024 16:15
@drodriguez drodriguez requested a review from artemcm as a code owner June 27, 2024 16:15
@drodriguez
Copy link
Contributor Author

@swift-ci please test

SmallString<261> InProcPluginServerPath = LibraryPath;
llvm::sys::path::append(InProcPluginServerPath,
"SwiftInProcPluginServer.dll");
Arguments.push_back("-in-process-plugin-server-path");
Arguments.push_back(Args.MakeArgString(InProcPluginServerPath));
addInProcPluginServerPath(LibraryPath, Args, Arguments);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rintaro what I was commenting in #74740 (comment) about "not fully sure if the code around it is completely correct" is about the calculation of PluginPath and LibraryPath. The code right now is part of the *ToolChains.cpp, but I am not sure if it should move to a more central place as well, since I think it depends on the compiler host, not the target as well.

I can do the change, but I am not sure about its correctness, so I will wait until someone with more knowledge can say if this is the right place or it should be moved out of the *ToolChains.cpp and into the ToolChains.cpp file as well.

@rintaro rintaro self-assigned this Jun 27, 2024
@drodriguez drodriguez force-pushed the inproc-plugin-server-right-library branch from 65ab3f3 to 979c98b Compare June 27, 2024 22:33
@drodriguez
Copy link
Contributor Author

@swift-ci please test

@@ -1468,6 +1468,25 @@ void ToolChain::addLinkRuntimeLib(const ArgList &Args, ArgStringList &Arguments,
Arguments.push_back(Args.MakeArgString(P));
}

void ToolChain::addInProcPluginServerPath(const StringRef PluginPath,
Copy link
Member

@rintaro rintaro Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is still incorrect. Say when building in macOS targeting Windows, WindowsToolchain is used, and it passes in the bin directory. But in macOS hosts this function expects lib/swift.

Yes, as you said the entire addPluginArguments looks not doing the right thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That's what I was asking in the comment above. It looks like more than this needs to move outside the *ToolChains.cpp and into ToolChains.cpp and make it compile conditionally depending on the platform, but I was not completely sure.

If I see things correctly I think this is what is happening:

  • In addPluginArguments
    • Calculate a base path.
      • Darwin and Unix: Use computeRuntimeResourcePathFromExecutablePath
      • Windows: Use executable path (?)
    • Add the -in-process-plugin-server-path (this PR)
      • Darwin and Unix: Use a "host" directory. Each one with its platform extension.
      • Windows: Don't use extra directories. No "lib" prefix and ".dll" extension.
    • Add a -plugin-path
      • Darwin and Unix: Uses base path + "host/plugins"
      • Windows: Uses the executable path (?)
    • Add a local plugin path
      • Darwin and Unix: Add a "../../local/lib/swift/host/plugins" with an extra -plugin-path
      • Windows: nothing
  • In addExternalPluginFrontendArgs and addPlatformSpecificPluginFrontendArgs
    • Only exist on Darwin
    • Does this need to be in fact for the Darwin compiler, and not for the Darwin targets?

I think all the code in addPluginArguments should be tied to the compiler host, and not the target, but I wanted to first have confirmation that this is in fact necessary. There might be something that I don't completely understand about the plugin compilation module. If you agree with the above and can answer about addPlatformSpecificPluginFrontendArgs I can try to do the changes. I knew this works in the case I was experiencing, probably because the addPluginArguments ended up being the same for Darwin and Unix after my changes, but I imagine Windows -> Unix will have its own share of problems as well.

Copy link
Member

@rintaro rintaro Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for doing this Daniel!

As for addPluginArguments, I believe everything you mentioned is correct. In Windows host, everything must be in the same directory as the getDriver().getSwiftProgramPath().

As for addExternalPluginFrontendArgs(), this is for users of the macOS/iOS/etc SDKs, so it's basically tied to the target, and I believe it's fine this is in DarwinToolchain. That said, since Apple doesn't provide swift-plugin-server that runs other host platforms, maybe we should wrap this function's body with #if defined(__APPLE__), but I don't think it's really necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hopefully matched the code as close as possible to the one in the swift-driver. Hopefully it is what you were asking for.

@drodriguez drodriguez force-pushed the inproc-plugin-server-right-library branch from 979c98b to 104ac44 Compare June 28, 2024 03:45
@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

Comment on lines 1473 to 1480
auto swiftProgramPath = getDriver().getSwiftProgramPath();
#if defined(_WIN32)
SmallString<261> pluginPathBase = StringRef(swiftProgramPath);
llvm::sys::path::remove_filename(pluginPathBase); // Remove `swift`
#elif defined(__APPLE__) || defined(__unix__)
SmallString<64> pluginPathBase;
CompilerInvocation::computeRuntimeResourcePathFromExecutablePath(
swiftProgramPath, /*shared=*/true, pluginPathBase);
#else
Copy link
Member

@rintaro rintaro Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked swift-driver implementation
https://github.com/apple/swift-driver/blob/6d1e5d2bd8e67432176cd843704bd2277616376f/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift#L793-L802
Since it's all based on the parent of the executable directory. Let's align with it.

Copy link
Member

@rintaro rintaro Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rintaro rintaro removed their assignment Jun 28, 2024
@drodriguez drodriguez force-pushed the inproc-plugin-server-right-library branch 2 times, most recently from ed88c2b to 177d342 Compare June 29, 2024 17:14
@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

@drodriguez drodriguez force-pushed the inproc-plugin-server-right-library branch from 177d342 to eb3cf18 Compare June 30, 2024 16:57
@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

…target

PR swiftlang#73725 introduced the in-process plugin server library, but the
selection of the library depends on the selected toolchain, which
depends on the compiler target, not the host. When cross-compiling (for
example from macOS to a embedded Unix target), the compiler will
incorrectly chose the `.so` file, not find it, and fail to compile
things like the `@debugDescription` macro.

Move the in-process plugin server library code from the platform
toolchains into the parent type, and code it so it uses the right name
depending on the compiler host at compilation time. This discards the
target and only relies on the compiler host for selecting the right
library.
@drodriguez drodriguez force-pushed the inproc-plugin-server-right-library branch from eb3cf18 to 55d9e74 Compare July 1, 2024 00:20
@drodriguez
Copy link
Contributor Author

@swift-ci please smoke test

@drodriguez
Copy link
Contributor Author

@rintaro seems that I got it right this time. Please review the final version and let me know if I should merge.

@rintaro
Copy link
Member

rintaro commented Jul 1, 2024

Thank you!

@drodriguez drodriguez merged commit 5280cea into swiftlang:main Jul 1, 2024
3 checks passed
@drodriguez drodriguez deleted the inproc-plugin-server-right-library branch July 1, 2024 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants