authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-11 00:32:40-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-11 00:32:40-04:00
log67735c6f1557092efe6e8c1712445c30655fe283
tree82eaedf770f3893192bb251bbae0db470762f230
parent5ee5933ade09c535bd1806d91cb606f49d07acea
signaturelock-open Commit is signed but in an unrecognized format.

ability to disable cache. off by default except for...

...zig run, zig build, compiler_rt.a, and builtin.a

11 files changed, 166 insertions(+), 34 deletions(-)

src/all_types.hpp+1
......@@ -1718,6 +1718,7 @@ struct CodeGen {
17181718 bool verbose_cimport;
17191719 bool error_during_imports;
17201720 bool generate_error_name_table;
1721 bool enable_cache;
17211722
17221723 //////////////////////////// Participates in Input Parameter Cache Hash
17231724 ZigList<LinkLib *> link_libs_list;
src/analyze.cpp+8
......@@ -6367,3 +6367,11 @@ not_integer:
63676367 }
63686368 return nullptr;
63696369}
6370
6371Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents) {
6372 if (g->enable_cache) {
6373 return cache_add_file_fetch(&g->cache_hash, resolved_path, contents);
6374 } else {
6375 return os_fetch_file_path(resolved_path, contents, false);
6376 }
6377}
src/analyze.hpp+3
......@@ -210,4 +210,7 @@ ZigType *get_primitive_type(CodeGen *g, Buf *name);
210210bool calling_convention_allows_zig_types(CallingConvention cc);
211211const char *calling_convention_name(CallingConvention cc);
212212
213
214Error ATTRIBUTE_MUST_USE file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents);
215
213216#endif
src/cache_hash.cpp+8
......@@ -467,3 +467,11 @@ void cache_release(CacheHash *ch) {
467467 assert(ch->manifest_file_path != nullptr);
468468 os_file_close(ch->manifest_file);
469469}
470
471Buf *get_random_basename() {
472 Buf *result = buf_alloc();
473 for (size_t i = 0; i < 16; i += 1) {
474 buf_append_char(result, base64_fs_alphabet[rand() % 64]);
475 }
476 return result;
477}
src/cache_hash.hpp+5
......@@ -67,4 +67,9 @@ Error ATTRIBUTE_MUST_USE cache_final(CacheHash *ch, Buf *out_b64_digest);
6767// Until this function is called, no one will be able to get a lock on your input params.
6868void cache_release(CacheHash *ch);
6969
70
71
72// Completely independent function. Just returns a random filename safe basename.
73Buf *get_random_basename();
74
7075#endif
src/codegen.cpp+74-29
......@@ -7048,7 +7048,7 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
70487048 *resolved_path = os_path_resolve(resolve_paths, 1);
70497049 Buf *import_code = buf_alloc();
70507050 Error err;
7051 if ((err = cache_add_file_fetch(&g->cache_hash, resolved_path, import_code))) {
7051 if ((err = file_fetch(g, resolved_path, import_code))) {
70527052 zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
70537053 }
70547054
......@@ -7480,10 +7480,7 @@ static void gen_h_file(CodeGen *g) {
74807480 GenH *gen_h = &gen_h_data;
74817481
74827482 assert(!g->is_test_build);
7483
7484 if (!g->out_h_path) {
7485 g->out_h_path = buf_sprintf("%s.h", buf_ptr(g->root_out_name));
7486 }
7483 assert(g->out_h_path != nullptr);
74877484
74887485 FILE *out_h = fopen(buf_ptr(g->out_h_path), "wb");
74897486 if (!out_h)
......@@ -7790,17 +7787,56 @@ static void resolve_out_paths(CodeGen *g) {
77907787 zig_unreachable();
77917788 }
77927789
7793 os_path_join(&g->artifact_dir, o_basename, &g->o_file_output_path);
7790 if (g->enable_cache || g->out_type != OutTypeObj) {
7791 os_path_join(&g->artifact_dir, o_basename, &g->o_file_output_path);
7792 } else {
7793 buf_init_from_buf(&g->o_file_output_path, o_basename);
7794 }
77947795
77957796 if (g->out_type == OutTypeObj) {
77967797 buf_init_from_buf(&g->output_file_path, &g->o_file_output_path);
77977798 } else if (g->out_type == OutTypeExe) {
7798 assert(g->root_out_name);
7799 if (!g->enable_cache && g->wanted_output_file_path != nullptr) {
7800 buf_init_from_buf(&g->output_file_path, g->wanted_output_file_path);
7801 } else {
7802 assert(g->root_out_name);
77997803
7800 Buf basename = BUF_INIT;
7801 buf_init_from_buf(&basename, g->root_out_name);
7802 buf_append_str(&basename, target_exe_file_ext(&g->zig_target));
7803 os_path_join(&g->artifact_dir, &basename, &g->output_file_path);
7804 Buf basename = BUF_INIT;
7805 buf_init_from_buf(&basename, g->root_out_name);
7806 buf_append_str(&basename, target_exe_file_ext(&g->zig_target));
7807 if (g->enable_cache) {
7808 os_path_join(&g->artifact_dir, &basename, &g->output_file_path);
7809 } else {
7810 buf_init_from_buf(&g->output_file_path, &basename);
7811 }
7812 }
7813 } else if (g->out_type == OutTypeLib) {
7814 if (!g->enable_cache && g->wanted_output_file_path != nullptr) {
7815 buf_init_from_buf(&g->output_file_path, g->wanted_output_file_path);
7816 } else {
7817 Buf basename = BUF_INIT;
7818 buf_init_from_buf(&basename, g->root_out_name);
7819 buf_append_str(&basename, target_lib_file_ext(&g->zig_target, g->is_static,
7820 g->version_major, g->version_minor, g->version_patch));
7821 if (g->enable_cache) {
7822 os_path_join(&g->artifact_dir, &basename, &g->output_file_path);
7823 } else {
7824 buf_init_from_buf(&g->output_file_path, &basename);
7825 }
7826 }
7827 } else {
7828 zig_unreachable();
7829 }
7830
7831 if (g->want_h_file && !g->out_h_path) {
7832 assert(g->root_out_name);
7833 Buf *h_basename = buf_sprintf("%s.h", buf_ptr(g->root_out_name));
7834 if (g->enable_cache) {
7835 g->out_h_path = buf_alloc();
7836 os_path_join(&g->artifact_dir, h_basename, g->out_h_path);
7837 } else {
7838 g->out_h_path = h_basename;
7839 }
78047840 }
78057841}
78067842
......@@ -7809,23 +7845,26 @@ void codegen_build_and_link(CodeGen *g) {
78097845 Error err;
78107846 assert(g->out_type != OutTypeUnknown);
78117847
7812 codegen_add_time_event(g, "Check Cache");
7813
78147848 Buf *stage1_dir = get_stage1_cache_path();
7849 Buf *artifact_dir = buf_alloc();
7850 Buf digest = BUF_INIT;
7851 if (g->enable_cache) {
7852 codegen_add_time_event(g, "Check Cache");
78157853
7816 Buf *manifest_dir = buf_alloc();
7817 os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir);
7854 Buf *manifest_dir = buf_alloc();
7855 os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir);
78187856
7819 Buf digest = BUF_INIT;
7820 if ((err = check_cache(g, manifest_dir, &digest))) {
7821 fprintf(stderr, "Unable to check cache: %s\n", err_str(err));
7822 exit(1);
7823 }
7857 if ((err = check_cache(g, manifest_dir, &digest))) {
7858 fprintf(stderr, "Unable to check cache: %s\n", err_str(err));
7859 exit(1);
7860 }
78247861
7825 Buf *artifact_dir = buf_alloc();
7826 os_path_join(stage1_dir, buf_create_from_str("artifact"), artifact_dir);
7862 os_path_join(stage1_dir, buf_create_from_str("artifact"), artifact_dir);
7863 } else {
7864 os_path_join(stage1_dir, buf_create_from_str("tmp"), artifact_dir);
7865 }
78277866
7828 if (buf_len(&digest) != 0) {
7867 if (g->enable_cache && buf_len(&digest) != 0) {
78297868 os_path_join(artifact_dir, &digest, &g->artifact_dir);
78307869 resolve_out_paths(g);
78317870 } else {
......@@ -7836,11 +7875,16 @@ void codegen_build_and_link(CodeGen *g) {
78367875 gen_global_asm(g);
78377876 gen_root_source(g);
78387877
7839 if ((err = cache_final(&g->cache_hash, &digest))) {
7840 fprintf(stderr, "Unable to finalize cache hash: %s\n", err_str(err));
7841 exit(1);
7878 if (g->enable_cache) {
7879 if ((err = cache_final(&g->cache_hash, &digest))) {
7880 fprintf(stderr, "Unable to finalize cache hash: %s\n", err_str(err));
7881 exit(1);
7882 }
7883 os_path_join(artifact_dir, &digest, &g->artifact_dir);
7884 } else {
7885 Buf *tmp_basename = get_random_basename();
7886 os_path_join(artifact_dir, tmp_basename, &g->artifact_dir);
78427887 }
7843 os_path_join(artifact_dir, &digest, &g->artifact_dir);
78447888 if ((err = os_make_path(&g->artifact_dir))) {
78457889 fprintf(stderr, "Unable to create artifact directory: %s\n", err_str(err));
78467890 exit(1);
......@@ -7857,9 +7901,10 @@ void codegen_build_and_link(CodeGen *g) {
78577901 codegen_link(g);
78587902 }
78597903 }
7860 // TODO hard link output_file_path to wanted_output_file_path
78617904
7862 cache_release(&g->cache_hash);
7905 if (g->enable_cache) {
7906 cache_release(&g->cache_hash);
7907 }
78637908 codegen_add_time_event(g, "Done");
78647909}
78657910
src/ir.cpp+2-2
......@@ -16277,7 +16277,7 @@ static ZigType *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructionImpor
1627716277 return ira->codegen->builtin_types.entry_namespace;
1627816278 }
1627916279
16280 if ((err = cache_add_file_fetch(&ira->codegen->cache_hash, resolved_path, import_code))) {
16280 if ((err = file_fetch(ira->codegen, resolved_path, import_code))) {
1628116281 if (err == ErrorFileNotFound) {
1628216282 ir_add_error_node(ira, source_node,
1628316283 buf_sprintf("unable to find '%s'", buf_ptr(import_target_path)));
......@@ -18108,7 +18108,7 @@ static ZigType *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstructionE
1810818108 // load from file system into const expr
1810918109 Buf *file_contents = buf_alloc();
1811018110 int err;
18111 if ((err = cache_add_file_fetch(&ira->codegen->cache_hash, &file_path, file_contents))) {
18111 if ((err = file_fetch(ira->codegen, &file_path, file_contents))) {
1811218112 if (err == ErrorFileNotFound) {
1811318113 ir_add_error(ira, instruction->name, buf_sprintf("unable to find '%s'", buf_ptr(&file_path)));
1811418114 return ira->codegen->builtin_types.entry_invalid;
src/link.cpp+1
......@@ -58,6 +58,7 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
5858 new_link_lib->provided_explicitly = link_lib->provided_explicitly;
5959 }
6060
61 child_gen->enable_cache = true;
6162 codegen_build_and_link(child_gen);
6263 return &child_gen->output_file_path;
6364}
src/main.cpp+47-3
......@@ -34,6 +34,7 @@ static int usage(const char *arg0) {
3434 "Compile Options:\n"
3535 " --assembly [source] add assembly file to build\n"
3636 " --cache-dir [path] override the cache directory\n"
37 " --cache [auto|off|on] build to the global cache and print output path to stdout\n"
3738 " --color [auto|off|on] enable or disable colored error messages\n"
3839 " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n"
3940 " --enable-timing-info print timing diagnostics\n"
......@@ -257,6 +258,24 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
257258 }
258259}
259260
261enum CacheOpt {
262 CacheOptAuto,
263 CacheOptOn,
264 CacheOptOff,
265};
266
267static bool get_cache_opt(CacheOpt opt, bool default_value) {
268 switch (opt) {
269 case CacheOptAuto:
270 return default_value;
271 case CacheOptOn:
272 return true;
273 case CacheOptOff:
274 return false;
275 }
276 zig_unreachable();
277}
278
260279int main(int argc, char **argv) {
261280 if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {
262281 printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
......@@ -301,6 +320,7 @@ int main(int argc, char **argv) {
301320 bool verbose_llvm_ir = false;
302321 bool verbose_cimport = false;
303322 ErrColor color = ErrColorAuto;
323 CacheOpt enable_cache = CacheOptAuto;
304324 const char *libc_lib_dir = nullptr;
305325 const char *libc_static_lib_dir = nullptr;
306326 const char *libc_include_dir = nullptr;
......@@ -465,6 +485,7 @@ int main(int argc, char **argv) {
465485 PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
466486 buf_ptr(&build_file_basename));
467487 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
488 g->enable_cache = get_cache_opt(enable_cache, true);
468489 codegen_build_and_link(g);
469490
470491 Termination term;
......@@ -562,6 +583,17 @@ int main(int argc, char **argv) {
562583 fprintf(stderr, "--color options are 'auto', 'on', or 'off'\n");
563584 return usage(arg0);
564585 }
586 } else if (strcmp(arg, "--cache") == 0) {
587 if (strcmp(argv[i], "auto") == 0) {
588 enable_cache = CacheOptAuto;
589 } else if (strcmp(argv[i], "on") == 0) {
590 enable_cache = CacheOptOn;
591 } else if (strcmp(argv[i], "off") == 0) {
592 enable_cache = CacheOptOff;
593 } else {
594 fprintf(stderr, "--cache options are 'auto', 'on', or 'off'\n");
595 return usage(arg0);
596 }
565597 } else if (strcmp(arg, "--emit") == 0) {
566598 if (strcmp(argv[i], "asm") == 0) {
567599 emit_file_type = EmitFileTypeAssembly;
......@@ -894,6 +926,7 @@ int main(int argc, char **argv) {
894926 if (cmd == CmdBuild || cmd == CmdRun) {
895927 codegen_set_emit_file_type(g, emit_file_type);
896928
929 g->enable_cache = get_cache_opt(enable_cache, cmd == CmdRun);
897930 codegen_build_and_link(g);
898931 if (timing_info)
899932 codegen_print_timing_report(g, stdout);
......@@ -913,9 +946,17 @@ int main(int argc, char **argv) {
913946 Termination term;
914947 os_spawn_process(exec_path, args, &term);
915948 return term.code;
949 } else if (cmd == CmdBuild) {
950 if (g->enable_cache) {
951 printf("%s\n", buf_ptr(&g->output_file_path));
952 if (g->out_h_path != nullptr) {
953 printf("%s\n", buf_ptr(g->out_h_path));
954 }
955 }
956 return EXIT_SUCCESS;
957 } else {
958 zig_unreachable();
916959 }
917
918 return EXIT_SUCCESS;
919960 } else if (cmd == CmdTranslateC) {
920961 codegen_translate_c(g, in_file_buf);
921962 ast_render(g, stdout, g->root_import->root, 4);
......@@ -928,8 +969,11 @@ int main(int argc, char **argv) {
928969 ZigTarget native;
929970 get_native_target(&native);
930971
972 g->enable_cache = get_cache_opt(enable_cache, false);
931973 codegen_build_and_link(g);
932 Buf *test_exe_path = &g->output_file_path;
974 Buf *test_exe_path_unresolved = &g->output_file_path;
975 Buf *test_exe_path = buf_alloc();
976 *test_exe_path = os_path_resolve(&test_exe_path_unresolved, 1);
933977
934978 for (size_t i = 0; i < test_exec_args.length; i += 1) {
935979 if (test_exec_args.items[i] == nullptr) {
src/target.cpp+16
......@@ -813,6 +813,22 @@ const char *target_exe_file_ext(ZigTarget *target) {
813813 }
814814}
815815
816const char *target_lib_file_ext(ZigTarget *target, bool is_static, size_t version_major, size_t version_minor, size_t version_patch) {
817 if (target->os == OsWindows) {
818 if (is_static) {
819 return ".lib";
820 } else {
821 return ".dll";
822 }
823 } else {
824 if (is_static) {
825 return ".a";
826 } else {
827 return buf_ptr(buf_sprintf(".so.%zu", version_major));
828 }
829 }
830}
831
816832enum FloatAbi {
817833 FloatAbiHard,
818834 FloatAbiSoft,
src/target.hpp+1
......@@ -113,6 +113,7 @@ const char *target_o_file_ext(ZigTarget *target);
113113const char *target_asm_file_ext(ZigTarget *target);
114114const char *target_llvm_ir_file_ext(ZigTarget *target);
115115const char *target_exe_file_ext(ZigTarget *target);
116const char *target_lib_file_ext(ZigTarget *target, bool is_static, size_t version_major, size_t version_minor, size_t version_patch);
116117
117118Buf *target_dynamic_linker(ZigTarget *target);
118119