aboutsummaryrefslogtreecommitdiff
path: root/src/os.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-03-08 22:53:35 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-03-08 23:23:11 -0500
commit91955dee587214722daa09e6f3dbff059ac3752e (patch)
tree7f56bf31f93081c63a4bcf03b9e38b7c5b1ca29c /src/os.cpp
parent1e634a384ccc0ae57e0343369f7adb0b1b4b9875 (diff)
downloadzig-91955dee587214722daa09e6f3dbff059ac3752e.tar.gz
zig-91955dee587214722daa09e6f3dbff059ac3752e.zip
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`.
Diffstat (limited to 'src/os.cpp')
-rw-r--r--src/os.cpp88
1 files changed, 2 insertions, 86 deletions
diff --git a/src/os.cpp b/src/os.cpp
index c50fb71884..d5195c92d8 100644
--- a/src/os.cpp
+++ b/src/os.cpp
@@ -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;