| ... | ... | @@ -25,6 +25,8 @@ static int usage(const char *arg0) { |
| 25 | 25 | " build-obj [source] create object from source or assembly\n" |
| 26 | 26 | " builtin show the source code of that @import(\"builtin\")\n" |
| 27 | 27 | " id print the base64-encoded compiler id\n" |
| 28 | " init-exe initialize a `zig build` application in the cwd\n" |
| 29 | " init-lib initialize a `zig build` library in the cwd\n" |
| 28 | 30 | " run [source] create executable and run immediately\n" |
| 29 | 31 | " translate-c [source] convert c code to zig code\n" |
| 30 | 32 | " targets list available compilation targets\n" |
| ... | ... | @@ -34,7 +36,7 @@ static int usage(const char *arg0) { |
| 34 | 36 | "Compile Options:\n" |
| 35 | 37 | " --assembly [source] add assembly file to build\n" |
| 36 | 38 | " --cache-dir [path] override the cache directory\n" |
| 37 | | " --cache [auto|off|on] build to the global cache and print output path to stdout\n" |
| 39 | " --cache [auto|off|on] build in global cache, print out paths to stdout\n" |
| 38 | 40 | " --color [auto|off|on] enable or disable colored error messages\n" |
| 39 | 41 | " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n" |
| 40 | 42 | " -ftime-report print timing diagnostics\n" |
| ... | ... | @@ -42,7 +44,7 @@ static int usage(const char *arg0) { |
| 42 | 44 | " --name [name] override output name\n" |
| 43 | 45 | " --output [file] override destination path\n" |
| 44 | 46 | " --output-h [file] override generated header file path\n" |
| 45 | | " --pkg-begin [name] [path] make package available to import and push current pkg\n" |
| 47 | " --pkg-begin [name] [path] make pkg available to import and push current pkg\n" |
| 46 | 48 | " --pkg-end pop current pkg\n" |
| 47 | 49 | " --release-fast build with optimizations on and safety off\n" |
| 48 | 50 | " --release-safe build with optimizations on and safety on\n" |
| ... | ... | @@ -52,17 +54,16 @@ static int usage(const char *arg0) { |
| 52 | 54 | " --target-arch [name] specify target architecture\n" |
| 53 | 55 | " --target-environ [name] specify target environment\n" |
| 54 | 56 | " --target-os [name] specify target operating system\n" |
| 55 | | " --verbose-tokenize turn on compiler debug output for tokenization\n" |
| 56 | | " --verbose-ast turn on compiler debug output for parsing into an AST\n" |
| 57 | | " --verbose-link turn on compiler debug output for linking\n" |
| 58 | | " --verbose-ir turn on compiler debug output for Zig IR\n" |
| 59 | | " --verbose-llvm-ir turn on compiler debug output for LLVM IR\n" |
| 60 | | " --verbose-cimport turn on compiler debug output for C imports\n" |
| 57 | " --verbose-tokenize enable compiler debug output for tokenization\n" |
| 58 | " --verbose-ast enable compiler debug output for AST parsing\n" |
| 59 | " --verbose-link enable compiler debug output for linking\n" |
| 60 | " --verbose-ir enable compiler debug output for Zig IR\n" |
| 61 | " --verbose-llvm-ir enable compiler debug output for LLVM IR\n" |
| 62 | " --verbose-cimport enable compiler debug output for C imports\n" |
| 61 | 63 | " -dirafter [dir] same as -isystem but do it last\n" |
| 62 | 64 | " -isystem [dir] add additional search path for other .h files\n" |
| 63 | | " -mllvm [arg] additional arguments to forward to LLVM's option processing\n" |
| 65 | " -mllvm [arg] forward an arg to LLVM's option processing\n" |
| 64 | 66 | "Link Options:\n" |
| 65 | | " --ar-path [path] set the path to ar\n" |
| 66 | 67 | " --dynamic-linker [path] set the path to ld.so\n" |
| 67 | 68 | " --each-lib-rpath add rpath for each used dynamic library\n" |
| 68 | 69 | " --libc-lib-dir [path] directory where libc crt1.o resides\n" |
| ... | ... | @@ -142,78 +143,6 @@ static int print_target_list(FILE *f) { |
| 142 | 143 | return EXIT_SUCCESS; |
| 143 | 144 | } |
| 144 | 145 | |
| 145 | | static bool test_zig_install_prefix(Buf *test_path, Buf *out_zig_lib_dir) { |
| 146 | | Buf lib_buf = BUF_INIT; |
| 147 | | buf_init_from_str(&lib_buf, "lib"); |
| 148 | | |
| 149 | | Buf zig_buf = BUF_INIT; |
| 150 | | buf_init_from_str(&zig_buf, "zig"); |
| 151 | | |
| 152 | | Buf std_buf = BUF_INIT; |
| 153 | | buf_init_from_str(&std_buf, "std"); |
| 154 | | |
| 155 | | Buf index_zig_buf = BUF_INIT; |
| 156 | | buf_init_from_str(&index_zig_buf, "index.zig"); |
| 157 | | |
| 158 | | Buf test_lib_dir = BUF_INIT; |
| 159 | | Buf test_zig_dir = BUF_INIT; |
| 160 | | Buf test_std_dir = BUF_INIT; |
| 161 | | Buf test_index_file = BUF_INIT; |
| 162 | | |
| 163 | | os_path_join(test_path, &lib_buf, &test_lib_dir); |
| 164 | | os_path_join(&test_lib_dir, &zig_buf, &test_zig_dir); |
| 165 | | os_path_join(&test_zig_dir, &std_buf, &test_std_dir); |
| 166 | | os_path_join(&test_std_dir, &index_zig_buf, &test_index_file); |
| 167 | | |
| 168 | | int err; |
| 169 | | bool exists; |
| 170 | | if ((err = os_file_exists(&test_index_file, &exists))) { |
| 171 | | exists = false; |
| 172 | | } |
| 173 | | if (exists) { |
| 174 | | buf_init_from_buf(out_zig_lib_dir, &test_zig_dir); |
| 175 | | return true; |
| 176 | | } |
| 177 | | return false; |
| 178 | | } |
| 179 | | |
| 180 | | static int find_zig_lib_dir(Buf *out_path) { |
| 181 | | int err; |
| 182 | | |
| 183 | | Buf self_exe_path = BUF_INIT; |
| 184 | | buf_resize(&self_exe_path, 0); |
| 185 | | if (!(err = os_self_exe_path(&self_exe_path))) { |
| 186 | | Buf *cur_path = &self_exe_path; |
| 187 | | |
| 188 | | for (;;) { |
| 189 | | Buf *test_dir = buf_alloc(); |
| 190 | | os_path_dirname(cur_path, test_dir); |
| 191 | | |
| 192 | | if (buf_eql_buf(test_dir, cur_path)) { |
| 193 | | break; |
| 194 | | } |
| 195 | | |
| 196 | | if (test_zig_install_prefix(test_dir, out_path)) { |
| 197 | | return 0; |
| 198 | | } |
| 199 | | |
| 200 | | cur_path = test_dir; |
| 201 | | } |
| 202 | | } |
| 203 | | |
| 204 | | return ErrorFileNotFound; |
| 205 | | } |
| 206 | | |
| 207 | | static Buf *resolve_zig_lib_dir(void) { |
| 208 | | int err; |
| 209 | | Buf *result = buf_alloc(); |
| 210 | | if ((err = find_zig_lib_dir(result))) { |
| 211 | | fprintf(stderr, "Unable to find zig lib directory\n"); |
| 212 | | exit(EXIT_FAILURE); |
| 213 | | } |
| 214 | | return result; |
| 215 | | } |
| 216 | | |
| 217 | 146 | enum Cmd { |
| 218 | 147 | CmdInvalid, |
| 219 | 148 | CmdBuild, |
| ... | ... | @@ -277,6 +206,9 @@ static bool get_cache_opt(CacheOpt opt, bool default_value) { |
| 277 | 206 | } |
| 278 | 207 | |
| 279 | 208 | int main(int argc, char **argv) { |
| 209 | char *arg0 = argv[0]; |
| 210 | Error err; |
| 211 | |
| 280 | 212 | if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) { |
| 281 | 213 | printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", |
| 282 | 214 | ZIG_CMAKE_BINARY_DIR, |
| ... | ... | @@ -290,8 +222,10 @@ int main(int argc, char **argv) { |
| 290 | 222 | return 0; |
| 291 | 223 | } |
| 292 | 224 | |
| 225 | // Must be before all os.hpp function calls. |
| 226 | os_init(); |
| 227 | |
| 293 | 228 | if (argc == 2 && strcmp(argv[1], "id") == 0) { |
| 294 | | Error err; |
| 295 | 229 | Buf *compiler_id; |
| 296 | 230 | if ((err = get_compiler_id(&compiler_id))) { |
| 297 | 231 | fprintf(stderr, "Unable to determine compiler id: %s\n", err_str(err)); |
| ... | ... | @@ -301,9 +235,97 @@ int main(int argc, char **argv) { |
| 301 | 235 | return EXIT_SUCCESS; |
| 302 | 236 | } |
| 303 | 237 | |
| 304 | | os_init(); |
| 238 | enum InitKind { |
| 239 | InitKindNone, |
| 240 | InitKindExe, |
| 241 | InitKindLib, |
| 242 | }; |
| 243 | InitKind init_kind = InitKindNone; |
| 244 | if (argc >= 2) { |
| 245 | const char *init_cmd = argv[1]; |
| 246 | if (strcmp(init_cmd, "init-exe") == 0) { |
| 247 | init_kind = InitKindExe; |
| 248 | } else if (strcmp(init_cmd, "init-lib") == 0) { |
| 249 | init_kind = InitKindLib; |
| 250 | } |
| 251 | if (init_kind != InitKindNone) { |
| 252 | if (argc >= 3) { |
| 253 | fprintf(stderr, "Unexpected extra argument: %s\n", argv[2]); |
| 254 | return usage(arg0); |
| 255 | } |
| 256 | Buf *cmd_template_path = buf_alloc(); |
| 257 | os_path_join(get_zig_special_dir(), buf_create_from_str(init_cmd), cmd_template_path); |
| 258 | Buf *build_zig_path = buf_alloc(); |
| 259 | os_path_join(cmd_template_path, buf_create_from_str("build.zig"), build_zig_path); |
| 260 | Buf *src_dir_path = buf_alloc(); |
| 261 | os_path_join(cmd_template_path, buf_create_from_str("src"), src_dir_path); |
| 262 | Buf *main_zig_path = buf_alloc(); |
| 263 | os_path_join(src_dir_path, buf_create_from_str("main.zig"), main_zig_path); |
| 264 | |
| 265 | Buf *cwd = buf_alloc(); |
| 266 | if ((err = os_get_cwd(cwd))) { |
| 267 | fprintf(stderr, "Unable to get cwd: %s\n", err_str(err)); |
| 268 | return EXIT_FAILURE; |
| 269 | } |
| 270 | Buf *cwd_basename = buf_alloc(); |
| 271 | os_path_split(cwd, nullptr, cwd_basename); |
| 272 | |
| 273 | Buf *build_zig_contents = buf_alloc(); |
| 274 | if ((err = os_fetch_file_path(build_zig_path, build_zig_contents, false))) { |
| 275 | fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(build_zig_path), err_str(err)); |
| 276 | return EXIT_FAILURE; |
| 277 | } |
| 278 | Buf *modified_build_zig_contents = buf_alloc(); |
| 279 | for (size_t i = 0; i < buf_len(build_zig_contents); i += 1) { |
| 280 | char c = buf_ptr(build_zig_contents)[i]; |
| 281 | if (c == '$') { |
| 282 | buf_append_buf(modified_build_zig_contents, cwd_basename); |
| 283 | } else { |
| 284 | buf_append_char(modified_build_zig_contents, c); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | Buf *main_zig_contents = buf_alloc(); |
| 289 | if ((err = os_fetch_file_path(main_zig_path, main_zig_contents, false))) { |
| 290 | fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(main_zig_path), err_str(err)); |
| 291 | return EXIT_FAILURE; |
| 292 | } |
| 293 | |
| 294 | Buf *out_build_zig_path = buf_create_from_str("build.zig"); |
| 295 | Buf *out_src_dir_path = buf_create_from_str("src"); |
| 296 | Buf *out_main_zig_path = buf_alloc(); |
| 297 | os_path_join(out_src_dir_path, buf_create_from_str("main.zig"), out_main_zig_path); |
| 298 | |
| 299 | bool already_exists; |
| 300 | if ((err = os_file_exists(out_build_zig_path, &already_exists))) { |
| 301 | fprintf(stderr, "Unable test existence of %s: %s\n", buf_ptr(out_build_zig_path), err_str(err)); |
| 302 | return EXIT_FAILURE; |
| 303 | } |
| 304 | if (already_exists) { |
| 305 | fprintf(stderr, "This file would be overwritten: %s\n", buf_ptr(out_build_zig_path)); |
| 306 | return EXIT_FAILURE; |
| 307 | } |
| 308 | |
| 309 | if ((err = os_make_dir(out_src_dir_path))) { |
| 310 | fprintf(stderr, "Unable to make directory: %s: %s\n", buf_ptr(out_src_dir_path), err_str(err)); |
| 311 | return EXIT_FAILURE; |
| 312 | } |
| 313 | os_write_file(out_build_zig_path, modified_build_zig_contents); |
| 314 | os_write_file(out_main_zig_path, main_zig_contents); |
| 315 | fprintf(stderr, "Created %s\n", buf_ptr(out_build_zig_path)); |
| 316 | fprintf(stderr, "Created %s\n", buf_ptr(out_main_zig_path)); |
| 317 | if (init_kind == InitKindExe) { |
| 318 | fprintf(stderr, "\nNext, try `zig build --help` or `zig build run`\n"); |
| 319 | } else if (init_kind == InitKindLib) { |
| 320 | fprintf(stderr, "\nNext, try `zig build --help` or `zig build test`\n"); |
| 321 | } else { |
| 322 | zig_unreachable(); |
| 323 | } |
| 324 | |
| 325 | return EXIT_SUCCESS; |
| 326 | } |
| 327 | } |
| 305 | 328 | |
| 306 | | char *arg0 = argv[0]; |
| 307 | 329 | Cmd cmd = CmdInvalid; |
| 308 | 330 | EmitFileType emit_file_type = EmitFileTypeBinary; |
| 309 | 331 | const char *in_file = nullptr; |
| ... | ... | @@ -333,7 +355,6 @@ int main(int argc, char **argv) { |
| 333 | 355 | ZigList<const char *> link_libs = {0}; |
| 334 | 356 | ZigList<const char *> forbidden_link_libs = {0}; |
| 335 | 357 | ZigList<const char *> frameworks = {0}; |
| 336 | | int err; |
| 337 | 358 | const char *target_arch = nullptr; |
| 338 | 359 | const char *target_os = nullptr; |
| 339 | 360 | const char *target_environ = nullptr; |
| ... | ... | @@ -364,7 +385,6 @@ int main(int argc, char **argv) { |
| 364 | 385 | const char *zig_exe_path = arg0; |
| 365 | 386 | const char *build_file = "build.zig"; |
| 366 | 387 | bool asked_for_help = false; |
| 367 | | bool asked_to_init = false; |
| 368 | 388 | |
| 369 | 389 | init_all_targets(); |
| 370 | 390 | |
| ... | ... | @@ -376,9 +396,6 @@ int main(int argc, char **argv) { |
| 376 | 396 | if (strcmp(argv[i], "--help") == 0) { |
| 377 | 397 | asked_for_help = true; |
| 378 | 398 | args.append(argv[i]); |
| 379 | | } else if (strcmp(argv[i], "--init") == 0) { |
| 380 | | asked_to_init = true; |
| 381 | | args.append(argv[i]); |
| 382 | 399 | } else if (i + 1 < argc && strcmp(argv[i], "--build-file") == 0) { |
| 383 | 400 | build_file = argv[i + 1]; |
| 384 | 401 | i += 1; |
| ... | ... | @@ -390,18 +407,10 @@ int main(int argc, char **argv) { |
| 390 | 407 | } |
| 391 | 408 | } |
| 392 | 409 | |
| 393 | | Buf *zig_lib_dir_buf = resolve_zig_lib_dir(); |
| 394 | | |
| 395 | | Buf *zig_std_dir = buf_alloc(); |
| 396 | | os_path_join(zig_lib_dir_buf, buf_create_from_str("std"), zig_std_dir); |
| 397 | | |
| 398 | | Buf *special_dir = buf_alloc(); |
| 399 | | os_path_join(zig_std_dir, buf_sprintf("special"), special_dir); |
| 400 | | |
| 401 | 410 | Buf *build_runner_path = buf_alloc(); |
| 402 | | os_path_join(special_dir, buf_create_from_str("build_runner.zig"), build_runner_path); |
| 411 | os_path_join(get_zig_special_dir(), buf_create_from_str("build_runner.zig"), build_runner_path); |
| 403 | 412 | |
| 404 | | CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, zig_lib_dir_buf); |
| 413 | CodeGen *g = codegen_create(build_runner_path, nullptr, OutTypeExe, BuildModeDebug, get_zig_lib_dir()); |
| 405 | 414 | g->enable_time_report = timing_info; |
| 406 | 415 | buf_init_from_str(&g->cache_dir, cache_dir ? cache_dir : default_zig_cache_name); |
| 407 | 416 | codegen_set_out_name(g, buf_create_from_str("build")); |
| ... | ... | @@ -437,7 +446,6 @@ int main(int argc, char **argv) { |
| 437 | 446 | "\n" |
| 438 | 447 | "General Options:\n" |
| 439 | 448 | " --help Print this help and exit\n" |
| 440 | | " --init Generate a build.zig template\n" |
| 441 | 449 | " --build-file [file] Override path to build.zig\n" |
| 442 | 450 | " --cache-dir [path] Override path to cache directory\n" |
| 443 | 451 | " --verbose Print commands before executing them\n" |
| ... | ... | @@ -463,22 +471,11 @@ int main(int argc, char **argv) { |
| 463 | 471 | "\n" |
| 464 | 472 | , zig_exe_path); |
| 465 | 473 | return EXIT_SUCCESS; |
| 466 | | } else if (asked_to_init) { |
| 467 | | Buf *build_template_path = buf_alloc(); |
| 468 | | os_path_join(special_dir, buf_create_from_str("build_file_template.zig"), build_template_path); |
| 469 | | |
| 470 | | if ((err = os_copy_file(build_template_path, &build_file_abs))) { |
| 471 | | fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err)); |
| 472 | | return EXIT_FAILURE; |
| 473 | | } else { |
| 474 | | fprintf(stderr, "Wrote build.zig template\n"); |
| 475 | | return EXIT_SUCCESS; |
| 476 | | } |
| 477 | 474 | } |
| 478 | 475 | |
| 479 | 476 | fprintf(stderr, |
| 480 | 477 | "No 'build.zig' file found.\n" |
| 481 | | "Initialize a 'build.zig' template file with `zig build --init`,\n" |
| 478 | "Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,\n" |
| 482 | 479 | "or build an executable directly with `zig build-exe $FILENAME.zig`.\n" |
| 483 | 480 | "See: `zig build --help` or `zig help` for more options.\n" |
| 484 | 481 | ); |
| ... | ... | @@ -773,8 +770,7 @@ int main(int argc, char **argv) { |
| 773 | 770 | |
| 774 | 771 | switch (cmd) { |
| 775 | 772 | case CmdBuiltin: { |
| 776 | | Buf *zig_lib_dir_buf = resolve_zig_lib_dir(); |
| 777 | | CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, zig_lib_dir_buf); |
| 773 | CodeGen *g = codegen_create(nullptr, target, out_type, build_mode, get_zig_lib_dir()); |
| 778 | 774 | Buf *builtin_source = codegen_generate_builtin_source(g); |
| 779 | 775 | if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) { |
| 780 | 776 | fprintf(stderr, "unable to write to stdout: %s\n", strerror(ferror(stdout))); |
| ... | ... | @@ -832,9 +828,7 @@ int main(int argc, char **argv) { |
| 832 | 828 | if (cmd == CmdRun && buf_out_name == nullptr) { |
| 833 | 829 | buf_out_name = buf_create_from_str("run"); |
| 834 | 830 | } |
| 835 | | Buf *zig_lib_dir_buf = resolve_zig_lib_dir(); |
| 836 | | |
| 837 | | CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf); |
| 831 | CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, get_zig_lib_dir()); |
| 838 | 832 | g->enable_time_report = timing_info; |
| 839 | 833 | buf_init_from_str(&g->cache_dir, cache_dir ? cache_dir : default_zig_cache_name); |
| 840 | 834 | codegen_set_out_name(g, buf_out_name); |