authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-04 21:33:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-07 17:27:09-07:00
log5a65caa2a3686e12e4dfcfd599ed02f8a09c6017
tree7c5a644b61bdecdb1704e77724fbff74043ae4fe
parent5f7b97e84c7e2bc7682510e97996ad30147026d0

ability to build stage1 using only a zig tarball

The main idea here is that there are now 2 ways to get a stage1 zig binary: * The cmake path. Requirements: cmake, system C++ compiler, system LLVM, LLD, Clang libraries, compiled by the system C++ compiler. * The zig path. Requirements: a zig installation, system LLVM, LLD, Clang libraries, compiled by the zig installation. Note that the former can be used to now take the latter path. Removed config.h.in and config.zig.in. The build.zig script no longer is coupled to the cmake script. cmake no longer tries to determine the zig version. A build with cmake will yield a stage1 zig binary that reports 0.0.0+zig0. This is going to get reverted. `zig build` now accepts `-Dstage1` which will build the stage1 compiler, and put the stage2 backend behind a feature flag. build.zig is simplified to only support the use case of enabling LLVM support when the LLVM, LLD, and Clang libraries were built by zig. This part is probably sadly going to have to get reverted to make package maintainers happy. Zig build system addBuildOption supports a couple new types. The biggest reason to make this change is that the zig path is an attractive option for doing compiler development work on Windows. It allows people to work on the compiler without having MSVC installed, using only a .zip file that contains Zig + LLVM/LLD/Clang libraries.

19 files changed, 539 insertions(+), 429 deletions(-)

