authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-01 16:35:10-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-01 16:35:10-04:00
log3cbd0065fa4b0f659dfb1733b25bd09e37442d09
tree2a732cbe2cd3cbd0fd804db39885e5c57117591e
parent17b935325e7c315304952f38037c7200595c5f10

basic support for specifying packages at the command line

See #226

7 files changed, 112 insertions(+), 7 deletions(-)

src/main.cpp+65-3
......@@ -36,6 +36,8 @@ static int usage(const char *arg0) {
3636 " --name [name] override output name\n"
3737 " --output [file] override destination path\n"
3838 " --output-h [file] override generated header file path\n"
39 " --pkg-begin [name] [path] make package available to import and push current pkg\n"
40 " --pkg-end pop current pkg\n"
3941 " --release build with optimizations on and debug protection off\n"
4042 " --static output will be statically linked\n"
4143 " --strip exclude debug symbols\n"
......@@ -121,6 +123,36 @@ enum Cmd {
121123
122124static const char *default_zig_cache_name = "zig-cache";
123125
126struct CliPkg {
127 const char *name;
128 const char *path;
129 ZigList<CliPkg *> children;
130 CliPkg *parent;
131};
132
133static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
134 for (size_t i = 0; i < cli_pkg->children.length; i += 1) {
135 CliPkg *child_cli_pkg = cli_pkg->children.at(i);
136
137 Buf *dirname = buf_alloc();
138 Buf *basename = buf_alloc();
139 os_path_split(buf_create_from_str(child_cli_pkg->path), dirname, basename);
140
141 PackageTableEntry *child_pkg = codegen_create_package(g, buf_ptr(dirname), buf_ptr(basename));
142 auto entry = pkg->package_table.put_unique(buf_create_from_str(child_cli_pkg->name), child_pkg);
143 if (entry) {
144 PackageTableEntry *existing_pkg = entry->value;
145 Buf *full_path = buf_alloc();
146 os_path_join(&existing_pkg->root_src_dir, &existing_pkg->root_src_path, full_path);
147 fprintf(stderr, "Unable to add package '%s'->'%s': already exists as '%s'\n",
148 child_cli_pkg->name, child_cli_pkg->path, buf_ptr(full_path));
149 exit(EXIT_FAILURE);
150 }
151
152 add_package(g, child_cli_pkg, child_pkg);
153 }
154}
155
124156int main(int argc, char **argv) {
125157 os_init();
126158
......@@ -168,6 +200,7 @@ int main(int argc, char **argv) {
168200 size_t ver_patch = 0;
169201 bool timing_info = false;
170202 const char *cache_dir = nullptr;
203 CliPkg *cur_pkg = allocate<CliPkg>(1);
171204
172205 if (argc >= 2 && strcmp(argv[1], "build") == 0) {
173206 const char *zig_exe_path = arg0;
......@@ -309,13 +342,31 @@ int main(int argc, char **argv) {
309342 } else if (arg[1] == 'L' && arg[2] != 0) {
310343 // alias for --library-path
311344 lib_dirs.append(&arg[2]);
345 } else if (strcmp(arg, "--pkg-begin") == 0) {
346 if (i + 2 >= argc) {
347 fprintf(stderr, "Expected 2 arguments after --pkg-begin\n");
348 return usage(arg0);
349 }
350 CliPkg *new_cur_pkg = allocate<CliPkg>(1);
351 i += 1;
352 new_cur_pkg->name = argv[i];
353 i += 1;
354 new_cur_pkg->path = argv[i];
355 new_cur_pkg->parent = cur_pkg;
356 cur_pkg->children.append(new_cur_pkg);
357 cur_pkg = new_cur_pkg;
358 } else if (strcmp(arg, "--pkg-end") == 0) {
359 if (cur_pkg->parent == nullptr) {
360 fprintf(stderr, "Encountered --pkg-end with no matching --pkg-begin\n");
361 return EXIT_FAILURE;
362 }
363 cur_pkg = cur_pkg->parent;
312364 } else if (i + 1 >= argc) {
365 fprintf(stderr, "Expected another argument after %s\n", arg);
313366 return usage(arg0);
314367 } else {
315368 i += 1;
316 if (i >= argc) {
317 return usage(arg0);
318 } else if (strcmp(arg, "--output") == 0) {
369 if (strcmp(arg, "--output") == 0) {
319370 out_file = argv[i];
320371 } else if (strcmp(arg, "--output-h") == 0) {
321372 out_file_h = argv[i];
......@@ -327,6 +378,7 @@ int main(int argc, char **argv) {
327378 } else if (strcmp(argv[i], "off") == 0) {
328379 color = ErrColorOff;
329380 } else {
381 fprintf(stderr, "--color options are 'auto', 'on', or 'off'\n");
330382 return usage(arg0);
331383 }
332384 } else if (strcmp(arg, "--name") == 0) {
......@@ -421,11 +473,13 @@ int main(int argc, char **argv) {
421473 if (!in_file) {
422474 in_file = arg;
423475 } else {
476 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
424477 return usage(arg0);
425478 }
426479 break;
427480 case CmdVersion:
428481 case CmdTargets:
482 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
429483 return usage(arg0);
430484 case CmdInvalid:
431485 zig_unreachable();
......@@ -433,6 +487,11 @@ int main(int argc, char **argv) {
433487 }
434488 }
435489
490 if (cur_pkg->parent != nullptr) {
491 fprintf(stderr, "Unmatched --pkg-begin\n");
492 return EXIT_FAILURE;
493 }
494
436495 switch (cmd) {
437496 case CmdBuild:
438497 case CmdParseH:
......@@ -579,6 +638,9 @@ int main(int argc, char **argv) {
579638 if (out_file_h)
580639 codegen_set_output_h_path(g, buf_create_from_str(out_file_h));
581640
641
642 add_package(g, cur_pkg, g->root_package);
643
582644 if (cmd == CmdBuild) {
583645 for (size_t i = 0; i < objects.length; i += 1) {
584646 codegen_add_object(g, buf_create_from_str(objects.at(i)));
std/build.zig+26-3
......@@ -679,6 +679,12 @@ pub const LibExeObjStep = struct {
679679 name_only_filename: []const u8,
680680 object_files: List([]const u8),
681681 assembly_files: List([]const u8),
682 packages: List(Pkg),
683
684 const Pkg = struct {
685 name: []const u8,
686 path: []const u8,
687 };
682688
683689 const Kind = enum {
684690 Exe,
......@@ -736,6 +742,7 @@ pub const LibExeObjStep = struct {
736742 .name_only_filename = undefined,
737743 .object_files = List([]const u8).init(builder.allocator),
738744 .assembly_files = List([]const u8).init(builder.allocator),
745 .packages = List(Pkg).init(builder.allocator),
739746 };
740747 self.computeOutFileNames();
741748 return self;
......@@ -835,6 +842,13 @@ pub const LibExeObjStep = struct {
835842 %%self.object_files.append(obj.getOutputPath());
836843 }
837844
845 pub fn addPackagePath(self: &LibExeObjStep, name: []const u8, pkg_index_path: []const u8) {
846 %%self.packages.append(Pkg {
847 .name = name,
848 .path = pkg_index_path,
849 });
850 }
851
838852 fn make(step: &Step) -> %void {
839853 const self = @fieldParentPtr(LibExeObjStep, "step", step);
840854 const builder = self.builder;
......@@ -883,9 +897,11 @@ pub const LibExeObjStep = struct {
883897 %%zig_args.append("--output");
884898 %%zig_args.append(output_path);
885899
886 const output_h_path = self.getOutputHPath();
887 %%zig_args.append("--output-h");
888 %%zig_args.append(builder.pathFromRoot(output_h_path));
900 if (self.kind != Kind.Exe) {
901 const output_h_path = self.getOutputHPath();
902 %%zig_args.append("--output-h");
903 %%zig_args.append(builder.pathFromRoot(output_h_path));
904 }
889905
890906 %%zig_args.append("--name");
891907 %%zig_args.append(self.name);
......@@ -929,6 +945,13 @@ pub const LibExeObjStep = struct {
929945 }
930946 }
931947
948 for (self.packages.toSliceConst()) |pkg| {
949 %%zig_args.append("--pkg-begin");
950 %%zig_args.append(pkg.name);
951 %%zig_args.append(builder.pathFromRoot(pkg.path));
952 %%zig_args.append("--pkg-end");
953 }
954
932955 for (builder.include_paths.toSliceConst()) |include_path| {
933956 %%zig_args.append("-isystem");
934957 %%zig_args.append(include_path);
test/build_examples.zig+1
......@@ -8,4 +8,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) {
88 cases.addBuildFile("example/shared_library/build.zig");
99 cases.addBuildFile("example/mix_o_files/build.zig");
1010 cases.addBuildFile("test/standalone/issue_339/build.zig");
11 cases.addBuildFile("test/standalone/pkg_import/build.zig");
1112}
test/standalone/pkg_import/build.zig created+12
......@@ -0,0 +1,12 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: &Builder) {
4 const exe = b.addExecutable("test", "test.zig");
5 exe.addPackagePath("my_pkg", "pkg.zig");
6
7 const run = b.addCommand(".", b.env_map, exe.getOutputPath(), [][]const u8{});
8 run.step.dependOn(&exe.step);
9
10 const test_step = b.step("test", "Test it");
11 test_step.dependOn(&run.step);
12}
test/standalone/pkg_import/pkg.zig created+1
......@@ -0,0 +1 @@
1pub fn add(a: i32, b: i32) -> i32 { a + b }
test/standalone/pkg_import/test.zig created+6
......@@ -0,0 +1,6 @@
1const my_pkg = @import("my_pkg");
2const assert = @import("std").debug.assert;
3
4pub fn main() -> %void {
5 assert(my_pkg.add(10, 20) == 30);
6}
test/tests.zig+1-1
......@@ -657,7 +657,7 @@ pub const BuildExamplesContext = struct {
657657 pub fn addBuildFile(self: &BuildExamplesContext, build_file: []const u8) {
658658 const b = self.b;
659659
660 const annotated_case_name = b.fmt("build {}", build_file);
660 const annotated_case_name = b.fmt("build {} (debug)", build_file);
661661 test (self.test_filter) |filter| {
662662 if (mem.indexOf(u8, annotated_case_name, filter) == null)
663663 return;