| ... | @@ -339,6 +339,7 @@ int main(int argc, char **argv) { | ... | @@ -339,6 +339,7 @@ int main(int argc, char **argv) { |
| 339 | const char *zig_exe_path = arg0; | 339 | const char *zig_exe_path = arg0; |
| 340 | const char *build_file = "build.zig"; | 340 | const char *build_file = "build.zig"; |
| 341 | bool asked_for_help = false; | 341 | bool asked_for_help = false; |
| | 342 | bool asked_to_init = false; |
| 342 | | 343 | |
| 343 | init_all_targets(); | 344 | init_all_targets(); |
| 344 | | 345 | |
| ... | @@ -350,6 +351,9 @@ int main(int argc, char **argv) { | ... | @@ -350,6 +351,9 @@ int main(int argc, char **argv) { |
| 350 | if (strcmp(argv[i], "--help") == 0) { | 351 | if (strcmp(argv[i], "--help") == 0) { |
| 351 | asked_for_help = true; | 352 | asked_for_help = true; |
| 352 | args.append(argv[i]); | 353 | args.append(argv[i]); |
| | 354 | } else if (strcmp(argv[i], "--init") == 0) { |
| | 355 | asked_to_init = true; |
| | 356 | args.append(argv[i]); |
| 353 | } else if (i + 1 < argc && strcmp(argv[i], "--build-file") == 0) { | 357 | } else if (i + 1 < argc && strcmp(argv[i], "--build-file") == 0) { |
| 354 | build_file = argv[i + 1]; | 358 | build_file = argv[i + 1]; |
| 355 | i += 1; | 359 | i += 1; |
| ... | @@ -414,6 +418,7 @@ int main(int argc, char **argv) { | ... | @@ -414,6 +418,7 @@ int main(int argc, char **argv) { |
| 414 | "\n" | 418 | "\n" |
| 415 | "General Options:\n" | 419 | "General Options:\n" |
| 416 | " --help Print this help and exit\n" | 420 | " --help Print this help and exit\n" |
| | 421 | " --init Generate a build.zig template\n" |
| 417 | " --build-file [file] Override path to build.zig\n" | 422 | " --build-file [file] Override path to build.zig\n" |
| 418 | " --cache-dir [path] Override path to cache directory\n" | 423 | " --cache-dir [path] Override path to cache directory\n" |
| 419 | " --verbose Print commands before executing them\n" | 424 | " --verbose Print commands before executing them\n" |
| ... | @@ -426,7 +431,6 @@ int main(int argc, char **argv) { | ... | @@ -426,7 +431,6 @@ int main(int argc, char **argv) { |
| 426 | " --prefix [path] Override default install prefix\n" | 431 | " --prefix [path] Override default install prefix\n" |
| 427 | "\n" | 432 | "\n" |
| 428 | "Project-specific options become available when the build file is found.\n" | 433 | "Project-specific options become available when the build file is found.\n" |
| 429 | "Run this command with no options to generate a build.zig template.\n" | | |
| 430 | "\n" | 434 | "\n" |
| 431 | "Advanced Options:\n" | 435 | "Advanced Options:\n" |
| 432 | " --build-file [file] Override path to build.zig\n" | 436 | " --build-file [file] Override path to build.zig\n" |
| ... | @@ -439,17 +443,26 @@ int main(int argc, char **argv) { | ... | @@ -439,17 +443,26 @@ int main(int argc, char **argv) { |
| 439 | " --verbose-cimport Enable compiler debug output for C imports\n" | 443 | " --verbose-cimport Enable compiler debug output for C imports\n" |
| 440 | "\n" | 444 | "\n" |
| 441 | , zig_exe_path); | 445 | , zig_exe_path); |
| 442 | return 0; | 446 | return EXIT_SUCCESS; |
| 443 | } | 447 | } else if (asked_to_init) { |
| 444 | Buf *build_template_path = buf_alloc(); | 448 | Buf *build_template_path = buf_alloc(); |
| 445 | os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path); | 449 | os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path); |
| 446 | | 450 | |
| 447 | if ((err = os_copy_file(build_template_path, &build_file_abs))) { | 451 | if ((err = os_copy_file(build_template_path, &build_file_abs))) { |
| 448 | fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err)); | 452 | fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err)); |
| 449 | } else { | 453 | } else { |
| 450 | fprintf(stderr, "Wrote build.zig template\n"); | 454 | fprintf(stderr, "Wrote build.zig template\n"); |
| | 455 | } |
| | 456 | return EXIT_SUCCESS; |
| 451 | } | 457 | } |
| 452 | return 1; | 458 | |
| | 459 | fprintf(stderr, |
| | 460 | "No 'build.zig' file found.\n" |
| | 461 | "Initialize a 'build.zig' template file with `zig build --init`,\n" |
| | 462 | "or build an executable directly with `zig build-exe $FILENAME.zig`.\n" |
| | 463 | "See: `zig build --help` or `zig help` for more options.\n" |
| | 464 | ); |
| | 465 | return EXIT_FAILURE; |
| 453 | } | 466 | } |
| 454 | | 467 | |
| 455 | PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname), | 468 | PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname), |