authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-01 18:29:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-01 18:30:31-04:00
log1962c8588f03f510c100d318472505315acaf3b2
tree0a75534a8915522f0817db4ddbedc60c10c5f081
parent9636603a3b14bd7205f762f8abb096d9f0ad1ec5

implement standard library path search

closes #463 See #302

10 files changed, 147 insertions(+), 32 deletions(-)

CMakeLists.txt+3-2
...@@ -327,8 +327,9 @@ set(ZIG_SOURCES...@@ -327,8 +327,9 @@ set(ZIG_SOURCES
327 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"327 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
328)328)
329329
330set(C_HEADERS_DEST "lib/zig/include")330set(ZIG_LIB_DIR "lib/zig")
331set(ZIG_STD_DEST "lib/zig/std")331set(C_HEADERS_DEST "${ZIG_LIB_DIR}/include")
332set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
332set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")333set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
333configure_file (334configure_file (
334 "${CMAKE_SOURCE_DIR}/src/config.h.in"335 "${CMAKE_SOURCE_DIR}/src/config.h.in"
src/all_types.hpp+2
...@@ -1456,7 +1456,9 @@ struct CodeGen {...@@ -1456,7 +1456,9 @@ struct CodeGen {
1456 Buf *libc_lib_dir;1456 Buf *libc_lib_dir;
1457 Buf *libc_static_lib_dir;1457 Buf *libc_static_lib_dir;
1458 Buf *libc_include_dir;1458 Buf *libc_include_dir;
1459 Buf *zig_lib_dir;
1459 Buf *zig_std_dir;1460 Buf *zig_std_dir;
1461 Buf *zig_c_headers_dir;
1460 Buf *zig_std_special_dir;1462 Buf *zig_std_special_dir;
1461 Buf *dynamic_linker;1463 Buf *dynamic_linker;
1462 Buf *ar_path;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,13 +56,20 @@ static PackageTableEntry *new_package(const char *root_src_dir, const char *root
56}56}
5757
58CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,58CodeGen *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 CodeGen *g = allocate<CodeGen>(1);61 CodeGen *g = allocate<CodeGen>(1);
6262
63 codegen_add_time_event(g, "Initialize");63 codegen_add_time_event(g, "Initialize");
6464
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 g->build_mode = build_mode;73 g->build_mode = build_mode;
67 g->out_type = out_type;74 g->out_type = out_type;
68 g->import_table.init(32);75 g->import_table.init(32);
src/codegen.hpp+1-1
...@@ -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_std_dir);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);
src/config.h.in+1-2
...@@ -13,8 +13,7 @@...@@ -13,8 +13,7 @@
13#define ZIG_VERSION_PATCH @ZIG_VERSION_PATCH@13#define ZIG_VERSION_PATCH @ZIG_VERSION_PATCH@
14#define ZIG_VERSION_STRING "@ZIG_VERSION@"14#define ZIG_VERSION_STRING "@ZIG_VERSION@"
1515
16#define ZIG_HEADERS_DIR "@CMAKE_INSTALL_PREFIX@/@C_HEADERS_DEST@"16#define ZIG_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@"
17#define ZIG_STD_DIR "@CMAKE_INSTALL_PREFIX@/@ZIG_STD_DEST@"
18#define ZIG_LIBC_INCLUDE_DIR "@ZIG_LIBC_INCLUDE_DIR_ESCAPED@"17#define ZIG_LIBC_INCLUDE_DIR "@ZIG_LIBC_INCLUDE_DIR_ESCAPED@"
19#define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR_ESCAPED@"18#define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR_ESCAPED@"
20#define ZIG_LIBC_STATIC_LIB_DIR "@ZIG_LIBC_STATIC_LIB_DIR_ESCAPED@"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,7 +34,7 @@ static const char *get_libc_static_file(CodeGen *g, const char *file) {
34static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {34static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {
35 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;35 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
36 CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode,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);
3838
39 child_gen->want_h_file = false;39 child_gen->want_h_file = false;
40 child_gen->verbose_link = parent_gen->verbose_link;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,7 +49,7 @@ static int usage(const char *arg0) {
49 " --verbose turn on compiler debug output\n"49 " --verbose turn on compiler debug output\n"
50 " --verbose-link turn on compiler debug output for linking only\n"50 " --verbose-link turn on compiler debug output for linking only\n"
51 " --verbose-ir turn on compiler debug output for IR only\n"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 " -dirafter [dir] same as -isystem but do it last\n"53 " -dirafter [dir] same as -isystem but do it last\n"
54 " -isystem [dir] add additional search path for other .h files\n"54 " -isystem [dir] add additional search path for other .h files\n"
55 " -mllvm [arg] additional arguments to forward to LLVM's option processing\n"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,6 +131,93 @@ static int print_target_list(FILE *f) {
131 return EXIT_SUCCESS;131 return EXIT_SUCCESS;
132}132}
133133
134static 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
169static 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
202static 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
134enum Cmd {221enum Cmd {
135 CmdInvalid,222 CmdInvalid,
136 CmdBuild,223 CmdBuild,
...@@ -192,7 +279,7 @@ int main(int argc, char **argv) {...@@ -192,7 +279,7 @@ int main(int argc, char **argv) {
192 const char *libc_lib_dir = nullptr;279 const char *libc_lib_dir = nullptr;
193 const char *libc_static_lib_dir = nullptr;280 const char *libc_static_lib_dir = nullptr;
194 const char *libc_include_dir = nullptr;281 const char *libc_include_dir = nullptr;
195 const char *zig_std_dir = nullptr;282 const char *zig_install_prefix = nullptr;
196 const char *dynamic_linker = nullptr;283 const char *dynamic_linker = nullptr;
197 ZigList<const char *> clang_argv = {0};284 ZigList<const char *> clang_argv = {0};
198 ZigList<const char *> llvm_argv = {0};285 ZigList<const char *> llvm_argv = {0};
...@@ -248,29 +335,26 @@ int main(int argc, char **argv) {...@@ -248,29 +335,26 @@ int main(int argc, char **argv) {
248 } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) {335 } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) {
249 cache_dir = argv[i + 1];336 cache_dir = argv[i + 1];
250 i += 1;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 args.append(argv[i]);339 args.append(argv[i]);
253 i += 1;340 i += 1;
254 zig_std_dir = argv[i];341 zig_install_prefix = argv[i];
255 args.append(zig_std_dir);342 args.append(zig_install_prefix);
256 } else {343 } else {
257 args.append(argv[i]);344 args.append(argv[i]);
258 }345 }
259 }346 }
260347
261 if (zig_std_dir == nullptr) {348 Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix);
262 zig_std_dir = ZIG_STD_DIR;
263 }
264 Buf *zig_std_dir_buf = buf_create_from_str(zig_std_dir);
265349
266 Buf *special_dir = buf_alloc();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);
268352
269 Buf *build_runner_path = buf_alloc();353 Buf *build_runner_path = buf_alloc();
270 os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path);354 os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path);
271355
272356
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 codegen_set_out_name(g, buf_create_from_str("build"));358 codegen_set_out_name(g, buf_create_from_str("build"));
275 codegen_set_verbose(g, verbose);359 codegen_set_verbose(g, verbose);
276360
...@@ -430,8 +514,8 @@ int main(int argc, char **argv) {...@@ -430,8 +514,8 @@ int main(int argc, char **argv) {
430 libc_static_lib_dir = argv[i];514 libc_static_lib_dir = argv[i];
431 } else if (strcmp(arg, "--libc-include-dir") == 0) {515 } else if (strcmp(arg, "--libc-include-dir") == 0) {
432 libc_include_dir = argv[i];516 libc_include_dir = argv[i];
433 } else if (strcmp(arg, "--zig-std-dir") == 0) {517 } else if (strcmp(arg, "--zig-install-prefix") == 0) {
434 zig_std_dir = argv[i];518 zig_install_prefix = argv[i];
435 } else if (strcmp(arg, "--dynamic-linker") == 0) {519 } else if (strcmp(arg, "--dynamic-linker") == 0) {
436 dynamic_linker = argv[i];520 dynamic_linker = argv[i];
437 } else if (strcmp(arg, "-isystem") == 0) {521 } else if (strcmp(arg, "-isystem") == 0) {
...@@ -619,12 +703,9 @@ int main(int argc, char **argv) {...@@ -619,12 +703,9 @@ int main(int argc, char **argv) {
619 buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),703 buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),
620 full_cache_dir);704 full_cache_dir);
621705
622 if (zig_std_dir == nullptr) {706 Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix);
623 zig_std_dir = ZIG_STD_DIR;
624 }
625707
626 CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode,708 CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf);
627 buf_create_from_str(zig_std_dir));
628 codegen_set_out_name(g, buf_out_name);709 codegen_set_out_name(g, buf_out_name);
629 codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);710 codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
630 codegen_set_is_test(g, cmd == CmdTest);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,11 +312,12 @@ int os_fetch_file(FILE *f, Buf *out_buf) {
312}312}
313313
314int os_file_exists(Buf *full_path, bool *result) {314int os_file_exists(Buf *full_path, bool *result) {
315#if defined(ZIG_OS_POSIX)315#if defined(ZIG_OS_WINDOWS)
316 *result = access(buf_ptr(full_path), F_OK) != -1;316 *result = GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES;
317 return 0;317 return 0;
318#else318#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#endif321#endif
321}322}
322323
...@@ -835,3 +836,26 @@ int os_init(void) {...@@ -835,3 +836,26 @@ int os_init(void) {
835#endif836#endif
836 return 0;837 return 0;
837}838}
839
840int 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,6 +64,8 @@ double os_get_time(void);
6464
65bool os_is_sep(uint8_t c);65bool os_is_sep(uint8_t c);
6666
67int os_self_exe_path(Buf *out_path);
68
67#if defined(__APPLE__)69#if defined(__APPLE__)
68#define ZIG_OS_DARWIN70#define ZIG_OS_DARWIN
69#elif defined(_WIN32)71#elif defined(_WIN32)
src/parsec.cpp+2-3
...@@ -8,7 +8,6 @@...@@ -8,7 +8,6 @@
8#include "all_types.hpp"8#include "all_types.hpp"
9#include "analyze.hpp"9#include "analyze.hpp"
10#include "c_tokenizer.hpp"10#include "c_tokenizer.hpp"
11#include "config.h"
12#include "error.hpp"11#include "error.hpp"
13#include "ir.hpp"12#include "ir.hpp"
14#include "os.hpp"13#include "os.hpp"
...@@ -3206,7 +3205,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch...@@ -3206,7 +3205,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
3206 }3205 }
32073206
3208 clang_argv.append("-isystem");3207 clang_argv.append("-isystem");
3209 clang_argv.append(ZIG_HEADERS_DIR);3208 clang_argv.append(buf_ptr(codegen->zig_c_headers_dir));
32103209
3211 clang_argv.append("-isystem");3210 clang_argv.append("-isystem");
3212 clang_argv.append(buf_ptr(codegen->libc_include_dir));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,7 +3243,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
3244 bool allow_pch_with_compiler_errors = false;3243 bool allow_pch_with_compiler_errors = false;
3245 bool single_file_parse = false;3244 bool single_file_parse = false;
3246 bool for_serialization = false;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 std::unique_ptr<ASTUnit> err_unit;3247 std::unique_ptr<ASTUnit> err_unit;
3249 std::unique_ptr<ASTUnit> ast_unit(ASTUnit::LoadFromCommandLine(3248 std::unique_ptr<ASTUnit> ast_unit(ASTUnit::LoadFromCommandLine(
3250 &clang_argv.at(0), &clang_argv.last(),3249 &clang_argv.at(0), &clang_argv.last(),