| ... | ... | @@ -89,126 +89,6 @@ static const char *symbols_that_llvm_depends_on[] = { |
| 89 | 89 | // TODO probably all of compiler-rt needs to go here |
| 90 | 90 | }; |
| 91 | 91 | |
| 92 | | CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, |
| 93 | | OutType out_type, BuildMode build_mode, Buf *override_lib_dir, Buf *override_std_dir, |
| 94 | | ZigLibCInstallation *libc, Buf *cache_dir) |
| 95 | | { |
| 96 | | CodeGen *g = allocate<CodeGen>(1); |
| 97 | | |
| 98 | | codegen_add_time_event(g, "Initialize"); |
| 99 | | |
| 100 | | g->subsystem = TargetSubsystemAuto; |
| 101 | | g->libc = libc; |
| 102 | | g->zig_target = target; |
| 103 | | g->cache_dir = cache_dir; |
| 104 | | |
| 105 | | if (override_lib_dir == nullptr) { |
| 106 | | g->zig_lib_dir = get_zig_lib_dir(); |
| 107 | | } else { |
| 108 | | g->zig_lib_dir = override_lib_dir; |
| 109 | | } |
| 110 | | |
| 111 | | if (override_std_dir == nullptr) { |
| 112 | | g->zig_std_dir = buf_alloc(); |
| 113 | | os_path_join(g->zig_lib_dir, buf_create_from_str("std"), g->zig_std_dir); |
| 114 | | } else { |
| 115 | | g->zig_std_dir = override_std_dir; |
| 116 | | } |
| 117 | | |
| 118 | | g->zig_c_headers_dir = buf_alloc(); |
| 119 | | os_path_join(g->zig_lib_dir, buf_create_from_str("include"), g->zig_c_headers_dir); |
| 120 | | |
| 121 | | g->build_mode = build_mode; |
| 122 | | g->out_type = out_type; |
| 123 | | g->import_table.init(32); |
| 124 | | g->builtin_fn_table.init(32); |
| 125 | | g->primitive_type_table.init(32); |
| 126 | | g->type_table.init(32); |
| 127 | | g->fn_type_table.init(32); |
| 128 | | g->error_table.init(16); |
| 129 | | g->generic_table.init(16); |
| 130 | | g->llvm_fn_table.init(16); |
| 131 | | g->memoized_fn_eval_table.init(16); |
| 132 | | g->exported_symbol_names.init(8); |
| 133 | | g->external_prototypes.init(8); |
| 134 | | g->string_literals_table.init(16); |
| 135 | | g->type_info_cache.init(32); |
| 136 | | g->is_test_build = false; |
| 137 | | g->is_single_threaded = false; |
| 138 | | buf_resize(&g->global_asm, 0); |
| 139 | | |
| 140 | | for (size_t i = 0; i < array_length(symbols_that_llvm_depends_on); i += 1) { |
| 141 | | g->external_prototypes.put(buf_create_from_str(symbols_that_llvm_depends_on[i]), nullptr); |
| 142 | | } |
| 143 | | |
| 144 | | if (root_src_path) { |
| 145 | | Buf *root_pkg_path; |
| 146 | | Buf *rel_root_src_path; |
| 147 | | if (main_pkg_path == nullptr) { |
| 148 | | Buf *src_basename = buf_alloc(); |
| 149 | | Buf *src_dir = buf_alloc(); |
| 150 | | os_path_split(root_src_path, src_dir, src_basename); |
| 151 | | |
| 152 | | if (buf_len(src_basename) == 0) { |
| 153 | | fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path)); |
| 154 | | exit(1); |
| 155 | | } |
| 156 | | root_pkg_path = src_dir; |
| 157 | | rel_root_src_path = src_basename; |
| 158 | | } else { |
| 159 | | Buf resolved_root_src_path = os_path_resolve(&root_src_path, 1); |
| 160 | | Buf resolved_main_pkg_path = os_path_resolve(&main_pkg_path, 1); |
| 161 | | |
| 162 | | if (!buf_starts_with_buf(&resolved_root_src_path, &resolved_main_pkg_path)) { |
| 163 | | fprintf(stderr, "Root source path '%s' outside main package path '%s'", |
| 164 | | buf_ptr(root_src_path), buf_ptr(main_pkg_path)); |
| 165 | | exit(1); |
| 166 | | } |
| 167 | | root_pkg_path = main_pkg_path; |
| 168 | | rel_root_src_path = buf_create_from_mem( |
| 169 | | buf_ptr(&resolved_root_src_path) + buf_len(&resolved_main_pkg_path) + 1, |
| 170 | | buf_len(&resolved_root_src_path) - buf_len(&resolved_main_pkg_path) - 1); |
| 171 | | } |
| 172 | | |
| 173 | | g->root_package = new_package(buf_ptr(root_pkg_path), buf_ptr(rel_root_src_path), ""); |
| 174 | | g->std_package = new_package(buf_ptr(g->zig_std_dir), "std.zig", "std"); |
| 175 | | g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); |
| 176 | | } else { |
| 177 | | g->root_package = new_package(".", "", ""); |
| 178 | | } |
| 179 | | |
| 180 | | g->root_package->package_table.put(buf_create_from_str("root"), g->root_package); |
| 181 | | |
| 182 | | g->zig_std_special_dir = buf_alloc(); |
| 183 | | os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir); |
| 184 | | |
| 185 | | assert(target != nullptr); |
| 186 | | if (!target->is_native) { |
| 187 | | g->each_lib_rpath = false; |
| 188 | | } else { |
| 189 | | g->each_lib_rpath = true; |
| 190 | | |
| 191 | | if (target_os_is_darwin(g->zig_target->os)) { |
| 192 | | init_darwin_native(g); |
| 193 | | } |
| 194 | | |
| 195 | | } |
| 196 | | |
| 197 | | if (target_os_requires_libc(g->zig_target->os)) { |
| 198 | | g->libc_link_lib = create_link_lib(buf_create_from_str("c")); |
| 199 | | g->link_libs_list.append(g->libc_link_lib); |
| 200 | | } |
| 201 | | |
| 202 | | target_triple_llvm(&g->llvm_triple_str, g->zig_target); |
| 203 | | g->pointer_size_bytes = target_arch_pointer_bit_width(g->zig_target->arch) / 8; |
| 204 | | |
| 205 | | if (!target_has_debug_info(g->zig_target)) { |
| 206 | | g->strip_debug_symbols = true; |
| 207 | | } |
| 208 | | |
| 209 | | return g; |
| 210 | | } |
| 211 | | |
| 212 | 92 | void codegen_set_clang_argv(CodeGen *g, const char **args, size_t len) { |
| 213 | 93 | g->clang_argv = args; |
| 214 | 94 | g->clang_argv_len = len; |
| ... | ... | @@ -233,10 +113,6 @@ void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patc |
| 233 | 113 | g->version_patch = patch; |
| 234 | 114 | } |
| 235 | 115 | |
| 236 | | void codegen_set_is_test(CodeGen *g, bool is_test_build) { |
| 237 | | g->is_test_build = is_test_build; |
| 238 | | } |
| 239 | | |
| 240 | 116 | void codegen_set_emit_file_type(CodeGen *g, EmitFileType emit_file_type) { |
| 241 | 117 | g->emit_file_type = emit_file_type; |
| 242 | 118 | } |
| ... | ... | @@ -7994,6 +7870,14 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { |
| 7994 | 7870 | return contents; |
| 7995 | 7871 | } |
| 7996 | 7872 | |
| 7873 | static ZigPackage *create_test_runner_pkg(CodeGen *g) { |
| 7874 | return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig", "std.special"); |
| 7875 | } |
| 7876 | |
| 7877 | static ZigPackage *create_panic_pkg(CodeGen *g) { |
| 7878 | return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig", "std.special"); |
| 7879 | } |
| 7880 | |
| 7997 | 7881 | static Error define_builtin_compile_vars(CodeGen *g) { |
| 7998 | 7882 | if (g->std_package == nullptr) |
| 7999 | 7883 | return ErrorNone; |
| ... | ... | @@ -8078,8 +7962,16 @@ static Error define_builtin_compile_vars(CodeGen *g) { |
| 8078 | 7962 | g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); |
| 8079 | 7963 | g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package); |
| 8080 | 7964 | g->std_package->package_table.put(buf_create_from_str("std"), g->std_package); |
| 8081 | | g->std_package->package_table.put(buf_create_from_str("root"), |
| 8082 | | g->is_test_build ? g->test_runner_package : g->root_package); |
| 7965 | ZigPackage *root_pkg; |
| 7966 | if (g->is_test_build) { |
| 7967 | if (g->test_runner_package == nullptr) { |
| 7968 | g->test_runner_package = create_test_runner_pkg(g); |
| 7969 | } |
| 7970 | root_pkg = g->test_runner_package; |
| 7971 | } else { |
| 7972 | root_pkg = g->root_package; |
| 7973 | } |
| 7974 | g->std_package->package_table.put(buf_create_from_str("root"), root_pkg); |
| 8083 | 7975 | g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents, |
| 8084 | 7976 | SourceKindPkgMain); |
| 8085 | 7977 | |
| ... | ... | @@ -8571,14 +8463,6 @@ static ZigPackage *create_start_pkg(CodeGen *g, ZigPackage *pkg_with_main) { |
| 8571 | 8463 | return package; |
| 8572 | 8464 | } |
| 8573 | 8465 | |
| 8574 | | static ZigPackage *create_test_runner_pkg(CodeGen *g) { |
| 8575 | | return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig", "std.special"); |
| 8576 | | } |
| 8577 | | |
| 8578 | | static ZigPackage *create_panic_pkg(CodeGen *g) { |
| 8579 | | return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig", "std.special"); |
| 8580 | | } |
| 8581 | | |
| 8582 | 8466 | static void create_test_compile_var_and_add_test_runner(CodeGen *g) { |
| 8583 | 8467 | Error err; |
| 8584 | 8468 | |
| ... | ... | @@ -8628,7 +8512,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) { |
| 8628 | 8512 | ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true); |
| 8629 | 8513 | |
| 8630 | 8514 | update_compile_var(g, buf_create_from_str("test_functions"), test_fn_slice); |
| 8631 | | g->test_runner_package = create_test_runner_pkg(g); |
| 8515 | assert(g->test_runner_package != nullptr); |
| 8632 | 8516 | g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig"); |
| 8633 | 8517 | } |
| 8634 | 8518 | |
| ... | ... | @@ -9742,7 +9626,8 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o |
| 9742 | 9626 | ZigLibCInstallation *libc) |
| 9743 | 9627 | { |
| 9744 | 9628 | CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type, |
| 9745 | | parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path()); |
| 9629 | parent_gen->build_mode, parent_gen->zig_lib_dir, parent_gen->zig_std_dir, libc, get_stage1_cache_path(), |
| 9630 | false); |
| 9746 | 9631 | child_gen->disable_gen_h = true; |
| 9747 | 9632 | child_gen->want_stack_check = WantStackCheckDisabled; |
| 9748 | 9633 | child_gen->verbose_tokenize = parent_gen->verbose_tokenize; |
| ... | ... | @@ -9769,3 +9654,123 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o |
| 9769 | 9654 | return child_gen; |
| 9770 | 9655 | } |
| 9771 | 9656 | |
| 9657 | CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, |
| 9658 | OutType out_type, BuildMode build_mode, Buf *override_lib_dir, Buf *override_std_dir, |
| 9659 | ZigLibCInstallation *libc, Buf *cache_dir, bool is_test_build) |
| 9660 | { |
| 9661 | CodeGen *g = allocate<CodeGen>(1); |
| 9662 | |
| 9663 | codegen_add_time_event(g, "Initialize"); |
| 9664 | |
| 9665 | g->subsystem = TargetSubsystemAuto; |
| 9666 | g->libc = libc; |
| 9667 | g->zig_target = target; |
| 9668 | g->cache_dir = cache_dir; |
| 9669 | |
| 9670 | if (override_lib_dir == nullptr) { |
| 9671 | g->zig_lib_dir = get_zig_lib_dir(); |
| 9672 | } else { |
| 9673 | g->zig_lib_dir = override_lib_dir; |
| 9674 | } |
| 9675 | |
| 9676 | if (override_std_dir == nullptr) { |
| 9677 | g->zig_std_dir = buf_alloc(); |
| 9678 | os_path_join(g->zig_lib_dir, buf_create_from_str("std"), g->zig_std_dir); |
| 9679 | } else { |
| 9680 | g->zig_std_dir = override_std_dir; |
| 9681 | } |
| 9682 | |
| 9683 | g->zig_c_headers_dir = buf_alloc(); |
| 9684 | os_path_join(g->zig_lib_dir, buf_create_from_str("include"), g->zig_c_headers_dir); |
| 9685 | |
| 9686 | g->build_mode = build_mode; |
| 9687 | g->out_type = out_type; |
| 9688 | g->import_table.init(32); |
| 9689 | g->builtin_fn_table.init(32); |
| 9690 | g->primitive_type_table.init(32); |
| 9691 | g->type_table.init(32); |
| 9692 | g->fn_type_table.init(32); |
| 9693 | g->error_table.init(16); |
| 9694 | g->generic_table.init(16); |
| 9695 | g->llvm_fn_table.init(16); |
| 9696 | g->memoized_fn_eval_table.init(16); |
| 9697 | g->exported_symbol_names.init(8); |
| 9698 | g->external_prototypes.init(8); |
| 9699 | g->string_literals_table.init(16); |
| 9700 | g->type_info_cache.init(32); |
| 9701 | g->is_test_build = is_test_build; |
| 9702 | g->is_single_threaded = false; |
| 9703 | buf_resize(&g->global_asm, 0); |
| 9704 | |
| 9705 | for (size_t i = 0; i < array_length(symbols_that_llvm_depends_on); i += 1) { |
| 9706 | g->external_prototypes.put(buf_create_from_str(symbols_that_llvm_depends_on[i]), nullptr); |
| 9707 | } |
| 9708 | |
| 9709 | if (root_src_path) { |
| 9710 | Buf *root_pkg_path; |
| 9711 | Buf *rel_root_src_path; |
| 9712 | if (main_pkg_path == nullptr) { |
| 9713 | Buf *src_basename = buf_alloc(); |
| 9714 | Buf *src_dir = buf_alloc(); |
| 9715 | os_path_split(root_src_path, src_dir, src_basename); |
| 9716 | |
| 9717 | if (buf_len(src_basename) == 0) { |
| 9718 | fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path)); |
| 9719 | exit(1); |
| 9720 | } |
| 9721 | root_pkg_path = src_dir; |
| 9722 | rel_root_src_path = src_basename; |
| 9723 | } else { |
| 9724 | Buf resolved_root_src_path = os_path_resolve(&root_src_path, 1); |
| 9725 | Buf resolved_main_pkg_path = os_path_resolve(&main_pkg_path, 1); |
| 9726 | |
| 9727 | if (!buf_starts_with_buf(&resolved_root_src_path, &resolved_main_pkg_path)) { |
| 9728 | fprintf(stderr, "Root source path '%s' outside main package path '%s'", |
| 9729 | buf_ptr(root_src_path), buf_ptr(main_pkg_path)); |
| 9730 | exit(1); |
| 9731 | } |
| 9732 | root_pkg_path = main_pkg_path; |
| 9733 | rel_root_src_path = buf_create_from_mem( |
| 9734 | buf_ptr(&resolved_root_src_path) + buf_len(&resolved_main_pkg_path) + 1, |
| 9735 | buf_len(&resolved_root_src_path) - buf_len(&resolved_main_pkg_path) - 1); |
| 9736 | } |
| 9737 | |
| 9738 | g->root_package = new_package(buf_ptr(root_pkg_path), buf_ptr(rel_root_src_path), ""); |
| 9739 | g->std_package = new_package(buf_ptr(g->zig_std_dir), "std.zig", "std"); |
| 9740 | g->root_package->package_table.put(buf_create_from_str("std"), g->std_package); |
| 9741 | } else { |
| 9742 | g->root_package = new_package(".", "", ""); |
| 9743 | } |
| 9744 | |
| 9745 | g->root_package->package_table.put(buf_create_from_str("root"), g->root_package); |
| 9746 | |
| 9747 | g->zig_std_special_dir = buf_alloc(); |
| 9748 | os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir); |
| 9749 | |
| 9750 | assert(target != nullptr); |
| 9751 | if (!target->is_native) { |
| 9752 | g->each_lib_rpath = false; |
| 9753 | } else { |
| 9754 | g->each_lib_rpath = true; |
| 9755 | |
| 9756 | if (target_os_is_darwin(g->zig_target->os)) { |
| 9757 | init_darwin_native(g); |
| 9758 | } |
| 9759 | |
| 9760 | } |
| 9761 | |
| 9762 | if (target_os_requires_libc(g->zig_target->os)) { |
| 9763 | g->libc_link_lib = create_link_lib(buf_create_from_str("c")); |
| 9764 | g->link_libs_list.append(g->libc_link_lib); |
| 9765 | } |
| 9766 | |
| 9767 | target_triple_llvm(&g->llvm_triple_str, g->zig_target); |
| 9768 | g->pointer_size_bytes = target_arch_pointer_bit_width(g->zig_target->arch) / 8; |
| 9769 | |
| 9770 | if (!target_has_debug_info(g->zig_target)) { |
| 9771 | g->strip_debug_symbols = true; |
| 9772 | } |
| 9773 | |
| 9774 | return g; |
| 9775 | } |
| 9776 | |