Skip to content

Add new Swift API for creating CustomTasks from byte arrays instead of strings #887

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 1 commit into from
Jul 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
3 changes: 3 additions & 0 deletions Xcode/Configs/Common.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ USE_HEADERMAP = NO
SWIFT_OBJC_INTERFACE_HEADER_NAME =
SWIFT_INSTALL_OBJC_HEADER = NO

// The major component of the user module version should be the date it was last changed (YYMMDD.N, where N is usually 0 but can be used if multiple API changes are made on the same day).
SWIFT_USER_MODULE_VERSION = 230727.0

// Enable InstallAPI.
SUPPORTS_TEXT_BASED_API = YES
TAPI_ENABLE_MODULES = YES
Expand Down
11 changes: 11 additions & 0 deletions products/libllbuild/BuildKey-C-API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ llb_build_key_t *llb_build_key_make_custom_task(const char *name, const char *ta
return (llb_build_key_t *)new CAPIBuildKey(BuildKey::makeCustomTask(StringRef(name), StringRef(taskData)));
}

llb_build_key_t *llb_build_key_make_custom_task_with_data(const char *name, llb_data_t data) {
return (llb_build_key_t *)new CAPIBuildKey(BuildKey::makeCustomTask(StringRef(name), StringRef((const char *)data.data, data.length)));
}


void llb_build_key_get_custom_task_name(llb_build_key_t *key, llb_data_t *out_name) {
auto name = ((CAPIBuildKey *)key)->getInternalBuildKey().getCustomTaskName();
out_name->length = name.size();
Expand All @@ -152,6 +157,12 @@ void llb_build_key_get_custom_task_data(llb_build_key_t *key, llb_data_t *out_ta
out_task_data->data = (const uint8_t*)strdup(data.str().c_str());
}

void llb_build_key_get_custom_task_data_no_copy(llb_build_key_t *key, llb_data_t *out_task_data) {
auto data = ((CAPIBuildKey *)key)->getInternalBuildKey().getCustomTaskData();
out_task_data->length = data.size();
out_task_data->data = (const uint8_t *)data.data();
}

llb_build_key_t *llb_build_key_make_directory_contents(const char *path) {
return (llb_build_key_t *)new CAPIBuildKey(BuildKey::makeDirectoryContents(StringRef(path)));
}
Expand Down
3 changes: 3 additions & 0 deletions products/libllbuild/include/llbuild/buildkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ LLBUILD_EXPORT void llb_build_key_get_command_name(llb_build_key_t *key, llb_dat

// Custom Task
LLBUILD_EXPORT llb_build_key_t *llb_build_key_make_custom_task(const char *name, const char *taskData);
LLBUILD_EXPORT llb_build_key_t *llb_build_key_make_custom_task_with_data(const char *name, llb_data_t data);
LLBUILD_EXPORT void llb_build_key_get_custom_task_name(llb_build_key_t *key, llb_data_t *out_name);
LLBUILD_EXPORT void llb_build_key_get_custom_task_data(llb_build_key_t *key, llb_data_t *out_task_data);
LLBUILD_EXPORT void llb_build_key_get_custom_task_data_no_copy(llb_build_key_t *key, llb_data_t *out_task_data);


// Directory Contents
LLBUILD_EXPORT llb_build_key_t *llb_build_key_make_directory_contents(const char *path);
Expand Down
4 changes: 3 additions & 1 deletion products/libllbuild/include/llbuild/llbuild-defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
///
/// Version History:
///
/// 16: Added more efficient C API for working with custom tasks
///
/// 15: Added `determined_rule_needs_to_run` delegate method
///
/// 14: Added configure API to CAPIExternalCommand
Expand Down Expand Up @@ -116,6 +118,6 @@
/// 1: Added `environment` parameter to llb_buildsystem_invocation_t.
///
/// 0: Pre-history
#define LLBUILD_C_API_VERSION 15
#define LLBUILD_C_API_VERSION 16

#endif
12 changes: 12 additions & 0 deletions products/llbuildSwift/BuildKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public class BuildKey: CustomStringConvertible, Equatable, Hashable {
self.init(llb_build_key_make_custom_task(name, taskData))
}

public convenience init(name: String, taskDataBytes: [UInt8]) {
self.init(taskDataBytes.withUnsafeBufferPointer { buf in
llb_build_key_make_custom_task_with_data(name, llb_data_t(length: UInt64(buf.count), data: buf.baseAddress))
})
}

/// The name of the task
public var name: String {
return formString {
Expand All @@ -162,6 +168,12 @@ public class BuildKey: CustomStringConvertible, Equatable, Hashable {
}
}

public var taskDataBytes: [UInt8] {
var data = llb_data_t()
llb_build_key_get_custom_task_data_no_copy(internalBuildKey, &data)
return Array(UnsafeBufferPointer(start: data.data, count: Int(data.length)))
}

public override var description: String {
return "<BuildKey.\(type(of: self)) name=\(name) taskData=\(taskData)>"
}
Expand Down