Skip to content
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

Merged
merged 1 commit into from
Apr 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6289,7 +6289,7 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err));
}
Buf *import_code = buf_alloc();
if ((err = os_fetch_file_path(abs_full_path, import_code))) {
if ((err = os_fetch_file_path(abs_full_path, import_code, false))) {
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err));
}

Expand Down Expand Up @@ -6377,7 +6377,7 @@ static void gen_root_source(CodeGen *g) {
}

Buf *source_code = buf_alloc();
if ((err = os_fetch_file_path(rel_full_path, source_code))) {
if ((err = os_fetch_file_path(rel_full_path, source_code, true))) {
zig_panic("unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err));
}

Expand Down Expand Up @@ -6442,7 +6442,7 @@ static void gen_global_asm(CodeGen *g) {
int err;
for (size_t i = 0; i < g->assembly_files.length; i += 1) {
Buf *asm_file = g->assembly_files.at(i);
if ((err = os_fetch_file_path(asm_file, &contents))) {
if ((err = os_fetch_file_path(asm_file, &contents, false))) {
zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));
}
buf_append_buf(&g->global_asm, &contents);
Expand Down
4 changes: 2 additions & 2 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14709,7 +14709,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
return ira->codegen->builtin_types.entry_namespace;
}

if ((err = os_fetch_file_path(abs_full_path, import_code))) {
if ((err = os_fetch_file_path(abs_full_path, import_code, true))) {
if (err == ErrorFileNotFound) {
ir_add_error_node(ira, source_node,
buf_sprintf("unable to find '%s'", buf_ptr(import_target_path)));
Expand Down Expand Up @@ -15570,7 +15570,7 @@ static TypeTableEntry *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstr
// load from file system into const expr
Buf *file_contents = buf_alloc();
int err;
if ((err = os_fetch_file_path(&file_path, file_contents))) {
if ((err = os_fetch_file_path(&file_path, file_contents, false))) {
if (err == ErrorFileNotFound) {
ir_add_error(ira, instruction->name, buf_sprintf("unable to find '%s'", buf_ptr(&file_path)));
return ira->codegen->builtin_types.entry_invalid;
Expand Down
58 changes: 50 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static int usage(const char *arg0) {
" build-exe [source] create executable from source or object files\n"
" build-lib [source] create library from source or object files\n"
" build-obj [source] create object from source or assembly\n"
" run [source] create executable and run immediately\n"
" translate-c [source] convert c code to zig code\n"
" targets list available compilation targets\n"
" test [source] create and run a test build\n"
Expand Down Expand Up @@ -227,6 +228,7 @@ static Buf *resolve_zig_lib_dir(const char *zig_install_prefix_arg) {
enum Cmd {
CmdInvalid,
CmdBuild,
CmdRun,
CmdTest,
CmdVersion,
CmdZen,
Expand Down Expand Up @@ -336,6 +338,8 @@ int main(int argc, char **argv) {
CliPkg *cur_pkg = allocate<CliPkg>(1);
BuildMode build_mode = BuildModeDebug;
ZigList<const char *> test_exec_args = {0};
int comptime_args_end = 0;
int runtime_args_start = argc;

if (argc >= 2 && strcmp(argv[1], "build") == 0) {
const char *zig_exe_path = arg0;
Expand Down Expand Up @@ -488,11 +492,15 @@ int main(int argc, char **argv) {
return (term.how == TerminationIdClean) ? term.code : -1;
}

for (int i = 1; i < argc; i += 1) {
for (int i = 1; i < argc; i += 1, comptime_args_end += 1) {
char *arg = argv[i];

if (arg[0] == '-') {
if (strcmp(arg, "--release-fast") == 0) {
if (strcmp(arg, "--") == 0) {
// ignore -- from both compile and runtime arg sets
runtime_args_start = i + 1;
break;
} else if (strcmp(arg, "--release-fast") == 0) {
build_mode = BuildModeFastRelease;
} else if (strcmp(arg, "--release-safe") == 0) {
build_mode = BuildModeSafeRelease;
Expand Down Expand Up @@ -659,6 +667,9 @@ int main(int argc, char **argv) {
} else if (strcmp(arg, "build-lib") == 0) {
cmd = CmdBuild;
out_type = OutTypeLib;
} else if (strcmp(arg, "run") == 0) {
cmd = CmdRun;
out_type = OutTypeExe;
} else if (strcmp(arg, "version") == 0) {
cmd = CmdVersion;
} else if (strcmp(arg, "zen") == 0) {
Expand All @@ -677,6 +688,7 @@ int main(int argc, char **argv) {
} else {
switch (cmd) {
case CmdBuild:
case CmdRun:
case CmdTranslateC:
case CmdTest:
if (!in_file) {
Expand Down Expand Up @@ -731,16 +743,16 @@ int main(int argc, char **argv) {
}
}


switch (cmd) {
case CmdRun:
case CmdBuild:
case CmdTranslateC:
case CmdTest:
{
if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0) {
fprintf(stderr, "Expected source file argument or at least one --object or --assembly argument.\n");
return usage(arg0);
} else if ((cmd == CmdTranslateC || cmd == CmdTest) && !in_file) {
} else if ((cmd == CmdTranslateC || cmd == CmdTest || cmd == CmdRun) && !in_file) {
fprintf(stderr, "Expected source file argument.\n");
return usage(arg0);
} else if (cmd == CmdBuild && out_type == OutTypeObj && objects.length != 0) {
Expand All @@ -752,6 +764,10 @@ int main(int argc, char **argv) {

bool need_name = (cmd == CmdBuild || cmd == CmdTranslateC);

if (cmd == CmdRun) {
out_name = "run";
}

Buf *in_file_buf = nullptr;

Buf *buf_out_name = (cmd == CmdTest) ? buf_create_from_str("test") :
Expand All @@ -776,9 +792,23 @@ int main(int argc, char **argv) {
Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf;

Buf *full_cache_dir = buf_alloc();
os_path_resolve(buf_create_from_str("."),
buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),
full_cache_dir);
Buf *run_exec_path = buf_alloc();
if (cmd == CmdRun) {
if (buf_out_name == nullptr) {
buf_out_name = buf_create_from_str("run");
}

Buf *global_cache_dir = buf_alloc();
os_get_global_cache_directory(global_cache_dir);
os_path_join(global_cache_dir, buf_out_name, run_exec_path);
os_path_resolve(buf_create_from_str("."), global_cache_dir, full_cache_dir);

out_file = buf_ptr(run_exec_path);
} else {
os_path_resolve(buf_create_from_str("."),
buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),
full_cache_dir);
}

Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix);

Expand Down Expand Up @@ -862,7 +892,7 @@ int main(int argc, char **argv) {

add_package(g, cur_pkg, g->root_package);

if (cmd == CmdBuild) {
if (cmd == CmdBuild || cmd == CmdRun) {
codegen_set_emit_file_type(g, emit_file_type);

for (size_t i = 0; i < objects.length; i += 1) {
Expand All @@ -875,6 +905,18 @@ int main(int argc, char **argv) {
codegen_link(g, out_file);
if (timing_info)
codegen_print_timing_report(g, stdout);

if (cmd == CmdRun) {
ZigList<const char*> args = {0};
for (int i = runtime_args_start; i < argc; ++i) {
args.append(argv[i]);
}

Termination term;
os_spawn_process(buf_ptr(run_exec_path), args, &term);
Copy link
Member

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:

  • Do you think we should use execve? Completely replace the current process with the new one?
  • If we did this, what about Windows?

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.

Copy link
Member Author

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.

return term.code;
}

return EXIT_SUCCESS;
} else if (cmd == CmdTranslateC) {
codegen_translate_c(g, in_file_buf);
Expand Down
75 changes: 70 additions & 5 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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 os.cpp. I've just opted for a variable to specify if we should skip the shebang and adjusted uses accordingly.

Copy link
Member

Choose a reason for hiding this comment

The 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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ int os_path_real(Buf *rel_path, Buf *out_abs_path);
void os_path_resolve(Buf *ref_path, Buf *target_path, Buf *out_abs_path);
bool os_path_is_absolute(Buf *path);

int os_get_global_cache_directory(Buf *out_tmp_path);

int os_make_path(Buf *path);
int os_make_dir(Buf *path);

void os_write_file(Buf *full_path, Buf *contents);
int os_copy_file(Buf *src_path, Buf *dest_path);

int os_fetch_file(FILE *file, Buf *out_contents);
int os_fetch_file_path(Buf *full_path, Buf *out_contents);
int os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);

int os_get_cwd(Buf *out_cwd);

Expand Down