authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-05 00:34:46-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-05 23:24:40-04:00
log97c9f61db47ddba43a0db562e13773514875fa6a
tree02bfcaf5f43533478ffa0329588dcd182f1dc0a3
parent2d4b95900e7d7273dbd3ce6b7b9a33d2a19779da
signaturelock-open Commit is signed but in an unrecognized format.

start creating a hash of input parameters

See #1416

5 files changed, 179 insertions(+), 30 deletions(-)

src/all_types.hpp+17-13
......@@ -1543,23 +1543,17 @@ struct LinkLib {
15431543 bool provided_explicitly;
15441544};
15451545
1546// When adding fields, check if they should be added to the hash computation in build_with_cache
15461547struct CodeGen {
1548 //////////////////////////// Runtime State
15471549 LLVMModuleRef module;
15481550 ZigList<ErrorMsg*> errors;
15491551 LLVMBuilderRef builder;
15501552 ZigLLVMDIBuilder *dbuilder;
15511553 ZigLLVMDICompileUnit *compile_unit;
15521554 ZigLLVMDIFile *compile_unit_file;
1553
1554 ZigList<LinkLib *> link_libs_list;
15551555 LinkLib *libc_link_lib;
15561556
1557 // add -framework [name] args to linker
1558 ZigList<Buf *> darwin_frameworks;
1559 // add -rpath [name] args to linker
1560 ZigList<Buf *> rpath_list;
1561
1562
15631557 // reminder: hash tables must be initialized before use
15641558 HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;
15651559 HashMap<Buf *, BuiltinFnEntry *, buf_hash, buf_eql_buf> builtin_fn_table;
......@@ -1575,7 +1569,6 @@ struct CodeGen {
15751569 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
15761570 HashMap<const ZigType *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
15771571
1578
15791572 ZigList<ImportTableEntry *> import_queue;
15801573 size_t import_queue_index;
15811574 ZigList<Tld *> resolve_queue;
......@@ -1620,7 +1613,20 @@ struct CodeGen {
16201613 ZigType *entry_promise;
16211614 } builtin_types;
16221615
1616 //////////////////////////// Participates in Input Parameter Cache Hash
1617 ZigList<LinkLib *> link_libs_list;
1618 // add -framework [name] args to linker
1619 ZigList<Buf *> darwin_frameworks;
1620 // add -rpath [name] args to linker
1621 ZigList<Buf *> rpath_list;
1622
16231623 EmitFileType emit_file_type;
1624 BuildMode build_mode;
1625 OutType out_type;
1626
1627
1628 //////////////////////////// Unsorted
1629
16241630 ZigTarget zig_target;
16251631 LLVMTargetDataRef target_data_ref;
16261632 unsigned pointer_size_bytes;
......@@ -1647,7 +1653,6 @@ struct CodeGen {
16471653 Buf *ar_path;
16481654 ZigWindowsSDK *win_sdk;
16491655 Buf triple_str;
1650 BuildMode build_mode;
16511656 bool is_test_build;
16521657 bool have_err_ret_tracing;
16531658 uint32_t target_os_index;
......@@ -1657,13 +1662,13 @@ struct CodeGen {
16571662 LLVMTargetMachineRef target_machine;
16581663 ZigLLVMDIFile *dummy_di_file;
16591664 bool is_native_target;
1660 PackageTableEntry *root_package;
1665 PackageTableEntry *root_package; // participates in cache hash
16611666 PackageTableEntry *std_package;
16621667 PackageTableEntry *panic_package;
16631668 PackageTableEntry *test_runner_package;
16641669 PackageTableEntry *compile_var_package;
16651670 ImportTableEntry *compile_var_import;
1666 Buf *root_out_name;
1671 Buf *root_out_name; // participates in cache hash
16671672 bool windows_subsystem_windows;
16681673 bool windows_subsystem_console;
16691674 Buf *mmacosx_version_min;
......@@ -1676,7 +1681,6 @@ struct CodeGen {
16761681 size_t fn_defs_index;
16771682 ZigList<TldVar *> global_vars;
16781683
1679 OutType out_type;
16801684 ZigFn *cur_fn;
16811685 ZigFn *main_fn;
16821686 ZigFn *panic_fn;
src/codegen.cpp+162-13
......@@ -19,6 +19,7 @@
1919#include "target.hpp"
2020#include "util.hpp"
2121#include "zig_llvm.h"
22#include "blake2.h"
2223
2324#include <stdio.h>
2425#include <errno.h>
......@@ -183,10 +184,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
183184 return g;
184185}
185186
186void codegen_destroy(CodeGen *codegen) {
187 LLVMDisposeTargetMachine(codegen->target_machine);
188}
189
190187void codegen_set_output_h_path(CodeGen *g, Buf *h_path) {
191188 g->out_h_path = h_path;
192189}
......@@ -7112,23 +7109,30 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
71127109 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");
71137110}
71147111
7115static void gen_root_source(CodeGen *g) {
7112static Buf *get_resolved_root_src_path(CodeGen *g) {
7113 // TODO memoize
71167114 if (buf_len(&g->root_package->root_src_path) == 0)
7117 return;
7118
7119 codegen_add_time_event(g, "Semantic Analysis");
7115 return nullptr;
71207116
7121 Buf *rel_full_path = buf_alloc();
7122 os_path_join(&g->root_package->root_src_dir, &g->root_package->root_src_path, rel_full_path);
7117 Buf rel_full_path = BUF_INIT;
7118 os_path_join(&g->root_package->root_src_dir, &g->root_package->root_src_path, &rel_full_path);
71237119
71247120 Buf *resolved_path = buf_alloc();
7125 Buf *resolve_paths[] = {rel_full_path};
7121 Buf *resolve_paths[] = {&rel_full_path};
71267122 *resolved_path = os_path_resolve(resolve_paths, 1);
71277123
7124 return resolved_path;
7125}
7126
7127static void gen_root_source(CodeGen *g) {
7128 Buf *resolved_path = get_resolved_root_src_path(g);
7129 if (resolved_path == nullptr)
7130 return;
7131
71287132 Buf *source_code = buf_alloc();
71297133 int err;
7130 if ((err = os_fetch_file_path(rel_full_path, source_code, true))) {
7131 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err));
7134 if ((err = os_fetch_file_path(resolved_path, source_code, true))) {
7135 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(resolved_path), err_str(err));
71327136 exit(1);
71337137 }
71347138
......@@ -7671,10 +7675,155 @@ void codegen_add_time_event(CodeGen *g, const char *name) {
76717675 g->timing_events.append({os_get_time(), name});
76727676}
76737677
7678static void add_cache_str(blake2b_state *blake, const char *ptr) {
7679 assert(ptr != nullptr);
7680 // + 1 to include the null byte
7681 blake2b_update(blake, ptr, strlen(ptr) + 1);
7682}
7683
7684static void add_cache_int(blake2b_state *blake, int x) {
7685 // + 1 to include the null byte
7686 uint8_t buf[sizeof(int) + 1];
7687 memcpy(buf, &x, sizeof(int));
7688 buf[sizeof(int)] = 0;
7689 blake2b_update(blake, buf, sizeof(int) + 1);
7690}
7691
7692static void add_cache_buf(blake2b_state *blake, Buf *buf) {
7693 assert(buf != nullptr);
7694 // + 1 to include the null byte
7695 blake2b_update(blake, buf_ptr(buf), buf_len(buf) + 1);
7696}
7697
7698static void add_cache_buf_opt(blake2b_state *blake, Buf *buf) {
7699 if (buf == nullptr) {
7700 add_cache_str(blake, "");
7701 add_cache_str(blake, "");
7702 } else {
7703 add_cache_buf(blake, buf);
7704 }
7705}
7706
7707static void add_cache_list_of_link_lib(blake2b_state *blake, LinkLib **ptr, size_t len) {
7708 for (size_t i = 0; i < len; i += 1) {
7709 LinkLib *lib = ptr[i];
7710 if (lib->provided_explicitly) {
7711 add_cache_buf(blake, lib->name);
7712 }
7713 }
7714 add_cache_str(blake, "");
7715}
7716
7717static void add_cache_list_of_buf(blake2b_state *blake, Buf **ptr, size_t len) {
7718 for (size_t i = 0; i < len; i += 1) {
7719 Buf *buf = ptr[i];
7720 add_cache_buf(blake, buf);
7721 }
7722 add_cache_str(blake, "");
7723}
7724
7725//static void add_cache_file(CodeGen *g, blake2b_state *blake, Buf *resolved_path) {
7726// assert(file_name != nullptr);
7727// g->cache_files.append(resolved_path);
7728//}
7729//
7730//static void add_cache_file_opt(CodeGen *g, blake2b_state *blake, Buf *resolved_path) {
7731// if (resolved_path == nullptr) {
7732// add_cache_str(blake, "");
7733// add_cache_str(blake, "");
7734// } else {
7735// add_cache_file(g, blake, resolved_path);
7736// }
7737//}
7738
7739// Ported from std/base64.zig
7740static uint8_t base64_fs_alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
7741static void base64_encode(Slice<uint8_t> dest, Slice<uint8_t> source) {
7742 size_t dest_len = ((source.len + 2) / 3) * 4;
7743 assert(dest.len == dest_len);
7744
7745 size_t i = 0;
7746 size_t out_index = 0;
7747 for (; i + 2 < source.len; i += 3) {
7748 dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i] >> 2) & 0x3f];
7749 out_index += 1;
7750
7751 dest.ptr[out_index] = base64_fs_alphabet[((source.ptr[i] & 0x3) << 4) | ((source.ptr[i + 1] & 0xf0) >> 4)];
7752 out_index += 1;
7753
7754 dest.ptr[out_index] = base64_fs_alphabet[((source.ptr[i + 1] & 0xf) << 2) | ((source.ptr[i + 2] & 0xc0) >> 6)];
7755 out_index += 1;
7756
7757 dest.ptr[out_index] = base64_fs_alphabet[source.ptr[i + 2] & 0x3f];
7758 out_index += 1;
7759 }
7760
7761 // Assert that we never need pad characters.
7762 assert(i == source.len);
7763 //if (i < source.len) {
7764 // dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i] >> 2) & 0x3f];
7765 // out_index += 1;
7766
7767 // if (i + 1 == source.len) {
7768 // dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i] & 0x3) << 4];
7769 // out_index += 1;
7770
7771 // dest.ptr[out_index] = encoder.pad_char;
7772 // out_index += 1;
7773 // } else {
7774 // dest.ptr[out_index] = base64_fs_alphabet[((source.ptr[i] & 0x3) << 4) | ((source.ptr[i + 1] & 0xf0) >> 4)];
7775 // out_index += 1;
7776
7777 // dest.ptr[out_index] = base64_fs_alphabet[(source.ptr[i + 1] & 0xf) << 2];
7778 // out_index += 1;
7779 // }
7780
7781 // dest.ptr[out_index] = encoder.pad_char;
7782 // out_index += 1;
7783 //}
7784}
7785
7786// Called before init()
7787static bool build_with_cache(CodeGen *g) {
7788 blake2b_state blake;
7789 int rc = blake2b_init(&blake, 48);
7790 assert(rc == 0);
7791
7792 // TODO zig exe & dynamic libraries
7793
7794 add_cache_buf(&blake, g->root_out_name);
7795 add_cache_buf_opt(&blake, get_resolved_root_src_path(g)); // Root source file
7796 add_cache_list_of_link_lib(&blake, g->link_libs_list.items, g->link_libs_list.length);
7797 add_cache_list_of_buf(&blake, g->darwin_frameworks.items, g->darwin_frameworks.length);
7798 add_cache_list_of_buf(&blake, g->rpath_list.items, g->rpath_list.length);
7799 add_cache_int(&blake, g->emit_file_type);
7800 add_cache_int(&blake, g->build_mode);
7801 add_cache_int(&blake, g->out_type);
7802 // TODO the rest of the struct CodeGen fields
7803
7804 uint8_t bin_digest[48];
7805 rc = blake2b_final(&blake, bin_digest, 48);
7806 assert(rc == 0);
7807
7808 Buf b64_digest = BUF_INIT;
7809 buf_resize(&b64_digest, 64);
7810 base64_encode(buf_to_slice(&b64_digest), {bin_digest, 48});
7811
7812 fprintf(stderr, "input params hash: %s\n", buf_ptr(&b64_digest));
7813 // TODO next look for a manifest file that has all the files from the input parameters
7814 // use that to construct the real hash, which looks up the output directory and another manifest file
7815
7816 return false;
7817}
7818
76747819void codegen_build(CodeGen *g) {
76757820 assert(g->out_type != OutTypeUnknown);
7821 if (build_with_cache(g))
7822 return;
76767823 init(g);
76777824
7825 codegen_add_time_event(g, "Semantic Analysis");
7826
76787827 gen_global_asm(g);
76797828 gen_root_source(g);
76807829 do_code_gen(g);
src/codegen.hpp-1
......@@ -16,7 +16,6 @@
1616
1717CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
1818 Buf *zig_lib_dir);
19void codegen_destroy(CodeGen *codegen);
2019
2120void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
2221void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);
src/link.cpp-2
......@@ -69,8 +69,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
6969 os_path_join(&parent_gen->cache_dir, o_out_name, output_path);
7070 codegen_link(child_gen, buf_ptr(output_path));
7171
72 codegen_destroy(child_gen);
73
7472 return output_path;
7573}
7674
src/main.cpp-1
......@@ -461,7 +461,6 @@ int main(int argc, char **argv) {
461461 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
462462 codegen_build(g);
463463 codegen_link(g, buf_ptr(path_to_build_exe));
464 codegen_destroy(g);
465464
466465 Termination term;
467466 os_spawn_process(buf_ptr(path_to_build_exe), args, &term);