CMakeLists.txt+8-45
...@@ -24,34 +24,6 @@ set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX...@@ -24,34 +24,6 @@ set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX
24project(zig C CXX)24project(zig C CXX)
25set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})25set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
2626
27set(ZIG_VERSION_MAJOR 0)
28set(ZIG_VERSION_MINOR 7)
29set(ZIG_VERSION_PATCH 0)
30set(ZIG_VERSION "" CACHE STRING "Override Zig version string. Default is to find out with git.")
31
32if("${ZIG_VERSION}" STREQUAL "")
33 set(ZIG_VERSION "${ZIG_VERSION_MAJOR}.${ZIG_VERSION_MINOR}.${ZIG_VERSION_PATCH}")
34 find_program(GIT_EXE NAMES git)
35 if(GIT_EXE)
36 execute_process(
37 COMMAND ${GIT_EXE} -C ${CMAKE_SOURCE_DIR} name-rev HEAD --tags --name-only --no-undefined --always
38 RESULT_VARIABLE EXIT_STATUS
39 OUTPUT_VARIABLE ZIG_GIT_REV
40 OUTPUT_STRIP_TRAILING_WHITESPACE
41 ERROR_QUIET)
42 if(EXIT_STATUS EQUAL "0")
43 if(ZIG_GIT_REV MATCHES "\\^0$")
44 if(NOT("${ZIG_GIT_REV}" STREQUAL "${ZIG_VERSION}^0"))
45 message("WARNING: Tag does not match configured Zig version")
46 endif()
47 else()
48 set(ZIG_VERSION "${ZIG_VERSION}+${ZIG_GIT_REV}")
49 endif()
50 endif()
51 endif()
52endif()
53message("Configuring zig version ${ZIG_VERSION}")
54
55set(ZIG_STATIC off CACHE BOOL "Attempt to build a static zig executable (not compatible with glibc)")27set(ZIG_STATIC off CACHE BOOL "Attempt to build a static zig executable (not compatible with glibc)")
56set(ZIG_STATIC_LLVM off CACHE BOOL "Prefer linking against static LLVM libraries")28set(ZIG_STATIC_LLVM off CACHE BOOL "Prefer linking against static LLVM libraries")
57set(ZIG_PREFER_CLANG_CPP_DYLIB off CACHE BOOL "Try to link against -lclang-cpp")29set(ZIG_PREFER_CLANG_CPP_DYLIB off CACHE BOOL "Try to link against -lclang-cpp")
...@@ -63,9 +35,6 @@ endif()...@@ -63,9 +35,6 @@ endif()
6335
64if(ZIG_STATIC)36if(ZIG_STATIC)
65 set(ZIG_STATIC_LLVM "on")37 set(ZIG_STATIC_LLVM "on")
66 set(ZIG_LINK_MODE "Static")
67else()
68 set(ZIG_LINK_MODE "Dynamic")
69endif()38endif()
7039
71string(REGEX REPLACE "\\\\" "\\\\\\\\" ZIG_LIBC_LIB_DIR_ESCAPED "${ZIG_LIBC_LIB_DIR}")40string(REGEX REPLACE "\\\\" "\\\\\\\\" ZIG_LIBC_LIB_DIR_ESCAPED "${ZIG_LIBC_LIB_DIR}")
...@@ -264,8 +233,6 @@ set(LIBC_FILES_DEST "${ZIG_LIB_DIR}/libc")...@@ -264,8 +233,6 @@ set(LIBC_FILES_DEST "${ZIG_LIB_DIR}/libc")
264set(LIBUNWIND_FILES_DEST "${ZIG_LIB_DIR}/libunwind")233set(LIBUNWIND_FILES_DEST "${ZIG_LIB_DIR}/libunwind")
265set(LIBCXX_FILES_DEST "${ZIG_LIB_DIR}/libcxx")234set(LIBCXX_FILES_DEST "${ZIG_LIB_DIR}/libcxx")
266set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")235set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
267set(ZIG_CONFIG_H_OUT "${CMAKE_BINARY_DIR}/config.h")
268set(ZIG_CONFIG_ZIG_OUT "${CMAKE_BINARY_DIR}/config.zig")
269236
270# This is our shim which will be replaced by stage1.zig.237# This is our shim which will be replaced by stage1.zig.
271set(ZIG0_SOURCES238set(ZIG0_SOURCES
...@@ -313,7 +280,6 @@ set(ZIG_CPP_SOURCES...@@ -313,7 +280,6 @@ set(ZIG_CPP_SOURCES
313# then manually running the build-obj command (see BUILD_ZIG1_ARGS), and then looking280# then manually running the build-obj command (see BUILD_ZIG1_ARGS), and then looking
314# in the zig-cache directory for the compiler-generated list of zig file dependencies.281# in the zig-cache directory for the compiler-generated list of zig file dependencies.
315set(ZIG_STAGE2_SOURCES282set(ZIG_STAGE2_SOURCES
316 "${ZIG_CONFIG_ZIG_OUT}"
317 "${CMAKE_SOURCE_DIR}/lib/std/array_hash_map.zig"283 "${CMAKE_SOURCE_DIR}/lib/std/array_hash_map.zig"
318 "${CMAKE_SOURCE_DIR}/lib/std/array_list.zig"284 "${CMAKE_SOURCE_DIR}/lib/std/array_list.zig"
319 "${CMAKE_SOURCE_DIR}/lib/std/ascii.zig"285 "${CMAKE_SOURCE_DIR}/lib/std/ascii.zig"
...@@ -553,6 +519,7 @@ set(ZIG_STAGE2_SOURCES...@@ -553,6 +519,7 @@ set(ZIG_STAGE2_SOURCES
553 "${CMAKE_SOURCE_DIR}/src/print_env.zig"519 "${CMAKE_SOURCE_DIR}/src/print_env.zig"
554 "${CMAKE_SOURCE_DIR}/src/print_targets.zig"520 "${CMAKE_SOURCE_DIR}/src/print_targets.zig"
555 "${CMAKE_SOURCE_DIR}/src/stage1.zig"521 "${CMAKE_SOURCE_DIR}/src/stage1.zig"
522 "${CMAKE_SOURCE_DIR}/src/stage1_config.zig"
556 "${CMAKE_SOURCE_DIR}/src/target.zig"523 "${CMAKE_SOURCE_DIR}/src/target.zig"
557 "${CMAKE_SOURCE_DIR}/src/tracy.zig"524 "${CMAKE_SOURCE_DIR}/src/tracy.zig"
558 "${CMAKE_SOURCE_DIR}/src/translate_c.zig"525 "${CMAKE_SOURCE_DIR}/src/translate_c.zig"
...@@ -571,15 +538,6 @@ if(MSVC)...@@ -571,15 +538,6 @@ if(MSVC)
571 endif()538 endif()
572endif()539endif()
573540
574configure_file (
575 "${CMAKE_SOURCE_DIR}/src/stage1/config.h.in"
576 "${ZIG_CONFIG_H_OUT}"
577)
578configure_file (
579 "${CMAKE_SOURCE_DIR}/src/config.zig.in"
580 "${ZIG_CONFIG_ZIG_OUT}"
581)
582
583include_directories(541include_directories(
584 ${CMAKE_SOURCE_DIR}542 ${CMAKE_SOURCE_DIR}
585 ${CMAKE_BINARY_DIR}543 ${CMAKE_BINARY_DIR}
...@@ -594,6 +552,12 @@ else(MSVC)...@@ -594,6 +552,12 @@ else(MSVC)
594 set(EXE_CFLAGS "-std=c++14")552 set(EXE_CFLAGS "-std=c++14")
595endif(MSVC)553endif(MSVC)
596554
555if(ZIG_STATIC)
556 set(EXE_CFLAGS "${EXE_CFLAGS} -DZIG_LINK_MODE=Static")
557else()
558 set(EXE_CFLAGS "${EXE_CFLAGS} -DZIG_LINK_MODE=Dynamic")
559endif()
560
597if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")561if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
598 if(MSVC)562 if(MSVC)
599 set(EXE_CFLAGS "${EXE_CFLAGS} /w")563 set(EXE_CFLAGS "${EXE_CFLAGS} /w")
...@@ -720,7 +684,7 @@ set(BUILD_ZIG1_ARGS...@@ -720,7 +684,7 @@ set(BUILD_ZIG1_ARGS
720 "-femit-bin=${ZIG1_OBJECT}"684 "-femit-bin=${ZIG1_OBJECT}"
721 "${ZIG1_RELEASE_ARG}"685 "${ZIG1_RELEASE_ARG}"
722 -lc686 -lc
723 --pkg-begin build_options "${ZIG_CONFIG_ZIG_OUT}"687 --pkg-begin build_options "${CMAKE_SOURCE_DIR}/src/stage1_config.zig"
724 --pkg-end688 --pkg-end
725 --pkg-begin compiler_rt "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt.zig"689 --pkg-begin compiler_rt "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt.zig"
726 --pkg-end690 --pkg-end
...@@ -769,7 +733,6 @@ set(ZIG_INSTALL_ARGS "build"...@@ -769,7 +733,6 @@ set(ZIG_INSTALL_ARGS "build"
769 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"733 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
770 "-Dlib-files-only"734 "-Dlib-files-only"
771 --prefix "${CMAKE_INSTALL_PREFIX}"735 --prefix "${CMAKE_INSTALL_PREFIX}"
772 "-Dconfig_h=${ZIG_CONFIG_H_OUT}"
773 install736 install
774)737)
775738
build.zig+403-329
...@@ -53,7 +53,8 @@ pub fn build(b: *Builder) !void {...@@ -53,7 +53,8 @@ pub fn build(b: *Builder) !void {
53 const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false;53 const skip_compile_errors = b.option(bool, "skip-compile-errors", "Main test suite skips compile error tests") orelse false;
5454
55 const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;55 const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;
56 const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse false;56 const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false;
57 const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse is_stage1;
57 const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");58 const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");
5859
59 b.installDirectory(InstallDirectoryOptions{60 b.installDirectory(InstallDirectoryOptions{
...@@ -76,6 +77,8 @@ pub fn build(b: *Builder) !void {...@@ -76,6 +77,8 @@ pub fn build(b: *Builder) !void {
76 const tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source");77 const tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source");
77 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm;78 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm;
7879
80 const main_file = if (is_stage1) "src/stage1.zig" else "src/main.zig";
81
79 var exe = b.addExecutable("zig", "src/main.zig");82 var exe = b.addExecutable("zig", "src/main.zig");
80 exe.install();83 exe.install();
81 exe.setBuildMode(mode);84 exe.setBuildMode(mode);
...@@ -86,15 +89,51 @@ pub fn build(b: *Builder) !void {...@@ -86,15 +89,51 @@ pub fn build(b: *Builder) !void {
86 exe.addBuildOption(bool, "skip_non_native", skip_non_native);89 exe.addBuildOption(bool, "skip_non_native", skip_non_native);
87 exe.addBuildOption(bool, "have_llvm", enable_llvm);90 exe.addBuildOption(bool, "have_llvm", enable_llvm);
88 if (enable_llvm) {91 if (enable_llvm) {
89 const config_h_text = if (config_h_path_option) |config_h_path|92 if (is_stage1) {
90 try std.fs.cwd().readFileAlloc(b.allocator, toNativePathSep(b, config_h_path), max_config_h_bytes)93 exe.addIncludeDir("src");
91 else94 exe.addIncludeDir("deps/SoftFloat-3e/source/include");
92 try findAndReadConfigH(b);95
96 const softfloat = b.addStaticLibrary("softfloat", null);
97 softfloat.setBuildMode(.ReleaseFast);
98 softfloat.setTarget(target);
99 softfloat.addIncludeDir("deps/SoftFloat-3e-prebuilt");
100 softfloat.addIncludeDir("deps/SoftFloat-3e/source/8086");
101 softfloat.addIncludeDir("deps/SoftFloat-3e/source/include");
102 softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" });
103 exe.linkLibrary(softfloat);
104
105 const exe_cflags = &[_][]const u8{
106 "-std=c++14",
107 "-D__STDC_CONSTANT_MACROS",
108 "-D__STDC_FORMAT_MACROS",
109 "-D__STDC_LIMIT_MACROS",
110 "-D_GNU_SOURCE",
111 "-fvisibility-inlines-hidden",
112 "-fno-exceptions",
113 "-fno-rtti",
114 "-Werror=type-limits",
115 "-Wno-missing-braces",
116 "-Wno-comment",
117 };
118 exe.addCSourceFiles(&stage1_sources, exe_cflags);
119 exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" });
120 exe.addCSourceFiles(&zig_cpp_sources, exe_cflags);
121 }
93122
94 var ctx = parseConfigH(b, config_h_text);123 for (clang_libs) |lib_name| {
95 ctx.llvm = try findLLVM(b, ctx.llvm_config_exe);124 exe.linkSystemLibrary(lib_name);
125 }
126
127 for (lld_libs) |lib_name| {
128 exe.linkSystemLibrary(lib_name);
129 }
96130
97 try configureStage2(b, exe, ctx, tracy != null);131 for (llvm_libs) |lib_name| {
132 exe.linkSystemLibrary(lib_name);
133 }
134
135 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
136 exe.linkSystemLibrary("c++");
98 }137 }
99 if (link_libc) {138 if (link_libc) {
100 exe.linkLibC();139 exe.linkLibC();
...@@ -141,7 +180,10 @@ pub fn build(b: *Builder) !void {...@@ -141,7 +180,10 @@ pub fn build(b: *Builder) !void {
141 break :v b.fmt("{}+{}{}", .{ version_string, git_sha_trimmed, dirty_suffix });180 break :v b.fmt("{}+{}{}", .{ version_string, git_sha_trimmed, dirty_suffix });
142 }181 }
143 };182 };
144 exe.addBuildOption([]const u8, "version", version);183 exe.addBuildOption([:0]const u8, "version", try b.allocator.dupeZ(u8, version));
184
185 const semver = try std.SemanticVersion.parse(version);
186 exe.addBuildOption(std.SemanticVersion, "semver", semver);
145187
146 exe.addBuildOption([]const []const u8, "log_scopes", log_scopes);188 exe.addBuildOption([]const []const u8, "log_scopes", log_scopes);
147 exe.addBuildOption([]const []const u8, "zir_dumps", zir_dumps);189 exe.addBuildOption([]const []const u8, "zir_dumps", zir_dumps);
...@@ -226,327 +268,359 @@ pub fn build(b: *Builder) !void {...@@ -226,327 +268,359 @@ pub fn build(b: *Builder) !void {
226 test_step.dependOn(docs_step);268 test_step.dependOn(docs_step);
227}269}
228270
229fn dependOnLib(b: *Builder, lib_exe_obj: anytype, dep: LibraryDep) void {271const softfloat_sources = [_][]const u8{
230 for (dep.libdirs.items) |lib_dir| {272 "deps/SoftFloat-3e/source/8086/f128M_isSignalingNaN.c",
231 lib_exe_obj.addLibPath(lib_dir);273 "deps/SoftFloat-3e/source/8086/s_commonNaNToF128M.c",
232 }274 "deps/SoftFloat-3e/source/8086/s_commonNaNToF16UI.c",
233 const lib_dir = fs.path.join(275 "deps/SoftFloat-3e/source/8086/s_commonNaNToF32UI.c",
234 b.allocator,276 "deps/SoftFloat-3e/source/8086/s_commonNaNToF64UI.c",
235 &[_][]const u8{ dep.prefix, "lib" },277 "deps/SoftFloat-3e/source/8086/s_f128MToCommonNaN.c",
236 ) catch unreachable;278 "deps/SoftFloat-3e/source/8086/s_f16UIToCommonNaN.c",
237 for (dep.system_libs.items) |lib| {279 "deps/SoftFloat-3e/source/8086/s_f32UIToCommonNaN.c",
238 const static_bare_name = if (mem.eql(u8, lib, "curses"))280 "deps/SoftFloat-3e/source/8086/s_f64UIToCommonNaN.c",
239 @as([]const u8, "libncurses.a")281 "deps/SoftFloat-3e/source/8086/s_propagateNaNF128M.c",
240 else282 "deps/SoftFloat-3e/source/8086/s_propagateNaNF16UI.c",
241 b.fmt("lib{}.a", .{lib});283 "deps/SoftFloat-3e/source/8086/softfloat_raiseFlags.c",
242 const static_lib_name = fs.path.join(284 "deps/SoftFloat-3e/source/f128M_add.c",
243 b.allocator,285 "deps/SoftFloat-3e/source/f128M_div.c",
244 &[_][]const u8{ lib_dir, static_bare_name },286 "deps/SoftFloat-3e/source/f128M_eq.c",
245 ) catch unreachable;287 "deps/SoftFloat-3e/source/f128M_eq_signaling.c",
246 const have_static = fileExists(static_lib_name) catch unreachable;288 "deps/SoftFloat-3e/source/f128M_le.c",
247 if (have_static) {289 "deps/SoftFloat-3e/source/f128M_le_quiet.c",
248 lib_exe_obj.addObjectFile(static_lib_name);290 "deps/SoftFloat-3e/source/f128M_lt.c",
249 } else {291 "deps/SoftFloat-3e/source/f128M_lt_quiet.c",
250 lib_exe_obj.linkSystemLibrary(lib);292 "deps/SoftFloat-3e/source/f128M_mul.c",
251 }293 "deps/SoftFloat-3e/source/f128M_mulAdd.c",
252 }294 "deps/SoftFloat-3e/source/f128M_rem.c",
253 for (dep.libs.items) |lib| {295 "deps/SoftFloat-3e/source/f128M_roundToInt.c",
254 lib_exe_obj.addObjectFile(lib);296 "deps/SoftFloat-3e/source/f128M_sqrt.c",
255 }297 "deps/SoftFloat-3e/source/f128M_sub.c",
256 for (dep.includes.items) |include_path| {298 "deps/SoftFloat-3e/source/f128M_to_f16.c",
257 lib_exe_obj.addIncludeDir(include_path);299 "deps/SoftFloat-3e/source/f128M_to_f32.c",
258 }300 "deps/SoftFloat-3e/source/f128M_to_f64.c",
259}301 "deps/SoftFloat-3e/source/f128M_to_i32.c",
260302 "deps/SoftFloat-3e/source/f128M_to_i32_r_minMag.c",
261fn fileExists(filename: []const u8) !bool {303 "deps/SoftFloat-3e/source/f128M_to_i64.c",
262 fs.cwd().access(filename, .{}) catch |err| switch (err) {304 "deps/SoftFloat-3e/source/f128M_to_i64_r_minMag.c",
263 error.FileNotFound => return false,305 "deps/SoftFloat-3e/source/f128M_to_ui32.c",
264 else => return err,306 "deps/SoftFloat-3e/source/f128M_to_ui32_r_minMag.c",
265 };307 "deps/SoftFloat-3e/source/f128M_to_ui64.c",
266 return true;308 "deps/SoftFloat-3e/source/f128M_to_ui64_r_minMag.c",
267}309 "deps/SoftFloat-3e/source/f16_add.c",
268310 "deps/SoftFloat-3e/source/f16_div.c",
269fn addCppLib(b: *Builder, lib_exe_obj: anytype, cmake_binary_dir: []const u8, lib_name: []const u8) void {311 "deps/SoftFloat-3e/source/f16_eq.c",
270 lib_exe_obj.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{312 "deps/SoftFloat-3e/source/f16_isSignalingNaN.c",
271 cmake_binary_dir,313 "deps/SoftFloat-3e/source/f16_lt.c",
272 "zigcpp",314 "deps/SoftFloat-3e/source/f16_mul.c",
273 b.fmt("{}{}{}", .{ lib_exe_obj.target.libPrefix(), lib_name, lib_exe_obj.target.staticLibSuffix() }),315 "deps/SoftFloat-3e/source/f16_mulAdd.c",
274 }) catch unreachable);316 "deps/SoftFloat-3e/source/f16_rem.c",
275}317 "deps/SoftFloat-3e/source/f16_roundToInt.c",
276318 "deps/SoftFloat-3e/source/f16_sqrt.c",
277const LibraryDep = struct {319 "deps/SoftFloat-3e/source/f16_sub.c",
278 prefix: []const u8,320 "deps/SoftFloat-3e/source/f16_to_f128M.c",
279 libdirs: ArrayList([]const u8),321 "deps/SoftFloat-3e/source/f16_to_f64.c",
280 libs: ArrayList([]const u8),322 "deps/SoftFloat-3e/source/f32_to_f128M.c",
281 system_libs: ArrayList([]const u8),323 "deps/SoftFloat-3e/source/f64_to_f128M.c",
282 includes: ArrayList([]const u8),324 "deps/SoftFloat-3e/source/f64_to_f16.c",
325 "deps/SoftFloat-3e/source/i32_to_f128M.c",
326 "deps/SoftFloat-3e/source/s_add256M.c",
327 "deps/SoftFloat-3e/source/s_addCarryM.c",
328 "deps/SoftFloat-3e/source/s_addComplCarryM.c",
329 "deps/SoftFloat-3e/source/s_addF128M.c",
330 "deps/SoftFloat-3e/source/s_addM.c",
331 "deps/SoftFloat-3e/source/s_addMagsF16.c",
332 "deps/SoftFloat-3e/source/s_addMagsF32.c",
333 "deps/SoftFloat-3e/source/s_addMagsF64.c",
334 "deps/SoftFloat-3e/source/s_approxRecip32_1.c",
335 "deps/SoftFloat-3e/source/s_approxRecipSqrt32_1.c",
336 "deps/SoftFloat-3e/source/s_approxRecipSqrt_1Ks.c",
337 "deps/SoftFloat-3e/source/s_approxRecip_1Ks.c",
338 "deps/SoftFloat-3e/source/s_compare128M.c",
339 "deps/SoftFloat-3e/source/s_compare96M.c",
340 "deps/SoftFloat-3e/source/s_countLeadingZeros16.c",
341 "deps/SoftFloat-3e/source/s_countLeadingZeros32.c",
342 "deps/SoftFloat-3e/source/s_countLeadingZeros64.c",
343 "deps/SoftFloat-3e/source/s_countLeadingZeros8.c",
344 "deps/SoftFloat-3e/source/s_eq128.c",
345 "deps/SoftFloat-3e/source/s_invalidF128M.c",
346 "deps/SoftFloat-3e/source/s_isNaNF128M.c",
347 "deps/SoftFloat-3e/source/s_le128.c",
348 "deps/SoftFloat-3e/source/s_lt128.c",
349 "deps/SoftFloat-3e/source/s_mul128MTo256M.c",
350 "deps/SoftFloat-3e/source/s_mul64To128M.c",
351 "deps/SoftFloat-3e/source/s_mulAddF128M.c",
352 "deps/SoftFloat-3e/source/s_mulAddF16.c",
353 "deps/SoftFloat-3e/source/s_mulAddF32.c",
354 "deps/SoftFloat-3e/source/s_mulAddF64.c",
355 "deps/SoftFloat-3e/source/s_negXM.c",
356 "deps/SoftFloat-3e/source/s_normRoundPackMToF128M.c",
357 "deps/SoftFloat-3e/source/s_normRoundPackToF16.c",
358 "deps/SoftFloat-3e/source/s_normRoundPackToF32.c",
359 "deps/SoftFloat-3e/source/s_normRoundPackToF64.c",
360 "deps/SoftFloat-3e/source/s_normSubnormalF128SigM.c",
361 "deps/SoftFloat-3e/source/s_normSubnormalF16Sig.c",
362 "deps/SoftFloat-3e/source/s_normSubnormalF32Sig.c",
363 "deps/SoftFloat-3e/source/s_normSubnormalF64Sig.c",
364 "deps/SoftFloat-3e/source/s_remStepMBy32.c",
365 "deps/SoftFloat-3e/source/s_roundMToI64.c",
366 "deps/SoftFloat-3e/source/s_roundMToUI64.c",
367 "deps/SoftFloat-3e/source/s_roundPackMToF128M.c",
368 "deps/SoftFloat-3e/source/s_roundPackToF16.c",
369 "deps/SoftFloat-3e/source/s_roundPackToF32.c",
370 "deps/SoftFloat-3e/source/s_roundPackToF64.c",
371 "deps/SoftFloat-3e/source/s_roundToI32.c",
372 "deps/SoftFloat-3e/source/s_roundToI64.c",
373 "deps/SoftFloat-3e/source/s_roundToUI32.c",
374 "deps/SoftFloat-3e/source/s_roundToUI64.c",
375 "deps/SoftFloat-3e/source/s_shiftLeftM.c",
376 "deps/SoftFloat-3e/source/s_shiftNormSigF128M.c",
377 "deps/SoftFloat-3e/source/s_shiftRightJam256M.c",
378 "deps/SoftFloat-3e/source/s_shiftRightJam32.c",
379 "deps/SoftFloat-3e/source/s_shiftRightJam64.c",
380 "deps/SoftFloat-3e/source/s_shiftRightJamM.c",
381 "deps/SoftFloat-3e/source/s_shiftRightM.c",
382 "deps/SoftFloat-3e/source/s_shortShiftLeft64To96M.c",
383 "deps/SoftFloat-3e/source/s_shortShiftLeftM.c",
384 "deps/SoftFloat-3e/source/s_shortShiftRightExtendM.c",
385 "deps/SoftFloat-3e/source/s_shortShiftRightJam64.c",
386 "deps/SoftFloat-3e/source/s_shortShiftRightJamM.c",
387 "deps/SoftFloat-3e/source/s_shortShiftRightM.c",
388 "deps/SoftFloat-3e/source/s_sub1XM.c",
389 "deps/SoftFloat-3e/source/s_sub256M.c",
390 "deps/SoftFloat-3e/source/s_subM.c",
391 "deps/SoftFloat-3e/source/s_subMagsF16.c",
392 "deps/SoftFloat-3e/source/s_subMagsF32.c",
393 "deps/SoftFloat-3e/source/s_subMagsF64.c",
394 "deps/SoftFloat-3e/source/s_tryPropagateNaNF128M.c",
395 "deps/SoftFloat-3e/source/softfloat_state.c",
396 "deps/SoftFloat-3e/source/ui32_to_f128M.c",
397 "deps/SoftFloat-3e/source/ui64_to_f128M.c",
283};398};
284399
285fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep {400const stage1_sources = [_][]const u8{
286 const shared_mode = try b.exec(&[_][]const u8{ llvm_config_exe, "--shared-mode" });401 "src/stage1/analyze.cpp",
287 const is_static = mem.startsWith(u8, shared_mode, "static");402 "src/stage1/ast_render.cpp",
288 const libs_output = if (is_static)403 "src/stage1/bigfloat.cpp",
289 try b.exec(&[_][]const u8{404 "src/stage1/bigint.cpp",
290 llvm_config_exe,405 "src/stage1/buffer.cpp",
291 "--libfiles",406 "src/stage1/codegen.cpp",
292 "--system-libs",407 "src/stage1/dump_analysis.cpp",
293 })408 "src/stage1/errmsg.cpp",
294 else409 "src/stage1/error.cpp",
295 try b.exec(&[_][]const u8{410 "src/stage1/heap.cpp",
296 llvm_config_exe,411 "src/stage1/ir.cpp",
297 "--libs",412 "src/stage1/ir_print.cpp",
298 });413 "src/stage1/mem.cpp",
299 const includes_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--includedir" });414 "src/stage1/os.cpp",
300 const libdir_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--libdir" });415 "src/stage1/parser.cpp",
301 const prefix_output = try b.exec(&[_][]const u8{ llvm_config_exe, "--prefix" });416 "src/stage1/range_set.cpp",
302417 "src/stage1/stage1.cpp",
303 var result = LibraryDep{418 "src/stage1/target.cpp",
304 .prefix = mem.tokenize(prefix_output, " \r\n").next().?,419 "src/stage1/tokenizer.cpp",
305 .libs = ArrayList([]const u8).init(b.allocator),420 "src/stage1/util.cpp",
306 .system_libs = ArrayList([]const u8).init(b.allocator),421 "src/stage1/softfloat_ext.cpp",
307 .includes = ArrayList([]const u8).init(b.allocator),422};
308 .libdirs = ArrayList([]const u8).init(b.allocator),423const optimized_c_sources = [_][]const u8{
309 };424 "src/stage1/parse_f128.c",
310 {425};
311 var it = mem.tokenize(libs_output, " \r\n");426const zig_cpp_sources = [_][]const u8{
312 while (it.next()) |lib_arg| {427 // These are planned to stay even when we are self-hosted.
313 if (mem.startsWith(u8, lib_arg, "-l")) {428 "src/zig_llvm.cpp",
314 try result.system_libs.append(lib_arg[2..]);429 "src/zig_clang.cpp",
315 } else {430 "src/zig_clang_driver.cpp",
316 if (fs.path.isAbsolute(lib_arg)) {431 "src/zig_clang_cc1_main.cpp",
317 try result.libs.append(lib_arg);432 "src/zig_clang_cc1as_main.cpp",
318 } else {433 // https://github.com/ziglang/zig/issues/6363
319 var lib_arg_copy = lib_arg;434 "src/windows_sdk.cpp",
320 if (mem.endsWith(u8, lib_arg, ".lib")) {
321 lib_arg_copy = lib_arg[0 .. lib_arg.len - 4];
322 }
323 try result.system_libs.append(lib_arg_copy);
324 }
325 }
326 }
327 }
328 {
329 var it = mem.tokenize(includes_output, " \r\n");
330 while (it.next()) |include_arg| {
331 if (mem.startsWith(u8, include_arg, "-I")) {
332 try result.includes.append(include_arg[2..]);
333 } else {
334 try result.includes.append(include_arg);
335 }
336 }
337 }
338 {
339 var it = mem.tokenize(libdir_output, " \r\n");
340 while (it.next()) |libdir| {
341 if (mem.startsWith(u8, libdir, "-L")) {
342 try result.libdirs.append(libdir[2..]);
343 } else {
344 try result.libdirs.append(libdir);
345 }
346 }
347 }
348 return result;
349}
350
351fn configureStage2(b: *Builder, exe: anytype, ctx: Context, need_cpp_includes: bool) !void {
352 exe.addIncludeDir("src");
353 exe.addIncludeDir(ctx.cmake_binary_dir);
354 addCppLib(b, exe, ctx.cmake_binary_dir, "zigcpp");
355 assert(ctx.lld_include_dir.len != 0);
356 exe.addIncludeDir(ctx.lld_include_dir);
357 {
358 var it = mem.tokenize(ctx.lld_libraries, ";");
359 while (it.next()) |lib| {
360 exe.addObjectFile(lib);
361 }
362 }
363 {
364 var it = mem.tokenize(ctx.clang_libraries, ";");
365 while (it.next()) |lib| {
366 exe.addObjectFile(lib);
367 }
368 }
369 dependOnLib(b, exe, ctx.llvm);
370
371 // Boy, it sure would be nice to simply linkSystemLibrary("c++") and rely on zig's
372 // ability to provide libc++ right? Well thanks to C++ not having a stable ABI this
373 // will cause linker errors. It would work in the situation when `zig cc` is used to
374 // build LLVM, Clang, and LLD, however when depending on them as system libraries, system
375 // libc++ must be used.
376 const cross_compile = false; // TODO
377 if (cross_compile) {
378 // In this case we assume that zig cc was used to build the LLVM, Clang, LLD dependencies.
379 exe.linkSystemLibrary("c++");
380 } else {
381 if (exe.target.getOsTag() == .linux) {
382 // First we try to static link against gcc libstdc++. If that doesn't work,
383 // we fall back to -lc++ and cross our fingers.
384 addCxxKnownPath(b, ctx, exe, "libstdc++.a", "", need_cpp_includes) catch |err| switch (err) {
385 error.RequiredLibraryNotFound => {
386 exe.linkSystemLibrary("c++");
387 },
388 else => |e| return e,
389 };
390
391 exe.linkSystemLibrary("pthread");
392 } else if (exe.target.isFreeBSD()) {
393 try addCxxKnownPath(b, ctx, exe, "libc++.a", null, need_cpp_includes);
394 exe.linkSystemLibrary("pthread");
395 } else if (exe.target.isDarwin()) {
396 if (addCxxKnownPath(b, ctx, exe, "libgcc_eh.a", "", need_cpp_includes)) {
397 // Compiler is GCC.
398 try addCxxKnownPath(b, ctx, exe, "libstdc++.a", null, need_cpp_includes);
399 exe.linkSystemLibrary("pthread");
400 // TODO LLD cannot perform this link.
401 // Set ZIG_SYSTEM_LINKER_HACK env var to use system linker ld instead.
402 // See https://github.com/ziglang/zig/issues/1535
403 } else |err| switch (err) {
404 error.RequiredLibraryNotFound => {
405 // System compiler, not gcc.
406 exe.linkSystemLibrary("c++");
407 },
408 else => |e| return e,
409 }
410 }
411
412 if (ctx.dia_guids_lib.len != 0) {
413 exe.addObjectFile(ctx.dia_guids_lib);
414 }
415 }
416}
417
418fn addCxxKnownPath(
419 b: *Builder,
420 ctx: Context,
421 exe: anytype,
422 objname: []const u8,
423 errtxt: ?[]const u8,
424 need_cpp_includes: bool,
425) !void {
426 const path_padded = try b.exec(&[_][]const u8{
427 ctx.cxx_compiler,
428 b.fmt("-print-file-name={}", .{objname}),
429 });
430 const path_unpadded = mem.tokenize(path_padded, "\r\n").next().?;
431 if (mem.eql(u8, path_unpadded, objname)) {
432 if (errtxt) |msg| {
433 warn("{}", .{msg});
434 } else {
435 warn("Unable to determine path to {}\n", .{objname});
436 }
437 return error.RequiredLibraryNotFound;
438 }
439 exe.addObjectFile(path_unpadded);
440
441 // TODO a way to integrate with system c++ include files here
442 // cc -E -Wp,-v -xc++ /dev/null
443 if (need_cpp_includes) {
444 // I used these temporarily for testing something but we obviously need a
445 // more general purpose solution here.
446 //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0");
447 //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-unknown-linux-gnu");
448 //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward");
449 }
450}
451
452const Context = struct {
453 cmake_binary_dir: []const u8,
454 cxx_compiler: []const u8,
455 llvm_config_exe: []const u8,
456 lld_include_dir: []const u8,
457 lld_libraries: []const u8,
458 clang_libraries: []const u8,
459 dia_guids_lib: []const u8,
460 llvm: LibraryDep,
461};435};
462436
463const max_config_h_bytes = 1 * 1024 * 1024;437const clang_libs = [_][]const u8{
464438 "clangFrontendTool",
465fn findAndReadConfigH(b: *Builder) ![]const u8 {439 "clangCodeGen",
466 var check_dir = fs.path.dirname(b.zig_exe).?;440 "clangFrontend",
467 while (true) {441 "clangDriver",
468 var dir = try fs.cwd().openDir(check_dir, .{});442 "clangSerialization",
469 defer dir.close();443 "clangSema",
470444 "clangStaticAnalyzerFrontend",
471 const config_h_text = dir.readFileAlloc(b.allocator, "config.h", max_config_h_bytes) catch |err| switch (err) {445 "clangStaticAnalyzerCheckers",
472 error.FileNotFound => {446 "clangStaticAnalyzerCore",
473 const new_check_dir = fs.path.dirname(check_dir);447 "clangAnalysis",
474 if (new_check_dir == null or mem.eql(u8, new_check_dir.?, check_dir)) {448 "clangASTMatchers",
475 std.debug.warn("Unable to find config.h file relative to Zig executable.\n", .{});449 "clangAST",
476 std.debug.warn("`zig build` must be run using a Zig executable within the source tree.\n", .{});450 "clangParse",
477 std.process.exit(1);451 "clangSema",
478 }452 "clangBasic",
479 check_dir = new_check_dir.?;453 "clangEdit",
480 continue;454 "clangLex",
481 },455 "clangARCMigrate",
482 else => |e| return e,456 "clangRewriteFrontend",
483 };457 "clangRewrite",
484 return config_h_text;458 "clangCrossTU",
485 } else unreachable; // TODO should not need `else unreachable`.459 "clangIndex",
486}460 "clangToolingCore",
487461};
488fn parseConfigH(b: *Builder, config_h_text: []const u8) Context {462const lld_libs = [_][]const u8{
489 var ctx: Context = .{463 "lldDriver",
490 .cmake_binary_dir = undefined,464 "lldMinGW",
491 .cxx_compiler = undefined,465 "lldELF",
492 .llvm_config_exe = undefined,466 "lldCOFF",
493 .lld_include_dir = undefined,467 "lldMachO",
494 .lld_libraries = undefined,468 "lldWasm",
495 .clang_libraries = undefined,469 "lldReaderWriter",
496 .dia_guids_lib = undefined,470 "lldCore",
497 .llvm = undefined,471 "lldYAML",
498 };472 "lldCommon",
499473};
500 const mappings = [_]struct { prefix: []const u8, field: []const u8 }{474// This list can be re-generated with `llvm-config --libfiles` and then
501 .{475// reformatting using your favorite text editor. Note we do not execute
502 .prefix = "#define ZIG_CMAKE_BINARY_DIR ",476// `llvm-config` here because we are cross compiling.
503 .field = "cmake_binary_dir",477const llvm_libs = [_][]const u8{
504 },478 "LLVMXRay",
505 .{479 "LLVMWindowsManifest",
506 .prefix = "#define ZIG_CXX_COMPILER ",480 "LLVMSymbolize",
507 .field = "cxx_compiler",481 "LLVMDebugInfoPDB",
508 },482 "LLVMOrcJIT",
509 .{483 "LLVMOrcError",
510 .prefix = "#define ZIG_LLD_INCLUDE_PATH ",484 "LLVMJITLink",
511 .field = "lld_include_dir",485 "LLVMObjectYAML",
512 },486 "LLVMMCA",
513 .{487 "LLVMLTO",
514 .prefix = "#define ZIG_LLD_LIBRARIES ",488 "LLVMPasses",
515 .field = "lld_libraries",489 "LLVMCoroutines",
516 },490 "LLVMObjCARCOpts",
517 .{491 "LLVMExtensions",
518 .prefix = "#define ZIG_CLANG_LIBRARIES ",492 "LLVMLineEditor",
519 .field = "clang_libraries",493 "LLVMLibDriver",
520 },494 "LLVMInterpreter",
521 .{495 "LLVMFuzzMutate",
522 .prefix = "#define ZIG_LLVM_CONFIG_EXE ",496 "LLVMMCJIT",
523 .field = "llvm_config_exe",497 "LLVMExecutionEngine",
524 },498 "LLVMRuntimeDyld",
525 .{499 "LLVMDWARFLinker",
526 .prefix = "#define ZIG_DIA_GUIDS_LIB ",500 "LLVMDlltoolDriver",
527 .field = "dia_guids_lib",501 "LLVMOption",
528 },502 "LLVMDebugInfoGSYM",
529 };503 "LLVMCoverage",
530504 "LLVMXCoreDisassembler",
531 var lines_it = mem.tokenize(config_h_text, "\r\n");505 "LLVMXCoreCodeGen",
532 while (lines_it.next()) |line| {506 "LLVMXCoreDesc",
533 inline for (mappings) |mapping| {507 "LLVMXCoreInfo",
534 if (mem.startsWith(u8, line, mapping.prefix)) {508 "LLVMX86Disassembler",
535 var it = mem.split(line, "\"");509 "LLVMX86CodeGen",
536 _ = it.next().?; // skip the stuff before the quote510 "LLVMX86AsmParser",
537 const quoted = it.next().?; // the stuff inside the quote511 "LLVMX86Desc",
538 @field(ctx, mapping.field) = toNativePathSep(b, quoted);512 "LLVMX86Info",
539 }513 "LLVMWebAssemblyDisassembler",
540 }514 "LLVMWebAssemblyCodeGen",
541 }515 "LLVMWebAssemblyDesc",
542 return ctx;516 "LLVMWebAssemblyAsmParser",
543}517 "LLVMWebAssemblyInfo",
544518 "LLVMSystemZDisassembler",
545fn toNativePathSep(b: *Builder, s: []const u8) []u8 {519 "LLVMSystemZCodeGen",
546 const duplicated = mem.dupe(b.allocator, u8, s) catch unreachable;520 "LLVMSystemZAsmParser",
547 for (duplicated) |*byte| switch (byte.*) {521 "LLVMSystemZDesc",
548 '/' => byte.* = fs.path.sep,522 "LLVMSystemZInfo",
549 else => {},523 "LLVMSparcDisassembler",
550 };524 "LLVMSparcCodeGen",
551 return duplicated;525 "LLVMSparcAsmParser",
552}526 "LLVMSparcDesc",
527 "LLVMSparcInfo",
528 "LLVMRISCVDisassembler",
529 "LLVMRISCVCodeGen",
530 "LLVMRISCVAsmParser",
531 "LLVMRISCVDesc",
532 "LLVMRISCVUtils",
533 "LLVMRISCVInfo",
534 "LLVMPowerPCDisassembler",
535 "LLVMPowerPCCodeGen",
536 "LLVMPowerPCAsmParser",
537 "LLVMPowerPCDesc",
538 "LLVMPowerPCInfo",
539 "LLVMNVPTXCodeGen",
540 "LLVMNVPTXDesc",
541 "LLVMNVPTXInfo",
542 "LLVMMSP430Disassembler",
543 "LLVMMSP430CodeGen",
544 "LLVMMSP430AsmParser",
545 "LLVMMSP430Desc",
546 "LLVMMSP430Info",
547 "LLVMMipsDisassembler",
548 "LLVMMipsCodeGen",
549 "LLVMMipsAsmParser",
550 "LLVMMipsDesc",
551 "LLVMMipsInfo",
552 "LLVMLanaiDisassembler",
553 "LLVMLanaiCodeGen",
554 "LLVMLanaiAsmParser",
555 "LLVMLanaiDesc",
556 "LLVMLanaiInfo",
557 "LLVMHexagonDisassembler",
558 "LLVMHexagonCodeGen",
559 "LLVMHexagonAsmParser",
560 "LLVMHexagonDesc",
561 "LLVMHexagonInfo",
562 "LLVMBPFDisassembler",
563 "LLVMBPFCodeGen",
564 "LLVMBPFAsmParser",
565 "LLVMBPFDesc",
566 "LLVMBPFInfo",
567 "LLVMAVRDisassembler",
568 "LLVMAVRCodeGen",
569 "LLVMAVRAsmParser",
570 "LLVMAVRDesc",
571 "LLVMAVRInfo",
572 "LLVMARMDisassembler",
573 "LLVMARMCodeGen",
574 "LLVMARMAsmParser",
575 "LLVMARMDesc",
576 "LLVMARMUtils",
577 "LLVMARMInfo",
578 "LLVMAMDGPUDisassembler",
579 "LLVMAMDGPUCodeGen",
580 "LLVMMIRParser",
581 "LLVMipo",
582 "LLVMInstrumentation",
583 "LLVMVectorize",
584 "LLVMLinker",
585 "LLVMIRReader",
586 "LLVMAsmParser",
587 "LLVMFrontendOpenMP",
588 "LLVMAMDGPUAsmParser",
589 "LLVMAMDGPUDesc",
590 "LLVMAMDGPUUtils",
591 "LLVMAMDGPUInfo",
592 "LLVMAArch64Disassembler",
593 "LLVMMCDisassembler",
594 "LLVMAArch64CodeGen",
595 "LLVMCFGuard",
596 "LLVMGlobalISel",
597 "LLVMSelectionDAG",
598 "LLVMAsmPrinter",
599 "LLVMDebugInfoDWARF",
600 "LLVMCodeGen",
601 "LLVMTarget",
602 "LLVMScalarOpts",
603 "LLVMInstCombine",
604 "LLVMAggressiveInstCombine",
605 "LLVMTransformUtils",
606 "LLVMBitWriter",
607 "LLVMAnalysis",
608 "LLVMProfileData",
609 "LLVMObject",
610 "LLVMTextAPI",
611 "LLVMBitReader",
612 "LLVMCore",
613 "LLVMRemarks",
614 "LLVMBitstreamReader",
615 "LLVMAArch64AsmParser",
616 "LLVMMCParser",
617 "LLVMAArch64Desc",
618 "LLVMMC",
619 "LLVMDebugInfoCodeView",
620 "LLVMDebugInfoMSF",
621 "LLVMBinaryFormat",
622 "LLVMAArch64Utils",
623 "LLVMAArch64Info",
624 "LLVMSupport",
625 "LLVMDemangle",
626};
lib/std/build.zig+76-4
...@@ -1150,6 +1150,11 @@ const CSourceFile = struct {...@@ -1150,6 +1150,11 @@ const CSourceFile = struct {
1150 args: []const []const u8,1150 args: []const []const u8,
1151};1151};
11521152
1153const CSourceFiles = struct {
1154 files: []const []const u8,
1155 flags: []const []const u8,
1156};
1157
1153fn isLibCLibrary(name: []const u8) bool {1158fn isLibCLibrary(name: []const u8) bool {
1154 const libc_libraries = [_][]const u8{ "c", "m", "dl", "rt", "pthread" };1159 const libc_libraries = [_][]const u8{ "c", "m", "dl", "rt", "pthread" };
1155 for (libc_libraries) |libc_lib_name| {1160 for (libc_libraries) |libc_lib_name| {
...@@ -1297,6 +1302,7 @@ pub const LibExeObjStep = struct {...@@ -1297,6 +1302,7 @@ pub const LibExeObjStep = struct {
1297 SystemLib: []const u8,1302 SystemLib: []const u8,
1298 AssemblyFile: FileSource,1303 AssemblyFile: FileSource,
1299 CSourceFile: *CSourceFile,1304 CSourceFile: *CSourceFile,
1305 CSourceFiles: *CSourceFiles,
1300 };1306 };
13011307
1302 const IncludeDir = union(enum) {1308 const IncludeDir = union(enum) {
...@@ -1670,9 +1676,25 @@ pub const LibExeObjStep = struct {...@@ -1670,9 +1676,25 @@ pub const LibExeObjStep = struct {
1670 self.filter = text;1676 self.filter = text;
1671 }1677 }
16721678
1673 pub fn addCSourceFile(self: *LibExeObjStep, file: []const u8, args: []const []const u8) void {1679 /// Handy when you have many C/C++ source files and want them all to have the same flags.
1680 pub fn addCSourceFiles(self: *LibExeObjStep, files: []const []const u8, flags: []const []const u8) void {
1681 const c_source_files = self.builder.allocator.create(CSourceFiles) catch unreachable;
1682
1683 const flags_copy = self.builder.allocator.alloc([]u8, flags.len) catch unreachable;
1684 for (flags) |flag, i| {
1685 flags_copy[i] = self.builder.dupe(flag);
1686 }
1687
1688 c_source_files.* = .{
1689 .files = files,
1690 .flags = flags_copy,
1691 };
1692 self.link_objects.append(LinkObject{ .CSourceFiles = c_source_files }) catch unreachable;
1693 }
1694
1695 pub fn addCSourceFile(self: *LibExeObjStep, file: []const u8, flags: []const []const u8) void {
1674 self.addCSourceFileSource(.{1696 self.addCSourceFileSource(.{
1675 .args = args,1697 .args = flags,
1676 .source = .{ .path = file },1698 .source = .{ .path = file },
1677 });1699 });
1678 }1700 }
...@@ -1795,6 +1817,10 @@ pub const LibExeObjStep = struct {...@@ -1795,6 +1817,10 @@ pub const LibExeObjStep = struct {
1795 out.writeAll("};\n") catch unreachable;1817 out.writeAll("};\n") catch unreachable;
1796 return;1818 return;
1797 },1819 },
1820 [:0]const u8 => {
1821 out.print("pub const {z}: [:0]const u8 = \"{Z}\";\n", .{ name, value }) catch unreachable;
1822 return;
1823 },
1798 []const u8 => {1824 []const u8 => {
1799 out.print("pub const {z}: []const u8 = \"{Z}\";\n", .{ name, value }) catch unreachable;1825 out.print("pub const {z}: []const u8 = \"{Z}\";\n", .{ name, value }) catch unreachable;
1800 return;1826 return;
...@@ -1808,6 +1834,22 @@ pub const LibExeObjStep = struct {...@@ -1808,6 +1834,22 @@ pub const LibExeObjStep = struct {
1808 }1834 }
1809 return;1835 return;
1810 },1836 },
1837 std.builtin.Version => {
1838 out.print(
1839 \\pub const {z}: @import("builtin").Version = .{{
1840 \\ .major = {d},
1841 \\ .minor = {d},
1842 \\ .patch = {d},
1843 \\}};
1844 \\
1845 , .{
1846 name,
1847
1848 value.major,
1849 value.minor,
1850 value.patch,
1851 }) catch unreachable;
1852 },
1811 std.SemanticVersion => {1853 std.SemanticVersion => {
1812 out.print(1854 out.print(
1813 \\pub const {z}: @import("std").SemanticVersion = .{{1855 \\pub const {z}: @import("std").SemanticVersion = .{{
...@@ -2015,8 +2057,7 @@ pub const LibExeObjStep = struct {...@@ -2015,8 +2057,7 @@ pub const LibExeObjStep = struct {
2015 },2057 },
2016 },2058 },
2017 .SystemLib => |name| {2059 .SystemLib => |name| {
2018 try zig_args.append("--library");2060 try zig_args.append(builder.fmt("-l{s}", .{name}));
2019 try zig_args.append(name);
2020 },2061 },
2021 .AssemblyFile => |asm_file| {2062 .AssemblyFile => |asm_file| {
2022 if (prev_has_extra_flags) {2063 if (prev_has_extra_flags) {
...@@ -2026,6 +2067,7 @@ pub const LibExeObjStep = struct {...@@ -2026,6 +2067,7 @@ pub const LibExeObjStep = struct {
2026 }2067 }
2027 try zig_args.append(asm_file.getPath(builder));2068 try zig_args.append(asm_file.getPath(builder));
2028 },2069 },
2070
2029 .CSourceFile => |c_source_file| {2071 .CSourceFile => |c_source_file| {
2030 if (c_source_file.args.len == 0) {2072 if (c_source_file.args.len == 0) {
2031 if (prev_has_extra_flags) {2073 if (prev_has_extra_flags) {
...@@ -2042,6 +2084,25 @@ pub const LibExeObjStep = struct {...@@ -2042,6 +2084,25 @@ pub const LibExeObjStep = struct {
2042 }2084 }
2043 try zig_args.append(c_source_file.source.getPath(builder));2085 try zig_args.append(c_source_file.source.getPath(builder));
2044 },2086 },
2087
2088 .CSourceFiles => |c_source_files| {
2089 if (c_source_files.flags.len == 0) {
2090 if (prev_has_extra_flags) {
2091 try zig_args.append("-cflags");
2092 try zig_args.append("--");
2093 prev_has_extra_flags = false;
2094 }
2095 } else {
2096 try zig_args.append("-cflags");
2097 for (c_source_files.flags) |flag| {
2098 try zig_args.append(flag);
2099 }
2100 try zig_args.append("--");
2101 }
2102 for (c_source_files.files) |file| {
2103 try zig_args.append(builder.pathFromRoot(file));
2104 }
2105 },
2045 }2106 }
2046 }2107 }
20472108
...@@ -2304,6 +2365,17 @@ pub const LibExeObjStep = struct {...@@ -2304,6 +2365,17 @@ pub const LibExeObjStep = struct {
2304 }2365 }
2305 }2366 }
23062367
2368 for (builder.search_prefixes.items) |search_prefix| {
2369 try zig_args.append("-L");
2370 try zig_args.append(try fs.path.join(builder.allocator, &[_][]const u8{
2371 search_prefix, "lib",
2372 }));
2373 try zig_args.append("-isystem");
2374 try zig_args.append(try fs.path.join(builder.allocator, &[_][]const u8{
2375 search_prefix, "include",
2376 }));
2377 }
2378
2307 if (self.valgrind_support) |valgrind_support| {2379 if (self.valgrind_support) |valgrind_support| {
2308 if (valgrind_support) {2380 if (valgrind_support) {
2309 try zig_args.append("-fvalgrind");2381 try zig_args.append("-fvalgrind");
src/config.zig.in deleted-7
...@@ -1,7 +0,0 @@
1pub const have_llvm = true;
2pub const version: []const u8 = "@ZIG_VERSION@";
3pub const log_scopes: []const []const u8 = &[_][]const u8{};
4pub const zir_dumps: []const []const u8 = &[_][]const u8{};
5pub const enable_tracy = false;
6pub const is_stage1 = true;
7pub const skip_non_native = false;
src/stage1.zig+14
...@@ -251,6 +251,20 @@ const Error = extern enum {...@@ -251,6 +251,20 @@ const Error = extern enum {
251 Locked,251 Locked,
252};252};
253253
254// ABI warning
255export fn stage2_version_string() [*:0]const u8 {
256 return build_options.version;
257}
258
259// ABI warning
260export fn stage2_version() Stage2SemVer {
261 return .{
262 .major = build_options.semver.major,
263 .minor = build_options.semver.minor,
264 .patch = build_options.semver.patch,
265 };
266}
267
254// ABI warning268// ABI warning
255export fn stage2_attach_segfault_handler() void {269export fn stage2_attach_segfault_handler() void {
256 if (std.debug.runtime_safety and std.debug.have_segfault_handling_support) {270 if (std.debug.runtime_safety and std.debug.have_segfault_handling_support) {
src/stage1/analyze.cpp-1
...@@ -8,7 +8,6 @@...@@ -8,7 +8,6 @@
8#include "analyze.hpp"8#include "analyze.hpp"
9#include "ast_render.hpp"9#include "ast_render.hpp"
10#include "codegen.hpp"10#include "codegen.hpp"
11#include "config.h"
12#include "error.hpp"11#include "error.hpp"
13#include "ir.hpp"12#include "ir.hpp"
14#include "ir_print.hpp"13#include "ir_print.hpp"
src/stage1/codegen.cpp+6-7
...@@ -8,7 +8,6 @@...@@ -8,7 +8,6 @@
8#include "analyze.hpp"8#include "analyze.hpp"
9#include "ast_render.hpp"9#include "ast_render.hpp"
10#include "codegen.hpp"10#include "codegen.hpp"
11#include "config.h"
12#include "errmsg.hpp"11#include "errmsg.hpp"
13#include "error.hpp"12#include "error.hpp"
14#include "hash_map.hpp"13#include "hash_map.hpp"
...@@ -9051,7 +9050,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -9051,7 +9050,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
9051 buf_append_str(contents, "/// Deprecated: use `std.Target.current.cpu.arch.endian()`\n");9050 buf_append_str(contents, "/// Deprecated: use `std.Target.current.cpu.arch.endian()`\n");
9052 buf_append_str(contents, "pub const endian = Target.current.cpu.arch.endian();\n");9051 buf_append_str(contents, "pub const endian = Target.current.cpu.arch.endian();\n");
9053 buf_appendf(contents, "pub const output_mode = OutputMode.Obj;\n");9052 buf_appendf(contents, "pub const output_mode = OutputMode.Obj;\n");
9054 buf_appendf(contents, "pub const link_mode = LinkMode.%s;\n", ZIG_LINK_MODE);9053 buf_appendf(contents, "pub const link_mode = LinkMode.%s;\n", ZIG_QUOTE(ZIG_LINK_MODE));
9055 buf_appendf(contents, "pub const is_test = false;\n");9054 buf_appendf(contents, "pub const is_test = false;\n");
9056 buf_appendf(contents, "pub const single_threaded = %s;\n", bool_to_str(g->is_single_threaded));9055 buf_appendf(contents, "pub const single_threaded = %s;\n", bool_to_str(g->is_single_threaded));
9057 buf_appendf(contents, "pub const abi = Abi.%s;\n", cur_abi);9056 buf_appendf(contents, "pub const abi = Abi.%s;\n", cur_abi);
...@@ -9251,9 +9250,9 @@ static void init(CodeGen *g) {...@@ -9251,9 +9250,9 @@ static void init(CodeGen *g) {
9251 g->builder = LLVMCreateBuilder();9250 g->builder = LLVMCreateBuilder();
9252 g->dbuilder = ZigLLVMCreateDIBuilder(g->module, true);9251 g->dbuilder = ZigLLVMCreateDIBuilder(g->module, true);
92539252
9254 // Don't use ZIG_VERSION_STRING here, llvm misparses it when it includes9253 // Don't use the version string here, llvm misparses it when it includes the git revision.
9255 // the git revision.9254 Stage2SemVer semver = stage2_version();
9256 Buf *producer = buf_sprintf("zig %d.%d.%d", ZIG_VERSION_MAJOR, ZIG_VERSION_MINOR, ZIG_VERSION_PATCH);9255 Buf *producer = buf_sprintf("zig %d.%d.%d", semver.major, semver.minor, semver.patch);
9257 const char *flags = "";9256 const char *flags = "";
9258 unsigned runtime_version = 0;9257 unsigned runtime_version = 0;
92599258
...@@ -9551,9 +9550,9 @@ void codegen_build_object(CodeGen *g) {...@@ -9551,9 +9550,9 @@ void codegen_build_object(CodeGen *g) {
9551 }9550 }
95529551
9553 do_code_gen(g);9552 do_code_gen(g);
9554 codegen_add_time_event(g, "LLVM Emit Output");9553 codegen_add_time_event(g, "LLVM Emit Object");
9555 {9554 {
9556 const char *progress_name = "LLVM Emit Output";9555 const char *progress_name = "LLVM Emit Object";
9557 codegen_switch_sub_prog_node(g, stage2_progress_start(g->main_progress_node,9556 codegen_switch_sub_prog_node(g, stage2_progress_start(g->main_progress_node,
9558 progress_name, strlen(progress_name), 0));9557 progress_name, strlen(progress_name), 0));
9559 }9558 }
src/stage1/config.h.in deleted-26
...@@ -1,26 +0,0 @@
1/*
2 * Copyright (c) 2016 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_CONFIG_H
9#define ZIG_CONFIG_H
10
11#define ZIG_VERSION_MAJOR @ZIG_VERSION_MAJOR@
12#define ZIG_VERSION_MINOR @ZIG_VERSION_MINOR@
13#define ZIG_VERSION_PATCH @ZIG_VERSION_PATCH@
14#define ZIG_VERSION_STRING "@ZIG_VERSION@"
15
16// Used for communicating build information to self hosted build.
17#define ZIG_CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@"
18#define ZIG_CXX_COMPILER "@CMAKE_CXX_COMPILER@"
19#define ZIG_LLD_INCLUDE_PATH "@LLD_INCLUDE_DIRS@"
20#define ZIG_LLD_LIBRARIES "@LLD_LIBRARIES@"
21#define ZIG_CLANG_LIBRARIES "@CLANG_LIBRARIES@"
22#define ZIG_LLVM_CONFIG_EXE "@LLVM_CONFIG_EXE@"
23#define ZIG_DIA_GUIDS_LIB "@ZIG_DIA_GUIDS_LIB_ESCAPED@"
24#define ZIG_LINK_MODE "@ZIG_LINK_MODE@"
25
26#endif
src/stage1/dump_analysis.cpp+1-2
...@@ -7,7 +7,6 @@...@@ -7,7 +7,6 @@
77
8#include "dump_analysis.hpp"8#include "dump_analysis.hpp"
9#include "analyze.hpp"9#include "analyze.hpp"
10#include "config.h"
11#include "ir.hpp"10#include "ir.hpp"
12#include "codegen.hpp"11#include "codegen.hpp"
13#include "os.hpp"12#include "os.hpp"
...@@ -1237,7 +1236,7 @@ void zig_print_analysis_dump(CodeGen *g, FILE *f, const char *one_indent, const...@@ -1237,7 +1236,7 @@ void zig_print_analysis_dump(CodeGen *g, FILE *f, const char *one_indent, const
1237 jw_begin_object(jw);1236 jw_begin_object(jw);
1238 {1237 {
1239 jw_object_field(jw, "zigVersion");1238 jw_object_field(jw, "zigVersion");
1240 jw_string(jw, ZIG_VERSION_STRING);1239 jw_string(jw, stage2_version_string());
12411240
1242 jw_object_field(jw, "builds");1241 jw_object_field(jw, "builds");
1243 jw_begin_array(jw);1242 jw_begin_array(jw);
src/stage1/heap.cpp-1
...@@ -8,7 +8,6 @@...@@ -8,7 +8,6 @@
8#include <new>8#include <new>
9#include <string.h>9#include <string.h>
1010
11#include "config.h"
12#include "heap.hpp"11#include "heap.hpp"
1312
14namespace heap {13namespace heap {
src/stage1/heap.hpp-1
...@@ -8,7 +8,6 @@...@@ -8,7 +8,6 @@
8#ifndef ZIG_HEAP_HPP8#ifndef ZIG_HEAP_HPP
9#define ZIG_HEAP_HPP9#define ZIG_HEAP_HPP
1010
11#include "config.h"
12#include "util_base.hpp"11#include "util_base.hpp"
13#include "mem.hpp"12#include "mem.hpp"
1413
src/stage1/mem.cpp-1
...@@ -5,7 +5,6 @@...@@ -5,7 +5,6 @@
5 * See http://opensource.org/licenses/MIT5 * See http://opensource.org/licenses/MIT
6 */6 */
77
8#include "config.h"
9#include "mem.hpp"8#include "mem.hpp"
10#include "heap.hpp"9#include "heap.hpp"
1110
src/stage1/mem.hpp-1
...@@ -12,7 +12,6 @@...@@ -12,7 +12,6 @@
12#include <stdio.h>12#include <stdio.h>
13#include <stdlib.h>13#include <stdlib.h>
1414
15#include "config.h"
16#include "util_base.hpp"15#include "util_base.hpp"
17#include "mem_type_info.hpp"16#include "mem_type_info.hpp"
1817
src/stage1/mem_type_info.hpp-2
...@@ -8,8 +8,6 @@...@@ -8,8 +8,6 @@
8#ifndef ZIG_MEM_TYPE_INFO_HPP8#ifndef ZIG_MEM_TYPE_INFO_HPP
9#define ZIG_MEM_TYPE_INFO_HPP9#define ZIG_MEM_TYPE_INFO_HPP
1010
11#include "config.h"
12
13namespace mem {11namespace mem {
1412
15struct TypeInfo {13struct TypeInfo {
src/stage1/stage2.h+6
...@@ -156,6 +156,12 @@ struct Stage2SemVer {...@@ -156,6 +156,12 @@ struct Stage2SemVer {
156 uint32_t patch;156 uint32_t patch;
157};157};
158158
159// ABI warning
160ZIG_EXTERN_C const char *stage2_version_string(void);
161
162// ABI warning
163ZIG_EXTERN_C Stage2SemVer stage2_version(void);
164
159// ABI warning165// ABI warning
160ZIG_EXTERN_C enum Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, const char *mcpu,166ZIG_EXTERN_C enum Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, const char *mcpu,
161 const char *dynamic_linker);167 const char *dynamic_linker);
src/stage1/util.hpp+3-1
...@@ -17,7 +17,9 @@...@@ -17,7 +17,9 @@
17#include <intrin.h> 17#include <intrin.h>
18#endif18#endif
1919
20#include "config.h"20#define ZIG_Q(x) #x
21#define ZIG_QUOTE(x) ZIG_Q(x)
22
21#include "util_base.hpp"23#include "util_base.hpp"
22#include "heap.hpp"24#include "heap.hpp"
23#include "mem.hpp"25#include "mem.hpp"
src/stage1/zig0.cpp+8
...@@ -537,3 +537,11 @@ const char *stage2_add_link_lib(struct ZigStage1 *stage1,...@@ -537,3 +537,11 @@ const char *stage2_add_link_lib(struct ZigStage1 *stage1,
537{537{
538 return nullptr;538 return nullptr;
539}539}
540
541const char *stage2_version_string(void) {
542 return "0.0.0+zig0";
543}
544
545struct Stage2SemVer stage2_version(void) {
546 return {0, 0, 0};
547}
src/stage1_config.zig created+13
...@@ -0,0 +1,13 @@
1pub const have_llvm = true;
2pub const version: [:0]const u8 = "0.0.0+zig0";
3pub const semver: @import("std").SemanticVersion = .{
4 .major = 0,
5 .minor = 0,
6 .patch = 0,
7 .build = "zig0",
8};
9pub const log_scopes: []const []const u8 = &[_][]const u8{};
10pub const zir_dumps: []const []const u8 = &[_][]const u8{};
11pub const enable_tracy = false;
12pub const is_stage1 = true;
13pub const skip_non_native = false;
src/zig_clang.h+1-1
...@@ -8,7 +8,7 @@...@@ -8,7 +8,7 @@
8#ifndef ZIG_ZIG_CLANG_H8#ifndef ZIG_ZIG_CLANG_H
9#define ZIG_ZIG_CLANG_H9#define ZIG_ZIG_CLANG_H
1010
11#include "stage2.h"11#include "stage1/stage2.h"
12#include <inttypes.h>12#include <inttypes.h>
13#include <stdbool.h>13#include <stdbool.h>
1414