| author | |
| committer | |
| log | 2864359950f8ebd9eca70a94801b78c4a0ed9955 |
| tree | 38002c801ca42903ac0c78a016af429a7d996bbc |
| parent | 7f47b0c271931466d8ce88c0928ff2bef8f8109f |
see #2046 files changed, 109 insertions(+), 1 deletions(-)
CMakeLists.txt+1| ... | ... | @@ -238,6 +238,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}") |
| 238 | 238 | install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}") |
| 239 | 239 | install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}") |
| 240 | 240 | install(FILES "${CMAKE_SOURCE_DIR}/std/special/bootstrap.zig" DESTINATION "${ZIG_STD_DEST}/special") |
| 241 | install(FILES "${CMAKE_SOURCE_DIR}/std/special/build_file_template.zig" DESTINATION "${ZIG_STD_DEST}/special") | |
| 241 | 242 | install(FILES "${CMAKE_SOURCE_DIR}/std/special/build_runner.zig" DESTINATION "${ZIG_STD_DEST}/special") |
| 242 | 243 | install(FILES "${CMAKE_SOURCE_DIR}/std/special/builtin.zig" DESTINATION "${ZIG_STD_DEST}/special") |
| 243 | 244 | install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt.zig" DESTINATION "${ZIG_STD_DEST}/special") |
src/main.cpp+45-1| ... | ... | @@ -154,6 +154,8 @@ int main(int argc, char **argv) { |
| 154 | 154 | |
| 155 | 155 | if (argc >= 2 && strcmp(argv[1], "build") == 0) { |
| 156 | 156 | const char *zig_exe_path = arg0; |
| 157 | const char *build_file = "build.zig"; | |
| 158 | bool asked_for_help = false; | |
| 157 | 159 | |
| 158 | 160 | init_all_targets(); |
| 159 | 161 | |
| ... | ... | @@ -169,6 +171,12 @@ int main(int argc, char **argv) { |
| 169 | 171 | for (int i = 2; i < argc; i += 1) { |
| 170 | 172 | if (strcmp(argv[i], "--debug-build-verbose") == 0) { |
| 171 | 173 | verbose = true; |
| 174 | } else if (strcmp(argv[i], "--help") == 0) { | |
| 175 | asked_for_help = true; | |
| 176 | args.append(argv[i]); | |
| 177 | } else if (i + 1 < argc && strcmp(argv[i], "--build-file") == 0) { | |
| 178 | build_file = argv[i + 1]; | |
| 179 | i += 1; | |
| 172 | 180 | } else { |
| 173 | 181 | args.append(argv[i]); |
| 174 | 182 | } |
| ... | ... | @@ -188,7 +196,43 @@ int main(int argc, char **argv) { |
| 188 | 196 | codegen_set_out_type(g, OutTypeExe); |
| 189 | 197 | codegen_set_verbose(g, verbose); |
| 190 | 198 | |
| 191 | PackageTableEntry *build_pkg = new_package(".", "build.zig"); | |
| 199 | Buf build_file_abs = BUF_INIT; | |
| 200 | os_path_resolve(buf_create_from_str("."), buf_create_from_str(build_file), &build_file_abs); | |
| 201 | Buf build_file_basename = BUF_INIT; | |
| 202 | Buf build_file_dirname = BUF_INIT; | |
| 203 | os_path_split(&build_file_abs, &build_file_dirname, &build_file_basename); | |
| 204 | ||
| 205 | bool build_file_exists; | |
| 206 | if ((err = os_file_exists(&build_file_abs, &build_file_exists))) { | |
| 207 | fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(&build_file_abs), err_str(err)); | |
| 208 | return 1; | |
| 209 | } | |
| 210 | if (!build_file_exists) { | |
| 211 | if (asked_for_help) { | |
| 212 | // This usage text has to be synchronized with std/special/build_runner.zig | |
| 213 | fprintf(stdout, | |
| 214 | "Usage: %s build [options]\n" | |
| 215 | "\n" | |
| 216 | "General Options:\n" | |
| 217 | " --help Print this help and exit.\n" | |
| 218 | " --build-file [file] Override path to build.zig.\n" | |
| 219 | " --verbose Print commands before executing them.\n" | |
| 220 | " --debug-build-verbose Print verbose debugging information for the build system itself.\n" | |
| 221 | , zig_exe_path); | |
| 222 | return 0; | |
| 223 | } | |
| 224 | Buf *build_template_path = buf_alloc(); | |
| 225 | os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path); | |
| 226 | ||
| 227 | if ((err = os_copy_file(build_template_path, &build_file_abs))) { | |
| 228 | fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err)); | |
| 229 | } else { | |
| 230 | fprintf(stderr, "Wrote build.zig template\n"); | |
| 231 | } | |
| 232 | return 1; | |
| 233 | } | |
| 234 | ||
| 235 | PackageTableEntry *build_pkg = new_package(buf_ptr(&build_file_dirname), buf_ptr(&build_file_basename)); | |
| 192 | 236 | build_pkg->package_table.put(buf_create_from_str("std"), g->std_package); |
| 193 | 237 | g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg); |
| 194 | 238 | codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code); |
src/os.cpp+52| ... | ... | @@ -490,6 +490,58 @@ void os_write_file(Buf *full_path, Buf *contents) { |
| 490 | 490 | zig_panic("close failed"); |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | int os_copy_file(Buf *src_path, Buf *dest_path) { | |
| 494 | FILE *src_f = fopen(buf_ptr(src_path), "rb"); | |
| 495 | if (!src_f) { | |
| 496 | int err = errno; | |
| 497 | if (err == ENOENT) { | |
| 498 | return ErrorFileNotFound; | |
| 499 | } else if (err == EACCES || err == EPERM) { | |
| 500 | return ErrorAccess; | |
| 501 | } else { | |
| 502 | return ErrorFileSystem; | |
| 503 | } | |
| 504 | } | |
| 505 | FILE *dest_f = fopen(buf_ptr(dest_path), "wb"); | |
| 506 | if (!dest_f) { | |
| 507 | int err = errno; | |
| 508 | if (err == ENOENT) { | |
| 509 | fclose(src_f); | |
| 510 | return ErrorFileNotFound; | |
| 511 | } else if (err == EACCES || err == EPERM) { | |
| 512 | fclose(src_f); | |
| 513 | return ErrorAccess; | |
| 514 | } else { | |
| 515 | fclose(src_f); | |
| 516 | return ErrorFileSystem; | |
| 517 | } | |
| 518 | } | |
| 519 | ||
| 520 | static const size_t buf_size = 2048; | |
| 521 | char buf[buf_size]; | |
| 522 | for (;;) { | |
| 523 | size_t amt_read = fread(buf, 1, buf_size, src_f); | |
| 524 | if (amt_read != buf_size) { | |
| 525 | if (ferror(src_f)) { | |
| 526 | fclose(src_f); | |
| 527 | fclose(dest_f); | |
| 528 | return ErrorFileSystem; | |
| 529 | } | |
| 530 | } | |
| 531 | size_t amt_written = fwrite(buf, 1, amt_read, dest_f); | |
| 532 | if (amt_written != amt_read) { | |
| 533 | fclose(src_f); | |
| 534 | fclose(dest_f); | |
| 535 | return ErrorFileSystem; | |
| 536 | } | |
| 537 | if (feof(src_f)) { | |
| 538 | fclose(src_f); | |
| 539 | fclose(dest_f); | |
| 540 | return 0; | |
| 541 | } | |
| 542 | } | |
| 543 | } | |
| 544 | ||
| 493 | 545 | int os_fetch_file_path(Buf *full_path, Buf *out_contents) { |
| 494 | 546 | FILE *f = fopen(buf_ptr(full_path), "rb"); |
| 495 | 547 | if (!f) { |
src/os.hpp+1| ... | ... | @@ -41,6 +41,7 @@ void os_path_resolve(Buf *ref_path, Buf *target_path, Buf *out_abs_path); |
| 41 | 41 | bool os_path_is_absolute(Buf *path); |
| 42 | 42 | |
| 43 | 43 | void os_write_file(Buf *full_path, Buf *contents); |
| 44 | int os_copy_file(Buf *src_path, Buf *dest_path); | |
| 44 | 45 | |
| 45 | 46 | int os_fetch_file(FILE *file, Buf *out_contents); |
| 46 | 47 | int os_fetch_file_path(Buf *full_path, Buf *out_contents); |
std/special/build_file_template.zig created+8| ... | ... | @@ -0,0 +1,8 @@ |
| 1 | const Builder = @import("std").build.Builder; | |
| 2 | ||
| 3 | pub fn build(b: &Builder) { | |
| 4 | const release = b.option(bool, "release", "optimizations on and safety off") ?? false; | |
| 5 | ||
| 6 | var exe = b.addExe("src/main.zig", "YOUR_NAME_HERE"); | |
| 7 | exe.setRelease(release); | |
| 8 | } |
std/special/build_runner.zig+2| ... | ... | @@ -77,11 +77,13 @@ fn usage(builder: &Builder, maybe_zig_exe: ?[]const u8, already_ran_build: bool, |
| 77 | 77 | root.build(builder); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | // This usage text has to be synchronized with src/main.cpp | |
| 80 | 81 | %%out_stream.printf( |
| 81 | 82 | \\Usage: {} build [options] |
| 82 | 83 | \\ |
| 83 | 84 | \\General Options: |
| 84 | 85 | \\ --help Print this help and exit. |
| 86 | \\ --build-file [file] Override path to build.zig. | |
| 85 | 87 | \\ --verbose Print commands before executing them. |
| 86 | 88 | \\ --debug-build-verbose Print verbose debugging information for the build system itself. |
| 87 | 89 | \\ |