authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-04 15:55:40-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-04-04 15:55:40-04:00
log5866dfe4d970ad105e8e580b6feaab8c7c5e82dc
treecea9b7b2fd5405e05fcca16f40bb3e2d94c53350
parentbe0f656c2130e1460a2ad48c202185c302e38c55
parent8fdf8b6972b11e180c69ea41f3b9059254349ff6
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2109 from emekoi/fix-mingw

fixed libc command on mingw

2 files changed, 167 insertions(+), 103 deletions(-)

src/libc_installation.cpp+166-102
......@@ -111,7 +111,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
111111 }
112112
113113 if (buf_len(&libc->msvc_lib_dir) == 0) {
114 if (target->os == OsWindows) {
114 if (target->os == OsWindows && !target_abi_is_gnu(target->abi)) {
115115 if (verbose) {
116116 fprintf(stderr, "msvc_lib_dir may not be empty for %s\n", target_os_name(target->os));
117117 }
......@@ -120,7 +120,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
120120 }
121121
122122 if (buf_len(&libc->kernel32_lib_dir) == 0) {
123 if (target->os == OsWindows) {
123 if (target->os == OsWindows && !target_abi_is_gnu(target->abi)) {
124124 if (verbose) {
125125 fprintf(stderr, "kernel32_lib_dir may not be empty for %s\n", target_os_name(target->os));
126126 }
......@@ -132,86 +132,24 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
132132}
133133
134134#if defined(ZIG_OS_WINDOWS)
135static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
136 Error err;
137 if ((err = os_get_win32_ucrt_include_path(sdk, &self->include_dir))) {
138 if (verbose) {
139 fprintf(stderr, "Unable to determine libc include path: %s\n", err_str(err));
140 }
141 return err;
142 }
143 return ErrorNone;
144}
145static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target,
146 bool verbose)
147{
148 Error err;
149 if ((err = os_get_win32_ucrt_lib_path(sdk, &self->crt_dir, target->arch))) {
150 if (verbose) {
151 fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err));
152 }
153 return err;
154 }
155 return ErrorNone;
156}
157static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target,
158 bool verbose)
159{
160 Error err;
161 if ((err = os_get_win32_kern32_path(sdk, &self->kernel32_lib_dir, target->arch))) {
162 if (verbose) {
163 fprintf(stderr, "Unable to determine kernel32 path: %s\n", err_str(err));
164 }
165 return err;
166 }
167 return ErrorNone;
168}
169static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
170 if (sdk->msvc_lib_dir_ptr == nullptr) {
171 if (verbose) {
172 fprintf(stderr, "Unable to determine vcruntime.lib path\n");
173 }
174 return ErrorFileNotFound;
175 }
176 buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);
177 return ErrorNone;
178}
179static Error zig_libc_find_native_msvc_include_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
180 Error err;
181 if (sdk->msvc_lib_dir_ptr == nullptr) {
182 if (verbose) {
183 fprintf(stderr, "Unable to determine vcruntime.h path\n");
184 }
185 return ErrorFileNotFound;
186 }
187 Buf search_path = BUF_INIT;
188 buf_init_from_mem(&search_path, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);
189 buf_append_str(&search_path, "\\..\\..\\include");
190
191 Buf *vcruntime_path = buf_sprintf("%s\\vcruntime.h", buf_ptr(&search_path));
192 bool exists;
193 if ((err = os_file_exists(vcruntime_path, &exists))) {
194 exists = false;
195 }
196 if (exists) {
197 self->sys_include_dir = search_path;
198 return ErrorNone;
199 }
200
201 if (verbose) {
202 fprintf(stderr, "Unable to determine vcruntime.h path\n");
203 }
204 return ErrorFileNotFound;
205}
135#define CC_EXE "cc.exe"
206136#else
137#define CC_EXE "cc"
138#endif
139
207140static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, bool verbose) {
208141 const char *cc_exe = getenv("CC");
209 cc_exe = (cc_exe == nullptr) ? "cc" : cc_exe;
142 cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe;
210143 ZigList<const char *> args = {};
211144 args.append("-E");
212145 args.append("-Wp,-v");
213146 args.append("-xc");
147 #if defined(ZIG_OS_WINDOWS)
148 args.append("nul");
149 #else
214150 args.append("/dev/null");
151 #endif
152
215153 Termination term;
216154 Buf *out_stderr = buf_alloc();
217155 Buf *out_stdout = buf_alloc();
......@@ -235,7 +173,12 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
235173 if (newline == nullptr) {
236174 break;
237175 }
176
177 #if defined(ZIG_OS_WINDOWS)
178 *(newline - 1) = 0;
179 #endif
238180 *newline = 0;
181
239182 if (prev_newline[0] == ' ') {
240183 search_paths.append(prev_newline);
241184 }
......@@ -255,6 +198,28 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
255198 search_path += 1;
256199 }
257200
201 #if defined(ZIG_OS_WINDOWS)
202 if (buf_len(&self->include_dir) == 0) {
203 Buf *stdlib_path = buf_sprintf("%s\\stdlib.h", search_path);
204 bool exists;
205 if ((err = os_file_exists(stdlib_path, &exists))) {
206 exists = false;
207 }
208 if (exists) {
209 buf_init_from_str(&self->include_dir, search_path);
210 }
211 }
212 if (buf_len(&self->sys_include_dir) == 0) {
213 Buf *stdlib_path = buf_sprintf("%s\\sys\\types.h", search_path);
214 bool exists;
215 if ((err = os_file_exists(stdlib_path, &exists))) {
216 exists = false;
217 }
218 if (exists) {
219 buf_init_from_str(&self->sys_include_dir, search_path);
220 }
221 }
222 #else
258223 if (buf_len(&self->include_dir) == 0) {
259224 Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path);
260225 bool exists;
......@@ -275,6 +240,8 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
275240 buf_init_from_str(&self->sys_include_dir, search_path);
276241 }
277242 }
243 #endif
244
278245 if (buf_len(&self->include_dir) != 0 && buf_len(&self->sys_include_dir) != 0) {
279246 return ErrorNone;
280247 }
......@@ -284,15 +251,19 @@ static Error zig_libc_find_native_include_dir_posix(ZigLibCInstallation *self, b
284251 fprintf(stderr, "unable to determine libc include path: stdlib.h not found in '%s' search paths\n", cc_exe);
285252 }
286253 if (buf_len(&self->sys_include_dir) == 0) {
254 #if defined(ZIG_OS_WINDOWS)
255 fprintf(stderr, "unable to determine libc include path: sys/types.h not found in '%s' search paths\n", cc_exe);
256 #else
287257 fprintf(stderr, "unable to determine libc include path: sys/errno.h not found in '%s' search paths\n", cc_exe);
258 #endif
288259 }
289260 }
290261 return ErrorFileNotFound;
291262}
292#if defined(ZIG_OS_LINUX)
263
293264Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose) {
294265 const char *cc_exe = getenv("CC");
295 cc_exe = (cc_exe == nullptr) ? "cc" : cc_exe;
266 cc_exe = (cc_exe == nullptr) ? CC_EXE : cc_exe;
296267 ZigList<const char *> args = {};
297268 args.append(buf_ptr(buf_sprintf("-print-file-name=%s", o_file)));
298269 Termination term;
......@@ -301,19 +272,25 @@ Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirnam
301272 Error err;
302273 if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
303274 if (verbose) {
304 fprintf(stderr, "unable to determine libc include path: executing '%s': %s\n", cc_exe, err_str(err));
275 fprintf(stderr, "unable to determine libc library path: executing '%s': %s\n", cc_exe, err_str(err));
305276 }
306277 return err;
307278 }
308279 if (term.how != TerminationIdClean || term.code != 0) {
309280 if (verbose) {
310 fprintf(stderr, "unable to determine libc include path: executing '%s' failed\n", cc_exe);
281 fprintf(stderr, "unable to determine libc library path: executing '%s' failed\n", cc_exe);
311282 }
312283 return ErrorCCompileErrors;
313284 }
285 #if defined(ZIG_OS_WINDOWS)
286 if (buf_ends_with_str(out_stdout, "\r\n")) {
287 buf_resize(out_stdout, buf_len(out_stdout) - 2);
288 }
289 #else
314290 if (buf_ends_with_str(out_stdout, "\n")) {
315291 buf_resize(out_stdout, buf_len(out_stdout) - 1);
316292 }
293 #endif
317294 if (buf_len(out_stdout) == 0 || buf_eql_str(out_stdout, o_file)) {
318295 return ErrorCCompilerCannotFindFile;
319296 }
......@@ -324,16 +301,95 @@ Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirnam
324301 }
325302 return ErrorNone;
326303}
304
305#undef CC_EXE
306
327307static Error zig_libc_find_native_crt_dir_posix(ZigLibCInstallation *self, bool verbose) {
328308 return zig_libc_cc_print_file_name("crt1.o", &self->crt_dir, true, verbose);
329309}
330#endif
310
311#if defined(ZIG_OS_WINDOWS)
312static Error zig_libc_find_native_include_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
313 Error err;
314 if ((err = os_get_win32_ucrt_include_path(sdk, &self->include_dir))) {
315 if (verbose) {
316 fprintf(stderr, "Unable to determine libc include path: %s\n", err_str(err));
317 }
318 return err;
319 }
320 return ErrorNone;
321}
322
323static Error zig_libc_find_crt_dir_windows(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target,
324 bool verbose)
325{
326 Error err;
327 if ((err = os_get_win32_ucrt_lib_path(sdk, &self->crt_dir, target->arch))) {
328 if (verbose) {
329 fprintf(stderr, "Unable to determine ucrt path: %s\n", err_str(err));
330 }
331 return err;
332 }
333 return ErrorNone;
334}
335
336static Error zig_libc_find_kernel32_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, ZigTarget *target,
337 bool verbose)
338{
339 Error err;
340 if ((err = os_get_win32_kern32_path(sdk, &self->kernel32_lib_dir, target->arch))) {
341 if (verbose) {
342 fprintf(stderr, "Unable to determine kernel32 path: %s\n", err_str(err));
343 }
344 return err;
345 }
346 return ErrorNone;
347}
348
349static Error zig_libc_find_native_msvc_lib_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
350 if (sdk->msvc_lib_dir_ptr == nullptr) {
351 if (verbose) {
352 fprintf(stderr, "Unable to determine vcruntime.lib path\n");
353 }
354 return ErrorFileNotFound;
355 }
356 buf_init_from_mem(&self->msvc_lib_dir, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);
357 return ErrorNone;
358}
359
360static Error zig_libc_find_native_msvc_include_dir(ZigLibCInstallation *self, ZigWindowsSDK *sdk, bool verbose) {
361 Error err;
362 if (sdk->msvc_lib_dir_ptr == nullptr) {
363 if (verbose) {
364 fprintf(stderr, "Unable to determine vcruntime.h path\n");
365 }
366 return ErrorFileNotFound;
367 }
368 Buf search_path = BUF_INIT;
369 buf_init_from_mem(&search_path, sdk->msvc_lib_dir_ptr, sdk->msvc_lib_dir_len);
370 buf_append_str(&search_path, "\\..\\..\\include");
371
372 Buf *vcruntime_path = buf_sprintf("%s\\vcruntime.h", buf_ptr(&search_path));
373 bool exists;
374 if ((err = os_file_exists(vcruntime_path, &exists))) {
375 exists = false;
376 }
377 if (exists) {
378 self->sys_include_dir = search_path;
379 return ErrorNone;
380 }
381
382 if (verbose) {
383 fprintf(stderr, "Unable to determine vcruntime.h path\n");
384 }
385 return ErrorFileNotFound;
386}
331387#endif
332388
333389void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
334390 fprintf(file,
335391 "# The directory that contains `stdlib.h`.\n"
336 "# On POSIX, include directories be found with: `cc -E -Wp,-v -xc /dev/null`\n"
392 "# On POSIX-like systems, include directories be found with: `cc -E -Wp,-v -xc /dev/null`\n"
337393 "include_dir=%s\n"
338394 "# The system-specific include directory. May be the same as `include_dir`.\n"
339395 "# On Windows it's the directory that includes `vcruntime.h`.\n"
......@@ -346,11 +402,11 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file) {
346402 "crt_dir=%s\n"
347403 "\n"
348404 "# The directory that contains `vcruntime.lib`.\n"
349 "# Only needed when targeting Windows.\n"
405 "# Only needed when targeting MSVC on Windows.\n"
350406 "msvc_lib_dir=%s\n"
351407 "\n"
352408 "# The directory that contains `kernel32.lib`.\n"
353 "# Only needed when targeting Windows.\n"
409 "# Only needed when targeting MSVC on Windows.\n"
354410 "kernel32_lib_dir=%s\n"
355411 "\n"
356412 ,
......@@ -368,26 +424,34 @@ Error zig_libc_find_native(ZigLibCInstallation *self, bool verbose) {
368424#if defined(ZIG_OS_WINDOWS)
369425 ZigTarget native_target;
370426 get_native_target(&native_target);
371 ZigWindowsSDK *sdk;
372 switch (zig_find_windows_sdk(&sdk)) {
373 case ZigFindWindowsSdkErrorNone:
374 if ((err = zig_libc_find_native_msvc_include_dir(self, sdk, verbose)))
375 return err;
376 if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose)))
377 return err;
378 if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose)))
379 return err;
380 if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose)))
381 return err;
382 if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose)))
383 return err;
384 return ErrorNone;
385 case ZigFindWindowsSdkErrorOutOfMemory:
386 return ErrorNoMem;
387 case ZigFindWindowsSdkErrorNotFound:
388 return ErrorFileNotFound;
389 case ZigFindWindowsSdkErrorPathTooLong:
390 return ErrorPathTooLong;
427 if (target_abi_is_gnu(native_target.abi)) {
428 if ((err = zig_libc_find_native_include_dir_posix(self, verbose)))
429 return err;
430 if ((err = zig_libc_find_native_crt_dir_posix(self, verbose)))
431 return err;
432 return ErrorNone;
433 } else {
434 ZigWindowsSDK *sdk;
435 switch (zig_find_windows_sdk(&sdk)) {
436 case ZigFindWindowsSdkErrorNone:
437 if ((err = zig_libc_find_native_msvc_include_dir(self, sdk, verbose)))
438 return err;
439 if ((err = zig_libc_find_native_msvc_lib_dir(self, sdk, verbose)))
440 return err;
441 if ((err = zig_libc_find_kernel32_lib_dir(self, sdk, &native_target, verbose)))
442 return err;
443 if ((err = zig_libc_find_native_include_dir_windows(self, sdk, verbose)))
444 return err;
445 if ((err = zig_libc_find_crt_dir_windows(self, sdk, &native_target, verbose)))
446 return err;
447 return ErrorNone;
448 case ZigFindWindowsSdkErrorOutOfMemory:
449 return ErrorNoMem;
450 case ZigFindWindowsSdkErrorNotFound:
451 return ErrorFileNotFound;
452 case ZigFindWindowsSdkErrorPathTooLong:
453 return ErrorPathTooLong;
454 }
391455 }
392456 zig_unreachable();
393457#else
src/libc_installation.hpp+1-1
......@@ -29,7 +29,7 @@ void zig_libc_render(ZigLibCInstallation *self, FILE *file);
2929
3030Error ATTRIBUTE_MUST_USE zig_libc_find_native(ZigLibCInstallation *self, bool verbose);
3131
32#if defined(ZIG_OS_LINUX)
32#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_WINDOWS)
3333Error zig_libc_cc_print_file_name(const char *o_file, Buf *out, bool want_dirname, bool verbose);
3434#endif
3535