| ... | ... | @@ -261,6 +261,15 @@ static bool get_cache_opt(CacheOpt opt, bool default_value) { |
| 261 | 261 | zig_unreachable(); |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | static int zig_error_no_build_file(void) { |
| 265 | fprintf(stderr, |
| 266 | "No 'build.zig' file found, in the current directory or any parent directories.\n" |
| 267 | "Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,\n" |
| 268 | "or see `zig --help` for more options.\n" |
| 269 | ); |
| 270 | return EXIT_FAILURE; |
| 271 | } |
| 272 | |
| 264 | 273 | extern "C" int ZigClang_main(int argc, char **argv); |
| 265 | 274 | |
| 266 | 275 | int main(int argc, char **argv) { |
| ... | ... | @@ -466,8 +475,7 @@ int main(int argc, char **argv) { |
| 466 | 475 | return EXIT_FAILURE; |
| 467 | 476 | } |
| 468 | 477 | const char *zig_exe_path = buf_ptr(&zig_exe_path_buf); |
| 469 | | const char *build_file = "build.zig"; |
| 470 | | bool asked_for_help = false; |
| 478 | const char *build_file = nullptr; |
| 471 | 479 | |
| 472 | 480 | init_all_targets(); |
| 473 | 481 | |
| ... | ... | @@ -478,7 +486,6 @@ int main(int argc, char **argv) { |
| 478 | 486 | args.append(NULL); // placeholder |
| 479 | 487 | for (int i = 2; i < argc; i += 1) { |
| 480 | 488 | if (strcmp(argv[i], "--help") == 0) { |
| 481 | | asked_for_help = true; |
| 482 | 489 | args.append(argv[i]); |
| 483 | 490 | } else if (i + 1 < argc && strcmp(argv[i], "--build-file") == 0) { |
| 484 | 491 | build_file = argv[i + 1]; |
| ... | ... | @@ -511,12 +518,36 @@ int main(int argc, char **argv) { |
| 511 | 518 | ZigTarget target; |
| 512 | 519 | get_native_target(&target); |
| 513 | 520 | |
| 514 | | Buf *build_file_buf = buf_create_from_str(build_file); |
| 521 | Buf *build_file_buf = buf_create_from_str((build_file != nullptr) ? build_file : "build.zig"); |
| 515 | 522 | Buf build_file_abs = os_path_resolve(&build_file_buf, 1); |
| 516 | 523 | Buf build_file_basename = BUF_INIT; |
| 517 | 524 | Buf build_file_dirname = BUF_INIT; |
| 518 | 525 | os_path_split(&build_file_abs, &build_file_dirname, &build_file_basename); |
| 519 | 526 | |
| 527 | for (;;) { |
| 528 | bool build_file_exists; |
| 529 | if ((err = os_file_exists(&build_file_abs, &build_file_exists))) { |
| 530 | fprintf(stderr, "unable to check existence of '%s': %s\n", buf_ptr(&build_file_abs), err_str(err)); |
| 531 | return 1; |
| 532 | } |
| 533 | if (build_file_exists) |
| 534 | break; |
| 535 | |
| 536 | if (build_file != nullptr) { |
| 537 | // they asked for a specific build file path. only look for that one |
| 538 | return zig_error_no_build_file(); |
| 539 | } |
| 540 | |
| 541 | Buf *next_dir = buf_alloc(); |
| 542 | os_path_dirname(&build_file_dirname, next_dir); |
| 543 | if (buf_eql_buf(&build_file_dirname, next_dir)) { |
| 544 | // no more parent directories to search, give up |
| 545 | return zig_error_no_build_file(); |
| 546 | } |
| 547 | os_path_join(next_dir, &build_file_basename, &build_file_abs); |
| 548 | buf_init_from_buf(&build_file_dirname, next_dir); |
| 549 | } |
| 550 | |
| 520 | 551 | Buf full_cache_dir = BUF_INIT; |
| 521 | 552 | if (cache_dir == nullptr) { |
| 522 | 553 | os_path_join(&build_file_dirname, buf_create_from_str(default_zig_cache_name), &full_cache_dir); |
| ... | ... | @@ -534,51 +565,6 @@ int main(int argc, char **argv) { |
| 534 | 565 | args.items[2] = buf_ptr(&build_file_dirname); |
| 535 | 566 | args.items[3] = buf_ptr(&full_cache_dir); |
| 536 | 567 | |
| 537 | | bool build_file_exists; |
| 538 | | if ((err = os_file_exists(&build_file_abs, &build_file_exists))) { |
| 539 | | fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(&build_file_abs), err_str(err)); |
| 540 | | return 1; |
| 541 | | } |
| 542 | | if (!build_file_exists) { |
| 543 | | if (asked_for_help) { |
| 544 | | // This usage text has to be synchronized with std/special/build_runner.zig |
| 545 | | fprintf(stdout, |
| 546 | | "Usage: %s build [options]\n" |
| 547 | | "\n" |
| 548 | | "General Options:\n" |
| 549 | | " --help Print this help and exit\n" |
| 550 | | " --verbose Print commands before executing them\n" |
| 551 | | " --prefix [path] Override default install prefix\n" |
| 552 | | " --search-prefix [path] Add a path to look for binaries, libraries, headers\n" |
| 553 | | "\n" |
| 554 | | "Project-specific options become available when the build file is found.\n" |
| 555 | | "\n" |
| 556 | | "Advanced Options:\n" |
| 557 | | " --build-file [file] Override path to build.zig\n" |
| 558 | | " --cache-dir [path] Override path to cache directory\n" |
| 559 | | " --override-std-dir [arg] Override path to Zig standard library\n" |
| 560 | | " --override-lib-dir [arg] Override path to Zig lib library\n" |
| 561 | | " --verbose-tokenize Enable compiler debug output for tokenization\n" |
| 562 | | " --verbose-ast Enable compiler debug output for parsing into an AST\n" |
| 563 | | " --verbose-link Enable compiler debug output for linking\n" |
| 564 | | " --verbose-ir Enable compiler debug output for Zig IR\n" |
| 565 | | " --verbose-llvm-ir Enable compiler debug output for LLVM IR\n" |
| 566 | | " --verbose-cimport Enable compiler debug output for C imports\n" |
| 567 | | " --verbose-cc Enable compiler debug output for C compilation\n" |
| 568 | | "\n" |
| 569 | | , zig_exe_path); |
| 570 | | return EXIT_SUCCESS; |
| 571 | | } |
| 572 | | |
| 573 | | fprintf(stderr, |
| 574 | | "No 'build.zig' file found.\n" |
| 575 | | "Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,\n" |
| 576 | | "or build an executable directly with `zig build-exe $FILENAME.zig`.\n" |
| 577 | | "See: `zig build --help` or `zig --help` for more options.\n" |
| 578 | | ); |
| 579 | | return EXIT_FAILURE; |
| 580 | | } |
| 581 | | |
| 582 | 568 | ZigPackage *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname), |
| 583 | 569 | buf_ptr(&build_file_basename), "std.special"); |
| 584 | 570 | g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg); |