authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-23 09:56:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-23 09:56:30-07:00
logd03fcc73fc7083558915f6c170432fce8fb84993
tree106c14aad3ac777dd9e41426cf8606ddaa747ff8
parent15b2bae517c54db22604eb303645d2f0023768e0

stage2: implement --main-pkg-path


3 files changed, 23 insertions(+), 6 deletions(-)

BRANCH_TODO-1
......@@ -1,4 +1,3 @@
1 * --main-pkg-path
21 * add CLI support for a way to pass extra flags to c source files
32 * musl
43 * support rpaths in ELF linker code
src/Compilation.zig+4-2
......@@ -2248,10 +2248,12 @@ fn updateStage1Module(comp: *Compilation) !void {
22482248
22492249 comp.stage1_cache_hash = &ch;
22502250
2251 const main_pkg_path = mod.root_pkg.root_src_directory.path orelse "";
2252
22512253 const stage1_module = stage1.create(
22522254 @enumToInt(comp.bin_file.options.optimize_mode),
2253 undefined,
2254 0, // TODO --main-pkg-path
2255 main_pkg_path.ptr,
2256 main_pkg_path.len,
22552257 main_zig_file.ptr,
22562258 main_zig_file.len,
22572259 zig_lib_dir.ptr,
src/main.zig+19-3
......@@ -217,6 +217,7 @@ const usage_build_generic =
217217 \\ ReleaseSmall Optimize for small binary, safety off
218218 \\ --pkg-begin [name] [path] Make pkg available to import and push current pkg
219219 \\ --pkg-end Pop current pkg
220 \\ --main-pkg-path Set the directory of the root package
220221 \\ -fPIC Force-enable Position Independent Code
221222 \\ -fno-PIC Force-disable Position Independent Code
222223 \\ -fstack-check Enable stack probing in unsafe builds
......@@ -234,8 +235,8 @@ const usage_build_generic =
234235 \\ c Compile to C source code
235236 \\ wasm WebAssembly
236237 \\ pe Portable Executable (Windows)
237 \\ coff (planned) Common Object File Format (Windows)
238 \\ macho (planned) macOS relocatables
238 \\ coff Common Object File Format (Windows)
239 \\ macho macOS relocatables
239240 \\ hex (planned) Intel IHEX
240241 \\ raw (planned) Dump machine code directly
241242 \\ -dirafter [dir] Add directory to AFTER include search path
......@@ -367,6 +368,7 @@ pub fn buildOutputType(
367368 var override_local_cache_dir: ?[]const u8 = null;
368369 var override_global_cache_dir: ?[]const u8 = null;
369370 var override_lib_dir: ?[]const u8 = null;
371 var main_pkg_path: ?[]const u8 = null;
370372
371373 var system_libs = std.ArrayList([]const u8).init(gpa);
372374 defer system_libs.deinit();
......@@ -463,6 +465,10 @@ pub fn buildOutputType(
463465 } else if (mem.eql(u8, arg, "--pkg-end")) {
464466 cur_pkg = cur_pkg.parent orelse
465467 fatal("encountered --pkg-end with no matching --pkg-begin", .{});
468 } else if (mem.eql(u8, arg, "--main-pkg-path")) {
469 if (i + 1 >= args.len) fatal("expected parameter after {}", .{arg});
470 i += 1;
471 main_pkg_path = args[i];
466472 } else if (mem.eql(u8, arg, "--color")) {
467473 if (i + 1 >= args.len) {
468474 fatal("expected [auto|on|off] after --color", .{});
......@@ -1248,8 +1254,18 @@ pub fn buildOutputType(
12481254 .yes => |p| p,
12491255 };
12501256
1257 var cleanup_root_dir: ?fs.Dir = null;
1258 defer if (cleanup_root_dir) |*dir| dir.close();
1259
12511260 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {
1252 root_pkg_memory.root_src_directory = .{ .path = null, .handle = fs.cwd() };
1261 root_pkg_memory.root_src_directory = m: {
1262 if (main_pkg_path) |p| {
1263 const dir = try fs.cwd().openDir(p, .{});
1264 cleanup_root_dir = dir;
1265 break :m .{ .path = p, .handle = dir };
1266 }
1267 break :m .{ .path = null, .handle = fs.cwd() };
1268 };
12531269 root_pkg_memory.root_src_path = src_path;
12541270 break :blk &root_pkg_memory;
12551271 } else null;