-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add run compiler command #874
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,13 +291,39 @@ void os_path_resolve(Buf *ref_path, Buf *target_path, Buf *out_abs_path) { | |
return; | ||
} | ||
|
||
int os_fetch_file(FILE *f, Buf *out_buf) { | ||
int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had a look and to me it still seemed appropriate keep the file reading in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good 👍 |
||
static const ssize_t buf_size = 0x2000; | ||
buf_resize(out_buf, buf_size); | ||
ssize_t actual_buf_len = 0; | ||
|
||
bool first_read = true; | ||
|
||
for (;;) { | ||
size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f); | ||
actual_buf_len += amt_read; | ||
|
||
if (skip_shebang && first_read && buf_starts_with_str(out_buf, "#!")) { | ||
size_t i = 0; | ||
while (true) { | ||
if (i > buf_len(out_buf)) { | ||
zig_panic("shebang line exceeded %zd characters", buf_size); | ||
} | ||
|
||
size_t current_pos = i; | ||
i += 1; | ||
|
||
if (out_buf->list.at(current_pos) == '\n') { | ||
break; | ||
} | ||
} | ||
|
||
ZigList<char> *list = &out_buf->list; | ||
memmove(list->items, list->items + i, list->length - i); | ||
list->length -= i; | ||
|
||
actual_buf_len -= i; | ||
} | ||
|
||
if (amt_read != buf_size) { | ||
if (feof(f)) { | ||
buf_resize(out_buf, actual_buf_len); | ||
|
@@ -308,6 +334,7 @@ int os_fetch_file(FILE *f, Buf *out_buf) { | |
} | ||
|
||
buf_resize(out_buf, actual_buf_len + buf_size); | ||
first_read = false; | ||
} | ||
zig_unreachable(); | ||
} | ||
|
@@ -377,8 +404,8 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args, | |
|
||
FILE *stdout_f = fdopen(stdout_pipe[0], "rb"); | ||
FILE *stderr_f = fdopen(stderr_pipe[0], "rb"); | ||
os_fetch_file(stdout_f, out_stdout); | ||
os_fetch_file(stderr_f, out_stderr); | ||
os_fetch_file(stdout_f, out_stdout, false); | ||
os_fetch_file(stderr_f, out_stderr, false); | ||
|
||
fclose(stdout_f); | ||
fclose(stderr_f); | ||
|
@@ -591,7 +618,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) { | |
} | ||
} | ||
|
||
int os_fetch_file_path(Buf *full_path, Buf *out_contents) { | ||
int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) { | ||
FILE *f = fopen(buf_ptr(full_path), "rb"); | ||
if (!f) { | ||
switch (errno) { | ||
|
@@ -610,7 +637,7 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents) { | |
return ErrorFileSystem; | ||
} | ||
} | ||
int result = os_fetch_file(f, out_contents); | ||
int result = os_fetch_file(f, out_contents, skip_shebang); | ||
fclose(f); | ||
return result; | ||
} | ||
|
@@ -783,6 +810,44 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) { | |
#endif | ||
} | ||
|
||
#if defined(ZIG_OS_POSIX) | ||
int os_get_global_cache_directory(Buf *out_tmp_path) { | ||
const char *tmp_dir = getenv("TMPDIR"); | ||
if (!tmp_dir) { | ||
tmp_dir = P_tmpdir; | ||
} | ||
|
||
Buf *tmp_dir_buf = buf_create_from_str(tmp_dir); | ||
Buf *cache_dirname_buf = buf_create_from_str("zig-cache"); | ||
|
||
buf_resize(out_tmp_path, 0); | ||
os_path_join(tmp_dir_buf, cache_dirname_buf, out_tmp_path); | ||
|
||
buf_deinit(tmp_dir_buf); | ||
buf_deinit(cache_dirname_buf); | ||
return 0; | ||
} | ||
#endif | ||
|
||
#if defined(ZIG_OS_WINDOWS) | ||
int os_get_global_cache_directory(Buf *out_tmp_path) { | ||
char tmp_dir[MAX_PATH + 1]; | ||
if (GetTempPath(MAX_PATH, tmp_dir) == 0) { | ||
zig_panic("GetTempPath failed"); | ||
} | ||
|
||
Buf *tmp_dir_buf = buf_create_from_str(tmp_dir); | ||
Buf *cache_dirname_buf = buf_create_from_str("zig-cache"); | ||
|
||
buf_resize(out_tmp_path, 0); | ||
os_path_join(tmp_dir_buf, cache_dirname_buf, out_tmp_path); | ||
|
||
buf_deinit(tmp_dir_buf); | ||
buf_deinit(cache_dirname_buf); | ||
return 0; | ||
} | ||
#endif | ||
|
||
int os_delete_file(Buf *path) { | ||
if (remove(buf_ptr(path))) { | ||
return ErrorFileSystem; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably a good first step, and I'm happy to merge with it like this, but 2 questions:
I'm thinking about the case where the process we're running gets SIGABRT. With this code, zig would call exit 1. Still exiting with an error, but a different way. Maybe it's fine though. I think if you usd gdb with follow-fork-child, it would still catch the abort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I'll need to look more into the small details to see if there is a way we could manage that use case. Will leave for now.