Skip to content

Commit 6a4af8e

Browse files
committed
[libllbuild] Add minimal status reporting APIs.
1 parent f5852ca commit 6a4af8e

File tree

4 files changed

+160
-27
lines changed

4 files changed

+160
-27
lines changed

examples/c-api/buildsystem/main.c

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,81 @@
2323

2424
static const char* progname;
2525
static void usage() {
26-
fprintf(stderr, "usage: %s <build file path>\n", progname);
27-
exit(0);
26+
fprintf(stderr, "usage: %s <build file path>\n", progname);
27+
exit(0);
2828
}
2929

3030
static const char* basename(const char* path) {
31-
const char* result = strrchr(path, '/');
32-
return result ? result : path;
31+
const char* result = strrchr(path, '/');
32+
return result ? result : path;
33+
}
34+
35+
static void command_started(void* context,
36+
llb_buildsystem_command_t* command) {
37+
llb_data_t name;
38+
llb_buildsystem_command_get_name(command, &name);
39+
printf("%s: %.*s\n", __FUNCTION__, (int)name.length, name.data);
40+
}
41+
42+
static void command_finished(void* context,
43+
llb_buildsystem_command_t* command) {
44+
llb_data_t name;
45+
llb_buildsystem_command_get_name(command, &name);
46+
printf("%s: %.*s\n", __FUNCTION__, (int)name.length, name.data);
47+
}
48+
49+
static void command_process_started(void* context,
50+
llb_buildsystem_command_t* command,
51+
llb_buildsystem_process_t* process) {
52+
}
53+
54+
static void command_process_had_output(void* context,
55+
llb_buildsystem_command_t* command,
56+
llb_buildsystem_process_t* process,
57+
const llb_data_t* data) {
58+
llb_data_t name;
59+
llb_buildsystem_command_get_name(command, &name);
60+
printf("%s: %.*s\n", __FUNCTION__, (int)name.length, name.data);
61+
fwrite(data->data, data->length, 1, stdout);
62+
}
63+
64+
static void command_process_finished(void* context,
65+
llb_buildsystem_command_t* command,
66+
llb_buildsystem_process_t* process,
67+
int exit_status) {
3368
}
3469

3570
int main(int argc, char **argv) {
36-
progname = basename(argv[0]);
71+
progname = basename(argv[0]);
3772

38-
if (argc != 2) {
39-
usage();
40-
}
73+
if (argc != 2) {
74+
usage();
75+
}
4176

42-
const char* buildFilePath = argv[1];
77+
const char* buildFilePath = argv[1];
4378

44-
// Create an invocation.
45-
llb_buildsystem_invocation_t invocation = {};
46-
invocation.buildFilePath = buildFilePath;
79+
// Create an invocation.
80+
llb_buildsystem_invocation_t invocation = {};
81+
invocation.buildFilePath = buildFilePath;
4782

48-
// Create a build system delegate.
49-
llb_buildsystem_delegate_t delegate = {};
83+
// Create a build system delegate.
84+
llb_buildsystem_delegate_t delegate = {};
85+
delegate.context = NULL;
86+
delegate.command_started = command_started;
87+
delegate.command_finished = command_finished;
88+
delegate.command_process_started = command_process_started;
89+
delegate.command_process_had_output = command_process_had_output;
90+
delegate.command_process_finished = command_process_finished;
5091

51-
// Create a build system.
52-
llb_buildsystem_t* system = llb_buildsystem_create(delegate, invocation);
92+
// Create a build system.
93+
llb_buildsystem_t* system = llb_buildsystem_create(delegate, invocation);
5394

54-
// Build the default target.
55-
llb_data_t key = { 0, NULL };
56-
llb_buildsystem_build(system, &key);
95+
// Build the default target.
96+
llb_data_t key = { 0, NULL };
97+
llb_buildsystem_build(system, &key);
5798

58-
// Destroy the build system.
59-
llb_buildsystem_destroy(system);
99+
// Destroy the build system.
100+
llb_buildsystem_destroy(system);
60101

61-
return 0;
102+
return 0;
62103
}

products/libllbuild/BuildSystem-C-API.cpp

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// Include the public API.
1414
#include <llbuild/llbuild.h>
1515

16+
#include "llbuild/BuildSystem/BuildFile.h"
1617
#include "llbuild/BuildSystem/BuildSystemFrontend.h"
1718
#include "llbuild/Core/BuildEngine.h"
1819

