| author | |
| committer | |
| log | 1962c8588f03f510c100d318472505315acaf3b2 |
| tree | 0a75534a8915522f0817db4ddbedc60c10c5f081 |
| parent | 9636603a3b14bd7205f762f8abb096d9f0ad1ec5 |
closes #463
See #30210 files changed, 147 insertions(+), 32 deletions(-)
CMakeLists.txt+3-2| ... | ... | @@ -327,8 +327,9 @@ set(ZIG_SOURCES |
| 327 | 327 | "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp" |
| 328 | 328 | ) |
| 329 | 329 | |
| 330 | set(C_HEADERS_DEST "lib/zig/include") | |
| 331 | set(ZIG_STD_DEST "lib/zig/std") | |
| 330 | set(ZIG_LIB_DIR "lib/zig") | |
| 331 | set(C_HEADERS_DEST "${ZIG_LIB_DIR}/include") | |
| 332 | set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std") | |
| 332 | 333 | set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h") |
| 333 | 334 | configure_file ( |
| 334 | 335 | "${CMAKE_SOURCE_DIR}/src/config.h.in" |
src/all_types.hpp+2| ... | ... | @@ -1456,7 +1456,9 @@ struct CodeGen { |
| 1456 | 1456 | Buf *libc_lib_dir; |
| 1457 | 1457 | Buf *libc_static_lib_dir; |
| 1458 | 1458 | Buf *libc_include_dir; |
| 1459 | Buf *zig_lib_dir; | |
| 1459 | 1460 | Buf *zig_std_dir; |
| 1461 | Buf *zig_c_headers_dir; | |
| 1460 | 1462 | Buf *zig_std_special_dir; |
| 1461 | 1463 | Buf *dynamic_linker; |
| 1462 | 1464 | Buf *ar_path; |
src/codegen.cpp+9-2| ... | ... | @@ -56,13 +56,20 @@ static PackageTableEntry *new_package(const char *root_src_dir, const char *root |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode, |
| 59 | Buf *zig_std_dir) | |
| 59 | Buf *zig_lib_dir) | |
| 60 | 60 | { |
| 61 | 61 | CodeGen *g = allocate<CodeGen>(1); |
| 62 | 62 | |
| 63 | 63 | codegen_add_time_event(g, "Initialize"); |
| 64 | 64 | |
| 65 | g->zig_std_dir = zig_std_dir; | |
| 65 | g->zig_lib_dir = zig_lib_dir; | |
| 66 | ||
| 67 | g->zig_std_dir = buf_alloc(); | |
| 68 | os_path_join(zig_lib_dir, buf_create_from_str("std"), g->zig_std_dir); | |
| 69 | ||
| 70 | g->zig_c_headers_dir = buf_alloc(); | |
| 71 | os_path_join(zig_lib_dir, buf_create_from_str("include"), g->zig_c_headers_dir); | |
| 72 | ||
| 66 | 73 | g->build_mode = build_mode; |
| 67 | 74 | g->out_type = out_type; |
| 68 | 75 | g->import_table.init(32); |
src/codegen.hpp+1-1| ... | ... | @@ -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_std_dir); | |
| 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); |
src/config.h.in+1-2| ... | ... | @@ -13,8 +13,7 @@ |
| 13 | 13 | #define ZIG_VERSION_PATCH @ZIG_VERSION_PATCH@ |
| 14 | 14 | #define ZIG_VERSION_STRING "@ZIG_VERSION@" |
| 15 | 15 | |
| 16 | #define ZIG_HEADERS_DIR "@CMAKE_INSTALL_PREFIX@/@C_HEADERS_DEST@" | |
| 17 | #define ZIG_STD_DIR "@CMAKE_INSTALL_PREFIX@/@ZIG_STD_DEST@" | |
| 16 | #define ZIG_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@" | |
| 18 | 17 | #define ZIG_LIBC_INCLUDE_DIR "@ZIG_LIBC_INCLUDE_DIR_ESCAPED@" |
| 19 | 18 | #define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR_ESCAPED@" |
| 20 | 19 | #define ZIG_LIBC_STATIC_LIB_DIR "@ZIG_LIBC_STATIC_LIB_DIR_ESCAPED@" |
src/link.cpp+1-1| ... | ... | @@ -34,7 +34,7 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) { |
| 34 | 34 | static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) { |
| 35 | 35 | ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target; |
| 36 | 36 | CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode, |
| 37 | parent_gen->zig_std_dir); | |
| 37 | parent_gen->zig_lib_dir); | |
| 38 | 38 | |
| 39 | 39 | child_gen->want_h_file = false; |
| 40 | 40 | child_gen->verbose_link = parent_gen->verbose_link; |
src/main.cpp+99-18| ... | ... | @@ -49,7 +49,7 @@ static int usage(const char *arg0) { |
| 49 | 49 | " --verbose turn on compiler debug output\n" |
| 50 | 50 | " --verbose-link turn on compiler debug output for linking only\n" |
| 51 | 51 | " --verbose-ir turn on compiler debug output for IR only\n" |
| 52 | " --zig-std-dir [path] directory where zig standard library resides\n" | |
| 52 | " --zig-install-prefix [path] override directory where zig thinks it is installed\n" | |
| 53 | 53 | " -dirafter [dir] same as -isystem but do it last\n" |
| 54 | 54 | " -isystem [dir] add additional search path for other .h files\n" |
| 55 | 55 | " -mllvm [arg] additional arguments to forward to LLVM's option processing\n" |
| ... | ... | @@ -131,6 +131,93 @@ static int print_target_list(FILE *f) { |
| 131 | 131 | return EXIT_SUCCESS; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | static bool test_zig_install_prefix(Buf *test_path, Buf *out_zig_lib_dir) { | |
| 135 | Buf lib_buf = BUF_INIT; | |
| 136 | buf_init_from_str(&lib_buf, "lib"); | |
| 137 | ||
| 138 | Buf zig_buf = BUF_INIT; | |
| 139 | buf_init_from_str(&zig_buf, "zig"); | |
| 140 | ||
| 141 | Buf std_buf = BUF_INIT; | |
| 142 | buf_init_from_str(&std_buf, "std"); | |
| 143 | ||
| 144 | Buf index_zig_buf = BUF_INIT; | |
| 145 | buf_init_from_str(&index_zig_buf, "index.zig"); | |
| 146 | ||
| 147 | Buf test_lib_dir = BUF_INIT; | |
| 148 | Buf test_zig_dir = BUF_INIT; | |
| 149 | Buf test_std_dir = BUF_INIT; | |
| 150 | Buf test_index_file = BUF_INIT; | |
| 151 | ||
| 152 | os_path_join(test_path, &lib_buf, &test_lib_dir); | |
| 153 | os_path_join(&test_lib_dir, &zig_buf, &test_zig_dir); | |
| 154 | os_path_join(&test_zig_dir, &std_buf, &test_std_dir); | |
| 155 | os_path_join(&test_std_dir, &index_zig_buf, &test_index_file); | |
| 156 | ||
| 157 | int err; | |
| 158 | bool exists; | |
| 159 | if ((err = os_file_exists(&test_index_file, &exists))) { | |
| 160 | exists = false; | |
| 161 | } | |
| 162 | if (exists) { | |
| 163 | buf_init_from_buf(out_zig_lib_dir, &test_zig_dir); | |
| 164 | return true; | |
| 165 | } | |
| 166 | return false; | |
| 167 | } | |
| 168 | ||
| 169 | static int find_zig_lib_dir(Buf *out_path) { | |
| 170 | int err; | |
| 171 | ||
| 172 | Buf self_exe_path = BUF_INIT; | |
| 173 | if (!(err = os_self_exe_path(&self_exe_path))) { | |
| 174 | Buf *cur_path = &self_exe_path; | |
| 175 | ||
| 176 | for (;;) { | |
| 177 | Buf *test_dir = buf_alloc(); | |
| 178 | os_path_dirname(cur_path, test_dir); | |
| 179 | ||
| 180 | if (buf_eql_buf(test_dir, cur_path)) { | |
| 181 | break; | |
| 182 | } | |
| 183 | ||
| 184 | if (test_zig_install_prefix(test_dir, out_path)) { | |
| 185 | return 0; | |
| 186 | } | |
| 187 | ||
| 188 | cur_path = test_dir; | |
| 189 | } | |
| 190 | } | |
| 191 | ||
| 192 | if (ZIG_INSTALL_PREFIX != nullptr) { | |
| 193 | if (test_zig_install_prefix(buf_create_from_str(ZIG_INSTALL_PREFIX), out_path)) { | |
| 194 | return 0; | |
| 195 | } | |
| 196 | } | |
| 197 | ||
| 198 | ||
| 199 | return ErrorFileNotFound; | |
| 200 | } | |
| 201 | ||
| 202 | static Buf *resolve_zig_lib_dir(const char *zig_install_prefix_arg) { | |
| 203 | int err; | |
| 204 | Buf *result = buf_alloc(); | |
| 205 | if (zig_install_prefix_arg == nullptr) { | |
| 206 | if ((err = find_zig_lib_dir(result))) { | |
| 207 | fprintf(stderr, "Unable to find zig lib directory. Reinstall Zig or use --zig-install-prefix.\n"); | |
| 208 | exit(EXIT_FAILURE); | |
| 209 | } | |
| 210 | return result; | |
| 211 | } | |
| 212 | Buf *zig_lib_dir_buf = buf_create_from_str(zig_install_prefix_arg); | |
| 213 | if (test_zig_install_prefix(zig_lib_dir_buf, result)) { | |
| 214 | return result; | |
| 215 | } | |
| 216 | ||
| 217 | fprintf(stderr, "No Zig installation found at prefix: %s\n", zig_install_prefix_arg); | |
| 218 | exit(EXIT_FAILURE); | |
| 219 | } | |
| 220 | ||
| 134 | 221 | enum Cmd { |
| 135 | 222 | CmdInvalid, |
| 136 | 223 | CmdBuild, |
| ... | ... | @@ -192,7 +279,7 @@ int main(int argc, char **argv) { |
| 192 | 279 | const char *libc_lib_dir = nullptr; |
| 193 | 280 | const char *libc_static_lib_dir = nullptr; |
| 194 | 281 | const char *libc_include_dir = nullptr; |
| 195 | const char *zig_std_dir = nullptr; | |
| 282 | const char *zig_install_prefix = nullptr; | |
| 196 | 283 | const char *dynamic_linker = nullptr; |
| 197 | 284 | ZigList<const char *> clang_argv = {0}; |
| 198 | 285 | ZigList<const char *> llvm_argv = {0}; |
| ... | ... | @@ -248,29 +335,26 @@ int main(int argc, char **argv) { |
| 248 | 335 | } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) { |
| 249 | 336 | cache_dir = argv[i + 1]; |
| 250 | 337 | i += 1; |
| 251 | } else if (i + 1 < argc && strcmp(argv[i], "--zig-std-dir") == 0) { | |
| 338 | } else if (i + 1 < argc && strcmp(argv[i], "--zig-install-prefix") == 0) { | |
| 252 | 339 | args.append(argv[i]); |
| 253 | 340 | i += 1; |
| 254 | zig_std_dir = argv[i]; | |
| 255 | args.append(zig_std_dir); | |
| 341 | zig_install_prefix = argv[i]; | |
| 342 | args.append(zig_install_prefix); | |
| 256 | 343 | } else { |
| 257 | 344 | args.append(argv[i]); |
| 258 | 345 | } |
| 259 | 346 | } |
| 260 | 347 | |
| 261 | if (zig_std_dir == nullptr) { | |
| 262 | zig_std_dir = ZIG_STD_DIR; | |
| 263 | } | |
| 264 | Buf *zig_std_dir_buf = buf_create_from_str(zig_std_dir); | |
| 348 | Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix); | |
| 265 | 349 | |
| 266 | 350 | Buf *special_dir = buf_alloc(); |
| 267 | os_path_join(zig_std_dir_buf, buf_sprintf("special"), special_dir); | |
| 351 | os_path_join(zig_lib_dir_buf, buf_sprintf("special"), special_dir); | |
| 268 | 352 | |
| 269 | 353 | Buf *build_runner_path = buf_alloc(); |
| 270 | 354 | os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path); |
| 271 | 355 | |
| 272 | 356 | |
| 273 | CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_std_dir_buf); | |
| 357 | CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf); | |
| 274 | 358 | codegen_set_out_name(g, buf_create_from_str("build")); |
| 275 | 359 | codegen_set_verbose(g, verbose); |
| 276 | 360 | |
| ... | ... | @@ -430,8 +514,8 @@ int main(int argc, char **argv) { |
| 430 | 514 | libc_static_lib_dir = argv[i]; |
| 431 | 515 | } else if (strcmp(arg, "--libc-include-dir") == 0) { |
| 432 | 516 | libc_include_dir = argv[i]; |
| 433 | } else if (strcmp(arg, "--zig-std-dir") == 0) { | |
| 434 | zig_std_dir = argv[i]; | |
| 517 | } else if (strcmp(arg, "--zig-install-prefix") == 0) { | |
| 518 | zig_install_prefix = argv[i]; | |
| 435 | 519 | } else if (strcmp(arg, "--dynamic-linker") == 0) { |
| 436 | 520 | dynamic_linker = argv[i]; |
| 437 | 521 | } else if (strcmp(arg, "-isystem") == 0) { |
| ... | ... | @@ -619,12 +703,9 @@ int main(int argc, char **argv) { |
| 619 | 703 | buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir), |
| 620 | 704 | full_cache_dir); |
| 621 | 705 | |
| 622 | if (zig_std_dir == nullptr) { | |
| 623 | zig_std_dir = ZIG_STD_DIR; | |
| 624 | } | |
| 706 | Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix); | |
| 625 | 707 | |
| 626 | CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, | |
| 627 | buf_create_from_str(zig_std_dir)); | |
| 708 | CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf); | |
| 628 | 709 | codegen_set_out_name(g, buf_out_name); |
| 629 | 710 | codegen_set_lib_version(g, ver_major, ver_minor, ver_patch); |
| 630 | 711 | codegen_set_is_test(g, cmd == CmdTest); |
src/os.cpp+27-3| ... | ... | @@ -312,11 +312,12 @@ int os_fetch_file(FILE *f, Buf *out_buf) { |
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | int os_file_exists(Buf *full_path, bool *result) { |
| 315 | #if defined(ZIG_OS_POSIX) | |
| 316 | *result = access(buf_ptr(full_path), F_OK) != -1; | |
| 315 | #if defined(ZIG_OS_WINDOWS) | |
| 316 | *result = GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES; | |
| 317 | 317 | return 0; |
| 318 | 318 | #else |
| 319 | return GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES; | |
| 319 | *result = access(buf_ptr(full_path), F_OK) != -1; | |
| 320 | return 0; | |
| 320 | 321 | #endif |
| 321 | 322 | } |
| 322 | 323 | |
| ... | ... | @@ -835,3 +836,26 @@ int os_init(void) { |
| 835 | 836 | #endif |
| 836 | 837 | return 0; |
| 837 | 838 | } |
| 839 | ||
| 840 | int os_self_exe_path(Buf *out_path) { | |
| 841 | #if defined(ZIG_OS_WINDOWS) | |
| 842 | buf_resize(out_path, 256); | |
| 843 | for (;;) { | |
| 844 | DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path)); | |
| 845 | if (copied_amt <= 0) { | |
| 846 | return ErrorFileNotFound; | |
| 847 | } | |
| 848 | if (copied_amt < buf_len(out_path)) { | |
| 849 | buf_resize(out_path, copied_amt); | |
| 850 | return 0; | |
| 851 | } | |
| 852 | buf_resize(out_path, buf_len(out_path) * 2); | |
| 853 | } | |
| 854 | ||
| 855 | #elif defined(ZIG_OS_DARWIN) | |
| 856 | return ErrorFileNotFound; | |
| 857 | #elif defined(ZIG_OS_LINUX) | |
| 858 | return ErrorFileNotFound; | |
| 859 | #endif | |
| 860 | return ErrorFileNotFound; | |
| 861 | } |
src/os.hpp+2| ... | ... | @@ -64,6 +64,8 @@ double os_get_time(void); |
| 64 | 64 | |
| 65 | 65 | bool os_is_sep(uint8_t c); |
| 66 | 66 | |
| 67 | int os_self_exe_path(Buf *out_path); | |
| 68 | ||
| 67 | 69 | #if defined(__APPLE__) |
| 68 | 70 | #define ZIG_OS_DARWIN |
| 69 | 71 | #elif defined(_WIN32) |
src/parsec.cpp+2-3| ... | ... | @@ -8,7 +8,6 @@ |
| 8 | 8 | #include "all_types.hpp" |
| 9 | 9 | #include "analyze.hpp" |
| 10 | 10 | #include "c_tokenizer.hpp" |
| 11 | #include "config.h" | |
| 12 | 11 | #include "error.hpp" |
| 13 | 12 | #include "ir.hpp" |
| 14 | 13 | #include "os.hpp" |
| ... | ... | @@ -3206,7 +3205,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch |
| 3206 | 3205 | } |
| 3207 | 3206 | |
| 3208 | 3207 | clang_argv.append("-isystem"); |
| 3209 | clang_argv.append(ZIG_HEADERS_DIR); | |
| 3208 | clang_argv.append(buf_ptr(codegen->zig_c_headers_dir)); | |
| 3210 | 3209 | |
| 3211 | 3210 | clang_argv.append("-isystem"); |
| 3212 | 3211 | clang_argv.append(buf_ptr(codegen->libc_include_dir)); |
| ... | ... | @@ -3244,7 +3243,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch |
| 3244 | 3243 | bool allow_pch_with_compiler_errors = false; |
| 3245 | 3244 | bool single_file_parse = false; |
| 3246 | 3245 | bool for_serialization = false; |
| 3247 | const char *resources_path = ZIG_HEADERS_DIR; | |
| 3246 | const char *resources_path = buf_ptr(codegen->zig_c_headers_dir); | |
| 3248 | 3247 | std::unique_ptr<ASTUnit> err_unit; |
| 3249 | 3248 | std::unique_ptr<ASTUnit> ast_unit(ASTUnit::LoadFromCommandLine( |
| 3250 | 3249 | &clang_argv.at(0), &clang_argv.last(), |