breaking changes to zig build API and improved caching

* in Zig build scripts, getOutputPath() is no longer a valid function
   to call, unless setOutputDir() was used, or within a custom make()
   function. Instead there is more convenient API to use which takes
   advantage of the caching system. Search this commit diff for
   `exe.run()` for an example.
 * Zig build by default enables caching. All build artifacts will go
   into zig-cache. If you want to access build artifacts in a convenient
   location, it is recommended to add an `install` step. Otherwise
   you can use the `run()` API mentioned above to execute programs
   directly from their location in the cache. Closes #330.
   `addSystemCommand` is available for programs not built with Zig
   build.
 * Please note that Zig does no cache evicting yet. You may have to
   manually delete zig-cache directories periodically to keep disk
   usage down. It's planned for this to be a simple Least Recently
   Used eviction system eventually.
 * `--output`, `--output-lib`, and `--output-h` are removed. Instead,
   use `--output-dir` which defaults to the current working directory.
   Or take advantage of `--cache on`, which will print the main output
   path to stdout, and the other artifacts will be in the same directory
   with predictable file names. `--disable-gen-h` is available when
   one wants to prevent .h file generation.
 * `@cImport` is always independently cached now. Closes #2015.
   It always writes the generated Zig code to disk which makes debug
   info and compile errors better. No more "TODO: remember C source
   location to display here"
 * Fix .d file parsing. (Fixes the MacOS CI failure)
 * Zig no longer creates "temporary files" other than inside a
   zig-cache directory.

This breaks the CLI API that Godbolt uses. The suggested new invocation
can be found in this commit diff, in the changes to `test/cli.zig`.
This commit is contained in:
Andrew Kelley
2019-03-08 22:53:35 -05:00
parent 1e634a384c
commit 91955dee58
26 changed files with 697 additions and 667 deletions

View File

@@ -1031,7 +1031,7 @@ Error os_exec_process(const char *exe, ZigList<const char *> &args,
#endif
}
void os_write_file(Buf *full_path, Buf *contents) {
Error os_write_file(Buf *full_path, Buf *contents) {
FILE *f = fopen(buf_ptr(full_path), "wb");
if (!f) {
zig_panic("os_write_file failed for %s", buf_ptr(full_path));
@@ -1041,6 +1041,7 @@ void os_write_file(Buf *full_path, Buf *contents) {
zig_panic("write failed: %s", strerror(errno));
if (fclose(f))
zig_panic("close failed");
return ErrorNone;
}
Error os_copy_file(Buf *src_path, Buf *dest_path) {
@@ -1208,91 +1209,6 @@ bool os_stderr_tty(void) {
#endif
}
#if defined(ZIG_OS_POSIX)
static Error os_buf_to_tmp_file_posix(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
const char *tmp_dir = getenv("TMPDIR");
if (!tmp_dir) {
tmp_dir = P_tmpdir;
}
buf_resize(out_tmp_path, 0);
buf_appendf(out_tmp_path, "%s/XXXXXX%s", tmp_dir, buf_ptr(suffix));
int fd = mkstemps(buf_ptr(out_tmp_path), (int)buf_len(suffix));
if (fd < 0) {
return ErrorFileSystem;
}
FILE *f = fdopen(fd, "wb");
if (!f) {
zig_panic("fdopen failed");
}
size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f);
if (amt_written != (size_t)buf_len(contents))
zig_panic("write failed: %s", strerror(errno));
if (fclose(f))
zig_panic("close failed");
return ErrorNone;
}
#endif
Buf *os_tmp_filename(Buf *prefix, Buf *suffix) {
Buf *result = buf_create_from_buf(prefix);
const char base64[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
assert(array_length(base64) == 64 + 1);
for (size_t i = 0; i < 12; i += 1) {
buf_append_char(result, base64[rand() % 64]);
}
buf_append_buf(result, suffix);
return result;
}
#if defined(ZIG_OS_WINDOWS)
static Error os_buf_to_tmp_file_windows(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
char tmp_dir[MAX_PATH + 1];
if (GetTempPath(MAX_PATH, tmp_dir) == 0) {
zig_panic("GetTempPath failed");
}
buf_init_from_str(out_tmp_path, tmp_dir);
const char base64[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
assert(array_length(base64) == 64 + 1);
for (size_t i = 0; i < 8; i += 1) {
buf_append_char(out_tmp_path, base64[rand() % 64]);
}
buf_append_buf(out_tmp_path, suffix);
FILE *f = fopen(buf_ptr(out_tmp_path), "wb");
if (!f) {
zig_panic("unable to open %s: %s", buf_ptr(out_tmp_path), strerror(errno));
}
size_t amt_written = fwrite(buf_ptr(contents), 1, buf_len(contents), f);
if (amt_written != (size_t)buf_len(contents)) {
zig_panic("write failed: %s", strerror(errno));
}
if (fclose(f)) {
zig_panic("fclose failed");
}
return ErrorNone;
}
#endif
Error os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
#if defined(ZIG_OS_WINDOWS)
return os_buf_to_tmp_file_windows(contents, suffix, out_tmp_path);
#elif defined(ZIG_OS_POSIX)
return os_buf_to_tmp_file_posix(contents, suffix, out_tmp_path);
#else
#error "missing os_buf_to_tmp_file implementation"
#endif
}
Error os_delete_file(Buf *path) {
if (remove(buf_ptr(path))) {
return ErrorFileSystem;