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,3 +1,4 @@
1 * separate libzigcpp.a and libzigstage1.a so that we can do non-stage1 builds
1 * repair @cImport2 * repair @cImport
2 * make sure zig cc works3 * make sure zig cc works
3 - using it as a preprocessor (-E)4 - using it as a preprocessor (-E)
...@@ -19,7 +20,6 @@...@@ -19,7 +20,6 @@
19 * COFF LLD linking20 * COFF LLD linking
20 * WASM LLD linking21 * WASM LLD linking
21 * --main-pkg-path22 * --main-pkg-path
22 * --pkg-begin, --pkg-end
23 * skip LLD caching when bin directory is not in the cache (so we don't put `id.txt` into the cwd)23 * skip LLD caching when bin directory is not in the cache (so we don't put `id.txt` into the cwd)
24 (maybe make it an explicit option and have main.zig disable it)24 (maybe make it an explicit option and have main.zig disable it)
25 * audit the CLI options for stage225 * audit the CLI options for stage2
...@@ -60,3 +60,4 @@...@@ -60,3 +60,4 @@
60 in builtin.zig60 in builtin.zig
61 * rename std.builtin.Mode to std.builtin.OptimizeMode61 * rename std.builtin.Mode to std.builtin.OptimizeMode
62 * implement `zig run` and `zig test` when combined with `--watch`62 * 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 {...@@ -1950,7 +1950,6 @@ pub const LibExeObjStep = struct {
1950 for (self.link_objects.span()) |link_object| {1950 for (self.link_objects.span()) |link_object| {
1951 switch (link_object) {1951 switch (link_object) {
1952 .StaticPath => |static_path| {1952 .StaticPath => |static_path| {
1953 try zig_args.append("--object");
1954 try zig_args.append(builder.pathFromRoot(static_path));1953 try zig_args.append(builder.pathFromRoot(static_path));
1955 },1954 },
19561955
...@@ -1958,12 +1957,10 @@ pub const LibExeObjStep = struct {...@@ -1958,12 +1957,10 @@ pub const LibExeObjStep = struct {
1958 .Exe => unreachable,1957 .Exe => unreachable,
1959 .Test => unreachable,1958 .Test => unreachable,
1960 .Obj => {1959 .Obj => {
1961 try zig_args.append("--object");
1962 try zig_args.append(other.getOutputPath());1960 try zig_args.append(other.getOutputPath());
1963 },1961 },
1964 .Lib => {1962 .Lib => {
1965 if (!other.is_dynamic or self.target.isWindows()) {1963 if (!other.is_dynamic or self.target.isWindows()) {
1966 try zig_args.append("--object");
1967 try zig_args.append(other.getOutputLibPath());1964 try zig_args.append(other.getOutputLibPath());
1968 } else {1965 } else {
1969 const full_path_lib = other.getOutputPath();1966 const full_path_lib = other.getOutputPath();
...@@ -1982,7 +1979,6 @@ pub const LibExeObjStep = struct {...@@ -1982,7 +1979,6 @@ pub const LibExeObjStep = struct {
1982 try zig_args.append(name);1979 try zig_args.append(name);
1983 },1980 },
1984 .AssemblyFile => |asm_file| {1981 .AssemblyFile => |asm_file| {
1985 try zig_args.append("--c-source");
1986 try zig_args.append(asm_file.getPath(builder));1982 try zig_args.append(asm_file.getPath(builder));
1987 },1983 },
1988 .CSourceFile => |c_source_file| {1984 .CSourceFile => |c_source_file| {
src/Package.zig+1
...@@ -4,6 +4,7 @@ root_src_directory: Compilation.Directory,...@@ -4,6 +4,7 @@ root_src_directory: Compilation.Directory,
4/// Relative to `root_src_directory`. May contain path separators.4/// Relative to `root_src_directory`. May contain path separators.
5root_src_path: []const u8,5root_src_path: []const u8,
6table: Table = .{},6table: Table = .{},
7parent: ?*Package = null,
78
8const std = @import("std");9const std = @import("std");
9const mem = std.mem;10const mem = std.mem;
src/main.zig+38-5
...@@ -215,6 +215,8 @@ const usage_build_generic =...@@ -215,6 +215,8 @@ const usage_build_generic =
215 \\ ReleaseFast Optimizations on, safety off215 \\ ReleaseFast Optimizations on, safety off
216 \\ ReleaseSafe Optimizations on, safety on216 \\ ReleaseSafe Optimizations on, safety on
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 pkg
219 \\ --pkg-end Pop current pkg
218 \\ -fPIC Force-enable Position Independent Code220 \\ -fPIC Force-enable Position Independent Code
219 \\ -fno-PIC Force-disable Position Independent Code221 \\ -fno-PIC Force-disable Position Independent Code
220 \\ -fstack-check Enable stack probing in unsafe builds222 \\ -fstack-check Enable stack probing in unsafe builds
...@@ -397,6 +399,13 @@ pub fn buildOutputType(...@@ -397,6 +399,13 @@ pub fn buildOutputType(
397 var test_exec_args = std.ArrayList(?[]const u8).init(gpa);399 var test_exec_args = std.ArrayList(?[]const u8).init(gpa);
398 defer test_exec_args.deinit();400 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
400 switch (arg_mode) {409 switch (arg_mode) {
401 .build, .translate_c, .zig_test, .run => {410 .build, .translate_c, .zig_test, .run => {
402 output_mode = switch (arg_mode) {411 output_mode = switch (arg_mode) {
...@@ -427,6 +436,33 @@ pub fn buildOutputType(...@@ -427,6 +436,33 @@ pub fn buildOutputType(
427 } else {436 } else {
428 fatal("unexpected end-of-parameter mark: --", .{});437 fatal("unexpected end-of-parameter mark: --", .{});
429 }438 }
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", .{});
430 } else if (mem.eql(u8, arg, "--color")) {466 } else if (mem.eql(u8, arg, "--color")) {
431 if (i + 1 >= args.len) {467 if (i + 1 >= args.len) {
432 fatal("expected [auto|on|off] after --color", .{});468 fatal("expected [auto|on|off] after --color", .{});
...@@ -1212,12 +1248,9 @@ pub fn buildOutputType(...@@ -1212,12 +1248,9 @@ pub fn buildOutputType(
1212 .yes => |p| p,1248 .yes => |p| p,
1213 };1249 };
12141250
1215 var root_pkg_memory: Package = undefined;
1216 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {1251 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {
1217 root_pkg_memory = .{1252 root_pkg_memory.root_src_directory = .{ .path = null, .handle = fs.cwd() };
1218 .root_src_directory = .{ .path = null, .handle = fs.cwd() },1253 root_pkg_memory.root_src_path = src_path;
1219 .root_src_path = src_path,
1220 };
1221 break :blk &root_pkg_memory;1254 break :blk &root_pkg_memory;
1222 } else null;1255 } else null;
12231256