authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-17 00:24:51-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-04-17 00:24:51-04:00
logbc2d60c11f7e90f401a4412d3dc6fca985071169
tree8e3e4e71dc24d17b4028069381b6aee0a43df347
parent4c03746926562e4e9650eec7c4361836fabba5af
parentda8403bcdd6fb6928a26e5cb3813d9dc67db2eed
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2295 from ziglang/stage1-hybrid

stage1 is now a hybrid of C++ and Zig

14 files changed, 262 insertions(+), 89 deletions(-)

CMakeLists.txt+50-11
...@@ -407,6 +407,12 @@ set(SOFTFLOAT_LIBRARIES embedded_softfloat)...@@ -407,6 +407,12 @@ set(SOFTFLOAT_LIBRARIES embedded_softfloat)
407407
408find_package(Threads)408find_package(Threads)
409409
410# CMake doesn't let us create an empty executable, so we hang on to this one separately.
411set(ZIG_MAIN_SRC "${CMAKE_SOURCE_DIR}/src/main.cpp")
412
413# This is our shim which will be replaced by libuserland written in Zig.
414set(ZIG1_SHIM_SRC "${CMAKE_SOURCE_DIR}/src/userland.cpp")
415
410set(ZIG_SOURCES416set(ZIG_SOURCES
411 "${CMAKE_SOURCE_DIR}/src/analyze.cpp"417 "${CMAKE_SOURCE_DIR}/src/analyze.cpp"
412 "${CMAKE_SOURCE_DIR}/src/ast_render.cpp"418 "${CMAKE_SOURCE_DIR}/src/ast_render.cpp"
...@@ -423,7 +429,6 @@ set(ZIG_SOURCES...@@ -423,7 +429,6 @@ set(ZIG_SOURCES
423 "${CMAKE_SOURCE_DIR}/src/ir_print.cpp"429 "${CMAKE_SOURCE_DIR}/src/ir_print.cpp"
424 "${CMAKE_SOURCE_DIR}/src/libc_installation.cpp"430 "${CMAKE_SOURCE_DIR}/src/libc_installation.cpp"
425 "${CMAKE_SOURCE_DIR}/src/link.cpp"431 "${CMAKE_SOURCE_DIR}/src/link.cpp"
426 "${CMAKE_SOURCE_DIR}/src/main.cpp"
427 "${CMAKE_SOURCE_DIR}/src/os.cpp"432 "${CMAKE_SOURCE_DIR}/src/os.cpp"
428 "${CMAKE_SOURCE_DIR}/src/parser.cpp"433 "${CMAKE_SOURCE_DIR}/src/parser.cpp"
429 "${CMAKE_SOURCE_DIR}/src/range_set.cpp"434 "${CMAKE_SOURCE_DIR}/src/range_set.cpp"
...@@ -6635,19 +6640,19 @@ add_library(zig_cpp STATIC ${ZIG_CPP_SOURCES})...@@ -6635,19 +6640,19 @@ add_library(zig_cpp STATIC ${ZIG_CPP_SOURCES})
6635set_target_properties(zig_cpp PROPERTIES6640set_target_properties(zig_cpp PROPERTIES
6636 COMPILE_FLAGS ${EXE_CFLAGS}6641 COMPILE_FLAGS ${EXE_CFLAGS}
6637)6642)
6643install(TARGETS zig_cpp DESTINATION "${ZIG_CPP_LIB_DIR}")
66386644
6639add_library(opt_c_util STATIC ${OPTIMIZED_C_SOURCES})6645add_library(opt_c_util STATIC ${OPTIMIZED_C_SOURCES})
6640set_target_properties(opt_c_util PROPERTIES6646set_target_properties(opt_c_util PROPERTIES
6641 COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}"6647 COMPILE_FLAGS "${OPTIMIZED_C_FLAGS}"
6642)6648)
66436649
6644add_executable(zig ${ZIG_SOURCES})6650add_library(compiler STATIC ${ZIG_SOURCES})
6645set_target_properties(zig PROPERTIES6651set_target_properties(compiler PROPERTIES
6646 COMPILE_FLAGS ${EXE_CFLAGS}6652 COMPILE_FLAGS ${EXE_CFLAGS}
6647 LINK_FLAGS ${EXE_LDFLAGS}6653 LINK_FLAGS ${EXE_LDFLAGS}
6648)6654)
66496655target_link_libraries(compiler LINK_PUBLIC
6650target_link_libraries(zig LINK_PUBLIC
6651 zig_cpp6656 zig_cpp
6652 opt_c_util6657 opt_c_util
6653 ${SOFTFLOAT_LIBRARIES}6658 ${SOFTFLOAT_LIBRARIES}
...@@ -6656,24 +6661,58 @@ target_link_libraries(zig LINK_PUBLIC...@@ -6656,24 +6661,58 @@ target_link_libraries(zig LINK_PUBLIC
6656 ${LLVM_LIBRARIES}6661 ${LLVM_LIBRARIES}
6657 ${CMAKE_THREAD_LIBS_INIT}6662 ${CMAKE_THREAD_LIBS_INIT}
6658)6663)
6659
6660if(NOT MSVC)6664if(NOT MSVC)
6661 target_link_libraries(zig LINK_PUBLIC ${LIBXML2})6665 target_link_libraries(compiler LINK_PUBLIC ${LIBXML2})
6662endif()6666endif()
66636667
6664if(MINGW)6668if(MINGW)
6665 target_link_libraries(zig LINK_PUBLIC ${Z3_LIBRARIES})6669 target_link_libraries(compiler LINK_PUBLIC ${Z3_LIBRARIES})
6666endif()6670endif()
66676671
6668if(ZIG_DIA_GUIDS_LIB)6672if(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})
6670endif()6674endif()
66716675
6672if(MSVC OR MINGW)6676if(MSVC OR MINGW)
6673 target_link_libraries(zig LINK_PUBLIC version)6677 target_link_libraries(compiler LINK_PUBLIC version)
6678endif()
6679
6680add_executable(zig1 "${ZIG_MAIN_SRC}" "${ZIG1_SHIM_SRC}")
6681set_target_properties(zig1 PROPERTIES
6682 COMPILE_FLAGS ${EXE_CFLAGS}
6683 LINK_FLAGS ${EXE_LDFLAGS}
6684)
6685target_link_libraries(zig1 compiler)
6686
6687if(WIN32)
6688 set(LIBUSERLAND "${CMAKE_BINARY_DIR}/userland.lib")
6689elseif(APPLE)
6690 set(LIBUSERLAND "${CMAKE_BINARY_DIR}/userland.o")
6691else()
6692 set(LIBUSERLAND "${CMAKE_BINARY_DIR}/libuserland.a")
6674endif()6693endif()
6694add_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)
6706add_custom_target(userland_target DEPENDS "${LIBUSERLAND}")
6707add_executable(zig "${ZIG_MAIN_SRC}")
6708set_target_properties(zig PROPERTIES
6709 COMPILE_FLAGS ${EXE_CFLAGS}
6710 LINK_FLAGS ${EXE_LDFLAGS}
6711)
6712target_link_libraries(zig compiler "${LIBUSERLAND}")
6713add_dependencies(zig userland_target)
6675install(TARGETS zig DESTINATION bin)6714install(TARGETS zig DESTINATION bin)
6676install(TARGETS zig_cpp DESTINATION "${ZIG_CPP_LIB_DIR}")6715
66776716
6678foreach(file ${ZIG_C_HEADER_FILES})6717foreach(file ${ZIG_C_HEADER_FILES})
6679 get_filename_component(file_dir "${C_HEADERS_DEST}/${file}" DIRECTORY)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,6 +65,8 @@ pub fn build(b: *Builder) !void {
6565
66 b.default_step.dependOn(&exe.step);66 b.default_step.dependOn(&exe.step);
6767
68 addLibUserlandStep(b);
69
68 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;70 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
69 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;71 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;
70 const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release;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,3 +382,21 @@ const Context = struct {
380 dia_guids_lib: []const u8,382 dia_guids_lib: []const u8,
381 llvm: LibraryDep,383 llvm: LibraryDep,
382};384};
385
386fn 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,23 +858,7 @@ fn cmdHelp(allocator: *Allocator, args: []const []const u8) !void {
858 try stdout.write(usage);858 try stdout.write(usage);
859}859}
860860
861const info_zen =861const info_zen = @import("stage1.zig").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
878fn cmdZen(allocator: *Allocator, args: []const []const u8) !void {862fn cmdZen(allocator: *Allocator, args: []const []const u8) !void {
879 try stdout.write(info_zen);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.
3comptime {
4 _ = @import("translate_c.zig");
5}
6
7pub 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
24export 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
4const std = @import("std");
5
6export fn stage2_translate_c() void {
7 std.debug.panic("unimplemented");
8}
src/codegen.cpp+17-5
...@@ -19,6 +19,7 @@...@@ -19,6 +19,7 @@
19#include "target.hpp"19#include "target.hpp"
20#include "util.hpp"20#include "util.hpp"
21#include "zig_llvm.h"21#include "zig_llvm.h"
22#include "userland.h"
2223
23#include <stdio.h>24#include <stdio.h>
24#include <errno.h>25#include <errno.h>
...@@ -92,7 +93,7 @@ static const char *symbols_that_llvm_depends_on[] = {...@@ -92,7 +93,7 @@ static const char *symbols_that_llvm_depends_on[] = {
92};93};
9394
94CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target,95CodeGen *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 ZigLibCInstallation *libc, Buf *cache_dir)97 ZigLibCInstallation *libc, Buf *cache_dir)
97{98{
98 CodeGen *g = allocate<CodeGen>(1);99 CodeGen *g = allocate<CodeGen>(1);
...@@ -100,19 +101,24 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget...@@ -100,19 +101,24 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget
100 codegen_add_time_event(g, "Initialize");101 codegen_add_time_event(g, "Initialize");
101102
102 g->libc = libc;103 g->libc = libc;
103 g->zig_lib_dir = zig_lib_dir;
104 g->zig_target = target;104 g->zig_target = target;
105 g->cache_dir = cache_dir;105 g->cache_dir = cache_dir;
106106
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 if (override_std_dir == nullptr) {113 if (override_std_dir == nullptr) {
108 g->zig_std_dir = buf_alloc();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 } else {116 } else {
111 g->zig_std_dir = override_std_dir;117 g->zig_std_dir = override_std_dir;
112 }118 }
113119
114 g->zig_c_headers_dir = buf_alloc();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);
116122
117 g->build_mode = build_mode;123 g->build_mode = build_mode;
118 g->out_type = out_type;124 g->out_type = out_type;
...@@ -8147,7 +8153,7 @@ static void detect_libc(CodeGen *g) {...@@ -8147,7 +8153,7 @@ static void detect_libc(CodeGen *g) {
8147 }8153 }
8148}8154}
81498155
8150AstNode *codegen_translate_c(CodeGen *g, Buf *full_path) {8156AstNode *codegen_translate_c(CodeGen *g, Buf *full_path, bool use_userland_implementation) {
8151 Buf *src_basename = buf_alloc();8157 Buf *src_basename = buf_alloc();
8152 Buf *src_dirname = buf_alloc();8158 Buf *src_dirname = buf_alloc();
8153 os_path_split(full_path, src_dirname, src_basename);8159 os_path_split(full_path, src_dirname, src_basename);
...@@ -8159,6 +8165,12 @@ AstNode *codegen_translate_c(CodeGen *g, Buf *full_path) {...@@ -8159,6 +8165,12 @@ AstNode *codegen_translate_c(CodeGen *g, Buf *full_path) {
81598165
8160 init(g);8166 init(g);
81618167
8168 if (use_userland_implementation) {
8169 // TODO improve this
8170 stage2_translate_c();
8171 zig_panic("TODO");
8172 }
8173
8162 ZigList<ErrorMsg *> errors = {0};8174 ZigList<ErrorMsg *> errors = {0};
8163 AstNode *root_node;8175 AstNode *root_node;
8164 Error err = parse_h_file(&root_node, &errors, buf_ptr(full_path), g, nullptr);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,7 +50,7 @@ ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const c
50void codegen_add_assembly(CodeGen *g, Buf *path);50void codegen_add_assembly(CodeGen *g, Buf *path);
51void codegen_add_object(CodeGen *g, Buf *object_path);51void codegen_add_object(CodeGen *g, Buf *object_path);
5252
53AstNode *codegen_translate_c(CodeGen *g, Buf *path);53AstNode *codegen_translate_c(CodeGen *g, Buf *path, bool use_userland_implementation);
5454
55Buf *codegen_generate_builtin_source(CodeGen *g);55Buf *codegen_generate_builtin_source(CodeGen *g);
5656
src/compiler.cpp+4-4
...@@ -179,24 +179,24 @@ Buf *get_zig_lib_dir(void) {...@@ -179,24 +179,24 @@ Buf *get_zig_lib_dir(void) {
179 return &saved_lib_dir;179 return &saved_lib_dir;
180}180}
181181
182Buf *get_zig_std_dir() {182Buf *get_zig_std_dir(Buf *zig_lib_dir) {
183 if (saved_std_dir.list.length != 0) {183 if (saved_std_dir.list.length != 0) {
184 return &saved_std_dir;184 return &saved_std_dir;
185 }185 }
186 buf_resize(&saved_std_dir, 0);186 buf_resize(&saved_std_dir, 0);
187187
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);
189189
190 return &saved_std_dir;190 return &saved_std_dir;
191}191}
192192
193Buf *get_zig_special_dir() {193Buf *get_zig_special_dir(Buf *zig_lib_dir) {
194 if (saved_special_dir.list.length != 0) {194 if (saved_special_dir.list.length != 0) {
195 return &saved_special_dir;195 return &saved_special_dir;
196 }196 }
197 buf_resize(&saved_special_dir, 0);197 buf_resize(&saved_special_dir, 0);
198198
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);
200200
201 return &saved_special_dir;201 return &saved_special_dir;
202}202}
src/compiler.hpp+2-2
...@@ -16,7 +16,7 @@ Error get_compiler_id(Buf **result);...@@ -16,7 +16,7 @@ Error get_compiler_id(Buf **result);
16Buf *get_self_dynamic_linker_path(void);16Buf *get_self_dynamic_linker_path(void);
1717
18Buf *get_zig_lib_dir(void);18Buf *get_zig_lib_dir(void);
19Buf *get_zig_special_dir(void);19Buf *get_zig_special_dir(Buf *zig_lib_dir);
20Buf *get_zig_std_dir(void);20Buf *get_zig_std_dir(Buf *zig_lib_dir);
2121
22#endif22#endif
src/main.cpp+61-40
...@@ -14,6 +14,7 @@...@@ -14,6 +14,7 @@
14#include "os.hpp"14#include "os.hpp"
15#include "target.hpp"15#include "target.hpp"
16#include "libc_installation.hpp"16#include "libc_installation.hpp"
17#include "userland.h"
1718
18#include <stdio.h>19#include <stdio.h>
1920
...@@ -40,6 +41,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {...@@ -40,6 +41,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
40 " libc [paths_file] Display native libc paths file or validate one\n"41 " libc [paths_file] Display native libc paths file or validate one\n"
41 " run [source] [-- [args]] create executable and run immediately\n"42 " run [source] [-- [args]] create executable and run immediately\n"
42 " translate-c [source] convert c code to zig code\n"43 " translate-c [source] convert c code to zig code\n"
44 " translate-c-2 [source] experimental self-hosted translate-c\n"
43 " targets list available compilation targets\n"45 " targets list available compilation targets\n"
44 " test [source] create and run a test build\n"46 " test [source] create and run a test build\n"
45 " version print version number and exit\n"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,19 +133,6 @@ static int print_libc_usage(const char *arg0, FILE *file, int return_code) {
131 return return_code;133 return return_code;
132}134}
133135
134static 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
147static bool arch_available_in_llvm(ZigLLVM_ArchType arch) {136static bool arch_available_in_llvm(ZigLLVM_ArchType arch) {
148 LLVMTargetRef target_ref;137 LLVMTargetRef target_ref;
149 char *err_msg = nullptr;138 char *err_msg = nullptr;
...@@ -211,6 +200,7 @@ enum Cmd {...@@ -211,6 +200,7 @@ enum Cmd {
211 CmdTargets,200 CmdTargets,
212 CmdTest,201 CmdTest,
213 CmdTranslateC,202 CmdTranslateC,
203 CmdTranslateCUserland,
214 CmdVersion,204 CmdVersion,
215 CmdZen,205 CmdZen,
216 CmdLibC,206 CmdLibC,
...@@ -324,7 +314,7 @@ int main(int argc, char **argv) {...@@ -324,7 +314,7 @@ int main(int argc, char **argv) {
324 return print_error_usage(arg0);314 return print_error_usage(arg0);
325 }315 }
326 Buf *cmd_template_path = buf_alloc();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 Buf *build_zig_path = buf_alloc();318 Buf *build_zig_path = buf_alloc();
329 os_path_join(cmd_template_path, buf_create_from_str("build.zig"), build_zig_path);319 os_path_join(cmd_template_path, buf_create_from_str("build.zig"), build_zig_path);
330 Buf *src_dir_path = buf_alloc();320 Buf *src_dir_path = buf_alloc();
...@@ -453,6 +443,7 @@ int main(int argc, char **argv) {...@@ -453,6 +443,7 @@ int main(int argc, char **argv) {
453 bool want_single_threaded = false;443 bool want_single_threaded = false;
454 bool disable_gen_h = false;444 bool disable_gen_h = false;
455 Buf *override_std_dir = nullptr;445 Buf *override_std_dir = nullptr;
446 Buf *override_lib_dir = nullptr;
456 Buf *main_pkg_path = nullptr;447 Buf *main_pkg_path = nullptr;
457 ValgrindSupport valgrind_support = ValgrindSupportAuto;448 ValgrindSupport valgrind_support = ValgrindSupportAuto;
458 WantPIC want_pic = WantPICAuto;449 WantPIC want_pic = WantPICAuto;
...@@ -486,13 +477,27 @@ int main(int argc, char **argv) {...@@ -486,13 +477,27 @@ int main(int argc, char **argv) {
486 } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) {477 } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) {
487 cache_dir = argv[i + 1];478 cache_dir = argv[i + 1];
488 i += 1;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 } else {492 } else {
490 args.append(argv[i]);493 args.append(argv[i]);
491 }494 }
492 }495 }
493496
497 Buf *zig_lib_dir = (override_lib_dir == nullptr) ? get_zig_lib_dir() : override_lib_dir;
498
494 Buf *build_runner_path = buf_alloc();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);
496501
497 ZigTarget target;502 ZigTarget target;
498 get_native_target(&target);503 get_native_target(&target);
...@@ -512,7 +517,7 @@ int main(int argc, char **argv) {...@@ -512,7 +517,7 @@ int main(int argc, char **argv) {
512 }517 }
513518
514 CodeGen *g = codegen_create(main_pkg_path, build_runner_path, &target, OutTypeExe,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 g->valgrind_support = valgrind_support;521 g->valgrind_support = valgrind_support;
517 g->enable_time_report = timing_info;522 g->enable_time_report = timing_info;
518 codegen_set_out_name(g, buf_create_from_str("build"));523 codegen_set_out_name(g, buf_create_from_str("build"));
...@@ -532,23 +537,25 @@ int main(int argc, char **argv) {...@@ -532,23 +537,25 @@ int main(int argc, char **argv) {
532 "Usage: %s build [options]\n"537 "Usage: %s build [options]\n"
533 "\n"538 "\n"
534 "General Options:\n"539 "General Options:\n"
535 " --help Print this help and exit\n"540 " --help Print this help and exit\n"
536 " --verbose Print commands before executing them\n"541 " --verbose Print commands before executing them\n"
537 " --prefix [path] Override default install prefix\n"542 " --prefix [path] Override default install prefix\n"
538 " --search-prefix [path] Add a path to look for binaries, libraries, headers\n"543 " --search-prefix [path] Add a path to look for binaries, libraries, headers\n"
539 "\n"544 "\n"
540 "Project-specific options become available when the build file is found.\n"545 "Project-specific options become available when the build file is found.\n"
541 "\n"546 "\n"
542 "Advanced Options:\n"547 "Advanced Options:\n"
543 " --build-file [file] Override path to build.zig\n"548 " --build-file [file] Override path to build.zig\n"
544 " --cache-dir [path] Override path to cache directory\n"549 " --cache-dir [path] Override path to cache directory\n"
545 " --verbose-tokenize Enable compiler debug output for tokenization\n"550 " --override-std-dir [arg] Override path to Zig standard library\n"
546 " --verbose-ast Enable compiler debug output for parsing into an AST\n"551 " --override-lib-dir [arg] Override path to Zig lib library\n"
547 " --verbose-link Enable compiler debug output for linking\n"552 " --verbose-tokenize Enable compiler debug output for tokenization\n"
548 " --verbose-ir Enable compiler debug output for Zig IR\n"553 " --verbose-ast Enable compiler debug output for parsing into an AST\n"
549 " --verbose-llvm-ir Enable compiler debug output for LLVM IR\n"554 " --verbose-link Enable compiler debug output for linking\n"
550 " --verbose-cimport Enable compiler debug output for C imports\n"555 " --verbose-ir Enable compiler debug output for Zig IR\n"
551 " --verbose-cc Enable compiler debug output for C compilation\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 "\n"559 "\n"
553 , zig_exe_path);560 , zig_exe_path);
554 return EXIT_SUCCESS;561 return EXIT_SUCCESS;
...@@ -584,11 +591,12 @@ int main(int argc, char **argv) {...@@ -584,11 +591,12 @@ int main(int argc, char **argv) {
584 init_all_targets();591 init_all_targets();
585 ZigTarget target;592 ZigTarget target;
586 get_native_target(&target);593 get_native_target(&target);
594 Buf *zig_lib_dir = (override_lib_dir == nullptr) ? get_zig_lib_dir() : override_lib_dir;
587 Buf *fmt_runner_path = buf_alloc();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 Buf *cache_dir_buf = buf_create_from_str(cache_dir ? cache_dir : default_zig_cache_name);597 Buf *cache_dir_buf = buf_create_from_str(cache_dir ? cache_dir : default_zig_cache_name);
590 CodeGen *g = codegen_create(main_pkg_path, fmt_runner_path, &target, OutTypeExe,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 g->valgrind_support = valgrind_support;600 g->valgrind_support = valgrind_support;
593 g->want_single_threaded = true;601 g->want_single_threaded = true;
594 codegen_set_out_name(g, buf_create_from_str("fmt"));602 codegen_set_out_name(g, buf_create_from_str("fmt"));
...@@ -757,6 +765,8 @@ int main(int argc, char **argv) {...@@ -757,6 +765,8 @@ int main(int argc, char **argv) {
757 llvm_argv.append(argv[i]);765 llvm_argv.append(argv[i]);
758 } else if (strcmp(arg, "--override-std-dir") == 0) {766 } else if (strcmp(arg, "--override-std-dir") == 0) {
759 override_std_dir = buf_create_from_str(argv[i]);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 } else if (strcmp(arg, "--main-pkg-path") == 0) {770 } else if (strcmp(arg, "--main-pkg-path") == 0) {
761 main_pkg_path = buf_create_from_str(argv[i]);771 main_pkg_path = buf_create_from_str(argv[i]);
762 } else if (strcmp(arg, "--library-path") == 0 || strcmp(arg, "-L") == 0) {772 } else if (strcmp(arg, "--library-path") == 0 || strcmp(arg, "-L") == 0) {
...@@ -867,6 +877,8 @@ int main(int argc, char **argv) {...@@ -867,6 +877,8 @@ int main(int argc, char **argv) {
867 cmd = CmdLibC;877 cmd = CmdLibC;
868 } else if (strcmp(arg, "translate-c") == 0) {878 } else if (strcmp(arg, "translate-c") == 0) {
869 cmd = CmdTranslateC;879 cmd = CmdTranslateC;
880 } else if (strcmp(arg, "translate-c-2") == 0) {
881 cmd = CmdTranslateCUserland;
870 } else if (strcmp(arg, "test") == 0) {882 } else if (strcmp(arg, "test") == 0) {
871 cmd = CmdTest;883 cmd = CmdTest;
872 out_type = OutTypeExe;884 out_type = OutTypeExe;
...@@ -883,6 +895,7 @@ int main(int argc, char **argv) {...@@ -883,6 +895,7 @@ int main(int argc, char **argv) {
883 case CmdBuild:895 case CmdBuild:
884 case CmdRun:896 case CmdRun:
885 case CmdTranslateC:897 case CmdTranslateC:
898 case CmdTranslateCUserland:
886 case CmdTest:899 case CmdTest:
887 case CmdLibC:900 case CmdLibC:
888 if (!in_file) {901 if (!in_file) {
...@@ -959,7 +972,7 @@ int main(int argc, char **argv) {...@@ -959,7 +972,7 @@ int main(int argc, char **argv) {
959 }972 }
960 case CmdBuiltin: {973 case CmdBuiltin: {
961 CodeGen *g = codegen_create(main_pkg_path, nullptr, &target,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 g->valgrind_support = valgrind_support;976 g->valgrind_support = valgrind_support;
964 g->want_pic = want_pic;977 g->want_pic = want_pic;
965 g->want_single_threaded = want_single_threaded;978 g->want_single_threaded = want_single_threaded;
...@@ -973,6 +986,7 @@ int main(int argc, char **argv) {...@@ -973,6 +986,7 @@ int main(int argc, char **argv) {
973 case CmdRun:986 case CmdRun:
974 case CmdBuild:987 case CmdBuild:
975 case CmdTranslateC:988 case CmdTranslateC:
989 case CmdTranslateCUserland:
976 case CmdTest:990 case CmdTest:
977 {991 {
978 if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0 &&992 if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0 &&
...@@ -985,14 +999,16 @@ int main(int argc, char **argv) {...@@ -985,14 +999,16 @@ int main(int argc, char **argv) {
985 " * --assembly argument\n"999 " * --assembly argument\n"
986 " * --c-source argument\n");1000 " * --c-source argument\n");
987 return print_error_usage(arg0);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 fprintf(stderr, "Expected source file argument.\n");1005 fprintf(stderr, "Expected source file argument.\n");
990 return print_error_usage(arg0);1006 return print_error_usage(arg0);
991 }1007 }
9921008
993 assert(cmd != CmdBuild || out_type != OutTypeUnknown);1009 assert(cmd != CmdBuild || out_type != OutTypeUnknown);
9941010
995 bool need_name = (cmd == CmdBuild || cmd == CmdTranslateC);1011 bool need_name = (cmd == CmdBuild || cmd == CmdTranslateC || cmd == CmdTranslateCUserland);
9961012
997 if (cmd == CmdRun) {1013 if (cmd == CmdRun) {
998 out_name = "run";1014 out_name = "run";
...@@ -1026,7 +1042,8 @@ int main(int argc, char **argv) {...@@ -1026,7 +1042,8 @@ int main(int argc, char **argv) {
1026 return print_error_usage(arg0);1042 return print_error_usage(arg0);
1027 }1043 }
10281044
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;
10301047
1031 if (cmd == CmdRun && buf_out_name == nullptr) {1048 if (cmd == CmdRun && buf_out_name == nullptr) {
1032 buf_out_name = buf_create_from_str("run");1049 buf_out_name = buf_create_from_str("run");
...@@ -1050,7 +1067,7 @@ int main(int argc, char **argv) {...@@ -1050,7 +1067,7 @@ int main(int argc, char **argv) {
1050 cache_dir_buf = buf_create_from_str(cache_dir);1067 cache_dir_buf = buf_create_from_str(cache_dir);
1051 }1068 }
1052 CodeGen *g = codegen_create(main_pkg_path, zig_root_source_file, &target, out_type, build_mode,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 if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);1071 if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);
1055 g->valgrind_support = valgrind_support;1072 g->valgrind_support = valgrind_support;
1056 g->want_pic = want_pic;1073 g->want_pic = want_pic;
...@@ -1170,8 +1187,8 @@ int main(int argc, char **argv) {...@@ -1170,8 +1187,8 @@ int main(int argc, char **argv) {
1170 } else {1187 } else {
1171 zig_unreachable();1188 zig_unreachable();
1172 }1189 }
1173 } else if (cmd == CmdTranslateC) {1190 } else if (cmd == CmdTranslateC || cmd == CmdTranslateCUserland) {
1174 AstNode *root_node = codegen_translate_c(g, in_file_buf);1191 AstNode *root_node = codegen_translate_c(g, in_file_buf, cmd == CmdTranslateCUserland);
1175 ast_render(g, stdout, root_node, 4);1192 ast_render(g, stdout, root_node, 4);
1176 if (timing_info)1193 if (timing_info)
1177 codegen_print_timing_report(g, stderr);1194 codegen_print_timing_report(g, stderr);
...@@ -1229,9 +1246,13 @@ int main(int argc, char **argv) {...@@ -1229,9 +1246,13 @@ int main(int argc, char **argv) {
1229 case CmdVersion:1246 case CmdVersion:
1230 printf("%s\n", ZIG_VERSION_STRING);1247 printf("%s\n", ZIG_VERSION_STRING);
1231 return EXIT_SUCCESS;1248 return EXIT_SUCCESS;
1232 case CmdZen:1249 case CmdZen: {
1233 printf("%s\n", ZIG_ZEN);1250 const char *ptr;
1251 size_t len;
1252 stage2_zen(&ptr, &len);
1253 fwrite(ptr, len, 1, stdout);
1234 return EXIT_SUCCESS;1254 return EXIT_SUCCESS;
1255 }
1235 case CmdTargets:1256 case CmdTargets:
1236 return print_target_list(stdout);1257 return print_target_list(stdout);
1237 case CmdNone: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
6void stage2_translate_c(void) {}
7void 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
19ZIG_USERLAND_EXTERN_C void stage2_translate_c(void);
20
21ZIG_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,6 +50,8 @@ pub const Builder = struct {
50 build_root: []const u8,50 build_root: []const u8,
51 cache_root: []const u8,51 cache_root: []const u8,
52 release_mode: ?builtin.Mode,52 release_mode: ?builtin.Mode,
53 override_std_dir: ?[]const u8,
54 override_lib_dir: ?[]const u8,
5355
54 pub const CStd = enum {56 pub const CStd = enum {
55 C89,57 C89,
...@@ -133,6 +135,8 @@ pub const Builder = struct {...@@ -133,6 +135,8 @@ pub const Builder = struct {
133 },135 },
134 .have_install_step = false,136 .have_install_step = false,
135 .release_mode = null,137 .release_mode = null,
138 .override_std_dir = null,
139 .override_lib_dir = null,
136 };140 };
137 self.detectNativeSystemPaths();141 self.detectNativeSystemPaths();
138 self.default_step = self.step("default", "Build the project");142 self.default_step = self.step("default", "Build the project");
...@@ -939,6 +943,7 @@ pub const LibExeObjStep = struct {...@@ -939,6 +943,7 @@ pub const LibExeObjStep = struct {
939 disable_gen_h: bool,943 disable_gen_h: bool,
940 c_std: Builder.CStd,944 c_std: Builder.CStd,
941 override_std_dir: ?[]const u8,945 override_std_dir: ?[]const u8,
946 override_lib_dir: ?[]const u8,
942 main_pkg_path: ?[]const u8,947 main_pkg_path: ?[]const u8,
943 exec_cmd_args: ?[]const ?[]const u8,948 exec_cmd_args: ?[]const ?[]const u8,
944 name_prefix: []const u8,949 name_prefix: []const u8,
...@@ -1039,6 +1044,7 @@ pub const LibExeObjStep = struct {...@@ -1039,6 +1044,7 @@ pub const LibExeObjStep = struct {
1039 .c_std = Builder.CStd.C99,1044 .c_std = Builder.CStd.C99,
1040 .system_linker_hack = false,1045 .system_linker_hack = false,
1041 .override_std_dir = null,1046 .override_std_dir = null,
1047 .override_lib_dir = null,
1042 .main_pkg_path = null,1048 .main_pkg_path = null,
1043 .exec_cmd_args = null,1049 .exec_cmd_args = null,
1044 .name_prefix = "",1050 .name_prefix = "",
...@@ -1528,6 +1534,17 @@ pub const LibExeObjStep = struct {...@@ -1528,6 +1534,17 @@ pub const LibExeObjStep = struct {
1528 if (self.override_std_dir) |dir| {1534 if (self.override_std_dir) |dir| {
1529 try zig_args.append("--override-std-dir");1535 try zig_args.append("--override-std-dir");
1530 try zig_args.append(builder.pathFromRoot(dir));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 }
15321549
1533 if (self.main_pkg_path) |dir| {1550 if (self.main_pkg_path) |dir| {
std/special/build_runner.zig+21-9
...@@ -94,6 +94,16 @@ pub fn main() !void {...@@ -94,6 +94,16 @@ pub fn main() !void {
94 return usageAndErr(&builder, false, try stderr_stream);94 return usageAndErr(&builder, false, try stderr_stream);
95 });95 });
96 builder.addSearchPrefix(search_prefix);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 } else if (mem.eql(u8, arg, "--verbose-tokenize")) {107 } else if (mem.eql(u8, arg, "--verbose-tokenize")) {
98 builder.verbose_tokenize = true;108 builder.verbose_tokenize = true;
99 } else if (mem.eql(u8, arg, "--verbose-ast")) {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,15 +197,17 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: var) !void {
187 try out_stream.write(197 try out_stream.write(
188 \\198 \\
189 \\Advanced Options:199 \\Advanced Options:
190 \\ --build-file [file] Override path to build.zig200 \\ --build-file [file] Override path to build.zig
191 \\ --cache-dir [path] Override path to zig cache directory201 \\ --cache-dir [path] Override path to zig cache directory
192 \\ --verbose-tokenize Enable compiler debug output for tokenization202 \\ --override-std-dir [arg] Override path to Zig standard library
193 \\ --verbose-ast Enable compiler debug output for parsing into an AST203 \\ --override-lib-dir [arg] Override path to Zig lib directory
194 \\ --verbose-link Enable compiler debug output for linking204 \\ --verbose-tokenize Enable compiler debug output for tokenization
195 \\ --verbose-ir Enable compiler debug output for Zig IR205 \\ --verbose-ast Enable compiler debug output for parsing into an AST
196 \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR206 \\ --verbose-link Enable compiler debug output for linking
197 \\ --verbose-cimport Enable compiler debug output for C imports207 \\ --verbose-ir Enable compiler debug output for Zig IR
198 \\ --verbose-cc Enable compiler debug output for C compilation208 \\ --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}