Skip to content
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

[5.5] Pass compiler flags to alllow use of @main in single-source-file executable modules #3421

Merged
merged 1 commit into from
Apr 21, 2021

Conversation

abertelrud
Copy link
Contributor

This is the 5.5 nomination of #3410 which makes it possible to have single-file source modules with a @main by taking the same approach as Xcode does when building such modules (also increasing compatibility of packages between SwiftPM and Xcode).

The original PR is #3410 and has a lot more details.

… a single source file that isn't named `main.swift` (swiftlang#3410)

The Swift compiler has certain special behaviors regarding main source files:

- if a module has just a single source file of any name, it's treated as the main source file
- if a module has a source file named `main.swift`, it's treated as the main source file

If a source file is considered the main source file, it can have top level code.  But a source file that has top level code can't also have `@main`.

This means that a single source file executable module can't use `@main`, regardless of the name of that source file.  A second empty source file can be added as a workaround, but we can employ some countermeasures in SwiftPM.

Specifically, if the executable module consists of a single source file and it is not named `main.swift`, we pass `-parse-as-library` so that a single-source file module will work.  This matches what can be seen in the build logs in Xcode, meaning that packages will build the same in SwiftPM and in Xcode.

Note that this still does not allow use of `@main` in source files named `main.swift`, but that will require compiler support to address.

Since this has the potential to break existing packages that use top-level code in a single source file that isn't named `main.swift`, this behavior is gated by a 5.5 tools version.

 See https://bugs.swift.org/browse/SR-14488 for discussion about improvements so that SwiftPM can convey the intent to build an executable module to the compiler regardless of the number of files in the module or their names.

rdar://76746150
(cherry picked from commit 41b97c8)
@abertelrud abertelrud self-assigned this Apr 21, 2021
@abertelrud abertelrud added 5.5 ready Author believes the PR is ready to be merged & any feedback has been addressed labels Apr 21, 2021
@abertelrud abertelrud changed the base branch from main to release/5.5 April 21, 2021 01:59
@abertelrud
Copy link
Contributor Author

@swift-ci please test

@abertelrud abertelrud merged commit d46052e into swiftlang:release/5.5 Apr 21, 2021
@abertelrud abertelrud deleted the eng/5.5/76746150 branch April 21, 2021 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready Author believes the PR is ready to be merged & any feedback has been addressed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants