| author | |
| committer | |
| log | 5ee5933ade09c535bd1806d91cb606f49d07acea |
| tree | f53711be331f0b2249d8c008aba7b1ca1e22eea9 |
| parent | 32be6e9b2a9e6de501aadbe271c554a4682a10f8 |
| signature |
11 files changed, 187 insertions(+), 129 deletions(-)
CMakeLists.txt+1| ... | ... | @@ -411,6 +411,7 @@ set(ZIG_SOURCES |
| 411 | 411 | "${CMAKE_SOURCE_DIR}/src/c_tokenizer.cpp" |
| 412 | 412 | "${CMAKE_SOURCE_DIR}/src/cache_hash.cpp" |
| 413 | 413 | "${CMAKE_SOURCE_DIR}/src/codegen.cpp" |
| 414 | "${CMAKE_SOURCE_DIR}/src/compiler.cpp" | |
| 414 | 415 | "${CMAKE_SOURCE_DIR}/src/errmsg.cpp" |
| 415 | 416 | "${CMAKE_SOURCE_DIR}/src/error.cpp" |
| 416 | 417 | "${CMAKE_SOURCE_DIR}/src/ir.cpp" |
src/all_types.hpp-2| ... | ... | @@ -1671,7 +1671,6 @@ struct CodeGen { |
| 1671 | 1671 | Buf triple_str; |
| 1672 | 1672 | Buf global_asm; |
| 1673 | 1673 | Buf *out_h_path; |
| 1674 | Buf cache_dir; | |
| 1675 | 1674 | Buf artifact_dir; |
| 1676 | 1675 | Buf output_file_path; |
| 1677 | 1676 | Buf o_file_output_path; |
| ... | ... | @@ -1731,7 +1730,6 @@ struct CodeGen { |
| 1731 | 1730 | ZigList<Buf *> assembly_files; |
| 1732 | 1731 | ZigList<const char *> lib_dirs; |
| 1733 | 1732 | |
| 1734 | Buf *compiler_id; | |
| 1735 | 1733 | size_t version_major; |
| 1736 | 1734 | size_t version_minor; |
| 1737 | 1735 | size_t version_patch; |
src/cache_hash.cpp+7| ... | ... | @@ -248,6 +248,12 @@ Error cache_hit(CacheHash *ch, Buf *out_digest) { |
| 248 | 248 | int rc = blake2b_final(&ch->blake, bin_digest, 48); |
| 249 | 249 | assert(rc == 0); |
| 250 | 250 | |
| 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 | 257 | Buf b64_digest = BUF_INIT; |
| 252 | 258 | buf_resize(&b64_digest, 64); |
| 253 | 259 | base64_encode(buf_to_slice(&b64_digest), {bin_digest, 48}); |
| ... | ... | @@ -458,5 +464,6 @@ Error cache_final(CacheHash *ch, Buf *out_digest) { |
| 458 | 464 | } |
| 459 | 465 | |
| 460 | 466 | void cache_release(CacheHash *ch) { |
| 467 | assert(ch->manifest_file_path != nullptr); | |
| 461 | 468 | os_file_close(ch->manifest_file); |
| 462 | 469 | } |
src/codegen.cpp+75-35| ... | ... | @@ -8,6 +8,7 @@ |
| 8 | 8 | #include "analyze.hpp" |
| 9 | 9 | #include "ast_render.hpp" |
| 10 | 10 | #include "codegen.hpp" |
| 11 | #include "compiler.hpp" | |
| 11 | 12 | #include "config.h" |
| 12 | 13 | #include "errmsg.hpp" |
| 13 | 14 | #include "error.hpp" |
| ... | ... | @@ -87,13 +88,12 @@ static const char *symbols_that_llvm_depends_on[] = { |
| 87 | 88 | }; |
| 88 | 89 | |
| 89 | 90 | CodeGen *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 | 93 | CodeGen *g = allocate<CodeGen>(1); |
| 93 | 94 | |
| 94 | 95 | codegen_add_time_event(g, "Initialize"); |
| 95 | 96 | |
| 96 | g->compiler_id = compiler_id; | |
| 97 | 97 | g->zig_lib_dir = zig_lib_dir; |
| 98 | 98 | |
| 99 | 99 | g->zig_std_dir = buf_alloc(); |
| ... | ... | @@ -243,10 +243,6 @@ void codegen_set_out_name(CodeGen *g, Buf *out_name) { |
| 243 | 243 | g->root_out_name = out_name; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | void codegen_set_cache_dir(CodeGen *g, Buf cache_dir) { | |
| 247 | g->cache_dir = cache_dir; | |
| 248 | } | |
| 249 | ||
| 250 | 246 | void codegen_set_libc_lib_dir(CodeGen *g, Buf *libc_lib_dir) { |
| 251 | 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 | 5724 | return result; |
| 5729 | 5725 | } |
| 5730 | 5726 | |
| 5731 | static 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 | ||
| 5738 | 5727 | static void report_errors_and_maybe_exit(CodeGen *g) { |
| 5739 | 5728 | if (g->errors.length != 0) { |
| 5740 | 5729 | for (size_t i = 0; i < g->errors.length; i += 1) { |
| ... | ... | @@ -6824,36 +6813,84 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { |
| 6824 | 6813 | return contents; |
| 6825 | 6814 | } |
| 6826 | 6815 | |
| 6827 | static void define_builtin_compile_vars(CodeGen *g) { | |
| 6816 | static Error define_builtin_compile_vars(CodeGen *g) { | |
| 6828 | 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; | |
| 6830 | 6859 | |
| 6831 | 6860 | const char *builtin_zig_basename = "builtin.zig"; |
| 6832 | 6861 | Buf *builtin_zig_path = buf_alloc(); |
| 6833 | os_path_join(&g->cache_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); | |
| 6862 | os_path_join(this_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path); | |
| 6838 | 6863 | |
| 6839 | Buf *resolved_path = buf_alloc(); | |
| 6840 | Buf *resolve_paths[] = {builtin_zig_path}; | |
| 6841 | *resolved_path = os_path_resolve(resolve_paths, 1); | |
| 6864 | bool hit; | |
| 6865 | if ((err = os_file_exists(builtin_zig_path, &hit))) | |
| 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 | } | |
| 6842 | 6878 | |
| 6843 | 6879 | assert(g->root_package); |
| 6844 | 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 | 6882 | g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); |
| 6847 | 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 | 6885 | scan_import(g, g->compile_var_import); |
| 6886 | ||
| 6887 | return ErrorNone; | |
| 6850 | 6888 | } |
| 6851 | 6889 | |
| 6852 | 6890 | static void init(CodeGen *g) { |
| 6853 | 6891 | if (g->module) |
| 6854 | 6892 | return; |
| 6855 | 6893 | |
| 6856 | ||
| 6857 | 6894 | if (g->llvm_argv_len > 0) { |
| 6858 | 6895 | const char **args = allocate_nonzero<const char *>(g->llvm_argv_len + 2); |
| 6859 | 6896 | args[0] = "zig (LLVM option parsing)"; |
| ... | ... | @@ -6960,7 +6997,11 @@ static void init(CodeGen *g) { |
| 6960 | 6997 | g->have_err_ret_tracing = g->build_mode != BuildModeFastRelease && g->build_mode != BuildModeSmallRelease; |
| 6961 | 6998 | |
| 6962 | 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 | } |
| 6965 | 7006 | |
| 6966 | 7007 | void codegen_translate_c(CodeGen *g, Buf *full_path) { |
| ... | ... | @@ -7668,6 +7709,10 @@ static void add_cache_pkg(CodeGen *g, CacheHash *ch, PackageTableEntry *pkg) { |
| 7668 | 7709 | static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { |
| 7669 | 7710 | Error err; |
| 7670 | 7711 | |
| 7712 | Buf *compiler_id; | |
| 7713 | if ((err = get_compiler_id(&compiler_id))) | |
| 7714 | return err; | |
| 7715 | ||
| 7671 | 7716 | CacheHash *ch = &g->cache_hash; |
| 7672 | 7717 | cache_init(ch, manifest_dir); |
| 7673 | 7718 | |
| ... | ... | @@ -7675,7 +7720,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { |
| 7675 | 7720 | if (g->linker_script != nullptr) { |
| 7676 | 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 | 7724 | cache_buf(ch, g->root_out_name); |
| 7680 | 7725 | cache_list_of_link_lib(ch, g->link_libs_list.items, g->link_libs_list.length); |
| 7681 | 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 | 7811 | |
| 7767 | 7812 | codegen_add_time_event(g, "Check Cache"); |
| 7768 | 7813 | |
| 7769 | Buf app_data_dir = BUF_INIT; | |
| 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); | |
| 7814 | Buf *stage1_dir = get_stage1_cache_path(); | |
| 7776 | 7815 | |
| 7777 | 7816 | Buf *manifest_dir = buf_alloc(); |
| 7778 | 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 | 7859 | } |
| 7821 | 7860 | // TODO hard link output_file_path to wanted_output_file_path |
| 7822 | 7861 | |
| 7862 | cache_release(&g->cache_hash); | |
| 7823 | 7863 | codegen_add_time_event(g, "Done"); |
| 7824 | 7864 | } |
| 7825 | 7865 |
src/codegen.hpp+1-2| ... | ... | @@ -15,7 +15,7 @@ |
| 15 | 15 | #include <stdio.h> |
| 16 | 16 | |
| 17 | 17 | CodeGen *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); | |
| 19 | 19 | |
| 20 | 20 | void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len); |
| 21 | 21 | void 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 | 46 | void codegen_set_test_filter(CodeGen *g, Buf *filter); |
| 47 | 47 | void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix); |
| 48 | 48 | void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patch); |
| 49 | void codegen_set_cache_dir(CodeGen *g, Buf cache_dir); | |
| 50 | 49 | void codegen_set_output_h_path(CodeGen *g, Buf *h_path); |
| 51 | 50 | void codegen_set_output_path(CodeGen *g, Buf *path); |
| 52 | 51 | void 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 | ||
| 5 | static Buf saved_compiler_id = BUF_INIT; | |
| 6 | static Buf saved_app_data_dir = BUF_INIT; | |
| 7 | static Buf saved_stage1_path = BUF_INIT; | |
| 8 | ||
| 9 | Buf *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 | ||
| 22 | Error 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 | ||
| 14 | Buf *get_stage1_cache_path(); | |
| 15 | Error 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 | 32 | static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) { |
| 33 | 33 | ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target; |
| 34 | 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); | |
| 36 | 36 | |
| 37 | 37 | child_gen->want_h_file = false; |
| 38 | 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 | 42 | child_gen->verbose_llvm_ir = parent_gen->verbose_llvm_ir; |
| 43 | 43 | child_gen->verbose_cimport = parent_gen->verbose_cimport; |
| 44 | 44 | |
| 45 | codegen_set_cache_dir(child_gen, parent_gen->cache_dir); | |
| 46 | ||
| 47 | 45 | codegen_set_strip(child_gen, parent_gen->strip_debug_symbols); |
| 48 | 46 | codegen_set_is_static(child_gen, parent_gen->is_static); |
| 49 | 47 |
src/main.cpp+4-74| ... | ... | @@ -8,11 +8,11 @@ |
| 8 | 8 | #include "ast_render.hpp" |
| 9 | 9 | #include "buffer.hpp" |
| 10 | 10 | #include "codegen.hpp" |
| 11 | #include "compiler.hpp" | |
| 11 | 12 | #include "config.h" |
| 12 | 13 | #include "error.hpp" |
| 13 | 14 | #include "os.hpp" |
| 14 | 15 | #include "target.hpp" |
| 15 | #include "cache_hash.hpp" | |
| 16 | 16 | |
| 17 | 17 | #include <stdio.h> |
| 18 | 18 | |
| ... | ... | @@ -257,53 +257,6 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) { |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | static Buf saved_compiler_id = BUF_INIT; | |
| 261 | static 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 | ||
| 307 | 260 | int main(int argc, char **argv) { |
| 308 | 261 | if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) { |
| 309 | 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 | 381 | Buf *build_runner_path = buf_alloc(); |
| 429 | 382 | os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path); |
| 430 | 383 | |
| 431 | Buf *compiler_id; | |
| 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); | |
| 384 | CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf); | |
| 439 | 385 | codegen_set_out_name(g, buf_create_from_str("build")); |
| 440 | 386 | |
| 441 | 387 | Buf *build_file_buf = buf_create_from_str(build_file); |
| ... | ... | @@ -452,8 +398,6 @@ int main(int argc, char **argv) { |
| 452 | 398 | full_cache_dir = os_path_resolve(&cache_dir_buf, 1); |
| 453 | 399 | } |
| 454 | 400 | |
| 455 | codegen_set_cache_dir(g, full_cache_dir); | |
| 456 | ||
| 457 | 401 | args.items[1] = buf_ptr(&build_file_dirname); |
| 458 | 402 | args.items[2] = buf_ptr(&full_cache_dir); |
| 459 | 403 | |
| ... | ... | @@ -795,7 +739,7 @@ int main(int argc, char **argv) { |
| 795 | 739 | switch (cmd) { |
| 796 | 740 | case CmdBuiltin: { |
| 797 | 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 | 743 | Buf *builtin_source = codegen_generate_builtin_source(g); |
| 800 | 744 | if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) { |
| 801 | 745 | fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout))); |
| ... | ... | @@ -850,30 +794,16 @@ int main(int argc, char **argv) { |
| 850 | 794 | |
| 851 | 795 | Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf; |
| 852 | 796 | |
| 853 | Buf full_cache_dir = BUF_INIT; | |
| 854 | 797 | if (cmd == CmdRun && buf_out_name == nullptr) { |
| 855 | 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 | 800 | Buf *zig_lib_dir_buf = resolve_zig_lib_dir(); |
| 863 | 801 | |
| 864 | Buf *compiler_id; | |
| 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); | |
| 802 | CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf); | |
| 872 | 803 | codegen_set_out_name(g, buf_out_name); |
| 873 | 804 | codegen_set_lib_version(g, ver_major, ver_minor, ver_patch); |
| 874 | 805 | codegen_set_is_test(g, cmd == CmdTest); |
| 875 | 806 | codegen_set_linker_script(g, linker_script); |
| 876 | codegen_set_cache_dir(g, full_cache_dir); | |
| 877 | 807 | if (each_lib_rpath) |
| 878 | 808 | codegen_set_each_lib_rpath(g, each_lib_rpath); |
| 879 | 809 |
src/os.cpp+12-10| ... | ... | @@ -721,7 +721,7 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) { |
| 721 | 721 | #endif |
| 722 | 722 | } |
| 723 | 723 | |
| 724 | int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) { | |
| 724 | Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) { | |
| 725 | 725 | static const ssize_t buf_size = 0x2000; |
| 726 | 726 | buf_resize(out_buf, buf_size); |
| 727 | 727 | ssize_t actual_buf_len = 0; |
| ... | ... | @@ -757,7 +757,7 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) { |
| 757 | 757 | if (amt_read != buf_size) { |
| 758 | 758 | if (feof(f)) { |
| 759 | 759 | buf_resize(out_buf, actual_buf_len); |
| 760 | return 0; | |
| 760 | return ErrorNone; | |
| 761 | 761 | } else { |
| 762 | 762 | return ErrorFileSystem; |
| 763 | 763 | } |
| ... | ... | @@ -769,13 +769,13 @@ int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) { |
| 769 | 769 | zig_unreachable(); |
| 770 | 770 | } |
| 771 | 771 | |
| 772 | int os_file_exists(Buf *full_path, bool *result) { | |
| 772 | Error os_file_exists(Buf *full_path, bool *result) { | |
| 773 | 773 | #if defined(ZIG_OS_WINDOWS) |
| 774 | 774 | *result = GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES; |
| 775 | return 0; | |
| 775 | return ErrorNone; | |
| 776 | 776 | #else |
| 777 | 777 | *result = access(buf_ptr(full_path), F_OK) != -1; |
| 778 | return 0; | |
| 778 | return ErrorNone; | |
| 779 | 779 | #endif |
| 780 | 780 | } |
| 781 | 781 | |
| ... | ... | @@ -834,13 +834,15 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args, |
| 834 | 834 | |
| 835 | 835 | FILE *stdout_f = fdopen(stdout_pipe[0], "rb"); |
| 836 | 836 | FILE *stderr_f = fdopen(stderr_pipe[0], "rb"); |
| 837 | os_fetch_file(stdout_f, out_stdout, false); | |
| 838 | os_fetch_file(stderr_f, out_stderr, false); | |
| 837 | Error err1 = os_fetch_file(stdout_f, out_stdout, false); | |
| 838 | Error err2 = os_fetch_file(stderr_f, out_stderr, false); | |
| 839 | 839 | |
| 840 | 840 | fclose(stdout_f); |
| 841 | 841 | fclose(stderr_f); |
| 842 | 842 | |
| 843 | return 0; | |
| 843 | if (err1) return err1; | |
| 844 | if (err2) return err2; | |
| 845 | return ErrorNone; | |
| 844 | 846 | } |
| 845 | 847 | } |
| 846 | 848 | #endif |
| ... | ... | @@ -1064,7 +1066,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) { |
| 1064 | 1066 | } |
| 1065 | 1067 | } |
| 1066 | 1068 | |
| 1067 | int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) { | |
| 1069 | Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) { | |
| 1068 | 1070 | FILE *f = fopen(buf_ptr(full_path), "rb"); |
| 1069 | 1071 | if (!f) { |
| 1070 | 1072 | switch (errno) { |
| ... | ... | @@ -1083,7 +1085,7 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) { |
| 1083 | 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 | 1089 | fclose(f); |
| 1088 | 1090 | return result; |
| 1089 | 1091 | } |
src/os.hpp+3-3| ... | ... | @@ -111,8 +111,8 @@ void os_file_close(OsFile file); |
| 111 | 111 | void os_write_file(Buf *full_path, Buf *contents); |
| 112 | 112 | int os_copy_file(Buf *src_path, Buf *dest_path); |
| 113 | 113 | |
| 114 | int os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang); | |
| 115 | int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang); | |
| 114 | Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang); | |
| 115 | Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang); | |
| 116 | 116 | |
| 117 | 117 | int os_get_cwd(Buf *out_cwd); |
| 118 | 118 | |
| ... | ... | @@ -122,7 +122,7 @@ void os_stderr_set_color(TermColor color); |
| 122 | 122 | int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path); |
| 123 | 123 | int os_delete_file(Buf *path); |
| 124 | 124 | |
| 125 | int os_file_exists(Buf *full_path, bool *result); | |
| 125 | Error ATTRIBUTE_MUST_USE os_file_exists(Buf *full_path, bool *result); | |
| 126 | 126 | |
| 127 | 127 | int os_rename(Buf *src_path, Buf *dest_path); |
| 128 | 128 | double os_get_time(void); |