authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-24 00:06:34-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-07-24 00:06:34-04:00
log72599d420b1bebb37efb2179a91d8256287f7c28
tree2485d62496dd7436bcbabe80e7b47ca369ccbd38
parent2614ef056a83842ec9501bf2e4f21ae840f981f8

self-hosted: find all libc paths; windows linker code


5 files changed, 282 insertions(+), 66 deletions(-)

src-self-hosted/compilation.zig+1
......@@ -276,6 +276,7 @@ pub const Compilation = struct {
276276 LibCRequiredButNotProvidedOrFound,
277277 LibCMissingDynamicLinker,
278278 InvalidDarwinVersionString,
279 UnsupportedLinkArchitecture,
279280 };
280281
281282 pub const Event = union(enum) {
src-self-hosted/libc_installation.zig+126-61
......@@ -20,8 +20,10 @@ pub const LibCInstallation = struct {
2020 CCompilerExitCode,
2121 CCompilerCrashed,
2222 CCompilerCannotFindHeaders,
23 CCompilerCannotFindCRuntime,
23 LibCRuntimeNotFound,
2424 LibCStdLibHeaderNotFound,
25 LibCKernel32LibNotFound,
26 UnsupportedArchitecture,
2527 };
2628
2729 pub fn parse(
......@@ -111,7 +113,7 @@ pub const LibCInstallation = struct {
111113 \\lib_dir={}
112114 \\
113115 \\# The directory that contains `crtbegin.o`.
114 \\# On Linux, can be found with `cc -print-file-name=crt1.o`.
116 \\# On Linux, can be found with `cc -print-file-name=crtbegin.o`.
115117 \\# Not needed when targeting MacOS or Windows.
116118 \\static_lib_dir={}
117119 \\
......@@ -142,21 +144,22 @@ pub const LibCInstallation = struct {
142144 self.initEmpty();
143145 var group = event.Group(FindError!void).init(loop);
144146 errdefer group.cancelAll();
147 var windows_sdk: ?*c.ZigWindowsSDK = null;
148 errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk));
149
145150 switch (builtin.os) {
146151 builtin.Os.windows => {
147152 var sdk: *c.ZigWindowsSDK = undefined;
148153 switch (c.zig_find_windows_sdk(@ptrCast(?[*]?[*]c.ZigWindowsSDK, &sdk))) {
149154 c.ZigFindWindowsSdkError.None => {
150 defer c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk));
155 windows_sdk = sdk;
151156
152 errdefer if (self.msvc_lib_dir) |s| loop.allocator.free(s);
153157 if (sdk.msvc_lib_dir_ptr) |ptr| {
154158 self.msvc_lib_dir = try std.mem.dupe(loop.allocator, u8, ptr[0..sdk.msvc_lib_dir_len]);
155159 }
156 //try group.call(findNativeIncludeDirWindows, self, loop);
157 //try group.call(findNativeLibDirWindows, self, loop);
158 //try group.call(findNativeMsvcLibDir, self, loop);
159 //try group.call(findNativeKernel32LibDir, self, loop);
160 try group.call(findNativeKernel32LibDir, self, loop, sdk);
161 try group.call(findNativeIncludeDirWindows, self, loop, sdk);
162 try group.call(findNativeLibDirWindows, self, loop, sdk);
160163 },
161164 c.ZigFindWindowsSdkError.OutOfMemory => return error.OutOfMemory,
162165 c.ZigFindWindowsSdkError.NotFound => return error.NotFound,
......@@ -230,61 +233,64 @@ pub const LibCInstallation = struct {
230233 const stdlib_path = try std.os.path.join(loop.allocator, search_path, "stdlib.h");
231234 defer loop.allocator.free(stdlib_path);
232235
233 if (std.os.File.access(loop.allocator, stdlib_path)) |_| {
236 if (try fileExists(loop.allocator, stdlib_path)) {
234237 self.include_dir = try std.mem.dupe(loop.allocator, u8, search_path);
235238 return;
236 } else |err| switch (err) {
237 error.NotFound, error.PermissionDenied => continue,
238 error.OutOfMemory => return error.OutOfMemory,
239 else => return error.FileSystem,
240239 }
241240 }
242241
243242 return error.LibCStdLibHeaderNotFound;
244243 }
245244
246 async fn findNativeIncludeDirWindows(self: *LibCInstallation, loop: *event.Loop) !void {
247 // TODO
248 //ZigWindowsSDK *sdk = get_windows_sdk(g);
249 //g->libc_include_dir = buf_alloc();
250 //if (os_get_win32_ucrt_include_path(sdk, g->libc_include_dir)) {
251 // fprintf(stderr, "Unable to determine libc include path. --libc-include-dir");
252 // exit(1);
253 //}
254 @panic("TODO");
245 async fn findNativeIncludeDirWindows(self: *LibCInstallation, loop: *event.Loop, sdk: *c.ZigWindowsSDK) !void {
246 var search_buf: [2]Search = undefined;
247 const searches = fillSearch(&search_buf, sdk);
248
249 var result_buf = try std.Buffer.initSize(loop.allocator, 0);
250 defer result_buf.deinit();
251
252 for (searches) |search| {
253 result_buf.shrink(0);
254 const stream = &std.io.BufferOutStream.init(&result_buf).stream;
255 try stream.print("{}\\Include\\{}\\ucrt", search.path, search.version);
256
257 const stdlib_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "stdlib.h");
258 defer loop.allocator.free(stdlib_path);
259
260 if (try fileExists(loop.allocator, stdlib_path)) {
261 self.include_dir = result_buf.toOwnedSlice();
262 return;
263 }
264 }
265
266 return error.LibCStdLibHeaderNotFound;
255267 }
256268
257 async fn findNativeLibDirWindows(self: *LibCInstallation, loop: *event.Loop) FindError!void {
258 // TODO
259 //ZigWindowsSDK *sdk = get_windows_sdk(g);
260
261 //if (g->msvc_lib_dir == nullptr) {
262 // Buf* vc_lib_dir = buf_alloc();
263 // if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) {
264 // fprintf(stderr, "Unable to determine vcruntime path. --msvc-lib-dir");
265 // exit(1);
266 // }
267 // g->msvc_lib_dir = vc_lib_dir;
268 //}
269
270 //if (g->libc_lib_dir == nullptr) {
271 // Buf* ucrt_lib_path = buf_alloc();
272 // if (os_get_win32_ucrt_lib_path(sdk, ucrt_lib_path, g->zig_target.arch.arch)) {
273 // fprintf(stderr, "Unable to determine ucrt path. --libc-lib-dir");
274 // exit(1);
275 // }
276 // g->libc_lib_dir = ucrt_lib_path;
277 //}
278
279 //if (g->kernel32_lib_dir == nullptr) {
280 // Buf* kern_lib_path = buf_alloc();
281 // if (os_get_win32_kern32_path(sdk, kern_lib_path, g->zig_target.arch.arch)) {
282 // fprintf(stderr, "Unable to determine kernel32 path. --kernel32-lib-dir");
283 // exit(1);
284 // }
285 // g->kernel32_lib_dir = kern_lib_path;
286 //}
287 @panic("TODO");
269 async fn findNativeLibDirWindows(self: *LibCInstallation, loop: *event.Loop, sdk: *c.ZigWindowsSDK) FindError!void {
270 var search_buf: [2]Search = undefined;
271 const searches = fillSearch(&search_buf, sdk);
272
273 var result_buf = try std.Buffer.initSize(loop.allocator, 0);
274 defer result_buf.deinit();
275
276 for (searches) |search| {
277 result_buf.shrink(0);
278 const stream = &std.io.BufferOutStream.init(&result_buf).stream;
279 try stream.print("{}\\Lib\\{}\\ucrt\\", search.path, search.version);
280 switch (builtin.arch) {
281 builtin.Arch.i386 => try stream.write("x86"),
282 builtin.Arch.x86_64 => try stream.write("x64"),
283 builtin.Arch.aarch64 => try stream.write("arm"),
284 else => return error.UnsupportedArchitecture,
285 }
286 const ucrt_lib_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "ucrt.lib");
287 defer loop.allocator.free(ucrt_lib_path);
288 if (try fileExists(loop.allocator, ucrt_lib_path)) {
289 self.lib_dir = result_buf.toOwnedSlice();
290 return;
291 }
292 }
293 return error.LibCRuntimeNotFound;
288294 }
289295
290296 async fn findNativeLibDirLinux(self: *LibCInstallation, loop: *event.Loop) FindError!void {
......@@ -330,17 +336,37 @@ pub const LibCInstallation = struct {
330336 dyn_test.result = result;
331337 return;
332338 } else |err| switch (err) {
333 error.CCompilerCannotFindCRuntime => return,
339 error.LibCRuntimeNotFound => return,
334340 else => return err,
335341 }
336342 }
337343
338 async fn findNativeMsvcLibDir(self: *LibCInstallation, loop: *event.Loop) FindError!void {
339 @panic("TODO");
340 }
341344
342 async fn findNativeKernel32LibDir(self: *LibCInstallation, loop: *event.Loop) FindError!void {
343 @panic("TODO");
345 async fn findNativeKernel32LibDir(self: *LibCInstallation, loop: *event.Loop, sdk: *c.ZigWindowsSDK) FindError!void {
346 var search_buf: [2]Search = undefined;
347 const searches = fillSearch(&search_buf, sdk);
348
349 var result_buf = try std.Buffer.initSize(loop.allocator, 0);
350 defer result_buf.deinit();
351
352 for (searches) |search| {
353 result_buf.shrink(0);
354 const stream = &std.io.BufferOutStream.init(&result_buf).stream;
355 try stream.print("{}\\Lib\\{}\\um\\", search.path, search.version);
356 switch (builtin.arch) {
357 builtin.Arch.i386 => try stream.write("x86\\"),
358 builtin.Arch.x86_64 => try stream.write("x64\\"),
359 builtin.Arch.aarch64 => try stream.write("arm\\"),
360 else => return error.UnsupportedArchitecture,
361 }
362 const kernel32_path = try std.os.path.join(loop.allocator, result_buf.toSliceConst(), "kernel32.lib");
363 defer loop.allocator.free(kernel32_path);
364 if (try fileExists(loop.allocator, kernel32_path)) {
365 self.kernel32_lib_dir = result_buf.toOwnedSlice();
366 return;
367 }
368 }
369 return error.LibCKernel32LibNotFound;
344370 }
345371
346372 fn initEmpty(self: *LibCInstallation) void {
......@@ -386,8 +412,8 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
386412 },
387413 }
388414 var it = std.mem.split(exec_result.stdout, "\n\r");
389 const line = it.next() orelse return error.CCompilerCannotFindCRuntime;
390 const dirname = std.os.path.dirname(line) orelse return error.CCompilerCannotFindCRuntime;
415 const line = it.next() orelse return error.LibCRuntimeNotFound;
416 const dirname = std.os.path.dirname(line) orelse return error.LibCRuntimeNotFound;
391417
392418 if (want_dirname) {
393419 return std.mem.dupe(loop.allocator, u8, dirname);
......@@ -395,3 +421,42 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
395421 return std.mem.dupe(loop.allocator, u8, line);
396422 }
397423}
424
425const Search = struct {
426 path: []const u8,
427 version: []const u8,
428};
429
430fn fillSearch(search_buf: *[2]Search, sdk: *c.ZigWindowsSDK) []Search {
431 var search_end: usize = 0;
432 if (sdk.path10_ptr) |path10_ptr| {
433 if (sdk.version10_ptr) |ver10_ptr| {
434 search_buf[search_end] = Search{
435 .path = path10_ptr[0..sdk.path10_len],
436 .version = ver10_ptr[0..sdk.version10_len],
437 };
438 search_end += 1;
439 }
440 }
441 if (sdk.path81_ptr) |path81_ptr| {
442 if (sdk.version81_ptr) |ver81_ptr| {
443 search_buf[search_end] = Search{
444 .path = path81_ptr[0..sdk.path81_len],
445 .version = ver81_ptr[0..sdk.version81_len],
446 };
447 search_end += 1;
448 }
449 }
450 return search_buf[0..search_end];
451}
452
453
454fn fileExists(allocator: *std.mem.Allocator, path: []const u8) !bool {
455 if (std.os.File.access(allocator, path)) |_| {
456 return true;
457 } else |err| switch (err) {
458 error.NotFound, error.PermissionDenied => return false,
459 error.OutOfMemory => return error.OutOfMemory,
460 else => return error.FileSystem,
461 }
462}
src-self-hosted/link.zig+144-2
......@@ -313,8 +313,150 @@ fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void {
313313 try ctx.args.append(full_path_with_null.ptr);
314314}
315315
316fn constructLinkerArgsCoff(ctx: *Context) void {
317 @panic("TODO");
316fn constructLinkerArgsCoff(ctx: *Context) !void {
317 try ctx.args.append(c"-NOLOGO");
318
319 if (!ctx.comp.strip) {
320 try ctx.args.append(c"-DEBUG");
321 }
322
323 switch (ctx.comp.target.getArch()) {
324 builtin.Arch.i386 => try ctx.args.append(c"-MACHINE:X86"),
325 builtin.Arch.x86_64 => try ctx.args.append(c"-MACHINE:X64"),
326 builtin.Arch.aarch64 => try ctx.args.append(c"-MACHINE:ARM"),
327 else => return error.UnsupportedLinkArchitecture,
328 }
329
330 if (ctx.comp.windows_subsystem_windows) {
331 try ctx.args.append(c"/SUBSYSTEM:windows");
332 } else if (ctx.comp.windows_subsystem_console) {
333 try ctx.args.append(c"/SUBSYSTEM:console");
334 }
335
336 const is_library = ctx.comp.kind == Compilation.Kind.Lib;
337
338 const out_arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-OUT:{}\x00", ctx.out_file_path.toSliceConst());
339 try ctx.args.append(out_arg.ptr);
340
341 if (ctx.comp.haveLibC()) {
342 try ctx.args.append((try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", ctx.libc.msvc_lib_dir.?)).ptr);
343 try ctx.args.append((try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", ctx.libc.kernel32_lib_dir.?)).ptr);
344 try ctx.args.append((try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", ctx.libc.lib_dir.?)).ptr);
345 }
346
347 if (ctx.link_in_crt) {
348 const lib_str = if (ctx.comp.is_static) "lib" else "";
349 const d_str = if (ctx.comp.build_mode == builtin.Mode.Debug) "d" else "";
350
351 if (ctx.comp.is_static) {
352 const cmt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "libcmt{}.lib\x00", d_str);
353 try ctx.args.append(cmt_lib_name.ptr);
354 } else {
355 const msvcrt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "msvcrt{}.lib\x00", d_str);
356 try ctx.args.append(msvcrt_lib_name.ptr);
357 }
358
359 const vcruntime_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "{}vcruntime{}.lib\x00", lib_str, d_str);
360 try ctx.args.append(vcruntime_lib_name.ptr);
361
362 const crt_lib_name = try std.fmt.allocPrint(&ctx.arena.allocator, "{}ucrt{}.lib\x00", lib_str, d_str);
363 try ctx.args.append(crt_lib_name.ptr);
364
365 // Visual C++ 2015 Conformance Changes
366 // https://msdn.microsoft.com/en-us/library/bb531344.aspx
367 try ctx.args.append(c"legacy_stdio_definitions.lib");
368
369 // msvcrt depends on kernel32
370 try ctx.args.append(c"kernel32.lib");
371 } else {
372 try ctx.args.append(c"-NODEFAULTLIB");
373 if (!is_library) {
374 try ctx.args.append(c"-ENTRY:WinMainCRTStartup");
375 // TODO
376 //if (g->have_winmain) {
377 // lj->args.append("-ENTRY:WinMain");
378 //} else {
379 // lj->args.append("-ENTRY:WinMainCRTStartup");
380 //}
381 }
382 }
383
384 if (is_library and !ctx.comp.is_static) {
385 try ctx.args.append(c"-DLL");
386 }
387
388 //for (size_t i = 0; i < g->lib_dirs.length; i += 1) {
389 // const char *lib_dir = g->lib_dirs.at(i);
390 // lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", lib_dir)));
391 //}
392
393 for (ctx.comp.link_objects) |link_object| {
394 const link_obj_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, link_object);
395 try ctx.args.append(link_obj_with_null.ptr);
396 }
397 try addFnObjects(ctx);
398
399 switch (ctx.comp.kind) {
400 Compilation.Kind.Exe, Compilation.Kind.Lib => {
401 if (!ctx.comp.haveLibC()) {
402 @panic("TODO");
403 //Buf *builtin_o_path = build_o(g, "builtin");
404 //lj->args.append(buf_ptr(builtin_o_path));
405 }
406
407 // msvc compiler_rt is missing some stuff, so we still build it and rely on weak linkage
408 // TODO
409 //Buf *compiler_rt_o_path = build_compiler_rt(g);
410 //lj->args.append(buf_ptr(compiler_rt_o_path));
411 },
412 Compilation.Kind.Obj => {},
413 }
414
415 //Buf *def_contents = buf_alloc();
416 //ZigList<const char *> gen_lib_args = {0};
417 //for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {
418 // LinkLib *link_lib = g->link_libs_list.at(lib_i);
419 // if (buf_eql_str(link_lib->name, "c")) {
420 // continue;
421 // }
422 // if (link_lib->provided_explicitly) {
423 // if (lj->codegen->zig_target.env_type == ZigLLVM_GNU) {
424 // Buf *arg = buf_sprintf("-l%s", buf_ptr(link_lib->name));
425 // lj->args.append(buf_ptr(arg));
426 // }
427 // else {
428 // lj->args.append(buf_ptr(link_lib->name));
429 // }
430 // } else {
431 // buf_resize(def_contents, 0);
432 // buf_appendf(def_contents, "LIBRARY %s\nEXPORTS\n", buf_ptr(link_lib->name));
433 // for (size_t exp_i = 0; exp_i < link_lib->symbols.length; exp_i += 1) {
434 // Buf *symbol_name = link_lib->symbols.at(exp_i);
435 // buf_appendf(def_contents, "%s\n", buf_ptr(symbol_name));
436 // }
437 // buf_appendf(def_contents, "\n");
438
439 // Buf *def_path = buf_alloc();
440 // os_path_join(g->cache_dir, buf_sprintf("%s.def", buf_ptr(link_lib->name)), def_path);
441 // os_write_file(def_path, def_contents);
442
443 // Buf *generated_lib_path = buf_alloc();
444 // os_path_join(g->cache_dir, buf_sprintf("%s.lib", buf_ptr(link_lib->name)), generated_lib_path);
445
446 // gen_lib_args.resize(0);
447 // gen_lib_args.append("link");
448
449 // coff_append_machine_arg(g, &gen_lib_args);
450 // gen_lib_args.append(buf_ptr(buf_sprintf("-DEF:%s", buf_ptr(def_path))));
451 // gen_lib_args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(generated_lib_path))));
452 // Buf diag = BUF_INIT;
453 // if (!zig_lld_link(g->zig_target.oformat, gen_lib_args.items, gen_lib_args.length, &diag)) {
454 // fprintf(stderr, "%s\n", buf_ptr(&diag));
455 // exit(1);
456 // }
457 // lj->args.append(buf_ptr(generated_lib_path));
458 // }
459 //}
318460}
319461
320462fn constructLinkerArgsMachO(ctx: *Context) !void {
src/windows_sdk.cpp+8-2
......@@ -287,7 +287,10 @@ ZigFindWindowsSdkError zig_find_windows_sdk(struct ZigWindowsSDK **out_sdk) {
287287 }
288288 rc = RegQueryValueEx(key, "KitsRoot10", NULL, NULL, (LPBYTE)priv->base.path10_ptr, &tmp_buf_len);
289289 if (rc == ERROR_SUCCESS) {
290 priv->base.path10_len = tmp_buf_len;
290 priv->base.path10_len = tmp_buf_len - 1;
291 if (priv->base.path10_ptr[priv->base.path10_len - 1] == '\\') {
292 priv->base.path10_len -= 1;
293 }
291294 } else {
292295 free((void*)priv->base.path10_ptr);
293296 priv->base.path10_ptr = nullptr;
......@@ -302,7 +305,10 @@ ZigFindWindowsSdkError zig_find_windows_sdk(struct ZigWindowsSDK **out_sdk) {
302305 }
303306 rc = RegQueryValueEx(key, "KitsRoot81", NULL, NULL, (LPBYTE)priv->base.path81_ptr, &tmp_buf_len);
304307 if (rc == ERROR_SUCCESS) {
305 priv->base.path81_len = tmp_buf_len;
308 priv->base.path81_len = tmp_buf_len - 1;
309 if (priv->base.path81_ptr[priv->base.path81_len - 1] == '\\') {
310 priv->base.path81_len -= 1;
311 }
306312 } else {
307313 free((void*)priv->base.path81_ptr);
308314 priv->base.path81_ptr = nullptr;
std/os/file.zig+3-1
......@@ -139,7 +139,9 @@ pub const File = struct {
139139
140140 const err = windows.GetLastError();
141141 switch (err) {
142 windows.ERROR.FILE_NOT_FOUND => return error.NotFound,
142 windows.ERROR.FILE_NOT_FOUND,
143 windows.ERROR.PATH_NOT_FOUND,
144 => return error.NotFound,
143145 windows.ERROR.ACCESS_DENIED => return error.PermissionDenied,
144146 else => return os.unexpectedErrorWindows(err),
145147 }