diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-02-18 21:59:43 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-02-18 21:59:43 -0500 |
| commit | d7968c6d33b054a8293edd89ce248f385e108469 (patch) | |
| tree | 6f8120d2bb3187a90ba5e855c87332386141bf63 /src/main.cpp | |
| parent | dba12cd693495009ae624393337a04e80b0ea565 (diff) | |
| download | zig-d7968c6d33b054a8293edd89ce248f385e108469.tar.gz zig-d7968c6d33b054a8293edd89ce248f385e108469.zip | |
improvements which allow zig to emit multiple things at once
example: zig build-obj test.zig -femit-llvm-ir -femit-asm
this will generate all three: test.o test.s test.ll
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/src/main.cpp b/src/main.cpp index c8c2f1b4a2..5117348e34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,17 +62,21 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " -fno-stack-check disable stack probing in safe builds\n" " -fsanitize-c enable C undefined behavior detection in unsafe builds\n" " -fno-sanitize-c disable C undefined behavior detection in safe builds\n" - " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n" + " --emit [asm|bin|llvm-ir] (deprecated) emit a specific file format as compilation output\n" " -fPIC enable Position Independent Code\n" " -fno-PIC disable Position Independent Code\n" " -ftime-report print timing diagnostics\n" " -fstack-report print stack size diagnostics\n" -#ifdef ZIG_ENABLE_MEM_PROFILE " -fmem-report print memory usage diagnostics\n" -#endif " -fdump-analysis write analysis.json file with type information\n" " -femit-docs create a docs/ dir with html documentation\n" - " -fno-emit-bin skip emitting machine code\n" + " -fno-emit-docs do not produce docs/ dir with html documentation\n" + " -femit-bin (default) output machine code\n" + " -fno-emit-bin do not output machine code\n" + " -femit-asm output .s (assembly code)\n" + " -fno-emit-asm (default) do not output .s (assembly code)\n" + " -femit-llvm-ir produce a .ll file with LLVM IR\n" + " -fno-emit-llvm-ir (default) do not produce a .ll file with LLVM IR\n" " --libc [file] Provide a file which specifies libc paths\n" " --name [name] override output name\n" " --output-dir [dir] override output directory (defaults to cwd)\n" @@ -423,7 +427,7 @@ static int main0(int argc, char **argv) { bool stack_report = false; bool enable_dump_analysis = false; bool enable_doc_generation = false; - bool disable_bin_generation = false; + bool emit_bin = true; bool emit_asm = false; bool emit_llvm_ir = false; const char *cache_dir = nullptr; @@ -553,7 +557,7 @@ static int main0(int argc, char **argv) { } Termination term; - args.items[0] = buf_ptr(&g->output_file_path); + args.items[0] = buf_ptr(&g->bin_file_output_path); os_spawn_process(args, &term); if (term.how != TerminationIdClean || term.code != 0) { fprintf(stderr, "\nBuild failed. The following command failed:\n"); @@ -632,8 +636,6 @@ static int main0(int argc, char **argv) { enable_dump_analysis = true; } else if (strcmp(arg, "-femit-docs") == 0) { enable_doc_generation = true; - } else if (strcmp(arg, "-fno-emit-bin") == 0) { - disable_bin_generation = true; } else if (strcmp(arg, "--enable-valgrind") == 0) { valgrind_support = ValgrindSupportEnabled; } else if (strcmp(arg, "--disable-valgrind") == 0) { @@ -703,9 +705,9 @@ static int main0(int argc, char **argv) { } else if (strcmp(arg, "--test-evented-io") == 0) { test_evented_io = true; } else if (strcmp(arg, "-femit-bin") == 0) { - disable_bin_generation = false; + emit_bin = true; } else if (strcmp(arg, "-fno-emit-bin") == 0) { - disable_bin_generation = true; + emit_bin = false; } else if (strcmp(arg, "-femit-asm") == 0) { emit_asm = true; } else if (strcmp(arg, "-fno-emit-asm") == 0) { @@ -746,12 +748,12 @@ static int main0(int argc, char **argv) { } else if (strcmp(arg, "--emit") == 0) { if (strcmp(argv[i], "asm") == 0) { emit_asm = true; - disable_bin_generation = true; + emit_bin = false; } else if (strcmp(argv[i], "bin") == 0) { - disable_bin_generation = false; + emit_bin = true; } else if (strcmp(argv[i], "llvm-ir") == 0) { emit_llvm_ir = true; - disable_bin_generation = true; + emit_bin = false; } else { fprintf(stderr, "--emit options are 'asm', 'bin', or 'llvm-ir'\n"); return print_error_usage(arg0); @@ -1113,8 +1115,8 @@ static int main0(int argc, char **argv) { { fprintf(stderr, "Expected source file argument.\n"); return print_error_usage(arg0); - } else if (cmd == CmdRun && disable_bin_generation) { - fprintf(stderr, "Cannot run non-executable file.\n"); + } else if (cmd == CmdRun && !emit_bin) { + fprintf(stderr, "Cannot run without emitting a binary file.\n"); return print_error_usage(arg0); } @@ -1191,7 +1193,10 @@ static int main0(int argc, char **argv) { g->enable_stack_report = stack_report; g->enable_dump_analysis = enable_dump_analysis; g->enable_doc_generation = enable_doc_generation; - g->disable_bin_generation = disable_bin_generation; + g->emit_bin = emit_bin; + g->emit_asm = emit_asm; + g->emit_llvm_ir = emit_llvm_ir; + codegen_set_out_name(g, buf_out_name); codegen_set_lib_version(g, ver_major, ver_minor, ver_patch); g->want_single_threaded = want_single_threaded; @@ -1277,9 +1282,6 @@ static int main0(int argc, char **argv) { if (cmd == CmdBuild || cmd == CmdRun) { - codegen_set_emit_asm(g, emit_asm); - codegen_set_emit_llvm_ir(g, emit_llvm_ir); - g->enable_cache = get_cache_opt(enable_cache, cmd == CmdRun); codegen_build_and_link(g); if (root_progress_node != nullptr) { @@ -1297,7 +1299,7 @@ static int main0(int argc, char **argv) { mem::print_report(); #endif - const char *exec_path = buf_ptr(&g->output_file_path); + const char *exec_path = buf_ptr(&g->bin_file_output_path); ZigList<const char*> args = {0}; args.append(exec_path); @@ -1317,21 +1319,21 @@ static int main0(int argc, char **argv) { } else if (cmd == CmdBuild) { if (g->enable_cache) { #if defined(ZIG_OS_WINDOWS) - buf_replace(&g->output_file_path, '/', '\\'); + buf_replace(&g->bin_file_output_path, '/', '\\'); #endif if (final_output_dir_step != nullptr) { Buf *dest_basename = buf_alloc(); - os_path_split(&g->output_file_path, nullptr, dest_basename); + os_path_split(&g->bin_file_output_path, nullptr, dest_basename); Buf *dest_path = buf_alloc(); os_path_join(final_output_dir_step, dest_basename, dest_path); - if ((err = os_update_file(&g->output_file_path, dest_path))) { - fprintf(stderr, "unable to copy %s to %s: %s\n", buf_ptr(&g->output_file_path), + if ((err = os_update_file(&g->bin_file_output_path, dest_path))) { + fprintf(stderr, "unable to copy %s to %s: %s\n", buf_ptr(&g->bin_file_output_path), buf_ptr(dest_path), err_str(err)); return main_exit(root_progress_node, EXIT_FAILURE); } } else { - if (printf("%s\n", buf_ptr(&g->output_file_path)) < 0) + if (printf("%s\n", buf_ptr(&g->bin_file_output_path)) < 0) return main_exit(root_progress_node, EXIT_FAILURE); } } @@ -1346,9 +1348,6 @@ static int main0(int argc, char **argv) { codegen_print_timing_report(g, stderr); return main_exit(root_progress_node, EXIT_SUCCESS); } else if (cmd == CmdTest) { - codegen_set_emit_asm(g, emit_asm); - codegen_set_emit_llvm_ir(g, emit_llvm_ir); - ZigTarget native; get_native_target(&native); @@ -1367,17 +1366,17 @@ static int main0(int argc, char **argv) { zig_print_stack_report(g, stdout); } - if (g->disable_bin_generation) { + if (!g->emit_bin) { fprintf(stderr, "Semantic analysis complete. No binary produced due to -fno-emit-bin.\n"); return main_exit(root_progress_node, EXIT_SUCCESS); } - Buf *test_exe_path_unresolved = &g->output_file_path; + Buf *test_exe_path_unresolved = &g->bin_file_output_path; Buf *test_exe_path = buf_alloc(); *test_exe_path = os_path_resolve(&test_exe_path_unresolved, 1); - if (disable_bin_generation) { - fprintf(stderr, "Created %s but skipping execution because it is non executable.\n", + if (!g->emit_bin) { + fprintf(stderr, "Created %s but skipping execution because no binary generated.\n", buf_ptr(test_exe_path)); return main_exit(root_progress_node, EXIT_SUCCESS); } |
