authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-10 17:30:45-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-10 17:30:45-04:00
log5ee5933ade09c535bd1806d91cb606f49d07acea
treef53711be331f0b2249d8c008aba7b1ca1e22eea9
parent32be6e9b2a9e6de501aadbe271c554a4682a10f8
signaturelock-open Commit is signed but in an unrecognized format.

stage1 caching: zig no longer uses zig-cache


11 files changed, 187 insertions(+), 129 deletions(-)

CMakeLists.txt+1
...@@ -411,6 +411,7 @@ set(ZIG_SOURCES...@@ -411,6 +411,7 @@ set(ZIG_SOURCES
411 "${CMAKE_SOURCE_DIR}/src/c_tokenizer.cpp"411 "${CMAKE_SOURCE_DIR}/src/c_tokenizer.cpp"
412 "${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"412 "${CMAKE_SOURCE_DIR}/src/cache_hash.cpp"
413 "${CMAKE_SOURCE_DIR}/src/codegen.cpp"413 "${CMAKE_SOURCE_DIR}/src/codegen.cpp"
414 "${CMAKE_SOURCE_DIR}/src/compiler.cpp"
414 "${CMAKE_SOURCE_DIR}/src/errmsg.cpp"415 "${CMAKE_SOURCE_DIR}/src/errmsg.cpp"
415 "${CMAKE_SOURCE_DIR}/src/error.cpp"416 "${CMAKE_SOURCE_DIR}/src/error.cpp"
416 "${CMAKE_SOURCE_DIR}/src/ir.cpp"417 "${CMAKE_SOURCE_DIR}/src/ir.cpp"
src/all_types.hpp-2
...@@ -1671,7 +1671,6 @@ struct CodeGen {...@@ -1671,7 +1671,6 @@ struct CodeGen {
1671 Buf triple_str;1671 Buf triple_str;
1672 Buf global_asm;1672 Buf global_asm;
1673 Buf *out_h_path;1673 Buf *out_h_path;
1674 Buf cache_dir;
1675 Buf artifact_dir;1674 Buf artifact_dir;
1676 Buf output_file_path;1675 Buf output_file_path;
1677 Buf o_file_output_path;1676 Buf o_file_output_path;
...@@ -1731,7 +1730,6 @@ struct CodeGen {...@@ -1731,7 +1730,6 @@ struct CodeGen {
1731 ZigList<Buf *> assembly_files;1730 ZigList<Buf *> assembly_files;
1732 ZigList<const char *> lib_dirs;1731 ZigList<const char *> lib_dirs;
17331732
1734 Buf *compiler_id;
1735 size_t version_major;1733 size_t version_major;
1736 size_t version_minor;1734 size_t version_minor;
1737 size_t version_patch;1735 size_t version_patch;
src/cache_hash.cpp+7
...@@ -248,6 +248,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {...@@ -248,6 +248,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) {
248 int rc = blake2b_final(&ch->blake, bin_digest, 48);248 int rc = blake2b_final(&ch->blake, bin_digest, 48);
249 assert(rc == 0);249 assert(rc == 0);
250250
251 if (ch->files.length == 0) {
252 buf_resize(out_digest, 64);
253 base64_encode(buf_to_slice(out_digest), {bin_digest, 48});
254 return ErrorNone;
255 }
256
251 Buf b64_digest = BUF_INIT;257 Buf b64_digest = BUF_INIT;
252 buf_resize(&b64_digest, 64);258 buf_resize(&b64_digest, 64);
253 base64_encode(buf_to_slice(&b64_digest), {bin_digest, 48});259 base64_encode(buf_to_slice(&b64_digest), {bin_digest, 48});
...@@ -458,5 +464,6 @@ Error cache_final(CacheHash *ch, Buf *out_digest) {...@@ -458,5 +464,6 @@ Error cache_final(CacheHash *ch, Buf *out_digest) {
458}464}
459465
460void cache_release(CacheHash *ch) {466void cache_release(CacheHash *ch) {
467 assert(ch->manifest_file_path != nullptr);
461 os_file_close(ch->manifest_file);468 os_file_close(ch->manifest_file);
462}469}
src/codegen.cpp+75-35
...@@ -8,6 +8,7 @@...@@ -8,6 +8,7 @@
8#include "analyze.hpp"8#include "analyze.hpp"
9#include "ast_render.hpp"9#include "ast_render.hpp"
10#include "codegen.hpp"10#include "codegen.hpp"
11#include "compiler.hpp"
11#include "config.h"12#include "config.h"
12#include "errmsg.hpp"13#include "errmsg.hpp"
13#include "error.hpp"14#include "error.hpp"
...@@ -87,13 +88,12 @@ static const char *symbols_that_llvm_depends_on[] = {...@@ -87,13 +88,12 @@ static const char *symbols_that_llvm_depends_on[] = {
87};88};
8889
89CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,90CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
90 Buf *zig_lib_dir, Buf *compiler_id)91 Buf *zig_lib_dir)
91{92{
92 CodeGen *g = allocate<CodeGen>(1);93 CodeGen *g = allocate<CodeGen>(1);
9394
94 codegen_add_time_event(g, "Initialize");95 codegen_add_time_event(g, "Initialize");
9596
96 g->compiler_id = compiler_id;
97 g->zig_lib_dir = zig_lib_dir;97 g->zig_lib_dir = zig_lib_dir;
9898
99 g->zig_std_dir = buf_alloc();99 g->zig_std_dir = buf_alloc();
...@@ -243,10 +243,6 @@ void codegen_set_out_name(CodeGen *g, Buf *out_name) {...@@ -243,10 +243,6 @@ void codegen_set_out_name(CodeGen *g, Buf *out_name) {
243 g->root_out_name = out_name;243 g->root_out_name = out_name;
244}244}
245245
246void codegen_set_cache_dir(CodeGen *g, Buf cache_dir) {
247 g->cache_dir = cache_dir;
248}
249
250void codegen_set_libc_lib_dir(CodeGen *g, Buf *libc_lib_dir) {246void codegen_set_libc_lib_dir(CodeGen *g, Buf *libc_lib_dir) {
251 g->libc_lib_dir = libc_lib_dir;247 g->libc_lib_dir = libc_lib_dir;
252}248}
...@@ -5728,13 +5724,6 @@ static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *na...@@ -5728,13 +5724,6 @@ static LLVMValueRef build_alloca(CodeGen *g, ZigType *type_entry, const char *na
5728 return result;5724 return result;
5729}5725}
57305726
5731static void ensure_cache_dir(CodeGen *g) {
5732 int err;
5733 if ((err = os_make_path(&g->cache_dir))) {
5734 zig_panic("unable to make cache dir: %s", err_str(err));
5735 }
5736}
5737
5738static void report_errors_and_maybe_exit(CodeGen *g) {5727static void report_errors_and_maybe_exit(CodeGen *g) {
5739 if (g->errors.length != 0) {5728 if (g->errors.length != 0) {
5740 for (size_t i = 0; i < g->errors.length; i += 1) {5729 for (size_t i = 0; i < g->errors.length; i += 1) {
...@@ -6824,36 +6813,84 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -6824,36 +6813,84 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
6824 return contents;6813 return contents;
6825}6814}
68266815
6827static void define_builtin_compile_vars(CodeGen *g) {6816static Error define_builtin_compile_vars(CodeGen *g) {
6828 if (g->std_package == nullptr)6817 if (g->std_package == nullptr)
6829 return;6818 return ErrorNone;
6819
6820 Error err;
6821
6822 Buf *manifest_dir = buf_alloc();
6823 os_path_join(get_stage1_cache_path(), buf_create_from_str("builtin"), manifest_dir);
6824
6825 CacheHash cache_hash;
6826 cache_init(&cache_hash, manifest_dir);
6827
6828 Buf *compiler_id;
6829 if ((err = get_compiler_id(&compiler_id)))
6830 return err;
6831
6832 // Only a few things affect builtin.zig
6833 cache_buf(&cache_hash, compiler_id);
6834 cache_int(&cache_hash, g->build_mode);
6835 cache_bool(&cache_hash, g->is_test_build);
6836 cache_int(&cache_hash, g->zig_target.arch.arch);
6837 cache_int(&cache_hash, g->zig_target.arch.sub_arch);
6838 cache_int(&cache_hash, g->zig_target.vendor);
6839 cache_int(&cache_hash, g->zig_target.os);
6840 cache_int(&cache_hash, g->zig_target.env_type);
6841 cache_int(&cache_hash, g->zig_target.oformat);
6842 cache_bool(&cache_hash, g->have_err_ret_tracing);
6843 cache_bool(&cache_hash, g->libc_link_lib != nullptr);
6844
6845 Buf digest = BUF_INIT;
6846 buf_resize(&digest, 0);
6847 if ((err = cache_hit(&cache_hash, &digest)))
6848 return err;
6849
6850 // We should always get a cache hit because there are no
6851 // files in the input hash.
6852 assert(buf_len(&digest) != 0);
6853
6854 Buf *this_dir = buf_alloc();
6855 os_path_join(manifest_dir, &digest, this_dir);
6856
6857 if ((err = os_make_path(this_dir)))
6858 return err;
68306859
6831 const char *builtin_zig_basename = "builtin.zig";6860 const char *builtin_zig_basename = "builtin.zig";
6832 Buf *builtin_zig_path = buf_alloc();6861 Buf *builtin_zig_path = buf_alloc();
6833 os_path_join(&g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);6862 os_path_join(this_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
6834
6835 Buf *contents = codegen_generate_builtin_source(g);
6836 ensure_cache_dir(g);
6837 os_write_file(builtin_zig_path, contents);
68386863
6839 Buf *resolved_path = buf_alloc();6864 bool hit;
6840 Buf *resolve_paths[] = {builtin_zig_path};6865 if ((err = os_file_exists(builtin_zig_path, &hit)))
6841 *resolved_path = os_path_resolve(resolve_paths, 1);6866 return err;
6867 Buf *contents;
6868 if (hit) {
6869 contents = buf_alloc();
6870 if ((err = os_fetch_file_path(builtin_zig_path, contents, false))) {
6871 fprintf(stderr, "Unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));
6872 exit(1);
6873 }
6874 } else {
6875 contents = codegen_generate_builtin_source(g);
6876 os_write_file(builtin_zig_path, contents);
6877 }
68426878
6843 assert(g->root_package);6879 assert(g->root_package);
6844 assert(g->std_package);6880 assert(g->std_package);
6845 g->compile_var_package = new_package(buf_ptr(&g->cache_dir), builtin_zig_basename);6881 g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename);
6846 g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);6882 g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
6847 g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);6883 g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
6848 g->compile_var_import = add_source_file(g, g->compile_var_package, resolved_path, contents);6884 g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents);
6849 scan_import(g, g->compile_var_import);6885 scan_import(g, g->compile_var_import);
6886
6887 return ErrorNone;
6850}6888}
68516889
6852static void init(CodeGen *g) {6890static void init(CodeGen *g) {
6853 if (g->module)6891 if (g->module)
6854 return;6892 return;
68556893
6856
6857 if (g->llvm_argv_len > 0) {6894 if (g->llvm_argv_len > 0) {
6858 const char **args = allocate_nonzero<const char *>(g->llvm_argv_len + 2);6895 const char **args = allocate_nonzero<const char *>(g->llvm_argv_len + 2);
6859 args[0] = "zig (LLVM option parsing)";6896 args[0] = "zig (LLVM option parsing)";
...@@ -6960,7 +6997,11 @@ static void init(CodeGen *g) {...@@ -6960,7 +6997,11 @@ static void init(CodeGen *g) {
6960 g->have_err_ret_tracing = g->build_mode != BuildModeFastRelease && g->build_mode != BuildModeSmallRelease;6997 g->have_err_ret_tracing = g->build_mode != BuildModeFastRelease && g->build_mode != BuildModeSmallRelease;
69616998
6962 define_builtin_fns(g);6999 define_builtin_fns(g);
6963 define_builtin_compile_vars(g);7000 Error err;
7001 if ((err = define_builtin_compile_vars(g))) {
7002 fprintf(stderr, "Unable to create builtin.zig: %s\n", err_str(err));
7003 exit(1);
7004 }
6964}7005}
69657006
6966void codegen_translate_c(CodeGen *g, Buf *full_path) {7007void codegen_translate_c(CodeGen *g, Buf *full_path) {
...@@ -7668,6 +7709,10 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, PackageTableEntry *pkg) {...@@ -7668,6 +7709,10 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, PackageTableEntry *pkg) {
7668static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {7709static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
7669 Error err;7710 Error err;
76707711
7712 Buf *compiler_id;
7713 if ((err = get_compiler_id(&compiler_id)))
7714 return err;
7715
7671 CacheHash *ch = &g->cache_hash;7716 CacheHash *ch = &g->cache_hash;
7672 cache_init(ch, manifest_dir);7717 cache_init(ch, manifest_dir);
76737718
...@@ -7675,7 +7720,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -7675,7 +7720,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
7675 if (g->linker_script != nullptr) {7720 if (g->linker_script != nullptr) {
7676 cache_file(ch, buf_create_from_str(g->linker_script));7721 cache_file(ch, buf_create_from_str(g->linker_script));
7677 }7722 }
7678 cache_buf(ch, g->compiler_id);7723 cache_buf(ch, compiler_id);
7679 cache_buf(ch, g->root_out_name);7724 cache_buf(ch, g->root_out_name);
7680 cache_list_of_link_lib(ch, g->link_libs_list.items, g->link_libs_list.length);7725 cache_list_of_link_lib(ch, g->link_libs_list.items, g->link_libs_list.length);
7681 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);7726 cache_list_of_buf(ch, g->darwin_frameworks.items, g->darwin_frameworks.length);
...@@ -7766,13 +7811,7 @@ void codegen_build_and_link(CodeGen *g) {...@@ -7766,13 +7811,7 @@ void codegen_build_and_link(CodeGen *g) {
77667811
7767 codegen_add_time_event(g, "Check Cache");7812 codegen_add_time_event(g, "Check Cache");
77687813
7769 Buf app_data_dir = BUF_INIT;7814 Buf *stage1_dir = get_stage1_cache_path();
7770 if ((err = os_get_app_data_dir(&app_data_dir, "zig"))) {
7771 fprintf(stderr, "Unable to get app data dir: %s\n", err_str(err));
7772 exit(1);
7773 }
7774 Buf *stage1_dir = buf_alloc();
7775 os_path_join(&app_data_dir, buf_create_from_str("stage1"), stage1_dir);
77767815
7777 Buf *manifest_dir = buf_alloc();7816 Buf *manifest_dir = buf_alloc();
7778 os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir);7817 os_path_join(stage1_dir, buf_create_from_str("build"), manifest_dir);
...@@ -7820,6 +7859,7 @@ void codegen_build_and_link(CodeGen *g) {...@@ -7820,6 +7859,7 @@ void codegen_build_and_link(CodeGen *g) {
7820 }7859 }
7821 // TODO hard link output_file_path to wanted_output_file_path7860 // TODO hard link output_file_path to wanted_output_file_path
78227861
7862 cache_release(&g->cache_hash);
7823 codegen_add_time_event(g, "Done");7863 codegen_add_time_event(g, "Done");
7824}7864}
78257865
src/codegen.hpp+1-2
...@@ -15,7 +15,7 @@...@@ -15,7 +15,7 @@
15#include <stdio.h>15#include <stdio.h>
1616
17CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,17CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
18 Buf *zig_lib_dir, Buf *compiler_id);18 Buf *zig_lib_dir);
1919
20void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);20void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
21void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);21void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);
...@@ -46,7 +46,6 @@ void codegen_set_linker_script(CodeGen *g, const char *linker_script);...@@ -46,7 +46,6 @@ void codegen_set_linker_script(CodeGen *g, const char *linker_script);
46void codegen_set_test_filter(CodeGen *g, Buf *filter);46void codegen_set_test_filter(CodeGen *g, Buf *filter);
47void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix);47void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix);
48void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patch);48void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patch);
49void codegen_set_cache_dir(CodeGen *g, Buf cache_dir);
50void codegen_set_output_h_path(CodeGen *g, Buf *h_path);49void codegen_set_output_h_path(CodeGen *g, Buf *h_path);
51void codegen_set_output_path(CodeGen *g, Buf *path);50void codegen_set_output_path(CodeGen *g, Buf *path);
52void codegen_add_time_event(CodeGen *g, const char *name);51void codegen_add_time_event(CodeGen *g, const char *name);
src/compiler.cpp created+66
...@@ -0,0 +1,66 @@
1#include "cache_hash.hpp"
2
3#include <stdio.h>
4
5static Buf saved_compiler_id = BUF_INIT;
6static Buf saved_app_data_dir = BUF_INIT;
7static Buf saved_stage1_path = BUF_INIT;
8
9Buf *get_stage1_cache_path() {
10 if (saved_stage1_path.list.length != 0) {
11 return &saved_stage1_path;
12 }
13 Error err;
14 if ((err = os_get_app_data_dir(&saved_app_data_dir, "zig"))) {
15 fprintf(stderr, "Unable to get app data dir: %s\n", err_str(err));
16 exit(1);
17 }
18 os_path_join(&saved_app_data_dir, buf_create_from_str("stage1"), &saved_stage1_path);
19 return &saved_stage1_path;
20}
21
22Error get_compiler_id(Buf **result) {
23 if (saved_compiler_id.list.length != 0) {
24 *result = &saved_compiler_id;
25 return ErrorNone;
26 }
27
28 Error err;
29 Buf *stage1_dir = get_stage1_cache_path();
30 Buf *manifest_dir = buf_alloc();
31 os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
32
33 CacheHash cache_hash;
34 CacheHash *ch = &cache_hash;
35 cache_init(ch, manifest_dir);
36 Buf self_exe_path = BUF_INIT;
37 if ((err = os_self_exe_path(&self_exe_path)))
38 return err;
39
40 cache_file(ch, &self_exe_path);
41
42 buf_resize(&saved_compiler_id, 0);
43 if ((err = cache_hit(ch, &saved_compiler_id)))
44 return err;
45 if (buf_len(&saved_compiler_id) != 0) {
46 cache_release(ch);
47 *result = &saved_compiler_id;
48 return ErrorNone;
49 }
50 ZigList<Buf *> lib_paths = {};
51 if ((err = os_self_exe_shared_libs(lib_paths)))
52 return err;
53 for (size_t i = 0; i < lib_paths.length; i += 1) {
54 Buf *lib_path = lib_paths.at(i);
55 if ((err = cache_add_file(ch, lib_path)))
56 return err;
57 }
58 if ((err = cache_final(ch, &saved_compiler_id)))
59 return err;
60
61 cache_release(ch);
62
63 *result = &saved_compiler_id;
64 return ErrorNone;
65}
66
src/compiler.hpp created+17
...@@ -0,0 +1,17 @@
1/*
2 * Copyright (c) 2018 Andrew Kelley
3 *
4 * This file is part of zig, which is MIT licensed.
5 * See http://opensource.org/licenses/MIT
6 */
7
8#ifndef ZIG_COMPILER_HPP
9#define ZIG_COMPILER_HPP
10
11#include "buffer.hpp"
12#include "error.hpp"
13
14Buf *get_stage1_cache_path();
15Error get_compiler_id(Buf **result);
16
17#endif
src/link.cpp+1-3
...@@ -32,7 +32,7 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) {...@@ -32,7 +32,7 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) {
32static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {32static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {
33 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;33 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
34 CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode,34 CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode,
35 parent_gen->zig_lib_dir, parent_gen->compiler_id);35 parent_gen->zig_lib_dir);
3636
37 child_gen->want_h_file = false;37 child_gen->want_h_file = false;
38 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;38 child_gen->verbose_tokenize = parent_gen->verbose_tokenize;
...@@ -42,8 +42,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)...@@ -42,8 +42,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
42 child_gen->verbose_llvm_ir = parent_gen->verbose_llvm_ir;42 child_gen->verbose_llvm_ir = parent_gen->verbose_llvm_ir;
43 child_gen->verbose_cimport = parent_gen->verbose_cimport;43 child_gen->verbose_cimport = parent_gen->verbose_cimport;
4444
45 codegen_set_cache_dir(child_gen, parent_gen->cache_dir);
46
47 codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);45 codegen_set_strip(child_gen, parent_gen->strip_debug_symbols);
48 codegen_set_is_static(child_gen, parent_gen->is_static);46 codegen_set_is_static(child_gen, parent_gen->is_static);
4947
src/main.cpp+4-74
...@@ -8,11 +8,11 @@...@@ -8,11 +8,11 @@
8#include "ast_render.hpp"8#include "ast_render.hpp"
9#include "buffer.hpp"9#include "buffer.hpp"
10#include "codegen.hpp"10#include "codegen.hpp"
11#include "compiler.hpp"
11#include "config.h"12#include "config.h"
12#include "error.hpp"13#include "error.hpp"
13#include "os.hpp"14#include "os.hpp"
14#include "target.hpp"15#include "target.hpp"
15#include "cache_hash.hpp"
1616
17#include <stdio.h>17#include <stdio.h>
1818
...@@ -257,53 +257,6 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {...@@ -257,53 +257,6 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
257 }257 }
258}258}
259259
260static Buf saved_compiler_id = BUF_INIT;
261static Error get_compiler_id(Buf **result) {
262 if (saved_compiler_id.list.length != 0) {
263 *result = &saved_compiler_id;
264 return ErrorNone;
265 }
266
267 Error err;
268 Buf app_data_dir = BUF_INIT;
269 if ((err = os_get_app_data_dir(&app_data_dir, "zig")))
270 return err;
271 Buf *stage1_dir = buf_alloc();
272 os_path_join(&app_data_dir, buf_create_from_str("stage1"), stage1_dir);
273 Buf *manifest_dir = buf_alloc();
274 os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
275
276 CacheHash cache_hash;
277 CacheHash *ch = &cache_hash;
278 cache_init(ch, manifest_dir);
279 Buf self_exe_path = BUF_INIT;
280 if ((err = os_self_exe_path(&self_exe_path)))
281 return err;
282
283 cache_file(ch, &self_exe_path);
284
285 buf_resize(&saved_compiler_id, 0);
286 if ((err = cache_hit(ch, &saved_compiler_id)))
287 return err;
288 if (buf_len(&saved_compiler_id) != 0) {
289 *result = &saved_compiler_id;
290 return ErrorNone;
291 }
292 ZigList<Buf *> lib_paths = {};
293 if ((err = os_self_exe_shared_libs(lib_paths)))
294 return err;
295 for (size_t i = 0; i < lib_paths.length; i += 1) {
296 Buf *lib_path = lib_paths.at(i);
297 if ((err = cache_add_file(ch, lib_path)))
298 return err;
299 }
300 if ((err = cache_final(ch, &saved_compiler_id)))
301 return err;
302
303 *result = &saved_compiler_id;
304 return ErrorNone;
305}
306
307int main(int argc, char **argv) {260int main(int argc, char **argv) {
308 if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {261 if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {
309 printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",262 printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
...@@ -428,14 +381,7 @@ int main(int argc, char **argv) {...@@ -428,14 +381,7 @@ int main(int argc, char **argv) {
428 Buf *build_runner_path = buf_alloc();381 Buf *build_runner_path = buf_alloc();
429 os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path);382 os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path);
430383
431 Buf *compiler_id;384 CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf);
432 if ((err = get_compiler_id(&compiler_id))) {
433 fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err));
434 return EXIT_FAILURE;
435 }
436
437 CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf,
438 compiler_id);
439 codegen_set_out_name(g, buf_create_from_str("build"));385 codegen_set_out_name(g, buf_create_from_str("build"));
440386
441 Buf *build_file_buf = buf_create_from_str(build_file);387 Buf *build_file_buf = buf_create_from_str(build_file);
...@@ -452,8 +398,6 @@ int main(int argc, char **argv) {...@@ -452,8 +398,6 @@ int main(int argc, char **argv) {
452 full_cache_dir = os_path_resolve(&cache_dir_buf, 1);398 full_cache_dir = os_path_resolve(&cache_dir_buf, 1);
453 }399 }
454400
455 codegen_set_cache_dir(g, full_cache_dir);
456
457 args.items[1] = buf_ptr(&build_file_dirname);401 args.items[1] = buf_ptr(&build_file_dirname);
458 args.items[2] = buf_ptr(&full_cache_dir);402 args.items[2] = buf_ptr(&full_cache_dir);
459403
...@@ -795,7 +739,7 @@ int main(int argc, char **argv) {...@@ -795,7 +739,7 @@ int main(int argc, char **argv) {
795 switch (cmd) {739 switch (cmd) {
796 case CmdBuiltin: {740 case CmdBuiltin: {
797 Buf *zig_lib_dir_buf = resolve_zig_lib_dir();741 Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
798 CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf, nullptr);742 CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf);
799 Buf *builtin_source = codegen_generate_builtin_source(g);743 Buf *builtin_source = codegen_generate_builtin_source(g);
800 if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {744 if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) {
801 fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout)));745 fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout)));
...@@ -850,30 +794,16 @@ int main(int argc, char **argv) {...@@ -850,30 +794,16 @@ int main(int argc, char **argv) {
850794
851 Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf;795 Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf;
852796
853 Buf full_cache_dir = BUF_INIT;
854 if (cmd == CmdRun && buf_out_name == nullptr) {797 if (cmd == CmdRun && buf_out_name == nullptr) {
855 buf_out_name = buf_create_from_str("run");798 buf_out_name = buf_create_from_str("run");
856 }799 }
857 {
858 Buf *resolve_paths = buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir);
859 full_cache_dir = os_path_resolve(&resolve_paths, 1);
860 }
861
862 Buf *zig_lib_dir_buf = resolve_zig_lib_dir();800 Buf *zig_lib_dir_buf = resolve_zig_lib_dir();
863801
864 Buf *compiler_id;802 CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf);
865 if ((err = get_compiler_id(&compiler_id))) {
866 fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err));
867 return EXIT_FAILURE;
868 }
869
870 CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf,
871 compiler_id);
872 codegen_set_out_name(g, buf_out_name);803 codegen_set_out_name(g, buf_out_name);
873 codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);804 codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
874 codegen_set_is_test(g, cmd == CmdTest);805 codegen_set_is_test(g, cmd == CmdTest);
875 codegen_set_linker_script(g, linker_script);806 codegen_set_linker_script(g, linker_script);
876 codegen_set_cache_dir(g, full_cache_dir);
877 if (each_lib_rpath)807 if (each_lib_rpath)
878 codegen_set_each_lib_rpath(g, each_lib_rpath);808 codegen_set_each_lib_rpath(g, each_lib_rpath);
879809
src/os.cpp+12-10
...@@ -721,7 +721,7 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {...@@ -721,7 +721,7 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {
721#endif721#endif
722}722}
723723
724int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {724Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
725 static const ssize_t buf_size = 0x2000;725 static const ssize_t buf_size = 0x2000;
726 buf_resize(out_buf, buf_size);726 buf_resize(out_buf, buf_size);
727 ssize_t actual_buf_len = 0;727 ssize_t actual_buf_len = 0;
...@@ -757,7 +757,7 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {...@@ -757,7 +757,7 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
757 if (amt_read != buf_size) {757 if (amt_read != buf_size) {
758 if (feof(f)) {758 if (feof(f)) {
759 buf_resize(out_buf, actual_buf_len);759 buf_resize(out_buf, actual_buf_len);
760 return 0;760 return ErrorNone;
761 } else {761 } else {
762 return ErrorFileSystem;762 return ErrorFileSystem;
763 }763 }
...@@ -769,13 +769,13 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {...@@ -769,13 +769,13 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
769 zig_unreachable();769 zig_unreachable();
770}770}
771771
772int os_file_exists(Buf *full_path, bool *result) {772Error os_file_exists(Buf *full_path, bool *result) {
773#if defined(ZIG_OS_WINDOWS)773#if defined(ZIG_OS_WINDOWS)
774 *result = GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES;774 *result = GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES;
775 return 0;775 return ErrorNone;
776#else776#else
777 *result = access(buf_ptr(full_path), F_OK) != -1;777 *result = access(buf_ptr(full_path), F_OK) != -1;
778 return 0;778 return ErrorNone;
779#endif779#endif
780}780}
781781
...@@ -834,13 +834,15 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -834,13 +834,15 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,
834834
835 FILE *stdout_f = fdopen(stdout_pipe[0], "rb");835 FILE *stdout_f = fdopen(stdout_pipe[0], "rb");
836 FILE *stderr_f = fdopen(stderr_pipe[0], "rb");836 FILE *stderr_f = fdopen(stderr_pipe[0], "rb");
837 os_fetch_file(stdout_f, out_stdout, false);837 Error err1 = os_fetch_file(stdout_f, out_stdout, false);
838 os_fetch_file(stderr_f, out_stderr, false);838 Error err2 = os_fetch_file(stderr_f, out_stderr, false);
839839
840 fclose(stdout_f);840 fclose(stdout_f);
841 fclose(stderr_f);841 fclose(stderr_f);
842842
843 return 0;843 if (err1) return err1;
844 if (err2) return err2;
845 return ErrorNone;
844 }846 }
845}847}
846#endif848#endif
...@@ -1064,7 +1066,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) {...@@ -1064,7 +1066,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) {
1064 }1066 }
1065}1067}
10661068
1067int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {1069Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
1068 FILE *f = fopen(buf_ptr(full_path), "rb");1070 FILE *f = fopen(buf_ptr(full_path), "rb");
1069 if (!f) {1071 if (!f) {
1070 switch (errno) {1072 switch (errno) {
...@@ -1083,7 +1085,7 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {...@@ -1083,7 +1085,7 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
1083 return ErrorFileSystem;1085 return ErrorFileSystem;
1084 }1086 }
1085 }1087 }
1086 int result = os_fetch_file(f, out_contents, skip_shebang);1088 Error result = os_fetch_file(f, out_contents, skip_shebang);
1087 fclose(f);1089 fclose(f);
1088 return result;1090 return result;
1089}1091}
src/os.hpp+3-3
...@@ -111,8 +111,8 @@ void os_file_close(OsFile file);...@@ -111,8 +111,8 @@ void os_file_close(OsFile file);
111void os_write_file(Buf *full_path, Buf *contents);111void os_write_file(Buf *full_path, Buf *contents);
112int os_copy_file(Buf *src_path, Buf *dest_path);112int os_copy_file(Buf *src_path, Buf *dest_path);
113113
114int os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);114Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
115int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);115Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
116116
117int os_get_cwd(Buf *out_cwd);117int os_get_cwd(Buf *out_cwd);
118118
...@@ -122,7 +122,7 @@ void os_stderr_set_color(TermColor color);...@@ -122,7 +122,7 @@ void os_stderr_set_color(TermColor color);
122int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);122int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path);
123int os_delete_file(Buf *path);123int os_delete_file(Buf *path);
124124
125int os_file_exists(Buf *full_path, bool *result);125Error ATTRIBUTE_MUST_USE os_file_exists(Buf *full_path, bool *result);
126126
127int os_rename(Buf *src_path, Buf *dest_path);127int os_rename(Buf *src_path, Buf *dest_path);
128double os_get_time(void);128double os_get_time(void);