authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-26 17:17:20-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-26 17:26:34-05:00
log6365f5a9f3f60c1a65f91dcf2926fefee4228b21
tree14afab25b963c1649db598c815cebe58df61439c
parent33174f11ef5cbb3534217dbc1502886d97226778

introduce sys_include_dir for when sys/* files are not with stdlib.h


4 files changed, 92 insertions(+), 28 deletions(-)

src/codegen.cpp+6-6
...@@ -8181,14 +8181,13 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {...@@ -8181,14 +8181,13 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
8181 args.append(buf_ptr(g->zig_c_headers_dir));8181 args.append(buf_ptr(g->zig_c_headers_dir));
81828182
8183 if (g->libc != nullptr) {8183 if (g->libc != nullptr) {
8184 if (buf_len(&g->libc->msvc_lib_dir) != 0) {
8185 Buf *include_dir = buf_sprintf("%s" OS_SEP ".." OS_SEP ".." OS_SEP "include", buf_ptr(&g->libc->msvc_lib_dir));
8186 args.append("-isystem");
8187 args.append(buf_ptr(include_dir));
8188 }
8189
8190 args.append("-isystem");8184 args.append("-isystem");
8191 args.append(buf_ptr(&g->libc->include_dir));8185 args.append(buf_ptr(&g->libc->include_dir));
8186
8187 if (!buf_eql_buf(&g->libc->include_dir, &g->libc->sys_include_dir)) {
8188 args.append("-isystem");
8189 args.append(buf_ptr(&g->libc->sys_include_dir));
8190 }
8192 }8191 }
81938192
8194 if (g->zig_target->is_native) {8193 if (g->zig_target->is_native) {
...@@ -8848,6 +8847,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -8848,6 +8847,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
8848 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);8847 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);
8849 if (g->libc) {8848 if (g->libc) {
8850 cache_buf(ch, &g->libc->include_dir);8849 cache_buf(ch, &g->libc->include_dir);
8850 cache_buf(ch, &g->libc->sys_include_dir);
8851 cache_buf(ch, &g->libc->crt_dir);8851 cache_buf(ch, &g->libc->crt_dir);
8852 cache_buf(ch, &g->libc->lib_dir);8852 cache_buf(ch, &g->libc->lib_dir);
8853 cache_buf(ch, &g->libc->static_lib_dir);8853 cache_buf(ch, &g->libc->static_lib_dir);
src/libc_installation.cpp+80-16
...@@ -12,6 +12,7 @@...@@ -12,6 +12,7 @@
1212
13static const char *zig_libc_keys[] = {13static const char *zig_libc_keys[] = {
14 "include_dir",14 "include_dir",
15 "sys_include_dir",
15 "crt_dir",16 "crt_dir",
16 "lib_dir",17 "lib_dir",
17 "static_lib_dir",18 "static_lib_dir",
...@@ -34,6 +35,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *...@@ -34,6 +35,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *
34static void zig_libc_init_empty(ZigLibCInstallation *libc) {35static void zig_libc_init_empty(ZigLibCInstallation *libc) {
35 *libc = {};36 *libc = {};
36 buf_init_from_str(&libc->include_dir, "");37 buf_init_from_str(&libc->include_dir, "");
38 buf_init_from_str(&libc->sys_include_dir, "");
37 buf_init_from_str(&libc->crt_dir, "");39 buf_init_from_str(&libc->crt_dir, "");
38 buf_init_from_str(&libc->lib_dir, "");40 buf_init_from_str(&libc->lib_dir, "");
39 buf_init_from_str(&libc->static_lib_dir, "");41 buf_init_from_str(&libc->static_lib_dir, "");
...@@ -46,7 +48,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget...@@ -46,7 +48,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
46 Error err;48 Error err;
47 zig_libc_init_empty(libc);49 zig_libc_init_empty(libc);
4850
49 bool found_keys[array_length(zig_libc_keys)] = {}; // zig_libc_keys_len51 bool found_keys[array_length(zig_libc_keys)] = {};
5052
51 Buf *contents = buf_alloc();53 Buf *contents = buf_alloc();
52 if ((err = os_fetch_file_path(libc_file, contents, false))) {54 if ((err = os_fetch_file_path(libc_file, contents, false))) {
...@@ -76,12 +78,13 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget...@@ -76,12 +78,13 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
76 Slice<uint8_t> value = SplitIterator_rest(&line_it);78 Slice<uint8_t> value = SplitIterator_rest(&line_it);
77 bool match = false;79 bool match = false;
78 match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir);80 match = match || zig_libc_match_key(name, value, found_keys, 0, &libc->include_dir);
79 match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->crt_dir);81 match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->sys_include_dir);
80 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->lib_dir);82 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->crt_dir);
81 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_lib_dir);83 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->lib_dir);
82 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir);84 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->static_lib_dir);
83 match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->kernel32_lib_dir);85 match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->msvc_lib_dir);
84 match = match || zig_libc_match_key(name, value, found_keys, 6, &libc->dynamic_linker_path);86 match = match || zig_libc_match_key(name, value, found_keys, 6, &libc->kernel32_lib_dir);
87 match = match || zig_libc_match_key(name, value, found_keys, 7, &libc->dynamic_linker_path);
85 }88 }
8689
87 for (size_t i = 0; i < zig_libc_keys_len; i += 1) {90 for (size_t i = 0; i < zig_libc_keys_len; i += 1) {
...@@ -100,6 +103,13 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget...@@ -100,6 +103,13 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
100 return ErrorSemanticAnalyzeFail;103 return ErrorSemanticAnalyzeFail;
101 }104 }
102105
106 if (buf_len(&libc->sys_include_dir) == 0) {
107 if (verbose) {
108 fprintf(stderr, "sys_include_dir may not be empty\n");
109 }
110 return ErrorSemanticAnalyzeFail;
111 }
112
103 if (buf_len(&libc->crt_dir) == 0) {113 if (buf_len(&libc->crt_dir) == 0) {
104 if (!target_is_darwin(target)) {114 if (!target_is_darwin(target)) {
105 if (verbose) {115 if (verbose) {
...@@ -195,13 +205,40 @@ static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindow...@@ -195,13 +205,40 @@ static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindow
195static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {205static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
196 if (sdk->msvc_lib_dir_ptr == nullptr) {206 if (sdk->msvc_lib_dir_ptr == nullptr) {
197 if (verbose) {207 if (verbose) {
198 fprintf(stderr, "Unable to determine vcruntime path\n");208 fprintf(stderr, "Unable to determine vcruntime.lib path\n");
199 }209 }
200 return ErrorFileNotFound;210 return ErrorFileNotFound;
201 }211 }
202 buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);212 buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);
203 return ErrorNone;213 return ErrorNone;
204}214}
215static Error zig_libc_find_native_msvc_include_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
216 Error err;
217 if (sdk->msvc_lib_dir_ptr == nullptr) {
218 if (verbose) {
219 fprintf(stderr, "Unable to determine vcruntime.h path\n");
220 }
221 return ErrorFileNotFound;
222 }
223 Buf search_path = BUF_INIT;
224 buf_init_from_mem(&search_path, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);
225 buf_append_str(&search_path, "\\..\\..\\include");
226
227 Buf *vcruntime_path = buf_sprintf("%s\\vcruntime.h", buf_ptr(&search_path));
228 bool exists;
229 if ((err = os_file_exists(vcruntime_path, &exists))) {
230 exists = false;
231 }
232 if (exists) {
233 self->sys_include_dir = search_path;
234 return ErrorNone;
235 }
236
237 if (verbose) {
238 fprintf(stderr, "Unable to determine vcruntime.h path\n");
239 }
240 return ErrorFileNotFound;
241}
205#else242#else
206static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, bool verbose) {243static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, bool verbose) {
207 const char *cc_exe = getenv("CC");244 const char *cc_exe = getenv("CC");
...@@ -253,18 +290,38 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b...@@ -253,18 +290,38 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
253 while (*search_path == ' ') {290 while (*search_path == ' ') {
254 search_path += 1;291 search_path += 1;
255 }292 }
256 Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path);293
257 bool exists;294 if (buf_len(&self->include_dir) == 0) {
258 if ((err = os_file_exists(stdlib_path, &exists))) {295 Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path);
259 exists = false;296 bool exists;
297 if ((err = os_file_exists(stdlib_path, &exists))) {
298 exists = false;
299 }
300 if (exists) {
301 buf_init_from_str(&self->include_dir, search_path);
302 }
303 }
304 if (buf_len(&self->sys_include_dir) == 0) {
305 Buf *stdlib_path = buf_sprintf("%s/sys/errno.h", search_path);
306 bool exists;
307 if ((err = os_file_exists(stdlib_path, &exists))) {
308 exists = false;
309 }
310 if (exists) {
311 buf_init_from_str(&self->sys_include_dir, search_path);
312 }
260 }313 }
261 if (exists) {314 if (buf_len(&self->include_dir) != 0 && buf_len(&self->sys_include_dir) != 0) {
262 buf_init_from_str(&self->include_dir, search_path);
263 return ErrorNone;315 return ErrorNone;
264 }316 }
265 }317 }
266 if (verbose) {318 if (verbose) {
267 fprintf(stderr, "unable to determine libc include path: stdlib.h not found in '%s' search paths\n", cc_exe);319 if (buf_len(&self->include_dir) == 0) {
320 fprintf(stderr, "unable to determine libc include path: stdlib.h not found in '%s' search paths\n", cc_exe);
321 }
322 if (buf_len(&self->sys_include_dir) == 0) {
323 fprintf(stderr, "unable to determine libc include path: sys/errno.h not found in '%s' search paths\n", cc_exe);
324 }
268 }325 }
269 return ErrorFileNotFound;326 return ErrorFileNotFound;
270}327}
...@@ -345,8 +402,12 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self...@@ -345,8 +402,12 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self
345void zig_libc_render(ZigLibCInstallation *self, FILE *file) {402void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
346 fprintf(file,403 fprintf(file,
347 "# The directory that contains `stdlib.h`.\n"404 "# The directory that contains `stdlib.h`.\n"
348 "# On POSIX, can be found with: `cc -E -Wp,-v -xc /dev/null`\n"405 "# On POSIX, include directories be found with: `cc -E -Wp,-v -xc /dev/null`\n"
349 "include_dir=%s\n"406 "include_dir=%s\n"
407 "# The system-specific include directory. May be the same as `include_dir`.\n"
408 "# On Windows it's the directory that includes `vcruntime.h`.\n"
409 "# On POSIX it's the directory that includes `sys/errno.h`.\n"
410 "sys_include_dir=%s\n"
350 "\n"411 "\n"
351 "# The directory that contains `crt1.o`.\n"412 "# The directory that contains `crt1.o`.\n"
352 "# On POSIX, can be found with `cc -print-file-name=crt1.o`.\n"413 "# On POSIX, can be found with `cc -print-file-name=crt1.o`.\n"
...@@ -377,6 +438,7 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) {...@@ -377,6 +438,7 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
377 "\n"438 "\n"
378 ,439 ,
379 buf_ptr(&self->include_dir),440 buf_ptr(&self->include_dir),
441 buf_ptr(&self->sys_include_dir),
380 buf_ptr(&self->crt_dir),442 buf_ptr(&self->crt_dir),
381 buf_ptr(&self->lib_dir),443 buf_ptr(&self->lib_dir),
382 buf_ptr(&self->static_lib_dir),444 buf_ptr(&self->static_lib_dir),
...@@ -395,6 +457,8 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {...@@ -395,6 +457,8 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {
395 ZigWindowsSDK *sdk;457 ZigWindowsSDK *sdk;
396 switch (zig_find_windows_sdk(&sdk)) {458 switch (zig_find_windows_sdk(&sdk)) {
397 case ZigFindWindowsSdkErrorNone:459 case ZigFindWindowsSdkErrorNone:
460 if ((err = zig_libc_find_native_msvc_include_dir(self, sdk, verbose)))
461 return err;
398 if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose)))462 if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose)))
399 return err;463 return err;
400 if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose)))464 if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose)))
src/libc_installation.hpp+1
...@@ -17,6 +17,7 @@...@@ -17,6 +17,7 @@
17// Must be synchronized with zig_libc_keys17// Must be synchronized with zig_libc_keys
18struct ZigLibCInstallation {18struct ZigLibCInstallation {
19 Buf include_dir;19 Buf include_dir;
20 Buf sys_include_dir;
20 Buf crt_dir;21 Buf crt_dir;
21 Buf lib_dir;22 Buf lib_dir;
22 Buf static_lib_dir;23 Buf static_lib_dir;
src/translate_c.cpp+5-6
...@@ -4815,14 +4815,13 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const...@@ -4815,14 +4815,13 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
4815 clang_argv.append(buf_ptr(codegen->zig_c_headers_dir));4815 clang_argv.append(buf_ptr(codegen->zig_c_headers_dir));
48164816
4817 if (codegen->libc != nullptr) {4817 if (codegen->libc != nullptr) {
4818 if (buf_len(&codegen->libc->msvc_lib_dir) != 0) {
4819 Buf *include_dir = buf_sprintf("%s" OS_SEP ".." OS_SEP ".." OS_SEP "include", buf_ptr(&codegen->libc->msvc_lib_dir));
4820 clang_argv.append("-isystem");
4821 clang_argv.append(buf_ptr(include_dir));
4822 }
4823
4824 clang_argv.append("-isystem");4818 clang_argv.append("-isystem");
4825 clang_argv.append(buf_ptr(&codegen->libc->include_dir));4819 clang_argv.append(buf_ptr(&codegen->libc->include_dir));
4820
4821 if (!buf_eql_buf(&codegen->libc->include_dir, &codegen->libc->sys_include_dir)) {
4822 clang_argv.append("-isystem");
4823 clang_argv.append(buf_ptr(&codegen->libc->sys_include_dir));
4824 }
4826 }4825 }
48274826
4828 // windows c runtime requires -D_DEBUG if using debug libraries4827 // windows c runtime requires -D_DEBUG if using debug libraries