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) {...@@ -36,6 +36,8 @@ static int usage(const char *arg0) {
36 " --name [name] override output name\n"36 " --name [name] override output name\n"
37 " --output [file] override destination path\n"37 " --output [file] override destination path\n"
38 " --output-h [file] override generated header file path\n"38 " --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"
39 " --release build with optimizations on and debug protection off\n"41 " --release build with optimizations on and debug protection off\n"
40 " --static output will be statically linked\n"42 " --static output will be statically linked\n"
41 " --strip exclude debug symbols\n"43 " --strip exclude debug symbols\n"
...@@ -121,6 +123,36 @@ enum Cmd {...@@ -121,6 +123,36 @@ enum Cmd {
121123
122static const char *default_zig_cache_name = "zig-cache";124static 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
124int main(int argc, char **argv) {156int main(int argc, char **argv) {
125 os_init();157 os_init();
126158
...@@ -168,6 +200,7 @@ int main(int argc, char **argv) {...@@ -168,6 +200,7 @@ int main(int argc, char **argv) {
168 size_t ver_patch = 0;200 size_t ver_patch = 0;
169 bool timing_info = false;201 bool timing_info = false;
170 const char *cache_dir = nullptr;202 const char *cache_dir = nullptr;
203 CliPkg *cur_pkg = allocate<CliPkg>(1);
171204
172 if (argc >= 2 && strcmp(argv[1], "build") == 0) {205 if (argc >= 2 && strcmp(argv[1], "build") == 0) {
173 const char *zig_exe_path = arg0;206 const char *zig_exe_path = arg0;
...@@ -309,13 +342,31 @@ int main(int argc, char **argv) {...@@ -309,13 +342,31 @@ int main(int argc, char **argv) {
309 } else if (arg[1] == 'L' && arg[2] != 0) {342 } else if (arg[1] == 'L' && arg[2] != 0) {
310 // alias for --library-path343 // alias for --library-path
311 lib_dirs.append(&arg[2]);344 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;
312 } else if (i + 1 >= argc) {364 } else if (i + 1 >= argc) {
365 fprintf(stderr, "Expected another argument after %s\n", arg);
313 return usage(arg0);366 return usage(arg0);
314 } else {367 } else {
315 i += 1;368 i += 1;
316 if (i >= argc) {369 if (strcmp(arg, "--output") == 0) {
317 return usage(arg0);
318 } else if (strcmp(arg, "--output") == 0) {
319 out_file = argv[i];370 out_file = argv[i];
320 } else if (strcmp(arg, "--output-h") == 0) {371 } else if (strcmp(arg, "--output-h") == 0) {
321 out_file_h = argv[i];372 out_file_h = argv[i];
...@@ -327,6 +378,7 @@ int main(int argc, char **argv) {...@@ -327,6 +378,7 @@ int main(int argc, char **argv) {
327 } else if (strcmp(argv[i], "off") == 0) {378 } else if (strcmp(argv[i], "off") == 0) {
328 color = ErrColorOff;379 color = ErrColorOff;
329 } else {380 } else {
381 fprintf(stderr, "--color options are 'auto', 'on', or 'off'\n");
330 return usage(arg0);382 return usage(arg0);
331 }383 }
332 } else if (strcmp(arg, "--name") == 0) {384 } else if (strcmp(arg, "--name") == 0) {
...@@ -421,11 +473,13 @@ int main(int argc, char **argv) {...@@ -421,11 +473,13 @@ int main(int argc, char **argv) {
421 if (!in_file) {473 if (!in_file) {
422 in_file = arg;474 in_file = arg;
423 } else {475 } else {
476 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
424 return usage(arg0);477 return usage(arg0);
425 }478 }
426 break;479 break;
427 case CmdVersion:480 case CmdVersion:
428 case CmdTargets:481 case CmdTargets:
482 fprintf(stderr, "Unexpected extra parameter: %s\n", arg);
429 return usage(arg0);483 return usage(arg0);
430 case CmdInvalid:484 case CmdInvalid:
431 zig_unreachable();485 zig_unreachable();
...@@ -433,6 +487,11 @@ int main(int argc, char **argv) {...@@ -433,6 +487,11 @@ int main(int argc, char **argv) {
433 }487 }
434 }488 }
435489
490 if (cur_pkg->parent != nullptr) {
491 fprintf(stderr, "Unmatched --pkg-begin\n");
492 return EXIT_FAILURE;
493 }
494
436 switch (cmd) {495 switch (cmd) {
437 case CmdBuild:496 case CmdBuild:
438 case CmdParseH:497 case CmdParseH:
...@@ -579,6 +638,9 @@ int main(int argc, char **argv) {...@@ -579,6 +638,9 @@ int main(int argc, char **argv) {
579 if (out_file_h)638 if (out_file_h)
580 codegen_set_output_h_path(g, buf_create_from_str(out_file_h));639 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
582 if (cmd == CmdBuild) {644 if (cmd == CmdBuild) {
583 for (size_t i = 0; i < objects.length; i += 1) {645 for (size_t i = 0; i < objects.length; i += 1) {
584 codegen_add_object(g, buf_create_from_str(objects.at(i)));646 codegen_add_object(g, buf_create_from_str(objects.at(i)));
std/build.zig+26-3
...@@ -679,6 +679,12 @@ pub const LibExeObjStep = struct {...@@ -679,6 +679,12 @@ pub const LibExeObjStep = struct {
679 name_only_filename: []const u8,679 name_only_filename: []const u8,
680 object_files: List([]const u8),680 object_files: List([]const u8),
681 assembly_files: List([]const u8),681 assembly_files: List([]const u8),
682 packages: List(Pkg),
683
684 const Pkg = struct {
685 name: []const u8,
686 path: []const u8,
687 };
682688
683 const Kind = enum {689 const Kind = enum {
684 Exe,690 Exe,
...@@ -736,6 +742,7 @@ pub const LibExeObjStep = struct {...@@ -736,6 +742,7 @@ pub const LibExeObjStep = struct {
736 .name_only_filename = undefined,742 .name_only_filename = undefined,
737 .object_files = List([]const u8).init(builder.allocator),743 .object_files = List([]const u8).init(builder.allocator),
738 .assembly_files = List([]const u8).init(builder.allocator),744 .assembly_files = List([]const u8).init(builder.allocator),
745 .packages = List(Pkg).init(builder.allocator),
739 };746 };
740 self.computeOutFileNames();747 self.computeOutFileNames();
741 return self;748 return self;
...@@ -835,6 +842,13 @@ pub const LibExeObjStep = struct {...@@ -835,6 +842,13 @@ pub const LibExeObjStep = struct {
835 %%self.object_files.append(obj.getOutputPath());842 %%self.object_files.append(obj.getOutputPath());
836 }843 }
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
838 fn make(step: &Step) -> %void {852 fn make(step: &Step) -> %void {
839 const self = @fieldParentPtr(LibExeObjStep, "step", step);853 const self = @fieldParentPtr(LibExeObjStep, "step", step);
840 const builder = self.builder;854 const builder = self.builder;
...@@ -883,9 +897,11 @@ pub const LibExeObjStep = struct {...@@ -883,9 +897,11 @@ pub const LibExeObjStep = struct {
883 %%zig_args.append("--output");897 %%zig_args.append("--output");
884 %%zig_args.append(output_path);898 %%zig_args.append(output_path);
885899
886 const output_h_path = self.getOutputHPath();900 if (self.kind != Kind.Exe) {
887 %%zig_args.append("--output-h");901 const output_h_path = self.getOutputHPath();
888 %%zig_args.append(builder.pathFromRoot(output_h_path));902 %%zig_args.append("--output-h");
903 %%zig_args.append(builder.pathFromRoot(output_h_path));
904 }
889905
890 %%zig_args.append("--name");906 %%zig_args.append("--name");
891 %%zig_args.append(self.name);907 %%zig_args.append(self.name);
...@@ -929,6 +945,13 @@ pub const LibExeObjStep = struct {...@@ -929,6 +945,13 @@ pub const LibExeObjStep = struct {
929 }945 }
930 }946 }
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
932 for (builder.include_paths.toSliceConst()) |include_path| {955 for (builder.include_paths.toSliceConst()) |include_path| {
933 %%zig_args.append("-isystem");956 %%zig_args.append("-isystem");
934 %%zig_args.append(include_path);957 %%zig_args.append(include_path);
test/build_examples.zig+1
...@@ -8,4 +8,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) {...@@ -8,4 +8,5 @@ pub fn addCases(cases: &tests.BuildExamplesContext) {
8 cases.addBuildFile("example/shared_library/build.zig");8 cases.addBuildFile("example/shared_library/build.zig");
9 cases.addBuildFile("example/mix_o_files/build.zig");9 cases.addBuildFile("example/mix_o_files/build.zig");
10 cases.addBuildFile("test/standalone/issue_339/build.zig");10 cases.addBuildFile("test/standalone/issue_339/build.zig");
11 cases.addBuildFile("test/standalone/pkg_import/build.zig");
11}12}
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 {...@@ -657,7 +657,7 @@ pub const BuildExamplesContext = struct {
657 pub fn addBuildFile(self: &BuildExamplesContext, build_file: []const u8) {657 pub fn addBuildFile(self: &BuildExamplesContext, build_file: []const u8) {
658 const b = self.b;658 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);
661 test (self.test_filter) |filter| {661 test (self.test_filter) |filter| {
662 if (mem.indexOf(u8, annotated_case_name, filter) == null)662 if (mem.indexOf(u8, annotated_case_name, filter) == null)
663 return;663 return;