Skip to content

Read SwiftSyntax version for macro template from a configuration file #6774

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 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Package.resolved
*.pyc
.docc-build
.vscode
Utilities/InstalledSwiftPMConfiguration/config.json
1 change: 1 addition & 0 deletions Sources/Commands/PackageTools/Init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extension SwiftPackageTool {
name: packageName,
packageType: initMode,
destinationPath: cwd,
installedSwiftPMConfiguration: swiftTool.getHostToolchain().installedSwiftPMConfiguration,
fileSystem: swiftTool.fileSystem
)
initPackage.progressReporter = { message in
Expand Down
1 change: 1 addition & 0 deletions Sources/PackageModel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(PackageModel
DependencyMapper.swift
Diagnostics.swift
IdentityResolver.swift
InstalledSwiftPMConfiguration.swift
Manifest/Manifest.swift
Manifest/PackageConditionDescription.swift
Manifest/PackageDependencyDescription.swift
Expand Down
38 changes: 38 additions & 0 deletions Sources/PackageModel/InstalledSwiftPMConfiguration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

public struct InstalledSwiftPMConfiguration: Codable {
public struct Version: Codable, CustomStringConvertible {
let major: Int
let minor: Int
let patch: Int
let prereleaseIdentifier: String?

public init(major: Int, minor: Int, patch: Int, prereleaseIdentifier: String? = nil) {
self.major = major
self.minor = minor
self.patch = patch
self.prereleaseIdentifier = prereleaseIdentifier
}

public var description: String {
return "\(major).\(minor).\(patch).\(prereleaseIdentifier.map { "-\($0)" } ?? "")"
}
}

let version: Int
public let swiftSyntaxVersionForMacroTemplate: Version

public static var `default`: InstalledSwiftPMConfiguration {
return .init(version: 0, swiftSyntaxVersionForMacroTemplate: .init(major: 509, minor: 0, patch: 0))
}
}
3 changes: 3 additions & 0 deletions Sources/PackageModel/Toolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public protocol Toolchain {
/// An array of paths to search for libraries at link time.
var librarySearchPaths: [AbsolutePath] { get }

/// Configuration from the used toolchain.
var installedSwiftPMConfiguration: InstalledSwiftPMConfiguration { get }

/// Path of the `clang` compiler.
func getClangCompiler() throws -> AbsolutePath

Expand Down
17 changes: 16 additions & 1 deletion Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public final class UserToolchain: Toolchain {

public let isSwiftDevelopmentToolchain: Bool

public let installedSwiftPMConfiguration: InstalledSwiftPMConfiguration

/// Returns the runtime library for the given sanitizer.
public func runtimeLibrary(for sanitizer: Sanitizer) throws -> AbsolutePath {
// FIXME: This is only for SwiftPM development time support. It is OK
Expand Down Expand Up @@ -479,7 +481,8 @@ public final class UserToolchain: Toolchain {
swiftSDK: SwiftSDK,
environment: EnvironmentVariables = .process(),
searchStrategy: SearchStrategy = .default,
customLibrariesLocation: ToolchainConfiguration.SwiftPMLibrariesLocation? = nil
customLibrariesLocation: ToolchainConfiguration.SwiftPMLibrariesLocation? = nil,
customInstalledSwiftPMConfiguration: InstalledSwiftPMConfiguration? = nil
) throws {
self.swiftSDK = swiftSDK
self.environment = environment
Expand Down Expand Up @@ -522,6 +525,18 @@ public final class UserToolchain: Toolchain {
self.isSwiftDevelopmentToolchain = false
#endif

if let customInstalledSwiftPMConfiguration {
self.installedSwiftPMConfiguration = customInstalledSwiftPMConfiguration
} else {
let path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "config.json"])
if localFileSystem.exists(path) {
self.installedSwiftPMConfiguration = try JSONDecoder.makeWithDefaults().decode(path: path, fileSystem: localFileSystem, as: InstalledSwiftPMConfiguration.self)
} else {
// We *could* eventually make this an error, but not for a few releases.
self.installedSwiftPMConfiguration = InstalledSwiftPMConfiguration.default
}
}

// Use the triple from Swift SDK or compute the host triple using swiftc.
var triple = try swiftSDK.targetTriple ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)

Expand Down
17 changes: 17 additions & 0 deletions Sources/SPMTestSupport/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,20 @@ extension RelativePath: ExpressibleByStringInterpolation {
try! self.init(validating: value)
}
}

extension InitPackage {
public convenience init(
name: String,
packageType: PackageType,
destinationPath: AbsolutePath,
fileSystem: FileSystem
) throws {
try self.init(
name: name,
options: InitPackageOptions(packageType: packageType),
destinationPath: destinationPath,
installedSwiftPMConfiguration: .default,
fileSystem: fileSystem
)
}
}
10 changes: 8 additions & 2 deletions Sources/Workspace/InitPackage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public final class InitPackage {
/// The options for package to create.
let options: InitPackageOptions

/// Configuration from the used toolchain.
let installedSwiftPMConfiguration: InstalledSwiftPMConfiguration

/// The name of the package to create.
let pkgname: String

Expand All @@ -85,12 +88,14 @@ public final class InitPackage {
name: String,
packageType: PackageType,
destinationPath: AbsolutePath,
installedSwiftPMConfiguration: InstalledSwiftPMConfiguration,
fileSystem: FileSystem
) throws {
try self.init(
name: name,
options: InitPackageOptions(packageType: packageType),
destinationPath: destinationPath,
installedSwiftPMConfiguration: installedSwiftPMConfiguration,
fileSystem: fileSystem
)
}
Expand All @@ -100,12 +105,14 @@ public final class InitPackage {
name: String,
options: InitPackageOptions,
destinationPath: AbsolutePath,
installedSwiftPMConfiguration: InstalledSwiftPMConfiguration,
fileSystem: FileSystem
) throws {
self.options = options
self.pkgname = name
self.moduleName = name.spm_mangledToC99ExtendedIdentifier()
self.destinationPath = destinationPath
self.installedSwiftPMConfiguration = installedSwiftPMConfiguration
self.fileSystem = fileSystem
}

Expand Down Expand Up @@ -259,8 +266,7 @@ public final class InitPackage {
} else if packageType == .macro {
pkgParams.append("""
dependencies: [
// Depend on the Swift 5.9 release of SwiftSyntax
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
.package(url: "https://github.com/apple/swift-syntax.git", from: "\(self.installedSwiftPMConfiguration.swiftSyntaxVersionForMacroTemplate.description)"),
]
""")
}
Expand Down
1 change: 1 addition & 0 deletions Tests/BuildTests/MockBuildTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct MockToolchain: PackageModel.Toolchain {
let isSwiftDevelopmentToolchain = false
let swiftPluginServerPath: AbsolutePath? = nil
let extraFlags = PackageModel.BuildFlags()
let installedSwiftPMConfiguration = InstalledSwiftPMConfiguration.default

func getClangCompiler() throws -> AbsolutePath {
return "/fake/path/to/clang"
Expand Down
1 change: 1 addition & 0 deletions Tests/WorkspaceTests/InitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class InitTests: XCTestCase {
name: "Foo",
options: options,
destinationPath: packageRoot,
installedSwiftPMConfiguration: .default,
fileSystem: localFileSystem
)
try initPackage.writePackageStructure()
Expand Down
12 changes: 12 additions & 0 deletions Utilities/InstalledSwiftPMConfiguration/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "InstalledSwiftPMConfiguration",
targets: [
.executableTarget(
name: "InstalledSwiftPMConfiguration"
),
]
)
10 changes: 10 additions & 0 deletions Utilities/InstalledSwiftPMConfiguration/Sources/exec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Foundation

@main
struct Exec {
static func main() throws {
let config = InstalledSwiftPMConfiguration(version: 1, swiftSyntaxVersionForMacroTemplate: .init(major: 509, minor: 0, patch: 0))
let data = try JSONEncoder().encode(config)
try data.write(to: URL(fileURLWithPath: "config.json"))
}
}
6 changes: 5 additions & 1 deletion Utilities/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ def install(args):
# Install swiftpm content in all of the passed prefixes.
for prefix in args.install_prefixes:
install_swiftpm(prefix, args)
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.json")
install_file(args, config_path, os.path.join(os.path.join(prefix, "share"), "pm"))

# Install libSwiftPM if an install directory was provided.
if args.libswiftpm_install_dir:
Expand Down Expand Up @@ -461,9 +463,11 @@ def install_dylib(args, library_name, install_dir, module_names):
# Helper function that installs a single built artifact to a particular directory. The source may be either a file or a directory.
def install_binary(args, binary, destination, destination_is_directory=True, ignored_patterns=[]):
src = os.path.join(args.bin_dir, binary)
install_file(args, src, destination, destination_is_directory=destination_is_directory, ignored_patterns=ignored_patterns)

def install_file(args, src, destination, destination_is_directory=True, ignored_patterns=[]):
if destination_is_directory:
dest = os.path.join(destination, binary)
dest = os.path.join(destination, os.path.basename(src))
mkdir_p(os.path.dirname(dest))
else:
dest = destination
Expand Down
1 change: 1 addition & 0 deletions Utilities/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"swiftSyntaxVersionForMacroTemplate":{"major":509,"minor":0,"patch":0}}