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,4 +1,3 @@
1 * --main-pkg-path
2 * add CLI support for a way to pass extra flags to c source files1 * add CLI support for a way to pass extra flags to c source files
3 * musl2 * musl
4 * support rpaths in ELF linker code3 * support rpaths in ELF linker code
src/Compilation.zig+4-2
...@@ -2248,10 +2248,12 @@ fn updateStage1Module(comp: *Compilation) !void {...@@ -2248,10 +2248,12 @@ fn updateStage1Module(comp: *Compilation) !void {
22482248
2249 comp.stage1_cache_hash = &ch;2249 comp.stage1_cache_hash = &ch;
22502250
2251 const main_pkg_path = mod.root_pkg.root_src_directory.path orelse "";
2252
2251 const stage1_module = stage1.create(2253 const stage1_module = stage1.create(
2252 @enumToInt(comp.bin_file.options.optimize_mode),2254 @enumToInt(comp.bin_file.options.optimize_mode),
2253 undefined,2255 main_pkg_path.ptr,
2254 0, // TODO --main-pkg-path2256 main_pkg_path.len,
2255 main_zig_file.ptr,2257 main_zig_file.ptr,
2256 main_zig_file.len,2258 main_zig_file.len,
2257 zig_lib_dir.ptr,2259 zig_lib_dir.ptr,
src/main.zig+19-3
...@@ -217,6 +217,7 @@ const usage_build_generic =...@@ -217,6 +217,7 @@ const usage_build_generic =
217 \\ ReleaseSmall Optimize for small binary, safety off217 \\ ReleaseSmall Optimize for small binary, safety off
218 \\ --pkg-begin [name] [path] Make pkg available to import and push current pkg218 \\ --pkg-begin [name] [path] Make pkg available to import and push current pkg
219 \\ --pkg-end Pop current pkg219 \\ --pkg-end Pop current pkg
220 \\ --main-pkg-path Set the directory of the root package
220 \\ -fPIC Force-enable Position Independent Code221 \\ -fPIC Force-enable Position Independent Code
221 \\ -fno-PIC Force-disable Position Independent Code222 \\ -fno-PIC Force-disable Position Independent Code
222 \\ -fstack-check Enable stack probing in unsafe builds223 \\ -fstack-check Enable stack probing in unsafe builds
...@@ -234,8 +235,8 @@ const usage_build_generic =...@@ -234,8 +235,8 @@ const usage_build_generic =
234 \\ c Compile to C source code235 \\ c Compile to C source code
235 \\ wasm WebAssembly236 \\ wasm WebAssembly
236 \\ pe Portable Executable (Windows)237 \\ pe Portable Executable (Windows)
237 \\ coff (planned) Common Object File Format (Windows)238 \\ coff Common Object File Format (Windows)
238 \\ macho (planned) macOS relocatables239 \\ macho macOS relocatables
239 \\ hex (planned) Intel IHEX240 \\ hex (planned) Intel IHEX
240 \\ raw (planned) Dump machine code directly241 \\ raw (planned) Dump machine code directly
241 \\ -dirafter [dir] Add directory to AFTER include search path242 \\ -dirafter [dir] Add directory to AFTER include search path
...@@ -367,6 +368,7 @@ pub fn buildOutputType(...@@ -367,6 +368,7 @@ pub fn buildOutputType(
367 var override_local_cache_dir: ?[]const u8 = null;368 var override_local_cache_dir: ?[]const u8 = null;
368 var override_global_cache_dir: ?[]const u8 = null;369 var override_global_cache_dir: ?[]const u8 = null;
369 var override_lib_dir: ?[]const u8 = null;370 var override_lib_dir: ?[]const u8 = null;
371 var main_pkg_path: ?[]const u8 = null;
370372
371 var system_libs = std.ArrayList([]const u8).init(gpa);373 var system_libs = std.ArrayList([]const u8).init(gpa);
372 defer system_libs.deinit();374 defer system_libs.deinit();
...@@ -463,6 +465,10 @@ pub fn buildOutputType(...@@ -463,6 +465,10 @@ pub fn buildOutputType(
463 } else if (mem.eql(u8, arg, "--pkg-end")) {465 } else if (mem.eql(u8, arg, "--pkg-end")) {
464 cur_pkg = cur_pkg.parent orelse466 cur_pkg = cur_pkg.parent orelse
465 fatal("encountered --pkg-end with no matching --pkg-begin", .{});467 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];
466 } else if (mem.eql(u8, arg, "--color")) {472 } else if (mem.eql(u8, arg, "--color")) {
467 if (i + 1 >= args.len) {473 if (i + 1 >= args.len) {
468 fatal("expected [auto|on|off] after --color", .{});474 fatal("expected [auto|on|off] after --color", .{});
...@@ -1248,8 +1254,18 @@ pub fn buildOutputType(...@@ -1248,8 +1254,18 @@ pub fn buildOutputType(
1248 .yes => |p| p,1254 .yes => |p| p,
1249 };1255 };
12501256
1257 var cleanup_root_dir: ?fs.Dir = null;
1258 defer if (cleanup_root_dir) |*dir| dir.close();
1259
1251 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {1260 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 };
1253 root_pkg_memory.root_src_path = src_path;1269 root_pkg_memory.root_src_path = src_path;
1254 break :blk &root_pkg_memory;1270 break :blk &root_pkg_memory;
1255 } else null;1271 } else null;