-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Run build tool plugins for C-family targets #6516
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Sources/Build/BuildDescription/SharedTargetBuildDescription.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Basics | ||
import PackageGraph | ||
import PackageLoading | ||
import PackageModel | ||
import SPMBuildCore | ||
|
||
import struct Basics.AbsolutePath | ||
|
||
/// Shared functionality between `ClangTargetBuildDescription` and `SwiftTargetBuildDescription` with the eventual hope of having a single type. | ||
struct SharedTargetBuildDescription { | ||
static func computePluginGeneratedFiles( | ||
target: ResolvedTarget, | ||
toolsVersion: ToolsVersion, | ||
additionalFileRules: [FileRuleDescription], | ||
buildParameters: BuildParameters, | ||
buildToolPluginInvocationResults: [BuildToolPluginInvocationResult], | ||
prebuildCommandResults: [PrebuildCommandResult], | ||
observabilityScope: ObservabilityScope | ||
) -> (pluginDerivedSources: Sources, pluginDerivedResources: [Resource]) { | ||
var pluginDerivedSources = Sources(paths: [], root: buildParameters.dataPath) | ||
|
||
// Add any derived files that were declared for any commands from plugin invocations. | ||
var pluginDerivedFiles = [AbsolutePath]() | ||
for command in buildToolPluginInvocationResults.reduce([], { $0 + $1.buildCommands }) { | ||
for absPath in command.outputFiles { | ||
pluginDerivedFiles.append(absPath) | ||
} | ||
} | ||
|
||
// Add any derived files that were discovered from output directories of prebuild commands. | ||
for result in prebuildCommandResults { | ||
for path in result.derivedFiles { | ||
pluginDerivedFiles.append(path) | ||
} | ||
} | ||
|
||
// Let `TargetSourcesBuilder` compute the treatment of plugin generated files. | ||
let (derivedSources, derivedResources) = TargetSourcesBuilder.computeContents( | ||
for: pluginDerivedFiles, | ||
toolsVersion: toolsVersion, | ||
additionalFileRules: additionalFileRules, | ||
defaultLocalization: target.defaultLocalization, | ||
targetName: target.name, | ||
targetPath: target.underlyingTarget.path, | ||
observabilityScope: observabilityScope | ||
) | ||
let pluginDerivedResources = derivedResources | ||
derivedSources.forEach { absPath in | ||
let relPath = absPath.relative(to: pluginDerivedSources.root) | ||
pluginDerivedSources.relativePaths.append(relPath) | ||
} | ||
|
||
return (pluginDerivedSources, pluginDerivedResources) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,6 +207,41 @@ extension LLBuildManifestBuilder { | |
} | ||
} | ||
|
||
// MARK: - Compilation | ||
|
||
extension LLBuildManifestBuilder { | ||
private func addBuildToolPlugins(_ target: TargetBuildDescription) throws { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just extracted from |
||
// Add any regular build commands created by plugins for the target. | ||
for result in target.buildToolPluginInvocationResults { | ||
// Only go through the regular build commands — prebuild commands are handled separately. | ||
for command in result.buildCommands { | ||
// Create a shell command to invoke the executable. We include the path of the executable as a | ||
// dependency, and make sure the name is unique. | ||
let execPath = command.configuration.executable | ||
let uniquedName = ([execPath.pathString] + command.configuration.arguments).joined(separator: "|") | ||
let displayName = command.configuration.displayName ?? execPath.basename | ||
var commandLine = [execPath.pathString] + command.configuration.arguments | ||
if !self.disableSandboxForPluginCommands { | ||
commandLine = try Sandbox.apply( | ||
command: commandLine, | ||
strictness: .writableTemporaryDirectory, | ||
writableDirectories: [result.pluginOutputDirectory] | ||
) | ||
} | ||
self.manifest.addShellCmd( | ||
name: displayName + "-" + ByteString(encodingAsUTF8: uniquedName).sha256Checksum, | ||
description: displayName, | ||
inputs: command.inputFiles.map { .file($0) }, | ||
outputs: command.outputFiles.map { .file($0) }, | ||
arguments: commandLine, | ||
environment: command.configuration.environment, | ||
workingDirectory: command.configuration.workingDirectory?.pathString | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Compile Swift | ||
|
||
extension LLBuildManifestBuilder { | ||
|
@@ -681,34 +716,7 @@ extension LLBuildManifestBuilder { | |
} | ||
} | ||
|
||
// Add any regular build commands created by plugins for the target. | ||
for result in target.buildToolPluginInvocationResults { | ||
// Only go through the regular build commands — prebuild commands are handled separately. | ||
for command in result.buildCommands { | ||
// Create a shell command to invoke the executable. We include the path of the executable as a | ||
// dependency, and make sure the name is unique. | ||
let execPath = command.configuration.executable | ||
let uniquedName = ([execPath.pathString] + command.configuration.arguments).joined(separator: "|") | ||
let displayName = command.configuration.displayName ?? execPath.basename | ||
var commandLine = [execPath.pathString] + command.configuration.arguments | ||
if !self.disableSandboxForPluginCommands { | ||
commandLine = try Sandbox.apply( | ||
command: commandLine, | ||
strictness: .writableTemporaryDirectory, | ||
writableDirectories: [result.pluginOutputDirectory] | ||
) | ||
} | ||
self.manifest.addShellCmd( | ||
name: displayName + "-" + ByteString(encodingAsUTF8: uniquedName).sha256Checksum, | ||
description: displayName, | ||
inputs: command.inputFiles.map { .file($0) }, | ||
outputs: command.outputFiles.map { .file($0) }, | ||
arguments: commandLine, | ||
environment: command.configuration.environment, | ||
workingDirectory: command.configuration.workingDirectory?.pathString | ||
) | ||
} | ||
} | ||
try addBuildToolPlugins(.swift(target)) | ||
|
||
// Depend on any required macro product's output. | ||
try target.requiredMacroProducts.forEach { macro in | ||
|
@@ -882,6 +890,8 @@ extension LLBuildManifestBuilder { | |
) | ||
} | ||
|
||
try addBuildToolPlugins(.clang(target)) | ||
|
||
// Create a phony node to represent the entire target. | ||
let targetName = target.target.getLLBuildTargetName(config: self.buildConfig) | ||
let output: Node = .virtual(targetName) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just extracted from
SwiftTargetBuildDescription