| ... | @@ -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 { |
| 121 | | 123 | |
| 122 | static const char *default_zig_cache_name = "zig-cache"; | 124 | static const char *default_zig_cache_name = "zig-cache"; |
| 123 | | 125 | |
| | 126 | struct CliPkg { |
| | 127 | const char *name; |
| | 128 | const char *path; |
| | 129 | ZigList<CliPkg *> children; |
| | 130 | CliPkg *parent; |
| | 131 | }; |
| | 132 | |
| | 133 | static 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 | |
| 124 | int main(int argc, char **argv) { | 156 | int main(int argc, char **argv) { |
| 125 | os_init(); | 157 | os_init(); |
| 126 | | 158 | |
| ... | @@ -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); |
| 171 | | 204 | |
| 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-path | 343 | // 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 | } |
| 435 | | 489 | |
| | 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)); |
| 581 | | 640 | |
| | 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))); |