Skip to content

Improve handling of tool build flags #6475

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
merged 1 commit into from
May 3, 2023
Merged

Conversation

rauhul
Copy link
Member

@rauhul rauhul commented Apr 24, 2023

Refactors the various compilerArguments and linkerArgument functions
slightly to ensure flags from a user's sdk and cli properly propagate to
the expected subcommand invocations.

Adds a fairly comprehensive test to cover the toolset and cli flag
passing end result.


Fixes a bug where clang targets didn't respect the isCXX override.

Fixes a bug where cxxCompiler extraCLIFlags where not passed to clang
when building cxx targets. Future work: #6491. cxxCompiler extraCLIFlags
should also be passed to swiftc when compiling swift modules to ensure
the clang importer has the same interpretation of the module as the
initial compile.

Fixes a bug where linker extraCLIFlags were not passed to swiftc
prefixed by -Xlinker. The previous workaround to pass these flags in
swiftCompiler extraCLIFlags is no longer necessary.

Fixes a bug where a user could pass -whole-module-optimization or
-wmo to swiftc when linking causing the driver to either attempt to
compile the object files when using the legacy driver: rdar://27578292
or crash when using the new driver: rdar://108057797.

Fixes a bug where cCompiler extraCLIFlags were not passed to swiftc
prefixed by -Xcc. The previous workaround to pass these flags in
swiftCompiler extraCLIFlags is no longer necessary.

Fixes a bug where extraCLIFlags from an SDK's toolset.json files would
be repeated multiple times in child process invocations. The repeated
flags could cause issues with some downstream tools, such as repeated
-segaddr flags to ld64 for the same segment.

Fixes a bug where extra toolchain flags would be added to an xcode build
parameters file without escaping.

Fixes a bug where extra toolchain linker flags would not be added to an
xcode build parameters file.

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Can we add a test to verify that #6380 doesn't regress with this change?

@MaxDesiatov
Copy link
Contributor

MaxDesiatov commented Apr 25, 2023

I admit, it was on me to add a test in #6380, I guess I can add it in a separate PR myself.

This one still could benefit from a test that verifies that duplicate flags bug is fixed.

@rauhul rauhul force-pushed the dont-duplicate-toolset-flags branch from a1fc0ce to 0cf62db Compare April 27, 2023 07:49
@rauhul
Copy link
Member Author

rauhul commented Apr 27, 2023

Do not test yet please, I need to update some unit tests still

@rauhul rauhul force-pushed the dont-duplicate-toolset-flags branch from 0cf62db to 5ad71d5 Compare April 27, 2023 07:50
// User arguments (from -Xcc) should follow generated arguments to allow user overrides
result += self.buildParameters.flags.cCompilerFlags.asSwiftcCCompilerFlags()

result += self.buildParameters.toolchain.extraFlags.cxxCompilerFlags.asSwiftcCXXCompilerFlags()
Copy link
Member Author

@rauhul rauhul Apr 27, 2023

Choose a reason for hiding this comment

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

@compnerd @MaxDesiatov I'd really like to get some feedback here. I'm not sure if passing -Xcxx flags to swiftc is ok for all swift targets, but it seems maybe reasonable to do because the previous implementation passed the -Xcc flags (via buildParameters.swiftCompilerFlags).

The -Xcc flags can be needed when compiling a swift target to ensure additional args make it to the clang importer. For example I pass -D__none__ and -D__MACH__ to workaround some funkiness in my armv7em-apple-none-macho build. Similarly I figure -Xcxx flags maybe should be passed to the clang importer when importing c++ modules/headers.

AFAICT by default the only change is a new trailing arg to swiftc: swiftc <old args> -Xcc -lc++ when compiling a an object this shouldn't matter and when linking I assume the linker doesn't eager link libraries if no symbols from it are used.

Copy link
Member Author

@rauhul rauhul Apr 27, 2023

Choose a reason for hiding this comment

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

After reading #4994 I think the cxxCompiler flags should go to swiftc prefixed by -Xcxx however this option does not exist in swift-driver yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think in absence of -Xcxx we probably shouldn't forward the flags here, but if needed we could add another set of flags which are the CXX flags that are "safe" to pass to SwiftPM.

Copy link
Member Author

Choose a reason for hiding this comment

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

I talked to @hyp this morning and it seems like we will be adding an -Xcxx to swift-driver some time in the future. When that happens we can start propagating spm's cxx flags to swift-driver.

triple: triple,
destination: destination,
environment: environment),
linkerFlags: destination.toolset.knownTools[.linker]?.extraCLIOptions ?? [],
Copy link
Member Author

Choose a reason for hiding this comment

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

@MaxDesiatov toolset linker flags were previously getting lost here

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, great catch!

@rauhul
Copy link
Member Author

rauhul commented Apr 27, 2023

I will update the commit message/PR title tomorrow as well with more detail about the changes

@rauhul rauhul force-pushed the dont-duplicate-toolset-flags branch from 5ad71d5 to e44c050 Compare April 27, 2023 22:59
@rauhul rauhul changed the title Fix duplicated swiftc flags when using custom sdks Improve handling of tool build flags Apr 27, 2023
@neonichu
Copy link
Contributor

neonichu commented May 1, 2023

