File: BuildSystem-C-API.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (406 lines) | stat: -rw-r--r-- 16,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//===- unittests/CAPI/BuildSystem-C-API.cpp -------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 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
//
//===----------------------------------------------------------------------===//

#include "llbuild/Basic/PlatformUtility.h"
#include "llbuild/llbuild.h"
#include "llbuild/buildsystem.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include <stdlib.h>
#include <string.h>

#include "gtest/gtest.h"

namespace {

// Reads file contents to a string (private helper function for unit test).
static std::string readFileContents(std::string path) {
  FILE * fp = fopen(path.c_str(), "rb");
  std::string str;
  if (fp) {
    fseek(fp, 0, SEEK_END);
    uint64_t size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    str.resize(size);
    size_t pos = 0;
    while (pos < size) {
      size_t result = fread((void *)(str.data() + pos), 1, size - pos, fp);
      pos += result;
    }
    fclose(fp);
  }
  return str;
}

// Writes file contents from a string (private helper function for unit test).
static void writeFileContents(std::string path, std::string str) {
  FILE * fp = fopen(path.c_str(), "wb");
  if (fp) {
    const size_t size = str.size();
    size_t pos = 0;
    while (pos < size) {
      size_t result = fwrite((void *)(str.data() + pos), 1, size - pos, fp);
      pos += result;
    }
    fclose(fp);
  }
}

static void depinfo_tester_command_start(void* context,
                                         llb_buildsystem_command_t* command,
                                         llb_buildsystem_interface_t* bi,
                                         llb_task_interface_t ti) {}

static void depinfo_tester_command_provide_value(void* context,
                                                 llb_buildsystem_command_t* command,
                                                 llb_buildsystem_interface_t* bi,
                                                 llb_task_interface_t ti,
                                                 const llb_build_value* value,
                                                 uintptr_t inputID) {}
  
static llb_buildsystem_command_result_t
depinfo_tester_command_execute_command(void *context,
                                       llb_buildsystem_command_t* command,
                                       llb_buildsystem_interface_t* bi,
                                       llb_task_interface_t ti,
                                       llb_buildsystem_queue_job_context_t* job) {
  // The tester tool is given a direct input file whose only contents are the
  // path of another, indirect input file.  It is also given the paths to which
  // it should emit the output file and an ld-style dependency-info file.  It
  // copies the indirect input file to the output file, and creates the depinfo
  // file, recording the direct and indirect input files as input dependencies.
  
  // Because the llb_buildsystem_* API doesn't seem to currently give us access
  // to the command inputs and outputs, and because extending it to do so would
  // be beyond the scope of the change being tested here, we encode the paths of
  // the input, output, and dep-info files in the command the description, which
  // we can access using llb_buildsystem_* calls.
  
  // So we get the description, which is all we have access to, and rely on the
  // use of the pipe character as a record separator in the unit test manifest.
  // llb_buildsystem_command_get_description() function is documented to return
  // a mutable copy that we then have to free, so we're free to modify it.
  char * desc = llb_buildsystem_command_get_description(command);
  char * ptr = desc;
  llbuild::basic::sys::strsep(&ptr, "|"); // skip over rule name
  std::string directInputPath = llbuild::basic::sys::strsep(&ptr, "|");
  puts(directInputPath.c_str());
  std::string outputPath = llbuild::basic::sys::strsep(&ptr, "|");
  puts(outputPath.c_str());
  std::string depInfoPath = llbuild::basic::sys::strsep(&ptr, "|");
  puts(depInfoPath.c_str());
  
  // Read the absolute path of the indirect input from the direct input.
  std::string indirectInputPath = readFileContents(directInputPath);
  if (indirectInputPath.empty()) {
    return llb_buildsystem_command_result_failed;
  }
  
  // Read the contents of the indirect input.
  std::string indirectContents = readFileContents(indirectInputPath);
  if (indirectContents.empty()) {
    return llb_buildsystem_command_result_failed;
  }
  
  // Write the contents of the indirect input to the output.
  writeFileContents(outputPath, indirectContents);
  
  // Write out the ld-style dependency info file, which consists of a sequence
  // of records.  Each record consists of a type byte followed by a C string
  // (i.e. null-terminated).  Type zero is an information string about the tool
  // that produced the dep-info file (commonly containing its name and version)
  // and type 0x10 is plain input file.
  std::string depInfoContents;
  depInfoContents.append("\0", 1);
  depInfoContents.append(std::string("version"));
  depInfoContents.append("\0", 1);
  depInfoContents.append("\020");
  depInfoContents.append(directInputPath);
  depInfoContents.append("\0", 1);
  depInfoContents.append("\020");
  depInfoContents.append(indirectInputPath);
  depInfoContents.append("\0", 1);
  writeFileContents(depInfoPath, depInfoContents);

  // Clean up.
  llb_free(desc);
  return llb_buildsystem_command_result_succeeded;
}

static llb_buildsystem_command_t*
depinfo_tester_tool_create_command(void *context, const llb_data_t* name) {
  llb_buildsystem_external_command_delegate_t delegate;
  delegate.context = NULL;
  delegate.destroy_context = NULL;
  delegate.get_signature = NULL;
  delegate.configure = NULL;
  delegate.start = depinfo_tester_command_start;
  delegate.provide_value = depinfo_tester_command_provide_value;
  delegate.execute_command = depinfo_tester_command_execute_command;
  delegate.execute_command_ex = NULL;
  delegate.execute_command_detached = NULL;
  delegate.cancel_detached_command = NULL;
  delegate.is_result_valid = NULL;
  return llb_buildsystem_external_command_create(name, delegate);
}

static bool fs_get_file_contents(void* context, const char* path,
                                llb_data_t* data_out) {
#if defined(_WIN32)
  llvm::SmallVector<llvm::UTF16, 20> wPath;
  llvm::convertUTF8ToUTF16String(path, wPath);
  wprintf(L" -- read file contents: %ls\n", (LPCWSTR)wPath.data());
  fflush(stdout);
  FILE* fp;
  if (_wfopen_s(&fp, (LPCWSTR)wPath.data(), L"rb")) {
    return false;
  }
#else
  printf(" -- read file contents: %s\n", path);
  fflush(stdout);

  FILE *fp = fopen(path, "rb");
  if (!fp) {
    return false;
  }
#endif

  fseek(fp, 0, SEEK_END);
  long size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  uint8_t* buffer = (uint8_t*)llb_alloc(size);
  if (!buffer) {
    return false;
  }
  data_out->data = buffer;
  data_out->length = size;
  int n = fread(buffer, 1, size, fp);
  if (n != size) {
    return false;
  }
  
  return true;
}

static void fs_get_file_info(void* context, const char* path,
                             llb_fs_file_info_t* file_info_out) {
  printf(" -- stat: %s\n", path);
  fflush(stdout);
  
  struct stat buf;
  if (stat(path, &buf) != 0) {
    memset(file_info_out, 0, sizeof(*file_info_out));
    return;
  }
  
  file_info_out->device = buf.st_dev;
  file_info_out->inode = buf.st_ino;
  file_info_out->size = buf.st_size;
  file_info_out->mod_time.seconds = buf.st_mtime;
  file_info_out->mod_time.nanoseconds = 0;
}

static llb_buildsystem_tool_t* lookup_tool(void *context,
                                           const llb_data_t* name) {
  if (name->length == 26 && memcmp(name->data, "custom-depinfo-tester-tool", 5) == 0) {
    llb_buildsystem_tool_delegate_t delegate = {};
    delegate.create_command = depinfo_tester_tool_create_command;
    return llb_buildsystem_tool_create(name, delegate);
  }
  
  return NULL;
}

static void handle_diagnostic(void* context,
                              llb_buildsystem_diagnostic_kind_t kind,
                              const char* filename, int line, int column,
                              const char* message) {
  const char* kindName = llb_buildsystem_diagnostic_kind_get_name(kind);
  printf("%s:%d: %s: %s\n", filename, line, kindName, message);
  fflush(stdout);
}

static void had_command_failure(void* context_p) {
  printf("%s\n", __FUNCTION__);
  fflush(stdout);
}
  
static void command_started(void* context,
                            llb_buildsystem_command_t* command) {
  char* description = llb_buildsystem_command_get_description(command);
  llb_data_t name;
  llb_buildsystem_command_get_name(command, &name);
  printf("%s: %.*s -- %s\n", __FUNCTION__, (int)name.length, name.data,
         description);
  llb_free(description);
  fflush(stdout);
}

static void command_finished(void* context,
                             llb_buildsystem_command_t* command,
                             llb_buildsystem_command_result_t result) {
  llb_data_t name;
  llb_buildsystem_command_get_name(command, &name);
  printf("%s: %.*s\n", __FUNCTION__, (int)name.length, name.data);
  fflush(stdout);
}

static void command_found_discovered_dependency(void* context,
                                                llb_buildsystem_command_t* command,
                                                const char *path,
                                                llb_buildsystem_discovered_dependency_kind_t kind) {
}

static void command_process_started(void* context,
                                    llb_buildsystem_command_t* command,
                                    llb_buildsystem_process_t* process) {
}

static void command_process_had_error(void* context,
                                      llb_buildsystem_command_t* command,
                                      llb_buildsystem_process_t* process,
                                      const llb_data_t* data) {
  llb_data_t name;
  llb_buildsystem_command_get_name(command, &name);
  printf("%s: %.*s\n", __FUNCTION__, (int)name.length, name.data);
  fwrite(data->data, data->length, 1, stdout);
  fflush(stdout);
}

static void command_process_had_output(void* context,
                                       llb_buildsystem_command_t* command,
                                       llb_buildsystem_process_t* process,
                                       const llb_data_t* data) {
  llb_data_t name;
  llb_buildsystem_command_get_name(command, &name);
  printf("%s: %.*s\n", __FUNCTION__, (int)name.length, name.data);
  fwrite(data->data, data->length, 1, stdout);
  fflush(stdout);
}

static void command_process_finished(void* context,
                                     llb_buildsystem_command_t* command,
                                     llb_buildsystem_process_t* process,
                                     const llb_buildsystem_command_extended_result_t* result) {
}

TEST(BuildSystemCAPI, CustomToolWithDiscoveredDependencies) {
  std::string tmpDirPath = llbuild::basic::sys::makeTmpDir();
  for (auto& c : tmpDirPath) {
    if (c == '\\') {
      c = '/';
    }
  }
  // We write out an indirectly referenced file containing data to be copied to
  // the output file.
  std::string indirectInputFilePath = tmpDirPath + "/" + "indirect-input-file";
  writeFileContents(indirectInputFilePath, "1");
  
  // Write out a directly referenced file containing the path of the indirectly
  // referenced file.
  std::string directInputFilePath = tmpDirPath + "/" + "direct-input-file";
  writeFileContents(directInputFilePath, indirectInputFilePath);
  
  // The output file will be written by the tool.
  std::string outputFilePath = tmpDirPath + "/" + "output-file";
  
  // The dependency info file will also be written by the tool.
  std::string depInfoFilePath = tmpDirPath + "/" + "dep-info-file";
  
  // Write out a build manifest containing node definitions and a build rule to
  // use a custom rule to copy the contents of the indirect input file to the
  // output file, via the path in the direct input file.
  std::string buildFilePath = tmpDirPath + "/llbuild";  
  std::ostringstream buildFileContents;
  buildFileContents << "client:" << std::endl;
  buildFileContents << "  name: basic" << std::endl;
  buildFileContents << std::endl;
  buildFileContents << "targets:" << std::endl;
  buildFileContents << "  \"\": [\"<all>\"]" << std::endl;
  buildFileContents << std::endl;
  buildFileContents << "nodes:" << std::endl;
  buildFileContents << "  \"" << outputFilePath << "\": {}" << std::endl;
  buildFileContents << std::endl;
  buildFileContents << "commands:" << std::endl;
  buildFileContents << "  \"copy-indirectly|" << directInputFilePath << "|" << outputFilePath << "|" << depInfoFilePath << "\":" << std::endl;
  buildFileContents << "    tool: custom-depinfo-tester-tool" << std::endl;
  buildFileContents << "    inputs: [\"" << directInputFilePath << "\"]" << std::endl;
  buildFileContents << "    outputs: [\"" << outputFilePath << "\", \"" << depInfoFilePath << "\"]" << std::endl;
  buildFileContents << "    deps: \"" << depInfoFilePath << "\"" << std::endl;
  buildFileContents << "  \"<all>\":" << std::endl;
  buildFileContents << "    tool: phony" << std::endl;
  buildFileContents << "    inputs: [\"" << outputFilePath << "\"]" << std::endl;
  buildFileContents << "    outputs: [\"<all>\"]" << std::endl;
  writeFileContents(buildFilePath, buildFileContents.str());

  // Create an invocation.
  llb_buildsystem_invocation_t invocation = {};
  invocation.buildFilePath = buildFilePath.c_str();
  invocation.useSerialBuild = true;
  
  // Create a build system delegate.
  llb_buildsystem_delegate_t delegate = {};
  delegate.context = NULL;
  delegate.fs_get_file_contents = fs_get_file_contents;
  delegate.fs_get_file_info = fs_get_file_info;
  delegate.lookup_tool = lookup_tool;
  delegate.handle_diagnostic = handle_diagnostic;
  delegate.had_command_failure = had_command_failure;
  delegate.command_started = command_started;
  delegate.command_finished = command_finished;
  delegate.command_found_discovered_dependency = command_found_discovered_dependency;
  delegate.command_process_started = command_process_started;
  delegate.command_process_had_error = command_process_had_error;
  delegate.command_process_had_output = command_process_had_output;
  delegate.command_process_finished = command_process_finished;
  
  // Create a build system.
  llb_buildsystem_t* system = llb_buildsystem_create(delegate, invocation);
  
  // Initialize the system.
  llb_buildsystem_initialize(system);
  
  // Build the default target once.
  llb_data_t key = { 0, NULL };
  printf("initial build:\n");
  if (!llb_buildsystem_build(system, &key)) {
    printf("build had command failures\n");
  }
  
  // Check that the output file was produced and has the expected contents.
  std::string outputContentsFirstBuild = readFileContents(outputFilePath);
  EXPECT_EQ(outputContentsFirstBuild, "1");
  
  // Wait for at least a second because some filesystem only have file mod
  // times on the granularity of a second.
  // FIXME: might be better to just fake the timestamp on the file
  llbuild::basic::sys::sleep(1);

  // Change the contents of the indirect (not the direct) file.
  writeFileContents(indirectInputFilePath, "2");
  
  // Build the target again.
  printf("second build:\n");
  if (!llb_buildsystem_build(system, &key)) {
    printf("build had command failures\n");
  }
  
  // Check that the output file was updated and has the expected contents.
  std::string outputContentsSecondBuild = readFileContents(outputFilePath);
  EXPECT_EQ(outputContentsSecondBuild, "2");
  
  // Destroy the build system.
  llb_buildsystem_destroy(system);
}

}