Pass build-tools flags for macro compilation #9390
Open
+20
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Similar in spirit to #5966, but where that one was about plugins, this one is about Swift macros.
We reuse the existing
-Xbuild-tools-swiftcbecause that was what was designed to be used for (any usage ofswiftcused for the build tools, which in the case of macros, need to build for the building machine, to not the target machine).Motivation:
For most cases, compiling against the host toolchain does not need extra arguments, but mixing the arguments for the target with those for the host toolchain can create problems if the target is different enough to the host. In the case of Swift macros, any argument passed to
-Xswiftcand others was being forwarded to the Swift macro compilation, which is not correct, since the macros need to compile and execute in the host machine, not the target machine.The original intention of #5966 was that all host compilation used the "host" flags, and the original spelling proposed in #5966 (
-Xhost) was chosen because of this, but the PR review settled in-Xbuild-tools-swiftc, and I think macro compilation did not end up using those flags because it did not convey the actual intention of those flags.Modifications:
This creates a parallel variable to the existing
pluginSwiftCFlagsnamedhostSwiftCFlagswhich picks up thebuildToolsSwiftCFlagsthat come from the command line-Xbuild-tools-swiftcflags.With those, it builds a
hostBuildFlagswith mostly empty flags, except for theswiftCompilerFlagstaking its value fromhostSwiftCFlags. The rest of the flags are empty because there is not equivalent-Xbuild-tools-cc,-Xbuild-tools-cxx, to take their values from, and they have never been needed.When building the
SwiftCommandState, depending on thedestination, one set of flags or the other will be chosen, allowing macros to avoid the-Xswiftcflags intended for the target.Result:
With these modifications the
-Xswiftcflags of the target in complicated setups are not propagated to the macro compilation, while the ones from-Xbuild-tools-swiftcare, allowing issuingswift buildthat just works when host and target are very different.