authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-11 06:14:46-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-11 06:14:46-04:00
log2864359950f8ebd9eca70a94801b78c4a0ed9955
tree38002c801ca42903ac0c78a016af429a7d996bbc
parent7f47b0c271931466d8ce88c0928ff2bef8f8109f

zig build system writes template build.zig file when none exists

see #204

6 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,6 +238,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/rand.zig" DESTINATION "${ZIG_STD_DEST}")
238install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}")238install(FILES "${CMAKE_SOURCE_DIR}/std/rand_test.zig" DESTINATION "${ZIG_STD_DEST}")
239install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}")239install(FILES "${CMAKE_SOURCE_DIR}/std/sort.zig" DESTINATION "${ZIG_STD_DEST}")
240install(FILES "${CMAKE_SOURCE_DIR}/std/special/bootstrap.zig" DESTINATION "${ZIG_STD_DEST}/special")240install(FILES "${CMAKE_SOURCE_DIR}/std/special/bootstrap.zig" DESTINATION "${ZIG_STD_DEST}/special")
241install(FILES "${CMAKE_SOURCE_DIR}/std/special/build_file_template.zig" DESTINATION "${ZIG_STD_DEST}/special")
241install(FILES "${CMAKE_SOURCE_DIR}/std/special/build_runner.zig" DESTINATION "${ZIG_STD_DEST}/special")242install(FILES "${CMAKE_SOURCE_DIR}/std/special/build_runner.zig" DESTINATION "${ZIG_STD_DEST}/special")
242install(FILES "${CMAKE_SOURCE_DIR}/std/special/builtin.zig" DESTINATION "${ZIG_STD_DEST}/special")243install(FILES "${CMAKE_SOURCE_DIR}/std/special/builtin.zig" DESTINATION "${ZIG_STD_DEST}/special")
243install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt.zig" DESTINATION "${ZIG_STD_DEST}/special")244install(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,6 +154,8 @@ int main(int argc, char **argv) {
154154
155 if (argc >= 2 && strcmp(argv[1], "build") == 0) {155 if (argc >= 2 && strcmp(argv[1], "build") == 0) {
156 const char *zig_exe_path = arg0;156 const char *zig_exe_path = arg0;
157 const char *build_file = "build.zig";
158 bool asked_for_help = false;
157159
158 init_all_targets();160 init_all_targets();
159161
...@@ -169,6 +171,12 @@ int main(int argc, char **argv) {...@@ -169,6 +171,12 @@ int main(int argc, char **argv) {
169 for (int i = 2; i < argc; i += 1) {171 for (int i = 2; i < argc; i += 1) {
170 if (strcmp(argv[i], "--debug-build-verbose") == 0) {172 if (strcmp(argv[i], "--debug-build-verbose") == 0) {
171 verbose = true;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 } else {180 } else {
173 args.append(argv[i]);181 args.append(argv[i]);
174 }182 }
...@@ -188,7 +196,43 @@ int main(int argc, char **argv) {...@@ -188,7 +196,43 @@ int main(int argc, char **argv) {
188 codegen_set_out_type(g, OutTypeExe);196 codegen_set_out_type(g, OutTypeExe);
189 codegen_set_verbose(g, verbose);197 codegen_set_verbose(g, verbose);
190198
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 build_pkg->package_table.put(buf_create_from_str("std"), g->std_package);236 build_pkg->package_table.put(buf_create_from_str("std"), g->std_package);
193 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);237 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
194 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);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,6 +490,58 @@ void os_write_file(Buf *full_path, Buf *contents) {
490 zig_panic("close failed");490 zig_panic("close failed");
491}491}
492492
493int 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
493int os_fetch_file_path(Buf *full_path, Buf *out_contents) {545int os_fetch_file_path(Buf *full_path, Buf *out_contents) {
494 FILE *f = fopen(buf_ptr(full_path), "rb");546 FILE *f = fopen(buf_ptr(full_path), "rb");
495 if (!f) {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,6 +41,7 @@ void os_path_resolve(Buf *ref_path, Buf *target_path, Buf *out_abs_path);
41bool os_path_is_absolute(Buf *path);41bool os_path_is_absolute(Buf *path);
4242
43void os_write_file(Buf *full_path, Buf *contents);43void os_write_file(Buf *full_path, Buf *contents);
44int os_copy_file(Buf *src_path, Buf *dest_path);
4445
45int os_fetch_file(FILE *file, Buf *out_contents);46int os_fetch_file(FILE *file, Buf *out_contents);
46int os_fetch_file_path(Buf *full_path, Buf *out_contents);47int os_fetch_file_path(Buf *full_path, Buf *out_contents);
std/special/build_file_template.zig created+8
...@@ -0,0 +1,8 @@
1const Builder = @import("std").build.Builder;
2
3pub 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,11 +77,13 @@ fn usage(builder: &Builder, maybe_zig_exe: ?[]const u8, already_ran_build: bool,
77 root.build(builder);77 root.build(builder);
78 }78 }
7979
80 // This usage text has to be synchronized with src/main.cpp
80 %%out_stream.printf(81 %%out_stream.printf(
81 \\Usage: {} build [options]82 \\Usage: {} build [options]
82 \\83 \\
83 \\General Options:84 \\General Options:
84 \\ --help Print this help and exit.85 \\ --help Print this help and exit.
86 \\ --build-file [file] Override path to build.zig.
85 \\ --verbose Print commands before executing them.87 \\ --verbose Print commands before executing them.
86 \\ --debug-build-verbose Print verbose debugging information for the build system itself.88 \\ --debug-build-verbose Print verbose debugging information for the build system itself.
87 \\89 \\