Skip to content

Unify the two versions of PackageDescription into a single one using availability annotations instead of compile conditionals #3464

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

abertelrud
Copy link
Contributor

@abertelrud abertelrud commented Apr 30, 2021

Replace the compile-time conditionals in PackageDescription with availability annotations, and build a single runtime library that can be used for all package tools versions.

Motivation:

This is preparatory work for #3431 and will also make it easier for clients of libSwiftPM, since only one version of PackageDescription needs to be built.

Background:

The runtime support library for PackageDescription is currently built twice: once for SwiftPM 4.0 and another time for 4.2 and later. This is done using compile-time conditionals, producing two separate binaries in the file system. Code in the manifest loader chooses between them based on the declared tools version of the package.

Having two versions of the library has several problems:

  • it makes the build scripts more complicated; this is true for the bootstrap script, the CMake files, and the XBS build script
  • it makes the runtime code more complicated, and because PackageDescription 4 isn’t encountered as much, errors can go undetected
  • because SwiftPM doesn’t support building multiple version of the same target, only 4.2 and later is available in debug builds

We can unify the two versions using @Availability annotations, so that we only need one PackageDescription library. We should still keep it in a separate subdirectory from the PackagePlugins runtime support library. Instead of using the names “4” and “4_2” as the subdirectory names, we should take this opportunity to separate them based on something more meaningful, such as “ManifestAPI” and “PluginAPI”.

Modifications:

  • replace compile-time conditionals with availability annotations
  • adjust the package manifest to pass -package-description-version 5.5 and -enable-library-evolution
  • adjust a unit test to account for a different error message now when using 4.2 and trying to use ints for SwiftLanguageVersion

Result:

This builds PackageDescription libraries that aren't dependent on the compile time condition.

rdar://77441062

@@ -98,7 +98,7 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
guard case let ManifestParseError.invalidManifestFormat(output, _) = error else {
return XCTFail()
}
XCTAssertMatch(output, .and(.or(.contains("expected argument type"), .contains("expected element type")), .contains("SwiftVersion")))
XCTAssertMatch(output, .and(.contains("'init(name:pkgConfig:providers:products:dependencies:targets:swiftLanguageVersions:cLanguageStandard:cxxLanguageStandard:)' is unavailable"), .contains("was obsoleted in PackageDescription 4.2")))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This accounts for the difference in error message. The "is unavailable" is arguably a more accurate one anyway.

@abertelrud
Copy link
Contributor Author

@swift-ci please smoke test

@abertelrud abertelrud marked this pull request as draft April 30, 2021 20:18
@abertelrud abertelrud self-assigned this Apr 30, 2021
@abertelrud abertelrud force-pushed the eng/unified-packagedescription branch from e1adf20 to 7fde8bc Compare May 2, 2021 04:55
@abertelrud abertelrud changed the title WIP: Use availability annotations instead of #if to unify PackageDescriptiion 4.0 and 4.2+ WIP: Unify the two versions of PackageDescription into a single one using availability annotations instead of compile conditionals May 2, 2021
@abertelrud abertelrud force-pushed the eng/unified-packagedescription branch from 7fde8bc to 3b4a6ce Compare May 2, 2021 05:10
@abertelrud
Copy link
Contributor Author

@swift-ci please smoke test

@abertelrud abertelrud force-pushed the eng/unified-packagedescription branch from 3b4a6ce to 7c78ff9 Compare May 3, 2021 00:38
@abertelrud
Copy link
Contributor Author

@swift-ci please smoke test

