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) {
81818181 args.append(buf_ptr(g->zig_c_headers_dir));
81828182
81838183 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
81908184 args.append("-isystem");
81918185 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 }
81928191 }
81938192
81948193 if (g->zig_target->is_native) {
......@@ -8848,6 +8847,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
88488847 cache_list_of_str(ch, g->lib_dirs.items, g->lib_dirs.length);
88498848 if (g->libc) {
88508849 cache_buf(ch, &g->libc->include_dir);
8850 cache_buf(ch, &g->libc->sys_include_dir);
88518851 cache_buf(ch, &g->libc->crt_dir);
88528852 cache_buf(ch, &g->libc->lib_dir);
88538853 cache_buf(ch, &g->libc->static_lib_dir);
src/libc_installation.cpp+80-16
......@@ -12,6 +12,7 @@
1212
1313static const char *zig_libc_keys[] = {
1414 "include_dir",
15 "sys_include_dir",
1516 "crt_dir",
1617 "lib_dir",
1718 "static_lib_dir",
......@@ -34,6 +35,7 @@ static bool zig_libc_match_key(Slice<uint8_t> name, Slice<uint8_t> value, bool *
3435static void zig_libc_init_empty(ZigLibCInstallation *libc) {
3536 *libc = {};
3637 buf_init_from_str(&libc->include_dir, "");
38 buf_init_from_str(&libc->sys_include_dir, "");
3739 buf_init_from_str(&libc->crt_dir, "");
3840 buf_init_from_str(&libc->lib_dir, "");
3941 buf_init_from_str(&libc->static_lib_dir, "");
......@@ -46,7 +48,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
4648 Error err;
4749 zig_libc_init_empty(libc);
4850
49 bool found_keys[array_length(zig_libc_keys)] = {}; // zig_libc_keys_len
51 bool found_keys[array_length(zig_libc_keys)] = {};
5052
5153 Buf *contents = buf_alloc();
5254 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
7678 Slice<uint8_t> value = SplitIterator_rest(&line_it);
7779 bool match = false;
7880 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);
80 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->lib_dir);
81 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->static_lib_dir);
82 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->msvc_lib_dir);
83 match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->kernel32_lib_dir);
84 match = match || zig_libc_match_key(name, value, found_keys, 6, &libc->dynamic_linker_path);
81 match = match || zig_libc_match_key(name, value, found_keys, 1, &libc->sys_include_dir);
82 match = match || zig_libc_match_key(name, value, found_keys, 2, &libc->crt_dir);
83 match = match || zig_libc_match_key(name, value, found_keys, 3, &libc->lib_dir);
84 match = match || zig_libc_match_key(name, value, found_keys, 4, &libc->static_lib_dir);
85 match = match || zig_libc_match_key(name, value, found_keys, 5, &libc->msvc_lib_dir);
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);
8588 }
8689
8790 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
100103 return ErrorSemanticAnalyzeFail;
101104 }
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
103113 if (buf_len(&libc->crt_dir) == 0) {
104114 if (!target_is_darwin(target)) {
105115 if (verbose) {
......@@ -195,13 +205,40 @@ static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindow
195205static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
196206 if (sdk->msvc_lib_dir_ptr == nullptr) {
197207 if (verbose) {
198 fprintf(stderr, "Unable to determine vcruntime path\n");
208 fprintf(stderr, "Unable to determine vcruntime.lib path\n");
199209 }
200210 return ErrorFileNotFound;
201211 }
202212 buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);
203213 return ErrorNone;
204214}
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}
205242#else
206243static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, bool verbose) {
207244 const char *cc_exe = getenv("CC");
......@@ -253,18 +290,38 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
253290 while (*search_path == ' ') {
254291 search_path += 1;
255292 }
256 Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path);
257 bool exists;
258 if ((err = os_file_exists(stdlib_path, &exists))) {
259 exists = false;
293
294 if (buf_len(&self->include_dir) == 0) {
295 Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path);
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 }
260313 }
261 if (exists) {
262 buf_init_from_str(&self->include_dir, search_path);
314 if (buf_len(&self->include_dir) != 0 && buf_len(&self->sys_include_dir) != 0) {
263315 return ErrorNone;
264316 }
265317 }
266318 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 }
268325 }
269326 return ErrorFileNotFound;
270327}
......@@ -345,8 +402,12 @@ static Error zig_libc_find_native_dynamic_linker_posix(ZigLibCInstallation *self
345402void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
346403 fprintf(file,
347404 "# 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"
349406 "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"
350411 "\n"
351412 "# The directory that contains `crt1.o`.\n"
352413 "# 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) {
377438 "\n"
378439 ,
379440 buf_ptr(&self->include_dir),
441 buf_ptr(&self->sys_include_dir),
380442 buf_ptr(&self->crt_dir),
381443 buf_ptr(&self->lib_dir),
382444 buf_ptr(&self->static_lib_dir),
......@@ -395,6 +457,8 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {
395457 ZigWindowsSDK *sdk;
396458 switch (zig_find_windows_sdk(&sdk)) {
397459 case ZigFindWindowsSdkErrorNone:
460 if ((err = zig_libc_find_native_msvc_include_dir(self, sdk, verbose)))
461 return err;
398462 if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose)))
399463 return err;
400464 if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose)))
src/libc_installation.hpp+1
......@@ -17,6 +17,7 @@
1717// Must be synchronized with zig_libc_keys
1818struct ZigLibCInstallation {
1919 Buf include_dir;
20 Buf sys_include_dir;
2021 Buf crt_dir;
2122 Buf lib_dir;
2223 Buf static_lib_dir;
src/translate_c.cpp+5-6
......@@ -4815,14 +4815,13 @@ Error parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const
48154815 clang_argv.append(buf_ptr(codegen->zig_c_headers_dir));
48164816
48174817 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
48244818 clang_argv.append("-isystem");
48254819 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 }
48264825 }
48274826
48284827 // windows c runtime requires -D_DEBUG if using debug libraries