From 477e3f64fc632cc87956a9a6354c9069b93d5919 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 3 Jan 2018 21:32:50 -0500 Subject: [PATCH] self-hosted build: use llvm-config from stage1 --- build.zig | 100 +++++++++++++++++++++--------------------------- src/config.h.in | 1 + src/main.cpp | 3 +- 3 files changed, 46 insertions(+), 58 deletions(-) diff --git a/build.zig b/build.zig index 679c88bb7134ceb8ab200dd57da0639301f87d4b..2ff98819f171f29907c97fca162b2d4baddfe952 100644 --- a/build.zig +++ b/build.zig @@ -35,55 +35,54 @@ pub fn build(b: &Builder) { const test_step = b.step("test", "Run all the tests"); - if (findLLVM(b)) |llvm| { - // find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library - const build_info = b.exec([][]const u8{b.zig_exe, "BUILD_INFO"}); - var index: usize = 0; - const cmake_binary_dir = nextValue(&index, build_info); - const cxx_compiler = nextValue(&index, build_info); - const lld_include_dir = nextValue(&index, build_info); - const lld_libraries = nextValue(&index, build_info); - const std_files = nextValue(&index, build_info); - const c_header_files = nextValue(&index, build_info); - - var exe = b.addExecutable("zig", "src-self-hosted/main.zig"); - exe.setBuildMode(mode); - exe.addIncludeDir("src"); - exe.addIncludeDir(cmake_binary_dir); - addCppLib(b, exe, cmake_binary_dir, "zig_cpp"); - if (lld_include_dir.len != 0) { - exe.addIncludeDir(lld_include_dir); - var it = mem.split(lld_libraries, ";"); - while (it.next()) |lib| { - exe.addObjectFile(lib); - } - } else { - addCppLib(b, exe, cmake_binary_dir, "embedded_lld_elf"); - addCppLib(b, exe, cmake_binary_dir, "embedded_lld_coff"); - addCppLib(b, exe, cmake_binary_dir, "embedded_lld_lib"); - } - dependOnLib(exe, llvm); - - if (!exe.target.isWindows()) { - const libstdcxx_path_padded = b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"}); - const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next(); - exe.addObjectFile(libstdcxx_path); - - exe.linkSystemLibrary("pthread"); + // find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library + const build_info = b.exec([][]const u8{b.zig_exe, "BUILD_INFO"}); + var index: usize = 0; + const cmake_binary_dir = nextValue(&index, build_info); + const cxx_compiler = nextValue(&index, build_info); + const llvm_config_exe = nextValue(&index, build_info); + const lld_include_dir = nextValue(&index, build_info); + const lld_libraries = nextValue(&index, build_info); + const std_files = nextValue(&index, build_info); + const c_header_files = nextValue(&index, build_info); + + const llvm = findLLVM(b, llvm_config_exe); + + var exe = b.addExecutable("zig", "src-self-hosted/main.zig"); + exe.setBuildMode(mode); + exe.addIncludeDir("src"); + exe.addIncludeDir(cmake_binary_dir); + addCppLib(b, exe, cmake_binary_dir, "zig_cpp"); + if (lld_include_dir.len != 0) { + exe.addIncludeDir(lld_include_dir); + var it = mem.split(lld_libraries, ";"); + while (it.next()) |lib| { + exe.addObjectFile(lib); } + } else { + addCppLib(b, exe, cmake_binary_dir, "embedded_lld_elf"); + addCppLib(b, exe, cmake_binary_dir, "embedded_lld_coff"); + addCppLib(b, exe, cmake_binary_dir, "embedded_lld_lib"); + } + dependOnLib(exe, llvm); - exe.linkSystemLibrary("c"); + if (!exe.target.isWindows()) { + const libstdcxx_path_padded = b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"}); + const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next(); + exe.addObjectFile(libstdcxx_path); - b.default_step.dependOn(&exe.step); - b.default_step.dependOn(docs_step); - test_step.dependOn(&exe.step); + exe.linkSystemLibrary("pthread"); + } - b.installArtifact(exe); - installStdLib(b, std_files); - installCHeaders(b, c_header_files); + exe.linkSystemLibrary("c"); - } + b.default_step.dependOn(&exe.step); + b.default_step.dependOn(docs_step); + test_step.dependOn(&exe.step); + b.installArtifact(exe); + installStdLib(b, std_files); + installCHeaders(b, c_header_files); const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter"); const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") ?? false; @@ -142,20 +141,7 @@ const LibraryDep = struct { includes: ArrayList([]const u8), }; -fn findLLVM(b: &Builder) -> ?LibraryDep { - const llvm_config_exe = b.findProgram( - [][]const u8{"llvm-config-5.0", "llvm-config"}, - [][]const u8{ - "C:/Libraries/llvm-5.0.0/bin", - "/c/msys64/mingw64/bin", - "c:/msys64/mingw64/bin", - "/usr/local/opt/llvm@5/bin", - "/mingw64/bin", - }) %% |err| - { - warn("unable to find llvm-config: {}\n", err); - return null; - }; +fn findLLVM(b: &Builder, llvm_config_exe: []const u8) -> LibraryDep { const libs_output = b.exec([][]const u8{llvm_config_exe, "--libs", "--system-libs"}); const includes_output = b.exec([][]const u8{llvm_config_exe, "--includedir"}); const libdir_output = b.exec([][]const u8{llvm_config_exe, "--libdir"}); diff --git a/src/config.h.in b/src/config.h.in index 4202e670ff58eb8f3e4f9e04c461566853becb66..038d42d218fd0de838e551ce4cb2683ea2ddc420 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -29,6 +29,7 @@ #define ZIG_CXX_COMPILER "@CMAKE_CXX_COMPILER@" #define ZIG_LLD_INCLUDE_PATH "@LLD_INCLUDE_DIRS@" #define ZIG_LLD_LIBRARIES "@LLD_LIBRARIES@" +#define ZIG_LLVM_CONFIG_EXE "@LLVM_CONFIG_EXE@" #define ZIG_STD_FILES "@ZIG_STD_FILES@" #define ZIG_C_HEADER_FILES "@ZIG_C_HEADER_FILES@" diff --git a/src/main.cpp b/src/main.cpp index a3ca1bc164e68dca8937c4759884e3433f61133b..6f70317f985173e8d1f71bcf85b3d6517df441b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -267,9 +267,10 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) { int main(int argc, char **argv) { if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) { - printf("%s\n%s\n%s\n%s\n%s\n%s\n", + printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n", ZIG_CMAKE_BINARY_DIR, ZIG_CXX_COMPILER, + ZIG_LLVM_CONFIG_EXE, ZIG_LLD_INCLUDE_PATH, ZIG_LLD_LIBRARIES, ZIG_STD_FILES, -- 2.54.0