authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-22 23:00:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-22 23:00:33-07:00
log0638a020cf783486724d1999a4847140c6cc1442
treee3ff48e630664a8e826f34816ced8ebba0bcfe18
parentc2b1cd7c456e274c7ead96e597d147e2df7d317e

stage2: implement --pkg-begin and --pkg-end CLI args


4 files changed, 41 insertions(+), 10 deletions(-)

BRANCH_TODO+2-1
......@@ -1,3 +1,4 @@
1 * separate libzigcpp.a and libzigstage1.a so that we can do non-stage1 builds
12 * repair @cImport
23 * make sure zig cc works
34 - using it as a preprocessor (-E)
......@@ -19,7 +20,6 @@
1920 * COFF LLD linking
2021 * WASM LLD linking
2122 * --main-pkg-path
22 * --pkg-begin, --pkg-end
2323 * skip LLD caching when bin directory is not in the cache (so we don't put `id.txt` into the cwd)
2424 (maybe make it an explicit option and have main.zig disable it)
2525 * audit the CLI options for stage2
......@@ -60,3 +60,4 @@
6060 in builtin.zig
6161 * rename std.builtin.Mode to std.builtin.OptimizeMode
6262 * implement `zig run` and `zig test` when combined with `--watch`
63 * close the --pkg-begin --pkg-end Package directory handles
lib/std/build.zig-4
......@@ -1950,7 +1950,6 @@ pub const LibExeObjStep = struct {
19501950 for (self.link_objects.span()) |link_object| {
19511951 switch (link_object) {
19521952 .StaticPath => |static_path| {
1953 try zig_args.append("--object");
19541953 try zig_args.append(builder.pathFromRoot(static_path));
19551954 },
19561955
......@@ -1958,12 +1957,10 @@ pub const LibExeObjStep = struct {
19581957 .Exe => unreachable,
19591958 .Test => unreachable,
19601959 .Obj => {
1961 try zig_args.append("--object");
19621960 try zig_args.append(other.getOutputPath());
19631961 },
19641962 .Lib => {
19651963 if (!other.is_dynamic or self.target.isWindows()) {
1966 try zig_args.append("--object");
19671964 try zig_args.append(other.getOutputLibPath());
19681965 } else {
19691966 const full_path_lib = other.getOutputPath();
......@@ -1982,7 +1979,6 @@ pub const LibExeObjStep = struct {
19821979 try zig_args.append(name);
19831980 },
19841981 .AssemblyFile => |asm_file| {
1985 try zig_args.append("--c-source");
19861982 try zig_args.append(asm_file.getPath(builder));
19871983 },
19881984 .CSourceFile => |c_source_file| {
src/Package.zig+1
......@@ -4,6 +4,7 @@ root_src_directory: Compilation.Directory,
44/// Relative to `root_src_directory`. May contain path separators.
55root_src_path: []const u8,
66table: Table = .{},
7parent: ?*Package = null,
78
89const std = @import("std");
910const mem = std.mem;
src/main.zig+38-5
......@@ -215,6 +215,8 @@ const usage_build_generic =
215215 \\ ReleaseFast Optimizations on, safety off
216216 \\ ReleaseSafe Optimizations on, safety on
217217 \\ ReleaseSmall Optimize for small binary, safety off
218 \\ --pkg-begin [name] [path] Make pkg available to import and push current pkg
219 \\ --pkg-end Pop current pkg
218220 \\ -fPIC Force-enable Position Independent Code
219221 \\ -fno-PIC Force-disable Position Independent Code
220222 \\ -fstack-check Enable stack probing in unsafe builds
......@@ -397,6 +399,13 @@ pub fn buildOutputType(
397399 var test_exec_args = std.ArrayList(?[]const u8).init(gpa);
398400 defer test_exec_args.deinit();
399401
402 var root_pkg_memory: Package = .{
403 .root_src_directory = undefined,
404 .root_src_path = undefined,
405 };
406 defer root_pkg_memory.table.deinit(gpa);
407 var cur_pkg: *Package = &root_pkg_memory;
408
400409 switch (arg_mode) {
401410 .build, .translate_c, .zig_test, .run => {
402411 output_mode = switch (arg_mode) {
......@@ -427,6 +436,33 @@ pub fn buildOutputType(
427436 } else {
428437 fatal("unexpected end-of-parameter mark: --", .{});
429438 }
439 } else if (mem.eql(u8, arg, "--pkg-begin")) {
440 if (i + 2 >= args.len) fatal("Expected 2 arguments after {}", .{arg});
441 i += 1;
442 const pkg_name = args[i];
443 i += 1;
444 const pkg_path = args[i];
445
446 const new_cur_pkg = try arena.create(Package);
447 new_cur_pkg.* = .{
448 .root_src_directory = if (fs.path.dirname(pkg_path)) |dirname|
449 .{
450 .path = dirname,
451 .handle = try fs.cwd().openDir(dirname, .{}), // TODO close this fd
452 }
453 else
454 .{
455 .path = null,
456 .handle = fs.cwd(),
457 },
458 .root_src_path = fs.path.basename(pkg_path),
459 .parent = cur_pkg,
460 };
461 try cur_pkg.table.put(gpa, pkg_name, new_cur_pkg);
462 cur_pkg = new_cur_pkg;
463 } else if (mem.eql(u8, arg, "--pkg-end")) {
464 cur_pkg = cur_pkg.parent orelse
465 fatal("encountered --pkg-end with no matching --pkg-begin", .{});
430466 } else if (mem.eql(u8, arg, "--color")) {
431467 if (i + 1 >= args.len) {
432468 fatal("expected [auto|on|off] after --color", .{});
......@@ -1212,12 +1248,9 @@ pub fn buildOutputType(
12121248 .yes => |p| p,
12131249 };
12141250
1215 var root_pkg_memory: Package = undefined;
12161251 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {
1217 root_pkg_memory = .{
1218 .root_src_directory = .{ .path = null, .handle = fs.cwd() },
1219 .root_src_path = src_path,
1220 };
1252 root_pkg_memory.root_src_directory = .{ .path = null, .handle = fs.cwd() };
1253 root_pkg_memory.root_src_path = src_path;
12211254 break :blk &root_pkg_memory;
12221255 } else null;
12231256