@abertelrud abertelrud marked this pull request as ready for review May 3, 2021 01:27
@abertelrud abertelrud changed the title WIP: Unify the two versions of PackageDescription into a single one using availability annotations instead of compile conditionals Unify the two versions of PackageDescription into a single one using availability annotations instead of compile conditionals May 3, 2021
Target.swift
Version.swift
Version+StringLiteralConvertible.swift)
add_library(PackageDescription
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While unwinding the loop I also made this conform to the 2-space indentation that we seem to be using for CMakefiles.

@@ -397,22 +397,21 @@ def install_swiftpm(prefix, args):
runtime_lib_dest = os.path.join(prefix, "lib", "swift", "pm")
runtime_lib_src = os.path.join(args.bootstrap_dir, "pm")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that for now we’re still installing the bootstrap-built PackageDescription. This will change in the follow-on PR.

@abertelrud abertelrud force-pushed the eng/unified-packagedescription branch from 7c78ff9 to f157789 Compare May 3, 2021 04:12
@abertelrud
Copy link
Contributor Author

@swift-ci please smoke test

@@ -7,60 +7,63 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_library(PackagePlugin
PublicAPI/CommandConstructor.swift
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While modifying the CMakefile I made it conform to the 2-space indentation that we seem to be using for CMakefiles.

DESTINATION lib/swift/pm/${PACKAGE_DESCRIPTION_VERSION})
endforeach()
install(TARGETS PackageDescription
DESTINATION lib/swift/pm/ManifestAPI)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rather than relying on a directory named after the version number to keep the library apart from the PackagePlugin library, I added a subdirectory here named after the subsystem for which this is an API. The plugin one is called PluginAPI, and I can imagine that we'd have additional ones as needed.

endif()

install(TARGETS PackagePlugin
DESTINATION lib/swift/pm)
DESTINATION lib/swift/pm/PluginAPI)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With PackageDescription no longer living in a directory named after the version number to keep the library apart from the PackagePlugin library, I put each subsystem's API into a separate subdirectory here named after the subsystem for which this is an API. The plugin one is called PluginAPI, and I can imagine that we'd have additional ones as needed.

Copy link
Contributor

@tomerd tomerd left a comment

Choose a reason for hiding this comment

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

lgtm

would like to see what @neonichu thinks before we merge

…availability annotations instead of #if

Replace the compile-time conditionals in PackageDescription with availability annotations.  This lets us build a single PackageDescription, which will make the build scripts easier (with the goal of being able to build it entirely from the package itself).

As part of this we also clean up the runtime library subdirectories.  Until now we've had PackagePlugin directly in the runtime library directory, and PackageDescription under subdirectories called "4" and "4_2".  This has sufficed in keeping PackageDescription and PackagePlugin in separate directories (so that a manifest or plugin script can't import both).

This needs to be changed now that there is only one version of a PackageDescription, and anyway it's time to clean this up.  So as part of this we also establish two subdirectories for the runtime APIs: "ManifestAPI" and "PluginAPI".  If we add more kinds of scripts, we can then add new subdirectories here.

rdar://77441062
@abertelrud abertelrud force-pushed the eng/unified-packagedescription branch from f157789 to 2d64d68 Compare May 3, 2021 18:12
@abertelrud
Copy link
Contributor Author

@swift-ci please smoke test

@abertelrud
Copy link
Contributor Author

Error doesn't look related. Rerunning.

@abertelrud
Copy link
Contributor Author

@swift-ci please smoke test linux

@abertelrud
Copy link
Contributor Author

@neonichu Do you have any concerns about this change?

@abertelrud abertelrud merged commit 2d2b387 into swiftlang:main May 6, 2021
@abertelrud abertelrud deleted the eng/unified-packagedescription branch May 6, 2021 22:05
@@ -203,27 +189,21 @@ public final class Package {
/// The list of package dependencies.
public var dependencies: [Dependency]

#if PACKAGE_DESCRIPTION_4
/// The list of Swift versions that this package is compatible with.
public var swiftLanguageVersions: [Int]?
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a breaking change, right? In the past, we went through the trouble creating two versions because we didn't want to break this part of the API but I doubt that there are a lot of packages that will actually get affected by this..

compnerd added a commit to compnerd/swift-build that referenced this pull request May 7, 2021
This updates the devtools packaging to account for the unified module
layout that SPM adopted in swiftlang/swift-package-manager#3464.
compnerd added a commit to compnerd/swift-build that referenced this pull request May 7, 2021
This updates the devtools packaging to account for the unified module
layout that SPM adopted in swiftlang/swift-package-manager#3464.
compnerd added a commit to compnerd/swift-build that referenced this pull request May 7, 2021
This updates the devtools packaging to account for the unified module
layout that SPM adopted in swiftlang/swift-package-manager#3464.
compnerd added a commit to compnerd/swift-build that referenced this pull request May 7, 2021
This updates the devtools packaging to account for the unified module
layout that SPM adopted in swiftlang/swift-package-manager#3464.
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.

4 participants