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
327327 "${CMAKE_SOURCE_DIR}/src/zig_llvm.cpp"
328328)
329329
330set(C_HEADERS_DEST "lib/zig/include")
331set(ZIG_STD_DEST "lib/zig/std")
330set(ZIG_LIB_DIR "lib/zig")
331set(C_HEADERS_DEST "${ZIG_LIB_DIR}/include")
332set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
332333set(CONFIGURE_OUT_FILE "${CMAKE_BINARY_DIR}/config.h")
333334configure_file (
334335 "${CMAKE_SOURCE_DIR}/src/config.h.in"
src/all_types.hpp+2
......@@ -1456,7 +1456,9 @@ struct CodeGen {
14561456 Buf *libc_lib_dir;
14571457 Buf *libc_static_lib_dir;
14581458 Buf *libc_include_dir;
1459 Buf *zig_lib_dir;
14591460 Buf *zig_std_dir;
1461 Buf *zig_c_headers_dir;
14601462 Buf *zig_std_special_dir;
14611463 Buf *dynamic_linker;
14621464 Buf *ar_path;
src/codegen.cpp+9-2
......@@ -56,13 +56,20 @@ static PackageTableEntry *new_package(const char *root_src_dir, const char *root
5656}
5757
5858CodeGen *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)
6060{
6161 CodeGen *g = allocate<CodeGen>(1);
6262
6363 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
6673 g->build_mode = build_mode;
6774 g->out_type = out_type;
6875 g->import_table.init(32);
src/codegen.hpp+1-1
......@@ -15,7 +15,7 @@
1515#include <stdio.h>
1616
1717CodeGen *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
2020void codegen_set_clang_argv(CodeGen *codegen, const char **args, size_t len);
2121void codegen_set_llvm_argv(CodeGen *codegen, const char **args, size_t len);
src/config.h.in+1-2
......@@ -13,8 +13,7 @@
1313#define ZIG_VERSION_PATCH @ZIG_VERSION_PATCH@
1414#define ZIG_VERSION_STRING "@ZIG_VERSION@"
1515
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@"
1817#define ZIG_LIBC_INCLUDE_DIR "@ZIG_LIBC_INCLUDE_DIR_ESCAPED@"
1918#define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR_ESCAPED@"
2019#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) {
3434static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path) {
3535 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
3636 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
3939 child_gen->want_h_file = false;
4040 child_gen->verbose_link = parent_gen->verbose_link;
src/main.cpp+99-18
......@@ -49,7 +49,7 @@ static int usage(const char *arg0) {
4949 " --verbose turn on compiler debug output\n"
5050 " --verbose-link turn on compiler debug output for linking only\n"
5151 " --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"
5353 " -dirafter [dir] same as -isystem but do it last\n"
5454 " -isystem [dir] add additional search path for other .h files\n"
5555 " -mllvm [arg] additional arguments to forward to LLVM's option processing\n"
......@@ -131,6 +131,93 @@ static int print_target_list(FILE *f) {
131131 return EXIT_SUCCESS;
132132}
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
134221enum Cmd {
135222 CmdInvalid,
136223 CmdBuild,
......@@ -192,7 +279,7 @@ int main(int argc, char **argv) {
192279 const char *libc_lib_dir = nullptr;
193280 const char *libc_static_lib_dir = nullptr;
194281 const char *libc_include_dir = nullptr;
195 const char *zig_std_dir = nullptr;
282 const char *zig_install_prefix = nullptr;
196283 const char *dynamic_linker = nullptr;
197284 ZigList<const char *> clang_argv = {0};
198285 ZigList<const char *> llvm_argv = {0};
......@@ -248,29 +335,26 @@ int main(int argc, char **argv) {
248335 } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) {
249336 cache_dir = argv[i + 1];
250337 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) {
252339 args.append(argv[i]);
253340 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);
256343 } else {
257344 args.append(argv[i]);
258345 }
259346 }
260347
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);
265349
266350 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
269353 Buf *build_runner_path = buf_alloc();
270354 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);
274358 codegen_set_out_name(g, buf_create_from_str("build"));
275359 codegen_set_verbose(g, verbose);
276360
......@@ -430,8 +514,8 @@ int main(int argc, char **argv) {
430514 libc_static_lib_dir = argv[i];
431515 } else if (strcmp(arg, "--libc-include-dir") == 0) {
432516 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];
435519 } else if (strcmp(arg, "--dynamic-linker") == 0) {
436520 dynamic_linker = argv[i];
437521 } else if (strcmp(arg, "-isystem") == 0) {
......@@ -619,12 +703,9 @@ int main(int argc, char **argv) {
619703 buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),
620704 full_cache_dir);
621705
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);
625707
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);
628709 codegen_set_out_name(g, buf_out_name);
629710 codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
630711 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) {
312312}
313313
314314int 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;
317317 return 0;
318318#else
319 return GetFileAttributes(buf_ptr(full_path)) != INVALID_FILE_ATTRIBUTES;
319 *result = access(buf_ptr(full_path), F_OK) != -1;
320 return 0;
320321#endif
321322}
322323
......@@ -835,3 +836,26 @@ int os_init(void) {
835836#endif
836837 return 0;
837838}
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);
6464
6565bool os_is_sep(uint8_t c);
6666
67int os_self_exe_path(Buf *out_path);
68
6769#if defined(__APPLE__)
6870#define ZIG_OS_DARWIN
6971#elif defined(_WIN32)
src/parsec.cpp+2-3
......@@ -8,7 +8,6 @@
88#include "all_types.hpp"
99#include "analyze.hpp"
1010#include "c_tokenizer.hpp"
11#include "config.h"
1211#include "error.hpp"
1312#include "ir.hpp"
1413#include "os.hpp"
......@@ -3206,7 +3205,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
32063205 }
32073206
32083207 clang_argv.append("-isystem");
3209 clang_argv.append(ZIG_HEADERS_DIR);
3208 clang_argv.append(buf_ptr(codegen->zig_c_headers_dir));
32103209
32113210 clang_argv.append("-isystem");
32123211 clang_argv.append(buf_ptr(codegen->libc_include_dir));
......@@ -3244,7 +3243,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
32443243 bool allow_pch_with_compiler_errors = false;
32453244 bool single_file_parse = false;
32463245 bool for_serialization = false;
3247 const char *resources_path = ZIG_HEADERS_DIR;
3246 const char *resources_path = buf_ptr(codegen->zig_c_headers_dir);
32483247 std::unique_ptr<ASTUnit> err_unit;
32493248 std::unique_ptr<ASTUnit> ast_unit(ASTUnit::LoadFromCommandLine(
32503249 &clang_argv.at(0), &clang_argv.last(),