| author | |
| committer | |
| log | bc2d60c11f7e90f401a4412d3dc6fca985071169 |
| tree | 8e3e4e71dc24d17b4028069381b6aee0a43df347 |
| parent | 4c03746926562e4e9650eec7c4361836fabba5af |
| parent | da8403bcdd6fb6928a26e5cb3813d9dc67db2eed |
| signature |
stage1 is now a hybrid of C++ and Zig14 files changed, 262 insertions(+), 89 deletions(-)
CMakeLists.txt+50-11| ... | ... | @@ -407,6 +407,12 @@ set(SOFTFLOAT_LIBRARIES embedded_softfloat) |
| 407 | 407 | |
| 408 | 408 | find_package(Threads) |
| 409 | 409 | |
| 410 | # CMake doesn't let us create an empty executable, so we hang on to this one separately. | |
| 411 | set(ZIG_MAIN_SRC "${CMAKE_SOURCE_DIR}/src/main.cpp") | |
| 412 | ||
| 413 | # This is our shim which will be replaced by libuserland written in Zig. | |
| 414 | set(ZIG1_SHIM_SRC "${CMAKE_SOURCE_DIR}/src/userland.cpp") | |
| 415 | ||
| 410 | 416 | set(ZIG_SOURCES |
| 411 | 417 | "${CMAKE_SOURCE_DIR}/src/analyze.cpp" |
| 412 | 418 | "${CMAKE_SOURCE_DIR}/src/ast_render.cpp" |
| ... | ... | @@ -423,7 +429,6 @@ set(ZIG_SOURCES |
| 423 | 429 | "${CMAKE_SOURCE_DIR}/src/ir_print.cpp" |
| 424 | 430 | "${CMAKE_SOURCE_DIR}/src/libc_installation.cpp" |
| 425 | 431 | "${CMAKE_SOURCE_DIR}/src/link.cpp" |
| 426 | "${CMAKE_SOURCE_DIR}/src/main.cpp" | |
| 427 | 432 | "${CMAKE_SOURCE_DIR}/src/os.cpp" |
| 428 | 433 | "${CMAKE_SOURCE_DIR}/src/parser.cpp" |
| 429 | 434 | "${CMAKE_SOURCE_DIR}/src/range_set.cpp" |
| ... | ... | @@ -6635,19 +6640,19 @@ add_library(zig_cpp STATIC ${ZIG_CPP_SOURCES}) |
| 6635 | 6640 | set_target_properties(zig_cpp PROPERTIES |
| 6636 | 6641 | COMPILE_FLAGS ${EXE_CFLAGS} |
| 6637 | 6642 | ) |
| 6643 | install(TARGETS zig_cpp DESTINATION "${ZIG_CPP_LIB_DIR}") | |
| 6638 | 6644 | |
| 6639 | 6645 | add_library(opt_c_util STATIC ${OPTIMIZED_C_SOURCES}) |
| 6640 | 6646 | set_target_properties(opt_c_util PROPERTIES |
| 6641 | 6647 | COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}" |
| 6642 | 6648 | ) |
| 6643 | 6649 | |
| 6644 | add_executable(zig ${ZIG_SOURCES}) | |
| 6645 | set_target_properties(zig PROPERTIES | |
| 6650 | add_library(compiler STATIC ${ZIG_SOURCES}) | |
| 6651 | set_target_properties(compiler PROPERTIES | |
| 6646 | 6652 | COMPILE_FLAGS ${EXE_CFLAGS} |
| 6647 | 6653 | LINK_FLAGS ${EXE_LDFLAGS} |
| 6648 | 6654 | ) |
| 6649 | ||
| 6650 | target_link_libraries(zig LINK_PUBLIC | |
| 6655 | target_link_libraries(compiler LINK_PUBLIC | |
| 6651 | 6656 | zig_cpp |
| 6652 | 6657 | opt_c_util |
| 6653 | 6658 | ${SOFTFLOAT_LIBRARIES} |
| ... | ... | @@ -6656,24 +6661,58 @@ target_link_libraries(zig LINK_PUBLIC |
| 6656 | 6661 | ${LLVM_LIBRARIES} |
| 6657 | 6662 | ${CMAKE_THREAD_LIBS_INIT} |
| 6658 | 6663 | ) |
| 6659 | ||
| 6660 | 6664 | if(NOT MSVC) |
| 6661 | target_link_libraries(zig LINK_PUBLIC ${LIBXML2}) | |
| 6665 | target_link_libraries(compiler LINK_PUBLIC ${LIBXML2}) | |
| 6662 | 6666 | endif() |
| 6663 | 6667 | |
| 6664 | 6668 | if(MINGW) |
| 6665 | target_link_libraries(zig LINK_PUBLIC ${Z3_LIBRARIES}) | |
| 6669 | target_link_libraries(compiler LINK_PUBLIC ${Z3_LIBRARIES}) | |
| 6666 | 6670 | endif() |
| 6667 | 6671 | |
| 6668 | 6672 | if(ZIG_DIA_GUIDS_LIB) |
| 6669 | target_link_libraries(zig LINK_PUBLIC ${ZIG_DIA_GUIDS_LIB}) | |
| 6673 | target_link_libraries(compiler LINK_PUBLIC ${ZIG_DIA_GUIDS_LIB}) | |
| 6670 | 6674 | endif() |
| 6671 | 6675 | |
| 6672 | 6676 | if(MSVC OR MINGW) |
| 6673 | target_link_libraries(zig LINK_PUBLIC version) | |
| 6677 | target_link_libraries(compiler LINK_PUBLIC version) | |
| 6678 | endif() | |
| 6679 | ||
| 6680 | add_executable(zig1 "${ZIG_MAIN_SRC}" "${ZIG1_SHIM_SRC}") | |
| 6681 | set_target_properties(zig1 PROPERTIES | |
| 6682 | COMPILE_FLAGS ${EXE_CFLAGS} | |
| 6683 | LINK_FLAGS ${EXE_LDFLAGS} | |
| 6684 | ) | |
| 6685 | target_link_libraries(zig1 compiler) | |
| 6686 | ||
| 6687 | if(WIN32) | |
| 6688 | set(LIBUSERLAND "${CMAKE_BINARY_DIR}/userland.lib") | |
| 6689 | elseif(APPLE) | |
| 6690 | set(LIBUSERLAND "${CMAKE_BINARY_DIR}/userland.o") | |
| 6691 | else() | |
| 6692 | set(LIBUSERLAND "${CMAKE_BINARY_DIR}/libuserland.a") | |
| 6674 | 6693 | endif() |
| 6694 | add_custom_command( | |
| 6695 | OUTPUT "${LIBUSERLAND}" | |
| 6696 | COMMAND zig1 ARGS build | |
| 6697 | --override-std-dir std | |
| 6698 | --override-lib-dir "${CMAKE_SOURCE_DIR}" | |
| 6699 | libuserland | |
| 6700 | "-Doutput-dir=${CMAKE_BINARY_DIR}" | |
| 6701 | WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" | |
| 6702 | DEPENDS | |
| 6703 | "${CMAKE_SOURCE_DIR}/src-self-hosted/stage1.zig" | |
| 6704 | "${CMAKE_SOURCE_DIR}/src-self-hosted/translate_c.zig" | |
| 6705 | ) | |
| 6706 | add_custom_target(userland_target DEPENDS "${LIBUSERLAND}") | |
| 6707 | add_executable(zig "${ZIG_MAIN_SRC}") | |
| 6708 | set_target_properties(zig PROPERTIES | |
| 6709 | COMPILE_FLAGS ${EXE_CFLAGS} | |
| 6710 | LINK_FLAGS ${EXE_LDFLAGS} | |
| 6711 | ) | |
| 6712 | target_link_libraries(zig compiler "${LIBUSERLAND}") | |
| 6713 | add_dependencies(zig userland_target) | |
| 6675 | 6714 | install(TARGETS zig DESTINATION bin) |
| 6676 | install(TARGETS zig_cpp DESTINATION "${ZIG_CPP_LIB_DIR}") | |
| 6715 | ||
| 6677 | 6716 | |
| 6678 | 6717 | foreach(file ${ZIG_C_HEADER_FILES}) |
| 6679 | 6718 | get_filename_component(file_dir "${C_HEADERS_DEST}/${file}" DIRECTORY) |
build.zig+20| ... | ... | @@ -65,6 +65,8 @@ pub fn build(b: *Builder) !void { |
| 65 | 65 | |
| 66 | 66 | b.default_step.dependOn(&exe.step); |
| 67 | 67 | |
| 68 | addLibUserlandStep(b); | |
| 69 | ||
| 68 | 70 | const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false; |
| 69 | 71 | const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release; |
| 70 | 72 | const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release; |
| ... | ... | @@ -380,3 +382,21 @@ const Context = struct { |
| 380 | 382 | dia_guids_lib: []const u8, |
| 381 | 383 | llvm: LibraryDep, |
| 382 | 384 | }; |
| 385 | ||
| 386 | fn addLibUserlandStep(b: *Builder) void { | |
| 387 | const artifact = if (builtin.os == .macosx) | |
| 388 | b.addObject("userland", "src-self-hosted/stage1.zig") | |
| 389 | else | |
| 390 | b.addStaticLibrary("userland", "src-self-hosted/stage1.zig"); | |
| 391 | artifact.disable_gen_h = true; | |
| 392 | artifact.linkSystemLibrary("c"); | |
| 393 | const libuserland_step = b.step("libuserland", "Build the userland compiler library for use in stage1"); | |
| 394 | libuserland_step.dependOn(&artifact.step); | |
| 395 | ||
| 396 | const output_dir = b.option( | |
| 397 | []const u8, | |
| 398 | "output-dir", | |
| 399 | "For libuserland step, where to put the output", | |
| 400 | ) orelse return; | |
| 401 | artifact.setOutputDir(output_dir); | |
| 402 | } |
src-self-hosted/main.zig+1-17| ... | ... | @@ -858,23 +858,7 @@ fn cmdHelp(allocator: *Allocator, args: []const []const u8) !void { |
| 858 | 858 | try stdout.write(usage); |
| 859 | 859 | } |
| 860 | 860 | |
| 861 | const info_zen = | |
| 862 | \\ | |
| 863 | \\ * Communicate intent precisely. | |
| 864 | \\ * Edge cases matter. | |
| 865 | \\ * Favor reading code over writing code. | |
| 866 | \\ * Only one obvious way to do things. | |
| 867 | \\ * Runtime crashes are better than bugs. | |
| 868 | \\ * Compile errors are better than runtime crashes. | |
| 869 | \\ * Incremental improvements. | |
| 870 | \\ * Avoid local maximums. | |
| 871 | \\ * Reduce the amount one must remember. | |
| 872 | \\ * Minimize energy spent on coding style. | |
| 873 | \\ * Together we serve end users. | |
| 874 | \\ | |
| 875 | \\ | |
| 876 | ; | |
| 877 | ||
| 861 | const info_zen = @import("stage1.zig").info_zen; | |
| 878 | 862 | fn cmdZen(allocator: *Allocator, args: []const []const u8) !void { |
| 879 | 863 | try stdout.write(info_zen); |
| 880 | 864 | } |
src-self-hosted/stage1.zig created+27| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | // This is Zig code that is used by both stage1 and stage2. | |
| 2 | // The prototypes in src/userland.h must match these definitions. | |
| 3 | comptime { | |
| 4 | _ = @import("translate_c.zig"); | |
| 5 | } | |
| 6 | ||
| 7 | pub const info_zen = | |
| 8 | \\ | |
| 9 | \\ * Communicate intent precisely. | |
| 10 | \\ * Edge cases matter. | |
| 11 | \\ * Favor reading code over writing code. | |
| 12 | \\ * Only one obvious way to do things. | |
| 13 | \\ * Runtime crashes are better than bugs. | |
| 14 | \\ * Compile errors are better than runtime crashes. | |
| 15 | \\ * Incremental improvements. | |
| 16 | \\ * Avoid local maximums. | |
| 17 | \\ * Reduce the amount one must remember. | |
| 18 | \\ * Minimize energy spent on coding style. | |
| 19 | \\ * Together we serve end users. | |
| 20 | \\ | |
| 21 | \\ | |
| 22 | ; | |
| 23 | ||
| 24 | export fn stage2_zen(ptr: *[*]const u8, len: *usize) void { | |
| 25 | ptr.* = &info_zen; | |
| 26 | len.* = info_zen.len; | |
| 27 | } |
src-self-hosted/translate_c.zig created+8| ... | ... | @@ -0,0 +1,8 @@ |
| 1 | // This is the userland implementation of translate-c which will be used by both stage1 | |
| 2 | // and stage2. Currently it's not used by anything, as it's not feature complete. | |
| 3 | ||
| 4 | const std = @import("std"); | |
| 5 | ||
| 6 | export fn stage2_translate_c() void { | |
| 7 | std.debug.panic("unimplemented"); | |
| 8 | } |
src/codegen.cpp+17-5| ... | ... | @@ -19,6 +19,7 @@ |
| 19 | 19 | #include "target.hpp" |
| 20 | 20 | #include "util.hpp" |
| 21 | 21 | #include "zig_llvm.h" |
| 22 | #include "userland.h" | |
| 22 | 23 | |
| 23 | 24 | #include <stdio.h> |
| 24 | 25 | #include <errno.h> |
| ... | ... | @@ -92,7 +93,7 @@ static const char *symbols_that_llvm_depends_on[] = { |
| 92 | 93 | }; |
| 93 | 94 | |
| 94 | 95 | CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target, |
| 95 | OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, | |
| 96 | OutType out_type, BuildMode build_mode, Buf *override_lib_dir, Buf *override_std_dir, | |
| 96 | 97 | ZigLibCInstallation *libc, Buf *cache_dir) |
| 97 | 98 | { |
| 98 | 99 | CodeGen *g = allocate<CodeGen>(1); |
| ... | ... | @@ -100,19 +101,24 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget |
| 100 | 101 | codegen_add_time_event(g, "Initialize"); |
| 101 | 102 | |
| 102 | 103 | g->libc = libc; |
| 103 | g->zig_lib_dir = zig_lib_dir; | |
| 104 | 104 | g->zig_target = target; |
| 105 | 105 | g->cache_dir = cache_dir; |
| 106 | 106 | |
| 107 | if (override_lib_dir == nullptr) { | |
| 108 | g->zig_lib_dir = get_zig_lib_dir(); | |
| 109 | } else { | |
| 110 | g->zig_lib_dir = override_lib_dir; | |
| 111 | } | |
| 112 | ||
| 107 | 113 | if (override_std_dir == nullptr) { |
| 108 | 114 | g->zig_std_dir = buf_alloc(); |
| 109 | os_path_join(zig_lib_dir, buf_create_from_str("std"), g->zig_std_dir); | |
| 115 | os_path_join(g->zig_lib_dir, buf_create_from_str("std"), g->zig_std_dir); | |
| 110 | 116 | } else { |
| 111 | 117 | g->zig_std_dir = override_std_dir; |
| 112 | 118 | } |
| 113 | 119 | |
| 114 | 120 | g->zig_c_headers_dir = buf_alloc(); |
| 115 | os_path_join(zig_lib_dir, buf_create_from_str("include"), g->zig_c_headers_dir); | |
| 121 | os_path_join(g->zig_lib_dir, buf_create_from_str("include"), g->zig_c_headers_dir); | |
| 116 | 122 | |
| 117 | 123 | g->build_mode = build_mode; |
| 118 | 124 | g->out_type = out_type; |
| ... | ... | @@ -8147,7 +8153,7 @@ static void detect_libc(CodeGen *g) { |
| 8147 | 8153 | } |
| 8148 | 8154 | } |
| 8149 | 8155 | |
| 8150 | AstNode *codegen_translate_c(CodeGen *g, Buf *full_path) { | |
| 8156 | AstNode *codegen_translate_c(CodeGen *g, Buf *full_path, bool use_userland_implementation) { | |
| 8151 | 8157 | Buf *src_basename = buf_alloc(); |
| 8152 | 8158 | Buf *src_dirname = buf_alloc(); |
| 8153 | 8159 | os_path_split(full_path, src_dirname, src_basename); |
| ... | ... | @@ -8159,6 +8165,12 @@ AstNode *codegen_translate_c(CodeGen *g, Buf *full_path) { |
| 8159 | 8165 | |
| 8160 | 8166 | init(g); |
| 8161 | 8167 | |
| 8168 | if (use_userland_implementation) { | |
| 8169 | // TODO improve this | |
| 8170 | stage2_translate_c(); | |
| 8171 | zig_panic("TODO"); | |
| 8172 | } | |
| 8173 | ||
| 8162 | 8174 | ZigList<ErrorMsg *> errors = {0}; |
| 8163 | 8175 | AstNode *root_node; |
| 8164 | 8176 | Error err = parse_h_file(&root_node, &errors, buf_ptr(full_path), g, nullptr); |
src/codegen.hpp+1-1| ... | ... | @@ -50,7 +50,7 @@ ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const c |
| 50 | 50 | void codegen_add_assembly(CodeGen *g, Buf *path); |
| 51 | 51 | void codegen_add_object(CodeGen *g, Buf *object_path); |
| 52 | 52 | |
| 53 | AstNode *codegen_translate_c(CodeGen *g, Buf *path); | |
| 53 | AstNode *codegen_translate_c(CodeGen *g, Buf *path, bool use_userland_implementation); | |
| 54 | 54 | |
| 55 | 55 | Buf *codegen_generate_builtin_source(CodeGen *g); |
| 56 | 56 |
src/compiler.cpp+4-4| ... | ... | @@ -179,24 +179,24 @@ Buf *get_zig_lib_dir(void) { |
| 179 | 179 | return &saved_lib_dir; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | Buf *get_zig_std_dir() { | |
| 182 | Buf *get_zig_std_dir(Buf *zig_lib_dir) { | |
| 183 | 183 | if (saved_std_dir.list.length != 0) { |
| 184 | 184 | return &saved_std_dir; |
| 185 | 185 | } |
| 186 | 186 | buf_resize(&saved_std_dir, 0); |
| 187 | 187 | |
| 188 | os_path_join(get_zig_lib_dir(), buf_create_from_str("std"), &saved_std_dir); | |
| 188 | os_path_join(zig_lib_dir, buf_create_from_str("std"), &saved_std_dir); | |
| 189 | 189 | |
| 190 | 190 | return &saved_std_dir; |
| 191 | 191 | } |
| 192 | 192 | |
| 193 | Buf *get_zig_special_dir() { | |
| 193 | Buf *get_zig_special_dir(Buf *zig_lib_dir) { | |
| 194 | 194 | if (saved_special_dir.list.length != 0) { |
| 195 | 195 | return &saved_special_dir; |
| 196 | 196 | } |
| 197 | 197 | buf_resize(&saved_special_dir, 0); |
| 198 | 198 | |
| 199 | os_path_join(get_zig_std_dir(), buf_sprintf("special"), &saved_special_dir); | |
| 199 | os_path_join(get_zig_std_dir(zig_lib_dir), buf_sprintf("special"), &saved_special_dir); | |
| 200 | 200 | |
| 201 | 201 | return &saved_special_dir; |
| 202 | 202 | } |
src/compiler.hpp+2-2| ... | ... | @@ -16,7 +16,7 @@ Error get_compiler_id(Buf **result); |
| 16 | 16 | Buf *get_self_dynamic_linker_path(void); |
| 17 | 17 | |
| 18 | 18 | Buf *get_zig_lib_dir(void); |
| 19 | Buf *get_zig_special_dir(void); | |
| 20 | Buf *get_zig_std_dir(void); | |
| 19 | Buf *get_zig_special_dir(Buf *zig_lib_dir); | |
| 20 | Buf *get_zig_std_dir(Buf *zig_lib_dir); | |
| 21 | 21 | |
| 22 | 22 | #endif |
src/main.cpp+61-40| ... | ... | @@ -14,6 +14,7 @@ |
| 14 | 14 | #include "os.hpp" |
| 15 | 15 | #include "target.hpp" |
| 16 | 16 | #include "libc_installation.hpp" |
| 17 | #include "userland.h" | |
| 17 | 18 | |
| 18 | 19 | #include <stdio.h> |
| 19 | 20 | |
| ... | ... | @@ -40,6 +41,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { |
| 40 | 41 | " libc [paths_file] Display native libc paths file or validate one\n" |
| 41 | 42 | " run [source] [-- [args]] create executable and run immediately\n" |
| 42 | 43 | " translate-c [source] convert c code to zig code\n" |
| 44 | " translate-c-2 [source] experimental self-hosted translate-c\n" | |
| 43 | 45 | " targets list available compilation targets\n" |
| 44 | 46 | " test [source] create and run a test build\n" |
| 45 | 47 | " version print version number and exit\n" |
| ... | ... | @@ -131,19 +133,6 @@ static int print_libc_usage(const char *arg0, FILE *file, int return_code) { |
| 131 | 133 | return return_code; |
| 132 | 134 | } |
| 133 | 135 | |
| 134 | static const char *ZIG_ZEN = "\n" | |
| 135 | " * Communicate intent precisely.\n" | |
| 136 | " * Edge cases matter.\n" | |
| 137 | " * Favor reading code over writing code.\n" | |
| 138 | " * Only one obvious way to do things.\n" | |
| 139 | " * Runtime crashes are better than bugs.\n" | |
| 140 | " * Compile errors are better than runtime crashes.\n" | |
| 141 | " * Incremental improvements.\n" | |
| 142 | " * Avoid local maximums.\n" | |
| 143 | " * Reduce the amount one must remember.\n" | |
| 144 | " * Minimize energy spent on coding style.\n" | |
| 145 | " * Together we serve end users.\n"; | |
| 146 | ||
| 147 | 136 | static bool arch_available_in_llvm(ZigLLVM_ArchType arch) { |
| 148 | 137 | LLVMTargetRef target_ref; |
| 149 | 138 | char *err_msg = nullptr; |
| ... | ... | @@ -211,6 +200,7 @@ enum Cmd { |
| 211 | 200 | CmdTargets, |
| 212 | 201 | CmdTest, |
| 213 | 202 | CmdTranslateC, |
| 203 | CmdTranslateCUserland, | |
| 214 | 204 | CmdVersion, |
| 215 | 205 | CmdZen, |
| 216 | 206 | CmdLibC, |
| ... | ... | @@ -324,7 +314,7 @@ int main(int argc, char **argv) { |
| 324 | 314 | return print_error_usage(arg0); |
| 325 | 315 | } |
| 326 | 316 | Buf *cmd_template_path = buf_alloc(); |
| 327 | os_path_join(get_zig_special_dir(), buf_create_from_str(init_cmd), cmd_template_path); | |
| 317 | os_path_join(get_zig_special_dir(get_zig_lib_dir()), buf_create_from_str(init_cmd), cmd_template_path); | |
| 328 | 318 | Buf *build_zig_path = buf_alloc(); |
| 329 | 319 | os_path_join(cmd_template_path, buf_create_from_str("build.zig"), build_zig_path); |
| 330 | 320 | Buf *src_dir_path = buf_alloc(); |
| ... | ... | @@ -453,6 +443,7 @@ int main(int argc, char **argv) { |
| 453 | 443 | bool want_single_threaded = false; |
| 454 | 444 | bool disable_gen_h = false; |
| 455 | 445 | Buf *override_std_dir = nullptr; |
| 446 | Buf *override_lib_dir = nullptr; | |
| 456 | 447 | Buf *main_pkg_path = nullptr; |
| 457 | 448 | ValgrindSupport valgrind_support = ValgrindSupportAuto; |
| 458 | 449 | WantPIC want_pic = WantPICAuto; |
| ... | ... | @@ -486,13 +477,27 @@ int main(int argc, char **argv) { |
| 486 | 477 | } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) { |
| 487 | 478 | cache_dir = argv[i + 1]; |
| 488 | 479 | i += 1; |
| 480 | } else if (i + 1 < argc && strcmp(argv[i], "--override-std-dir") == 0) { | |
| 481 | override_std_dir = buf_create_from_str(argv[i + 1]); | |
| 482 | i += 1; | |
| 483 | ||
| 484 | args.append("--override-std-dir"); | |
| 485 | args.append(buf_ptr(override_std_dir)); | |
| 486 | } else if (i + 1 < argc && strcmp(argv[i], "--override-lib-dir") == 0) { | |
| 487 | override_lib_dir = buf_create_from_str(argv[i + 1]); | |
| 488 | i += 1; | |
| 489 | ||
| 490 | args.append("--override-lib-dir"); | |
| 491 | args.append(buf_ptr(override_lib_dir)); | |
| 489 | 492 | } else { |
| 490 | 493 | args.append(argv[i]); |
| 491 | 494 | } |
| 492 | 495 | } |
| 493 | 496 | |
| 497 | Buf *zig_lib_dir = (override_lib_dir == nullptr) ? get_zig_lib_dir() : override_lib_dir; | |
| 498 | ||
| 494 | 499 | Buf *build_runner_path = buf_alloc(); |
| 495 | os_path_join(get_zig_special_dir(), buf_create_from_str("build_runner.zig"), build_runner_path); | |
| 500 | os_path_join(get_zig_special_dir(zig_lib_dir), buf_create_from_str("build_runner.zig"), build_runner_path); | |
| 496 | 501 | |
| 497 | 502 | ZigTarget target; |
| 498 | 503 | get_native_target(&target); |
| ... | ... | @@ -512,7 +517,7 @@ int main(int argc, char **argv) { |
| 512 | 517 | } |
| 513 | 518 | |
| 514 | 519 | CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, OutTypeExe, |
| 515 | BuildModeDebug, get_zig_lib_dir(), override_std_dir, nullptr, &full_cache_dir); | |
| 520 | BuildModeDebug, override_lib_dir, override_std_dir, nullptr, &full_cache_dir); | |
| 516 | 521 | g->valgrind_support = valgrind_support; |
| 517 | 522 | g->enable_time_report = timing_info; |
| 518 | 523 | codegen_set_out_name(g, buf_create_from_str("build")); |
| ... | ... | @@ -532,23 +537,25 @@ int main(int argc, char **argv) { |
| 532 | 537 | "Usage: %s build [options]\n" |
| 533 | 538 | "\n" |
| 534 | 539 | "General Options:\n" |
| 535 | " --help Print this help and exit\n" | |
| 536 | " --verbose Print commands before executing them\n" | |
| 537 | " --prefix [path] Override default install prefix\n" | |
| 538 | " --search-prefix [path] Add a path to look for binaries, libraries, headers\n" | |
| 540 | " --help Print this help and exit\n" | |
| 541 | " --verbose Print commands before executing them\n" | |
| 542 | " --prefix [path] Override default install prefix\n" | |
| 543 | " --search-prefix [path] Add a path to look for binaries, libraries, headers\n" | |
| 539 | 544 | "\n" |
| 540 | 545 | "Project-specific options become available when the build file is found.\n" |
| 541 | 546 | "\n" |
| 542 | 547 | "Advanced Options:\n" |
| 543 | " --build-file [file] Override path to build.zig\n" | |
| 544 | " --cache-dir [path] Override path to cache directory\n" | |
| 545 | " --verbose-tokenize Enable compiler debug output for tokenization\n" | |
| 546 | " --verbose-ast Enable compiler debug output for parsing into an AST\n" | |
| 547 | " --verbose-link Enable compiler debug output for linking\n" | |
| 548 | " --verbose-ir Enable compiler debug output for Zig IR\n" | |
| 549 | " --verbose-llvm-ir Enable compiler debug output for LLVM IR\n" | |
| 550 | " --verbose-cimport Enable compiler debug output for C imports\n" | |
| 551 | " --verbose-cc Enable compiler debug output for C compilation\n" | |
| 548 | " --build-file [file] Override path to build.zig\n" | |
| 549 | " --cache-dir [path] Override path to cache directory\n" | |
| 550 | " --override-std-dir [arg] Override path to Zig standard library\n" | |
| 551 | " --override-lib-dir [arg] Override path to Zig lib library\n" | |
| 552 | " --verbose-tokenize Enable compiler debug output for tokenization\n" | |
| 553 | " --verbose-ast Enable compiler debug output for parsing into an AST\n" | |
| 554 | " --verbose-link Enable compiler debug output for linking\n" | |
| 555 | " --verbose-ir Enable compiler debug output for Zig IR\n" | |
| 556 | " --verbose-llvm-ir Enable compiler debug output for LLVM IR\n" | |
| 557 | " --verbose-cimport Enable compiler debug output for C imports\n" | |
| 558 | " --verbose-cc Enable compiler debug output for C compilation\n" | |
| 552 | 559 | "\n" |
| 553 | 560 | , zig_exe_path); |
| 554 | 561 | return EXIT_SUCCESS; |
| ... | ... | @@ -584,11 +591,12 @@ int main(int argc, char **argv) { |
| 584 | 591 | init_all_targets(); |
| 585 | 592 | ZigTarget target; |
| 586 | 593 | get_native_target(&target); |
| 594 | Buf *zig_lib_dir = (override_lib_dir == nullptr) ? get_zig_lib_dir() : override_lib_dir; | |
| 587 | 595 | Buf *fmt_runner_path = buf_alloc(); |
| 588 | os_path_join(get_zig_special_dir(), buf_create_from_str("fmt_runner.zig"), fmt_runner_path); | |
| 596 | os_path_join(get_zig_special_dir(zig_lib_dir), buf_create_from_str("fmt_runner.zig"), fmt_runner_path); | |
| 589 | 597 | Buf *cache_dir_buf = buf_create_from_str(cache_dir ? cache_dir : default_zig_cache_name); |
| 590 | 598 | CodeGen *g = codegen_create(main_pkg_path, fmt_runner_path, &target, OutTypeExe, |
| 591 | BuildModeDebug, get_zig_lib_dir(), nullptr, nullptr, cache_dir_buf); | |
| 599 | BuildModeDebug, zig_lib_dir, nullptr, nullptr, cache_dir_buf); | |
| 592 | 600 | g->valgrind_support = valgrind_support; |
| 593 | 601 | g->want_single_threaded = true; |
| 594 | 602 | codegen_set_out_name(g, buf_create_from_str("fmt")); |
| ... | ... | @@ -757,6 +765,8 @@ int main(int argc, char **argv) { |
| 757 | 765 | llvm_argv.append(argv[i]); |
| 758 | 766 | } else if (strcmp(arg, "--override-std-dir") == 0) { |
| 759 | 767 | override_std_dir = buf_create_from_str(argv[i]); |
| 768 | } else if (strcmp(arg, "--override-lib-dir") == 0) { | |
| 769 | override_lib_dir = buf_create_from_str(argv[i]); | |
| 760 | 770 | } else if (strcmp(arg, "--main-pkg-path") == 0) { |
| 761 | 771 | main_pkg_path = buf_create_from_str(argv[i]); |
| 762 | 772 | } else if (strcmp(arg, "--library-path") == 0 || strcmp(arg, "-L") == 0) { |
| ... | ... | @@ -867,6 +877,8 @@ int main(int argc, char **argv) { |
| 867 | 877 | cmd = CmdLibC; |
| 868 | 878 | } else if (strcmp(arg, "translate-c") == 0) { |
| 869 | 879 | cmd = CmdTranslateC; |
| 880 | } else if (strcmp(arg, "translate-c-2") == 0) { | |
| 881 | cmd = CmdTranslateCUserland; | |
| 870 | 882 | } else if (strcmp(arg, "test") == 0) { |
| 871 | 883 | cmd = CmdTest; |
| 872 | 884 | out_type = OutTypeExe; |
| ... | ... | @@ -883,6 +895,7 @@ int main(int argc, char **argv) { |
| 883 | 895 | case CmdBuild: |
| 884 | 896 | case CmdRun: |
| 885 | 897 | case CmdTranslateC: |
| 898 | case CmdTranslateCUserland: | |
| 886 | 899 | case CmdTest: |
| 887 | 900 | case CmdLibC: |
| 888 | 901 | if (!in_file) { |
| ... | ... | @@ -959,7 +972,7 @@ int main(int argc, char **argv) { |
| 959 | 972 | } |
| 960 | 973 | case CmdBuiltin: { |
| 961 | 974 | CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, |
| 962 | out_type, build_mode, get_zig_lib_dir(), override_std_dir, nullptr, nullptr); | |
| 975 | out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr); | |
| 963 | 976 | g->valgrind_support = valgrind_support; |
| 964 | 977 | g->want_pic = want_pic; |
| 965 | 978 | g->want_single_threaded = want_single_threaded; |
| ... | ... | @@ -973,6 +986,7 @@ int main(int argc, char **argv) { |
| 973 | 986 | case CmdRun: |
| 974 | 987 | case CmdBuild: |
| 975 | 988 | case CmdTranslateC: |
| 989 | case CmdTranslateCUserland: | |
| 976 | 990 | case CmdTest: |
| 977 | 991 | { |
| 978 | 992 | if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0 && |
| ... | ... | @@ -985,14 +999,16 @@ int main(int argc, char **argv) { |
| 985 | 999 | " * --assembly argument\n" |
| 986 | 1000 | " * --c-source argument\n"); |
| 987 | 1001 | return print_error_usage(arg0); |
| 988 | } else if ((cmd == CmdTranslateC || cmd == CmdTest || cmd == CmdRun) && !in_file) { | |
| 1002 | } else if ((cmd == CmdTranslateC || cmd == CmdTranslateCUserland || | |
| 1003 | cmd == CmdTest || cmd == CmdRun) && !in_file) | |
| 1004 | { | |
| 989 | 1005 | fprintf(stderr, "Expected source file argument.\n"); |
| 990 | 1006 | return print_error_usage(arg0); |
| 991 | 1007 | } |
| 992 | 1008 | |
| 993 | 1009 | assert(cmd != CmdBuild || out_type != OutTypeUnknown); |
| 994 | 1010 | |
| 995 | bool need_name = (cmd == CmdBuild || cmd == CmdTranslateC); | |
| 1011 | bool need_name = (cmd == CmdBuild || cmd == CmdTranslateC || cmd == CmdTranslateCUserland); | |
| 996 | 1012 | |
| 997 | 1013 | if (cmd == CmdRun) { |
| 998 | 1014 | out_name = "run"; |
| ... | ... | @@ -1026,7 +1042,8 @@ int main(int argc, char **argv) { |
| 1026 | 1042 | return print_error_usage(arg0); |
| 1027 | 1043 | } |
| 1028 | 1044 | |
| 1029 | Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf; | |
| 1045 | Buf *zig_root_source_file = (cmd == CmdTranslateC || cmd == CmdTranslateCUserland) ? | |
| 1046 | nullptr : in_file_buf; | |
| 1030 | 1047 | |
| 1031 | 1048 | if (cmd == CmdRun && buf_out_name == nullptr) { |
| 1032 | 1049 | buf_out_name = buf_create_from_str("run"); |
| ... | ... | @@ -1050,7 +1067,7 @@ int main(int argc, char **argv) { |
| 1050 | 1067 | cache_dir_buf = buf_create_from_str(cache_dir); |
| 1051 | 1068 | } |
| 1052 | 1069 | CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, out_type, build_mode, |
| 1053 | get_zig_lib_dir(), override_std_dir, libc, cache_dir_buf); | |
| 1070 | override_lib_dir, override_std_dir, libc, cache_dir_buf); | |
| 1054 | 1071 | if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2); |
| 1055 | 1072 | g->valgrind_support = valgrind_support; |
| 1056 | 1073 | g->want_pic = want_pic; |
| ... | ... | @@ -1170,8 +1187,8 @@ int main(int argc, char **argv) { |
| 1170 | 1187 | } else { |
| 1171 | 1188 | zig_unreachable(); |
| 1172 | 1189 | } |
| 1173 | } else if (cmd == CmdTranslateC) { | |
| 1174 | AstNode *root_node = codegen_translate_c(g, in_file_buf); | |
| 1190 | } else if (cmd == CmdTranslateC || cmd == CmdTranslateCUserland) { | |
| 1191 | AstNode *root_node = codegen_translate_c(g, in_file_buf, cmd == CmdTranslateCUserland); | |
| 1175 | 1192 | ast_render(g, stdout, root_node, 4); |
| 1176 | 1193 | if (timing_info) |
| 1177 | 1194 | codegen_print_timing_report(g, stderr); |
| ... | ... | @@ -1229,9 +1246,13 @@ int main(int argc, char **argv) { |
| 1229 | 1246 | case CmdVersion: |
| 1230 | 1247 | printf("%s\n", ZIG_VERSION_STRING); |
| 1231 | 1248 | return EXIT_SUCCESS; |
| 1232 | case CmdZen: | |
| 1233 | printf("%s\n", ZIG_ZEN); | |
| 1249 | case CmdZen: { | |
| 1250 | const char *ptr; | |
| 1251 | size_t len; | |
| 1252 | stage2_zen(&ptr, &len); | |
| 1253 | fwrite(ptr, len, 1, stdout); | |
| 1234 | 1254 | return EXIT_SUCCESS; |
| 1255 | } | |
| 1235 | 1256 | case CmdTargets: |
| 1236 | 1257 | return print_target_list(stdout); |
| 1237 | 1258 | case CmdNone: |
src/userland.cpp created+10| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | // This file is a shim for zig1. The real implementations of these are in | |
| 2 | // src-self-hosted/stage1.zig | |
| 3 | ||
| 4 | #include "userland.h" | |
| 5 | ||
| 6 | void stage2_translate_c(void) {} | |
| 7 | void stage2_zen(const char **ptr, size_t *len) { | |
| 8 | *ptr = nullptr; | |
| 9 | *len = 0; | |
| 10 | } |
src/userland.h created+23| ... | ... | @@ -0,0 +1,23 @@ |
| 1 | /* | |
| 2 | * Copyright (c) 2019 Andrew Kelley | |
| 3 | * | |
| 4 | * This file is part of zig, which is MIT licensed. | |
| 5 | * See http://opensource.org/licenses/MIT | |
| 6 | */ | |
| 7 | ||
| 8 | #ifndef ZIG_USERLAND_H | |
| 9 | #define ZIG_USERLAND_H | |
| 10 | ||
| 11 | #include <stddef.h> | |
| 12 | ||
| 13 | #ifdef __cplusplus | |
| 14 | #define ZIG_USERLAND_EXTERN_C extern "C" | |
| 15 | #else | |
| 16 | #define ZIG_USERLAND_EXTERN_C | |
| 17 | #endif | |
| 18 | ||
| 19 | ZIG_USERLAND_EXTERN_C void stage2_translate_c(void); | |
| 20 | ||
| 21 | ZIG_USERLAND_EXTERN_C void stage2_zen(const char **ptr, size_t *len); | |
| 22 | ||
| 23 | #endif |
std/build.zig+17| ... | ... | @@ -50,6 +50,8 @@ pub const Builder = struct { |
| 50 | 50 | build_root: []const u8, |
| 51 | 51 | cache_root: []const u8, |
| 52 | 52 | release_mode: ?builtin.Mode, |
| 53 | override_std_dir: ?[]const u8, | |
| 54 | override_lib_dir: ?[]const u8, | |
| 53 | 55 | |
| 54 | 56 | pub const CStd = enum { |
| 55 | 57 | C89, |
| ... | ... | @@ -133,6 +135,8 @@ pub const Builder = struct { |
| 133 | 135 | }, |
| 134 | 136 | .have_install_step = false, |
| 135 | 137 | .release_mode = null, |
| 138 | .override_std_dir = null, | |
| 139 | .override_lib_dir = null, | |
| 136 | 140 | }; |
| 137 | 141 | self.detectNativeSystemPaths(); |
| 138 | 142 | self.default_step = self.step("default", "Build the project"); |
| ... | ... | @@ -939,6 +943,7 @@ pub const LibExeObjStep = struct { |
| 939 | 943 | disable_gen_h: bool, |
| 940 | 944 | c_std: Builder.CStd, |
| 941 | 945 | override_std_dir: ?[]const u8, |
| 946 | override_lib_dir: ?[]const u8, | |
| 942 | 947 | main_pkg_path: ?[]const u8, |
| 943 | 948 | exec_cmd_args: ?[]const ?[]const u8, |
| 944 | 949 | name_prefix: []const u8, |
| ... | ... | @@ -1039,6 +1044,7 @@ pub const LibExeObjStep = struct { |
| 1039 | 1044 | .c_std = Builder.CStd.C99, |
| 1040 | 1045 | .system_linker_hack = false, |
| 1041 | 1046 | .override_std_dir = null, |
| 1047 | .override_lib_dir = null, | |
| 1042 | 1048 | .main_pkg_path = null, |
| 1043 | 1049 | .exec_cmd_args = null, |
| 1044 | 1050 | .name_prefix = "", |
| ... | ... | @@ -1528,6 +1534,17 @@ pub const LibExeObjStep = struct { |
| 1528 | 1534 | if (self.override_std_dir) |dir| { |
| 1529 | 1535 | try zig_args.append("--override-std-dir"); |
| 1530 | 1536 | try zig_args.append(builder.pathFromRoot(dir)); |
| 1537 | } else if (self.builder.override_std_dir) |dir| { | |
| 1538 | try zig_args.append("--override-std-dir"); | |
| 1539 | try zig_args.append(builder.pathFromRoot(dir)); | |
| 1540 | } | |
| 1541 | ||
| 1542 | if (self.override_lib_dir) |dir| { | |
| 1543 | try zig_args.append("--override-lib-dir"); | |
| 1544 | try zig_args.append(builder.pathFromRoot(dir)); | |
| 1545 | } else if (self.builder.override_lib_dir) |dir| { | |
| 1546 | try zig_args.append("--override-lib-dir"); | |
| 1547 | try zig_args.append(builder.pathFromRoot(dir)); | |
| 1531 | 1548 | } |
| 1532 | 1549 | |
| 1533 | 1550 | if (self.main_pkg_path) |dir| { |
std/special/build_runner.zig+21-9| ... | ... | @@ -94,6 +94,16 @@ pub fn main() !void { |
| 94 | 94 | return usageAndErr(&builder, false, try stderr_stream); |
| 95 | 95 | }); |
| 96 | 96 | builder.addSearchPrefix(search_prefix); |
| 97 | } else if (mem.eql(u8, arg, "--override-std-dir")) { | |
| 98 | builder.override_std_dir = try unwrapArg(arg_it.next(allocator) orelse { | |
| 99 | warn("Expected argument after --override-std-dir\n\n"); | |
| 100 | return usageAndErr(&builder, false, try stderr_stream); | |
| 101 | }); | |
| 102 | } else if (mem.eql(u8, arg, "--override-lib-dir")) { | |
| 103 | builder.override_lib_dir = try unwrapArg(arg_it.next(allocator) orelse { | |
| 104 | warn("Expected argument after --override-lib-dir\n\n"); | |
| 105 | return usageAndErr(&builder, false, try stderr_stream); | |
| 106 | }); | |
| 97 | 107 | } else if (mem.eql(u8, arg, "--verbose-tokenize")) { |
| 98 | 108 | builder.verbose_tokenize = true; |
| 99 | 109 | } else if (mem.eql(u8, arg, "--verbose-ast")) { |
| ... | ... | @@ -187,15 +197,17 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void { |
| 187 | 197 | try out_stream.write( |
| 188 | 198 | \\ |
| 189 | 199 | \\Advanced Options: |
| 190 | \\ --build-file [file] Override path to build.zig | |
| 191 | \\ --cache-dir [path] Override path to zig cache directory | |
| 192 | \\ --verbose-tokenize Enable compiler debug output for tokenization | |
| 193 | \\ --verbose-ast Enable compiler debug output for parsing into an AST | |
| 194 | \\ --verbose-link Enable compiler debug output for linking | |
| 195 | \\ --verbose-ir Enable compiler debug output for Zig IR | |
| 196 | \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR | |
| 197 | \\ --verbose-cimport Enable compiler debug output for C imports | |
| 198 | \\ --verbose-cc Enable compiler debug output for C compilation | |
| 200 | \\ --build-file [file] Override path to build.zig | |
| 201 | \\ --cache-dir [path] Override path to zig cache directory | |
| 202 | \\ --override-std-dir [arg] Override path to Zig standard library | |
| 203 | \\ --override-lib-dir [arg] Override path to Zig lib directory | |
| 204 | \\ --verbose-tokenize Enable compiler debug output for tokenization | |
| 205 | \\ --verbose-ast Enable compiler debug output for parsing into an AST | |
| 206 | \\ --verbose-link Enable compiler debug output for linking | |
| 207 | \\ --verbose-ir Enable compiler debug output for Zig IR | |
| 208 | \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR | |
| 209 | \\ --verbose-cimport Enable compiler debug output for C imports | |
| 210 | \\ --verbose-cc Enable compiler debug output for C compilation | |
| 199 | 211 | \\ |
| 200 | 212 | ); |
| 201 | 213 | } |