Skip to content
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
2 changes: 1 addition & 1 deletion include/llbuild/BuildSystem/BuildFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class BuildFileDelegate {
///
/// \param isImplicit Whether the node is an implicit one (created as a side
/// effect of being declared by a command).
virtual std::unique_ptr<Node> lookupNode(StringRef name,
virtual std::unique_ptr<Node> createNode(StringRef name,
bool isImplicit=false) = 0;
};

Expand Down
3 changes: 3 additions & 0 deletions include/llbuild/BuildSystem/BuildSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace buildsystem {

class BuildDescription;
class BuildKey;
class BuildNode;
class BuildValue;
class Command;
class Node;
Expand Down Expand Up @@ -312,6 +313,8 @@ class BuildSystem {
/// @}

ShellCommandHandler* resolveShellCommandHandler(ShellCommand* command);

BuildNode *lookupNode(StringRef name);
};

}
Expand Down
2 changes: 1 addition & 1 deletion lib/BuildSystem/BuildFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class BuildFileImpl {
return it->second.get();

// Otherwise, ask the delegate to create the node.
auto node = delegate.lookupNode(name, isImplicit);
auto node = delegate.createNode(name, isImplicit);
assert(node);
auto result = node.get();
nodes[name] = std::move(node);
Expand Down
60 changes: 38 additions & 22 deletions lib/BuildSystem/BuildSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class BuildSystemFileDelegate : public BuildFileDelegate {
virtual void loadedCommand(StringRef name,
const Command& target) override;

virtual std::unique_ptr<Node> lookupNode(StringRef name,
virtual std::unique_ptr<Node> createNode(StringRef name,
bool isImplicit=false) override;

/// @}
Expand Down Expand Up @@ -162,6 +162,8 @@ class BuildSystemEngineDelegate : public BuildEngineDelegate {
BuildSystemImpl& getBuildSystem() {
return system;
}

BuildNode *lookupNode(StringRef name);
};

class BuildSystemImpl {
Expand Down Expand Up @@ -274,8 +276,10 @@ class BuildSystemImpl {
const Twine& message) {
getDelegate().error(filename, at, message);
}

BuildNode* lookupNode(StringRef name);

std::unique_ptr<BuildNode> lookupNode(StringRef name,
std::unique_ptr<BuildNode> createNode(StringRef name,
bool isImplicit);

uint32_t getMergedSchemaVersion() {
Expand Down Expand Up @@ -1695,7 +1699,26 @@ class BuildSystemRule : public Rule {




BuildNode *BuildSystemEngineDelegate::lookupNode(StringRef name) {
// Find the node.
auto it = getBuildDescription().getNodes().find(name);
BuildNode* node;
if (it != getBuildDescription().getNodes().end()) {
node = static_cast<BuildNode*>(it->second.get());
} else {
auto it = dynamicNodes.find(name);
if (it != dynamicNodes.end()) {
node = it->second.get();
} else {
// Create nodes on the fly for any unknown ones.
auto nodeOwner = system.createNode(
name, /*isImplicit=*/true);
node = nodeOwner.get();
dynamicNodes[name] = std::move(nodeOwner);
}
}
return node;
}

std::unique_ptr<Rule> BuildSystemEngineDelegate::lookupRule(const KeyType& keyData) {
// Decode the key.
Expand Down Expand Up @@ -1858,22 +1881,7 @@ std::unique_ptr<Rule> BuildSystemEngineDelegate::lookupRule(const KeyType& keyDa

case BuildKey::Kind::Node: {
// Find the node.
auto it = getBuildDescription().getNodes().find(key.getNodeName());
BuildNode* node;
if (it != getBuildDescription().getNodes().end()) {
node = static_cast<BuildNode*>(it->second.get());
} else {
auto it = dynamicNodes.find(key.getNodeName());
if (it != dynamicNodes.end()) {
node = it->second.get();
} else {
// Create nodes on the fly for any unknown ones.
auto nodeOwner = system.lookupNode(
key.getNodeName(), /*isImplicit=*/true);
node = nodeOwner.get();
dynamicNodes[key.getNodeName()] = std::move(nodeOwner);
}
}
BuildNode* node = lookupNode(key.getNodeName());

// Create the rule used to construct this node.
//
Expand Down Expand Up @@ -2051,8 +2059,12 @@ void BuildSystemEngineDelegate::error(const Twine& message) {

#pragma mark - BuildSystemImpl implementation

BuildNode *BuildSystemImpl::lookupNode(StringRef name) {
return engineDelegate.lookupNode(name);
}

std::unique_ptr<BuildNode>
BuildSystemImpl::lookupNode(StringRef name, bool isImplicit) {
BuildSystemImpl::createNode(StringRef name, bool isImplicit) {
if (name.endswith("/")) {
return BuildNode::makeDirectory(name);
}
Expand Down Expand Up @@ -4084,9 +4096,9 @@ void BuildSystemFileDelegate::loadedCommand(StringRef name,
}

std::unique_ptr<Node>
BuildSystemFileDelegate::lookupNode(StringRef name,
BuildSystemFileDelegate::createNode(StringRef name,
bool isImplicit) {
return system.lookupNode(name, isImplicit);
return system.createNode(name, isImplicit);
}

}
Expand Down Expand Up @@ -4169,6 +4181,10 @@ BuildSystem::resolveShellCommandHandler(ShellCommand* command) {
return static_cast<BuildSystemImpl*>(impl)->resolveShellCommandHandler(command);
}

BuildNode* BuildSystem::lookupNode(StringRef name) {
return static_cast<BuildSystemImpl*>(impl)->lookupNode(name);
}

// This function checks if the given path is prefixed by another path.
bool llbuild::buildsystem::pathIsPrefixedByPath(std::string path,
std::string prefixPath) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Commands/BuildSystemCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ParseBuildFileDelegate : public BuildFileDelegate {

virtual void loadedDefaultTarget(StringRef target) override;

virtual std::unique_ptr<Node> lookupNode(StringRef name,
virtual std::unique_ptr<Node> createNode(StringRef name,
bool isImplicit) override;

virtual void loadedCommand(StringRef name,
Expand Down Expand Up @@ -368,7 +368,7 @@ void ParseBuildFileDelegate::loadedDefaultTarget(StringRef target) {
}

std::unique_ptr<Node>
ParseBuildFileDelegate::lookupNode(StringRef name,
ParseBuildFileDelegate::createNode(StringRef name,
bool isImplicit) {
if (!isImplicit) {
if (showOutput) {
Expand Down
13 changes: 13 additions & 0 deletions products/libllbuild/BuildSystem-C-API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ class CAPIExternalCommand : public ExternalCommand {

/// The paths to the dependency output files, if used.
SmallVector<std::string, 1> depsPaths{};

/// Names of output nodes of this command.
SmallVector<std::string, 1> outputNodeNames{};

/// The working directory used to resolve relative paths in dependency files.
std::string workingDirectory;
Expand Down Expand Up @@ -893,6 +896,9 @@ class CAPIExternalCommand : public ExternalCommand {
SmallString<PATH_MAX> wd = value;
llvm::sys::fs::make_absolute(wd);
workingDirectory = StringRef(wd);
} else if (name == "outputs") {
outputNodeNames.clear();
outputNodeNames.emplace_back(value);
} else {
return false;
}
Expand All @@ -904,6 +910,9 @@ class CAPIExternalCommand : public ExternalCommand {
if (name == "deps") {
depsPaths.clear();
depsPaths.insert(depsPaths.begin(), values.begin(), values.end());
} else if (name == "outputs") {
outputNodeNames.clear();
outputNodeNames.insert(outputNodeNames.begin(), values.begin(), values.end());
} else {
return false;
}
Expand Down Expand Up @@ -943,6 +952,10 @@ class CAPIExternalCommand : public ExternalCommand {

virtual void startExternalCommand(BuildSystem& system,
core::TaskInterface ti) override {
outputs.reserve(outputNodeNames.size());
for (auto name: outputNodeNames) {
outputs.push_back(system.lookupNode(name));
}
cAPIDelegate.start(cAPIDelegate.context,
(llb_buildsystem_command_t*)this,
(llb_buildsystem_interface_t*)&system,
Expand Down
3 changes: 2 additions & 1 deletion products/libllbuild/include/llbuild/llbuild-defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
/// compile for multiple versions of the API.
///
/// Version History:
/// 18: Added support for configuring outputs of dynamic tasks via the C API.
///
/// 17: Added `llb_buildsystem_dependency_data_format_makefile_ignoring_subsequent_outputs`
///
Expand Down Expand Up @@ -120,6 +121,6 @@
/// 1: Added `environment` parameter to llb_buildsystem_invocation_t.
///
/// 0: Pre-history
#define LLBUILD_C_API_VERSION 17
#define LLBUILD_C_API_VERSION 18

#endif
27 changes: 27 additions & 0 deletions products/llbuildSwift/BuildSystemBindings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ public protocol ExternalCommand: AnyObject {
/// This is checked to determine if the command needs to rebuild versus the last time it was run.
func getSignature(_ command: Command) -> [UInt8]

/// Outputs nodes of the command.
var outputs: [String] { get }

/// Paths to files that contain discovered dependencies after command executed successfully for subsequent builds.
var dependencyPaths: [String] { get }

Expand Down Expand Up @@ -300,6 +303,12 @@ public protocol ExternalCommand: AnyObject {
func execute(_ command: Command, _ commandInterface: BuildSystemCommandInterface, _ jobContext: JobContext) -> CommandResult
}

public extension ExternalCommand {
var outputs: [String] {
[]
}
}

public protocol ExternalDetachedCommand: AnyObject {
/// Whether the command should run outside the execution lanes.
/// If true the build system will call `executeDetached` and `cancelDetached`.
Expand Down Expand Up @@ -457,6 +466,24 @@ private final class CommandWrapper {
}
single(context, workingDirectoryKey, workingDirectoryValue)
}
if !command.outputs.isEmpty {
var outputs: [llb_data_t] = []
for output in command.outputs {
outputs.append(copiedDataFromBytes(Array(output.utf8)))
}
var key = copiedDataFromBytes(Array("outputs".utf8))
defer {
llb_data_destroy(&key)
for index in outputs.indices {
llb_data_destroy(&outputs[index])
}
}
if outputs.count == 1 {
single(context, key, outputs[0])
} else {
collection(context, key, &outputs, outputs.count)
}
}
}

func getSignature(_: OpaquePointer, _ data: UnsafeMutablePointer<llb_data_t>) {
Expand Down