aboutsummaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-09-03 20:23:00 -0700
committerAndrew Kelley <andrew@ziglang.org>2020-09-09 09:28:05 -0700
commit503ba7b27c6e8e248d271aec936623853cd8fcd1 (patch)
tree005438102ec6ba09cb9368397b9ff180f44790ad /build.zig
parent749417a1f3060f0695bbfe72d929f06b0be42535 (diff)
downloadzig-503ba7b27c6e8e248d271aec936623853cd8fcd1.tar.gz
zig-503ba7b27c6e8e248d271aec936623853cd8fcd1.zip
start moving `zig cc` to stage2
* build.zig: repair the ability to link against llvm, clang, and lld * move the zig cc arg parsing logic to stage2 - the preprocessor flag is still TODO - the clang arg iterator code is improved to use slices instead of raw pointers because it no longer has to deal with an extern struct. * clean up error printing with a `fatal` function and use log API for messages rather than std.debug.print * add support for more CLI options to stage2 & update usage text - hooking up most of these new options is TODO * clean up the way libc and libc++ are detected via command line options. target information is used to determine if any of the libc candidate names are chosen. * add native library directory detection * implement the ability to invoke clang from stage2 * introduce a build_options.have_llvm so we can comptime branch on whether LLVM is linked in or not.
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig89
1 files changed, 47 insertions, 42 deletions
diff --git a/build.zig b/build.zig
index 3f7f1a9038..43b3b7eebf 100644
--- a/build.zig
+++ b/build.zig
@@ -9,6 +9,7 @@ const ArrayList = std.ArrayList;
const io = std.io;
const fs = std.fs;
const InstallDirectoryOptions = std.build.InstallDirectoryOptions;
+const assert = std.debug.assert;
const zig_version = std.builtin.Version{ .major = 0, .minor = 6, .patch = 0 };
@@ -57,11 +58,13 @@ pub fn build(b: *Builder) !void {
if (!only_install_lib_files) {
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
+ exe.install();
exe.setBuildMode(mode);
exe.setTarget(target);
test_step.dependOn(&exe.step);
b.default_step.dependOn(&exe.step);
+ exe.addBuildOption(bool, "have_llvm", enable_llvm);
if (enable_llvm) {
const config_h_text = if (config_h_path_option) |config_h_path|
try std.fs.cwd().readFileAlloc(b.allocator, toNativePathSep(b, config_h_path), max_config_h_bytes)
@@ -73,11 +76,8 @@ pub fn build(b: *Builder) !void {
try configureStage2(b, exe, ctx);
}
- if (!only_install_lib_files) {
- exe.install();
- }
const tracy = b.option([]const u8, "tracy", "Enable Tracy integration. Supply path to Tracy source");
- const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse false;
+ const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm;
if (link_libc) {
exe.linkLibC();
test_stage2.linkLibC();
@@ -323,17 +323,13 @@ fn configureStage2(b: *Builder, exe: anytype, ctx: Context) !void {
exe.addIncludeDir("src");
exe.addIncludeDir(ctx.cmake_binary_dir);
addCppLib(b, exe, ctx.cmake_binary_dir, "zig_cpp");
- if (ctx.lld_include_dir.len != 0) {
- exe.addIncludeDir(ctx.lld_include_dir);
+ assert(ctx.lld_include_dir.len != 0);
+ exe.addIncludeDir(ctx.lld_include_dir);
+ {
var it = mem.tokenize(ctx.lld_libraries, ";");
while (it.next()) |lib| {
exe.addObjectFile(lib);
}
- } else {
- addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_wasm");
- addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_elf");
- addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_coff");
- addCppLib(b, exe, ctx.cmake_binary_dir, "embedded_lld_lib");
}
{
var it = mem.tokenize(ctx.clang_libraries, ";");
@@ -343,42 +339,51 @@ fn configureStage2(b: *Builder, exe: anytype, ctx: Context) !void {
}
dependOnLib(b, exe, ctx.llvm);
- if (exe.target.getOsTag() == .linux) {
- // First we try to static link against gcc libstdc++. If that doesn't work,
- // we fall back to -lc++ and cross our fingers.
- addCxxKnownPath(b, ctx, exe, "libstdc++.a", "") catch |err| switch (err) {
- error.RequiredLibraryNotFound => {
- exe.linkSystemLibrary("c++");
- },
- else => |e| return e,
- };
+ // Boy, it sure would be nice to simply linkSystemLibrary("c++") and rely on zig's
+ // ability to provide libc++ right? Well thanks to C++ not having a stable ABI this
+ // will cause linker errors. It would work in the situation when `zig cc` is used to
+ // build LLVM, Clang, and LLD, however when depending on them as system libraries, system
+ // libc++ must be used.
+ const cross_compile = false; // TODO
+ if (cross_compile) {
+ // In this case we assume that zig cc was used to build the LLVM, Clang, LLD dependencies.
+ exe.linkSystemLibrary("c++");
+ } else {
+ if (exe.target.getOsTag() == .linux) {
+ // First we try to static link against gcc libstdc++. If that doesn't work,
+ // we fall back to -lc++ and cross our fingers.
+ addCxxKnownPath(b, ctx, exe, "libstdc++.a", "") catch |err| switch (err) {
+ error.RequiredLibraryNotFound => {
+ exe.linkSystemLibrary("c++");
+ },
+ else => |e| return e,
+ };
- exe.linkSystemLibrary("pthread");
- } else if (exe.target.isFreeBSD()) {
- try addCxxKnownPath(b, ctx, exe, "libc++.a", null);
- exe.linkSystemLibrary("pthread");
- } else if (exe.target.isDarwin()) {
- if (addCxxKnownPath(b, ctx, exe, "libgcc_eh.a", "")) {
- // Compiler is GCC.
- try addCxxKnownPath(b, ctx, exe, "libstdc++.a", null);
exe.linkSystemLibrary("pthread");
- // TODO LLD cannot perform this link.
- // See https://github.com/ziglang/zig/issues/1535
- exe.enableSystemLinkerHack();
- } else |err| switch (err) {
- error.RequiredLibraryNotFound => {
- // System compiler, not gcc.
- exe.linkSystemLibrary("c++");
- },
- else => |e| return e,
+ } else if (exe.target.isFreeBSD()) {
+ try addCxxKnownPath(b, ctx, exe, "libc++.a", null);
+ exe.linkSystemLibrary("pthread");
+ } else if (exe.target.isDarwin()) {
+ if (addCxxKnownPath(b, ctx, exe, "libgcc_eh.a", "")) {
+ // Compiler is GCC.
+ try addCxxKnownPath(b, ctx, exe, "libstdc++.a", null);
+ exe.linkSystemLibrary("pthread");
+ // TODO LLD cannot perform this link.
+ // See https://github.com/ziglang/zig/issues/1535
+ exe.enableSystemLinkerHack();
+ } else |err| switch (err) {
+ error.RequiredLibraryNotFound => {
+ // System compiler, not gcc.
+ exe.linkSystemLibrary("c++");
+ },
+ else => |e| return e,
+ }
}
- }
- if (ctx.dia_guids_lib.len != 0) {
- exe.addObjectFile(ctx.dia_guids_lib);
+ if (ctx.dia_guids_lib.len != 0) {
+ exe.addObjectFile(ctx.dia_guids_lib);
+ }
}
-
- exe.linkSystemLibrary("c");
}
fn addCxxKnownPath(