All the duplication in target build description makes me a bit sad, but it's not an issue introduced by this PR. We should really refactor all of this...

Refactors the various compilerArguments and linkerArgument functions
slightly to ensure flags from a user's sdk and cli properly propagate to
the expected subcommand invocations.

Adds a fairly comprehensive test to cover the toolset and cli flag
passing end result.

---

Fixes a bug where clang targets didn't respect the `isCXX` override.

Fixes a bug where cxxCompiler extraCLIFlags where not passed to clang
when building cxx targets. Future work: #6491. cxxCompiler extraCLIFlags
should also be passed to `swiftc` when compiling swift modules to ensure
the clang importer has the same interpretation of the module as the
initial compile.

Fixes a bug where linker extraCLIFlags were not passed to `swiftc`
prefixed by `-Xlinker`. The previous workaround to pass these flags in
swiftCompiler extraCLIFlags is no longer necessary.

Fixes a bug where a user could pass `-whole-module-optimization` or
`-wmo` to swiftc when linking causing the driver to either attempt to
compile the object files when using the legacy driver: rdar://27578292
or crash when using the new driver: rdar://108057797.

Fixes a bug where cCompiler extraCLIFlags were not passed to `swiftc`
prefixed by `-Xcc`. The previous workaround to pass these flags in
swiftCompiler extraCLIFlags is no longer necessary.

Fixes a bug where extraCLIFlags from an SDKs toolset.json files would
be repeated multiple times in child process invocations. The repeated
flags could cause issues with some downstream tools, such as repeated
`-segaddr` flags to ld64 for the same segment.

Fixes a bug where extra toolchain flags would be added to an xcode build
parameters file without escaping.

Fixes a bug where extra toolchain linker flags would not be added to an
xcode build parameters file.
@rauhul rauhul force-pushed the dont-duplicate-toolset-flags branch from e44c050 to e7c1d3e Compare May 1, 2023 23:32
@rauhul rauhul closed this May 1, 2023
@rauhul rauhul reopened this May 1, 2023
@rauhul
Copy link
Member Author

rauhul commented May 1, 2023

@swift-ci please smoke test

@rauhul
Copy link
Member Author

rauhul commented May 1, 2023

@swift-ci please smoke test windows

2 similar comments
@rauhul
Copy link
Member Author

rauhul commented May 1, 2023

@swift-ci please smoke test windows

@rauhul
Copy link
Member Author

rauhul commented May 1, 2023

@swift-ci please smoke test windows

@rauhul rauhul enabled auto-merge (squash) May 2, 2023 05:30
@rauhul
Copy link
Member Author

rauhul commented May 2, 2023

@swift-ci please smoke test macOS

@MaxDesiatov
Copy link
Contributor

@swift-ci smoke test macos

@rauhul
Copy link
Member Author

rauhul commented May 2, 2023

Requires swiftlang/sourcekit-lsp#742 to pass macOS smoke test

@rauhul
Copy link
Member Author

rauhul commented May 3, 2023

@swift-ci please test macOS

@rauhul
Copy link
Member Author

rauhul commented May 3, 2023

@swift-ci please smoke test macOS

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

Thanks!

@rauhul rauhul merged commit 151343e into main May 3, 2023
@rauhul rauhul deleted the dont-duplicate-toolset-flags branch May 3, 2023 11:41
MaxDesiatov added a commit that referenced this pull request Jul 19, 2023
Cherry-pick of #6475.

Refactors the various compilerArguments and linkerArgument functions
slightly to ensure flags from a user's sdk and cli properly propagate to
the expected subcommand invocations.

Adds a fairly comprehensive test to cover the toolset and cli flag
passing end result.

---

Fixes a bug where clang targets didn't respect the `isCXX` override.

Fixes a bug where cxxCompiler extraCLIFlags where not passed to clang
when building cxx targets. Future work: #6491. cxxCompiler extraCLIFlags
should also be passed to `swiftc` when compiling swift modules to ensure
the clang importer has the same interpretation of the module as the
initial compile.

Fixes a bug where linker extraCLIFlags were not passed to `swiftc`
prefixed by `-Xlinker`. The previous workaround to pass these flags in
swiftCompiler extraCLIFlags is no longer necessary.

Fixes a bug where a user could pass `-whole-module-optimization` or
`-wmo` to swiftc when linking causing the driver to either attempt to
compile the object files when using the legacy driver: rdar://27578292
or crash when using the new driver: rdar://108057797.

Fixes a bug where cCompiler extraCLIFlags were not passed to `swiftc`
prefixed by `-Xcc`. The previous workaround to pass these flags in
swiftCompiler extraCLIFlags is no longer necessary.

Fixes a bug where extraCLIFlags from an SDKs toolset.json files would
be repeated multiple times in child process invocations. The repeated
flags could cause issues with some downstream tools, such as repeated
`-segaddr` flags to ld64 for the same segment.

Fixes a bug where extra toolchain flags would be added to an xcode build
parameters file without escaping.

Fixes a bug where extra toolchain linker flags would not be added to an
xcode build parameters file.

(cherry picked from commit 151343e)

# Conflicts:
#	Sources/Build/BuildPlan.swift
#	Tests/BuildTests/MockBuildTestHelper.swift

---------

Co-authored-by: Rauhul Varma <[email protected]>
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.

3 participants