@@ -29,13 +30,58 @@ using namespace llbuild::buildsystem;
2930
namespace {
3031

3132
class CAPIBuildSystemFrontendDelegate : public BuildSystemFrontendDelegate {
33+
llb_buildsystem_delegate_t cAPIDelegate;
34+
3235
public:
33-
using BuildSystemFrontendDelegate::BuildSystemFrontendDelegate;
36+
CAPIBuildSystemFrontendDelegate(llvm::SourceMgr& sourceMgr,
37+
BuildSystemInvocation& invocation,
38+
llb_buildsystem_delegate_t delegate)
39+
: BuildSystemFrontendDelegate(sourceMgr, invocation, "basic", 0),
40+
cAPIDelegate(delegate) { }
3441

3542
virtual std::unique_ptr<Tool> lookupTool(StringRef name) override {
3643
// No support for custom tools yet.
3744
return {};
3845
}
46+
47+
virtual void commandStarted(Command* command) override {
48+
cAPIDelegate.command_started(
49+
cAPIDelegate.context,
50+
(llb_buildsystem_command_t*) command);
51+
}
52+
53+
virtual void commandFinished(Command* command) override {
54+
cAPIDelegate.command_finished(
55+
cAPIDelegate.context,
56+
(llb_buildsystem_command_t*) command);
57+
}
58+
59+
virtual void commandProcessStarted(Command* command,
60+
ProcessHandle handle) override {
61+
cAPIDelegate.command_process_started(
62+
cAPIDelegate.context,
63+
(llb_buildsystem_command_t*) command,
64+
(llb_buildsystem_process_t*) handle.id);
65+
}
66+
67+
virtual void commandProcessHadOutput(Command* command, ProcessHandle handle,
68+
StringRef data) override {
69+
llb_data_t cData{ data.size(), (const uint8_t*) data.data() };
70+
cAPIDelegate.command_process_had_output(
71+
cAPIDelegate.context,
72+
(llb_buildsystem_command_t*) command,
73+
(llb_buildsystem_process_t*) handle.id,
74+
&cData);
75+
}
76+
77+
virtual void commandProcessFinished(Command* command, ProcessHandle handle,
78+
int exitStatus) override {
79+
cAPIDelegate.command_process_finished(
80+
cAPIDelegate.context,
81+
(llb_buildsystem_command_t*) command,
82+
(llb_buildsystem_process_t*) handle.id,
83+
exitStatus);
84+
}
3985
};
4086

4187
class CAPIBuildSystem {
@@ -64,8 +110,7 @@ class CAPIBuildSystem {
64110
frontendDelegate.reset(
65111
// FIXME: Need to get the client name and schema version from
66112
// parameters.
67-
new CAPIBuildSystemFrontendDelegate(sourceMgr, invocation,
68-
"basic", 0));
113+
new CAPIBuildSystemFrontendDelegate(sourceMgr, invocation, delegate));
69114

70115
// Allocate the actual frontend.
71116
frontend.reset(new BuildSystemFrontend(*frontendDelegate, invocation));
@@ -84,6 +129,13 @@ class CAPIBuildSystem {
84129
llb_buildsystem_t* llb_buildsystem_create(
85130
llb_buildsystem_delegate_t delegate,
86131
llb_buildsystem_invocation_t invocation) {
132+
// Check that all required methods are provided.
133+
assert(delegate.command_started);
134+
assert(delegate.command_finished);
135+
assert(delegate.command_process_started);
136+
assert(delegate.command_process_had_output);
137+
assert(delegate.command_process_finished);
138+
87139
return (llb_buildsystem_t*) new CAPIBuildSystem(delegate, invocation);
88140
}
89141

@@ -97,3 +149,12 @@ bool llb_buildsystem_build(llb_buildsystem_t* system_p, const llb_data_t* key) {
97149
return system->getFrontend().build(
98150
core::KeyType((const char*)key->data, key->length));
99151
}
152+
153+
void llb_buildsystem_command_get_name(llb_buildsystem_command_t* command_p,
154+
llb_data_t* key_out) {
155+
auto command = (Command*) command_p;
156+
157+
auto name = command->getName();
158+
key_out->length = name.size();
159+
key_out->data = (const uint8_t*) name.data();
160+
}

products/libllbuild/public-api/llbuild/buildsystem.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
/// Opaque handle to a build system.
3131
typedef struct llb_buildsystem_t_ llb_buildsystem_t;
3232

33+
/// Opaque handle to a build system command.
34+
typedef struct llb_buildsystem_command_t_ llb_buildsystem_command_t;
35+
36+
/// Opaque handle to a build system command's launched process.
37+
typedef struct llb_buildsystem_process_t_ llb_buildsystem_process_t;
38+
3339
/// Invocation parameters for a build system.
3440
typedef struct llb_buildsystem_invocation_t_ llb_buildsystem_invocation_t;
3541
struct llb_buildsystem_invocation_t_ {
@@ -55,6 +61,22 @@ struct llb_buildsystem_invocation_t_ {
5561
typedef struct llb_buildsystem_delegate_t_ {
5662
/// User context pointer.
5763
void* context;
64+
65+
void (*command_started)(void* context, llb_buildsystem_command_t* command);
66+
67+
void (*command_finished)(void* context, llb_buildsystem_command_t* command);
68+
69+
void (*command_process_started)(void* context,
70+
llb_buildsystem_command_t* command,
71+
llb_buildsystem_process_t* process);
72+
void (*command_process_had_output)(void* context,
73+
llb_buildsystem_command_t* command,
74+
llb_buildsystem_process_t* process,
75+
const llb_data_t* data);
76+
void (*command_process_finished)(void* context,
77+
llb_buildsystem_command_t* command,
78+
llb_buildsystem_process_t* process,
79+
int exit_status);
5880
} llb_buildsystem_delegate_t;
5981

6082
/// Create a new build system instance.
@@ -70,6 +92,13 @@ llb_buildsystem_destroy(llb_buildsystem_t* system);
7092
LLBUILD_EXPORT bool
7193
llb_buildsystem_build(llb_buildsystem_t* system, const llb_data_t* key);
7294

95+
/// Get the name of the given command.
96+
///
97+
/// \param key_out - On return, contains a pointer to the name of the command.
98+
LLBUILD_EXPORT void
99+
llb_buildsystem_command_get_name(llb_buildsystem_command_t* command,
100+
llb_data_t* key_out);
101+
73102
/// @}
74103

75104
#endif

tests/Examples/buildsystem-capi.llbuild

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# RUN: env LD_LIBRARY_PATH=%{llbuild-lib-dir} %t.exe %s > %t.out
55
# RUN: %{FileCheck} %s --input-file %t.out
66
#
7-
# CHECK: Hello
8-
# CHECK-NEXT: Hello, world!
7+
# CHECK: command_started: <hello>
8+
# CHECK: command_finished: <hello>
9+
# CHECK: command_started: <all>
10+
# CHECK: command_finished: <all>
911

1012
client:
1113
name: basic

0 commit comments

Comments
 (0)