2017-12-27 00:44:08 +00:00
|
|
|
const builtin = @import("builtin");
|
2017-12-12 04:34:59 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const Builder = std.build.Builder;
|
2017-04-19 05:13:15 +00:00
|
|
|
const tests = @import("test/tests.zig");
|
2017-12-12 04:34:59 +00:00
|
|
|
const os = std.os;
|
|
|
|
const BufMap = std.BufMap;
|
|
|
|
const warn = std.debug.warn;
|
|
|
|
const mem = std.mem;
|
|
|
|
const ArrayList = std.ArrayList;
|
|
|
|
const Buffer = std.Buffer;
|
|
|
|
const io = std.io;
|
2017-04-19 05:13:15 +00:00
|
|
|
|
|
|
|
pub fn build(b: &Builder) {
|
2017-10-21 21:31:06 +00:00
|
|
|
const mode = b.standardReleaseOptions();
|
|
|
|
|
2017-11-07 08:22:27 +00:00
|
|
|
var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
|
|
|
|
|
|
|
|
var docgen_cmd = b.addCommand(null, b.env_map, [][]const u8 {
|
|
|
|
docgen_exe.getOutputPath(),
|
|
|
|
"doc/langref.html.in",
|
|
|
|
%%os.path.join(b.allocator, b.cache_root, "langref.html"),
|
|
|
|
});
|
|
|
|
docgen_cmd.step.dependOn(&docgen_exe.step);
|
|
|
|
|
|
|
|
var docgen_home_cmd = b.addCommand(null, b.env_map, [][]const u8 {
|
|
|
|
docgen_exe.getOutputPath(),
|
|
|
|
"doc/home.html.in",
|
|
|
|
%%os.path.join(b.allocator, b.cache_root, "home.html"),
|
|
|
|
});
|
|
|
|
docgen_home_cmd.step.dependOn(&docgen_exe.step);
|
|
|
|
|
|
|
|
const docs_step = b.step("docs", "Build documentation");
|
|
|
|
docs_step.dependOn(&docgen_cmd.step);
|
|
|
|
docs_step.dependOn(&docgen_home_cmd.step);
|
|
|
|
|
2018-01-03 23:25:17 +00:00
|
|
|
const test_step = b.step("test", "Run all the tests");
|
|
|
|
|
2017-12-23 05:29:39 +00:00
|
|
|
if (findLLVM(b)) |llvm| {
|
2017-12-27 00:44:08 +00:00
|
|
|
// 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"});
|
2018-01-03 09:55:16 +00:00
|
|
|
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);
|
2017-12-27 00:44:08 +00:00
|
|
|
|
2017-12-23 05:29:39 +00:00
|
|
|
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
|
|
|
|
exe.setBuildMode(mode);
|
2017-12-27 00:44:08 +00:00
|
|
|
exe.addIncludeDir("src");
|
|
|
|
exe.addIncludeDir(cmake_binary_dir);
|
2018-01-03 09:55:16 +00:00
|
|
|
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");
|
|
|
|
}
|
2017-12-23 05:29:39 +00:00
|
|
|
dependOnLib(exe, llvm);
|
2017-11-07 08:22:27 +00:00
|
|
|
|
2017-12-27 00:44:08 +00:00
|
|
|
if (!exe.target.isWindows()) {
|
|
|
|
const libstdcxx_path_padded = b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"});
|
2018-01-03 09:55:16 +00:00
|
|
|
const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next();
|
2017-12-27 00:44:08 +00:00
|
|
|
exe.addObjectFile(libstdcxx_path);
|
|
|
|
|
|
|
|
exe.linkSystemLibrary("pthread");
|
|
|
|
}
|
|
|
|
|
|
|
|
exe.linkSystemLibrary("c");
|
|
|
|
|
2017-12-23 05:29:39 +00:00
|
|
|
b.default_step.dependOn(&exe.step);
|
|
|
|
b.default_step.dependOn(docs_step);
|
2018-01-03 23:25:17 +00:00
|
|
|
test_step.dependOn(&exe.step);
|
2017-10-21 21:31:06 +00:00
|
|
|
|
2017-12-23 05:29:39 +00:00
|
|
|
b.installArtifact(exe);
|
|
|
|
installStdLib(b);
|
2018-01-03 23:25:17 +00:00
|
|
|
|
2017-12-23 05:29:39 +00:00
|
|
|
}
|
2017-10-21 21:31:06 +00:00
|
|
|
|
|
|
|
|
2017-04-19 05:13:15 +00:00
|
|
|
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
|
2017-09-17 18:43:51 +00:00
|
|
|
const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") ?? false;
|
2017-04-19 05:13:15 +00:00
|
|
|
|
2017-11-07 08:22:27 +00:00
|
|
|
test_step.dependOn(docs_step);
|
|
|
|
|
2017-04-30 22:56:24 +00:00
|
|
|
test_step.dependOn(tests.addPkgTests(b, test_filter,
|
2017-09-17 18:43:51 +00:00
|
|
|
"test/behavior.zig", "behavior", "Run the behavior tests",
|
|
|
|
with_lldb));
|
2017-04-19 08:12:22 +00:00
|
|
|
|
2017-04-30 22:56:24 +00:00
|
|
|
test_step.dependOn(tests.addPkgTests(b, test_filter,
|
2017-09-17 18:43:51 +00:00
|
|
|
"std/index.zig", "std", "Run the standard library tests",
|
|
|
|
with_lldb));
|
2017-04-20 06:26:36 +00:00
|
|
|
|
2017-10-03 05:15:07 +00:00
|
|
|
test_step.dependOn(tests.addPkgTests(b, test_filter,
|
2017-09-17 18:43:51 +00:00
|
|
|
"std/special/compiler_rt/index.zig", "compiler-rt", "Run the compiler_rt tests",
|
|
|
|
with_lldb));
|
2017-08-18 17:51:16 +00:00
|
|
|
|
2017-12-11 02:26:28 +00:00
|
|
|
test_step.dependOn(tests.addPkgTests(b, test_filter,
|
|
|
|
"src-self-hosted/main.zig", "fmt", "Run the fmt tests",
|
|
|
|
with_lldb));
|
|
|
|
|
2017-04-30 22:56:24 +00:00
|
|
|
test_step.dependOn(tests.addCompareOutputTests(b, test_filter));
|
|
|
|
test_step.dependOn(tests.addBuildExampleTests(b, test_filter));
|
|
|
|
test_step.dependOn(tests.addCompileErrorTests(b, test_filter));
|
|
|
|
test_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter));
|
|
|
|
test_step.dependOn(tests.addDebugSafetyTests(b, test_filter));
|
2017-11-24 19:56:05 +00:00
|
|
|
test_step.dependOn(tests.addTranslateCTests(b, test_filter));
|
2017-04-19 05:13:15 +00:00
|
|
|
}
|
2017-12-12 04:34:59 +00:00
|
|
|
|
|
|
|
fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) {
|
|
|
|
for (dep.libdirs.toSliceConst()) |lib_dir| {
|
|
|
|
lib_exe_obj.addLibPath(lib_dir);
|
|
|
|
}
|
2017-12-24 01:21:57 +00:00
|
|
|
for (dep.system_libs.toSliceConst()) |lib| {
|
2017-12-12 04:34:59 +00:00
|
|
|
lib_exe_obj.linkSystemLibrary(lib);
|
|
|
|
}
|
2017-12-24 01:21:57 +00:00
|
|
|
for (dep.libs.toSliceConst()) |lib| {
|
|
|
|
lib_exe_obj.addObjectFile(lib);
|
|
|
|
}
|
2017-12-12 04:34:59 +00:00
|
|
|
for (dep.includes.toSliceConst()) |include_path| {
|
|
|
|
lib_exe_obj.addIncludeDir(include_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 00:44:08 +00:00
|
|
|
fn addCppLib(b: &Builder, lib_exe_obj: &std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) {
|
2018-01-03 09:55:16 +00:00
|
|
|
const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib";
|
2017-12-27 00:44:08 +00:00
|
|
|
lib_exe_obj.addObjectFile(%%os.path.join(b.allocator, cmake_binary_dir, "zig_cpp",
|
2018-01-03 09:55:16 +00:00
|
|
|
b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())));
|
2017-12-27 00:44:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 04:34:59 +00:00
|
|
|
const LibraryDep = struct {
|
|
|
|
libdirs: ArrayList([]const u8),
|
|
|
|
libs: ArrayList([]const u8),
|
2017-12-24 01:21:57 +00:00
|
|
|
system_libs: ArrayList([]const u8),
|
2017-12-12 04:34:59 +00:00
|
|
|
includes: ArrayList([]const u8),
|
|
|
|
};
|
|
|
|
|
2017-12-23 05:29:39 +00:00
|
|
|
fn findLLVM(b: &Builder) -> ?LibraryDep {
|
2017-12-12 21:03:20 +00:00
|
|
|
const llvm_config_exe = b.findProgram(
|
|
|
|
[][]const u8{"llvm-config-5.0", "llvm-config"},
|
|
|
|
[][]const u8{
|
2017-12-24 01:21:57 +00:00
|
|
|
"C:/Libraries/llvm-5.0.0/bin",
|
2017-12-12 21:03:20 +00:00
|
|
|
"/c/msys64/mingw64/bin",
|
|
|
|
"c:/msys64/mingw64/bin",
|
2017-12-24 01:21:57 +00:00
|
|
|
"/usr/local/opt/llvm@5/bin",
|
|
|
|
"/mingw64/bin",
|
2017-12-12 21:03:20 +00:00
|
|
|
}) %% |err|
|
|
|
|
{
|
2017-12-23 05:29:39 +00:00
|
|
|
warn("unable to find llvm-config: {}\n", err);
|
|
|
|
return null;
|
2017-12-12 04:34:59 +00:00
|
|
|
};
|
2017-12-12 21:03:20 +00:00
|
|
|
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"});
|
2017-12-12 04:34:59 +00:00
|
|
|
|
|
|
|
var result = LibraryDep {
|
|
|
|
.libs = ArrayList([]const u8).init(b.allocator),
|
2017-12-24 01:21:57 +00:00
|
|
|
.system_libs = ArrayList([]const u8).init(b.allocator),
|
2017-12-12 04:34:59 +00:00
|
|
|
.includes = ArrayList([]const u8).init(b.allocator),
|
|
|
|
.libdirs = ArrayList([]const u8).init(b.allocator),
|
|
|
|
};
|
|
|
|
{
|
2018-01-03 09:55:16 +00:00
|
|
|
var it = mem.split(libs_output, " \r\n");
|
2017-12-12 04:34:59 +00:00
|
|
|
while (it.next()) |lib_arg| {
|
|
|
|
if (mem.startsWith(u8, lib_arg, "-l")) {
|
2017-12-24 01:21:57 +00:00
|
|
|
%%result.system_libs.append(lib_arg[2..]);
|
|
|
|
} else {
|
2017-12-24 02:19:48 +00:00
|
|
|
if (os.path.isAbsolute(lib_arg)) {
|
|
|
|
%%result.libs.append(lib_arg);
|
|
|
|
} else {
|
|
|
|
%%result.system_libs.append(lib_arg);
|
|
|
|
}
|
2017-12-12 04:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2018-01-03 09:55:16 +00:00
|
|
|
var it = mem.split(includes_output, " \r\n");
|
2017-12-12 04:34:59 +00:00
|
|
|
while (it.next()) |include_arg| {
|
|
|
|
if (mem.startsWith(u8, include_arg, "-I")) {
|
|
|
|
%%result.includes.append(include_arg[2..]);
|
|
|
|
} else {
|
|
|
|
%%result.includes.append(include_arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2018-01-03 09:55:16 +00:00
|
|
|
var it = mem.split(libdir_output, " \r\n");
|
2017-12-12 04:34:59 +00:00
|
|
|
while (it.next()) |libdir| {
|
|
|
|
if (mem.startsWith(u8, libdir, "-L")) {
|
|
|
|
%%result.libdirs.append(libdir[2..]);
|
|
|
|
} else {
|
|
|
|
%%result.libdirs.append(libdir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-12-23 05:29:39 +00:00
|
|
|
|
|
|
|
pub fn installStdLib(b: &Builder) {
|
|
|
|
const stdlib_files = []const []const u8 {
|
|
|
|
"array_list.zig",
|
|
|
|
"base64.zig",
|
|
|
|
"buf_map.zig",
|
|
|
|
"buf_set.zig",
|
|
|
|
"buffer.zig",
|
|
|
|
"build.zig",
|
|
|
|
"c/darwin.zig",
|
|
|
|
"c/index.zig",
|
|
|
|
"c/linux.zig",
|
|
|
|
"c/windows.zig",
|
|
|
|
"cstr.zig",
|
2017-12-24 03:08:53 +00:00
|
|
|
"debug/failing_allocator.zig",
|
|
|
|
"debug/index.zig",
|
2017-12-23 05:29:39 +00:00
|
|
|
"dwarf.zig",
|
|
|
|
"elf.zig",
|
|
|
|
"empty.zig",
|
|
|
|
"endian.zig",
|
|
|
|
"fmt/errol/enum3.zig",
|
|
|
|
"fmt/errol/index.zig",
|
|
|
|
"fmt/errol/lookup.zig",
|
|
|
|
"fmt/index.zig",
|
|
|
|
"hash_map.zig",
|
|
|
|
"heap.zig",
|
|
|
|
"index.zig",
|
|
|
|
"io.zig",
|
|
|
|
"linked_list.zig",
|
|
|
|
"math/acos.zig",
|
|
|
|
"math/acosh.zig",
|
|
|
|
"math/asin.zig",
|
|
|
|
"math/asinh.zig",
|
|
|
|
"math/atan.zig",
|
|
|
|
"math/atan2.zig",
|
|
|
|
"math/atanh.zig",
|
|
|
|
"math/cbrt.zig",
|
|
|
|
"math/ceil.zig",
|
|
|
|
"math/copysign.zig",
|
|
|
|
"math/cos.zig",
|
|
|
|
"math/cosh.zig",
|
|
|
|
"math/exp.zig",
|
|
|
|
"math/exp2.zig",
|
|
|
|
"math/expm1.zig",
|
|
|
|
"math/expo2.zig",
|
|
|
|
"math/fabs.zig",
|
|
|
|
"math/floor.zig",
|
|
|
|
"math/fma.zig",
|
|
|
|
"math/frexp.zig",
|
|
|
|
"math/hypot.zig",
|
|
|
|
"math/ilogb.zig",
|
|
|
|
"math/index.zig",
|
|
|
|
"math/inf.zig",
|
|
|
|
"math/isfinite.zig",
|
|
|
|
"math/isinf.zig",
|
|
|
|
"math/isnan.zig",
|
|
|
|
"math/isnormal.zig",
|
|
|
|
"math/ln.zig",
|
|
|
|
"math/log.zig",
|
|
|
|
"math/log10.zig",
|
|
|
|
"math/log1p.zig",
|
|
|
|
"math/log2.zig",
|
|
|
|
"math/modf.zig",
|
|
|
|
"math/nan.zig",
|
|
|
|
"math/pow.zig",
|
|
|
|
"math/round.zig",
|
|
|
|
"math/scalbn.zig",
|
|
|
|
"math/signbit.zig",
|
|
|
|
"math/sin.zig",
|
|
|
|
"math/sinh.zig",
|
|
|
|
"math/sqrt.zig",
|
|
|
|
"math/tan.zig",
|
|
|
|
"math/tanh.zig",
|
|
|
|
"math/trunc.zig",
|
|
|
|
"mem.zig",
|
|
|
|
"net.zig",
|
|
|
|
"os/child_process.zig",
|
|
|
|
"os/darwin.zig",
|
|
|
|
"os/darwin_errno.zig",
|
|
|
|
"os/get_user_id.zig",
|
|
|
|
"os/index.zig",
|
|
|
|
"os/linux.zig",
|
|
|
|
"os/linux_errno.zig",
|
|
|
|
"os/linux_i386.zig",
|
|
|
|
"os/linux_x86_64.zig",
|
|
|
|
"os/path.zig",
|
|
|
|
"os/windows/error.zig",
|
|
|
|
"os/windows/index.zig",
|
|
|
|
"os/windows/util.zig",
|
|
|
|
"rand.zig",
|
|
|
|
"sort.zig",
|
2017-12-27 06:17:33 +00:00
|
|
|
"unicode.zig",
|
2017-12-23 05:29:39 +00:00
|
|
|
"special/bootstrap.zig",
|
|
|
|
"special/bootstrap_lib.zig",
|
|
|
|
"special/build_file_template.zig",
|
|
|
|
"special/build_runner.zig",
|
|
|
|
"special/builtin.zig",
|
|
|
|
"special/compiler_rt/aulldiv.zig",
|
|
|
|
"special/compiler_rt/aullrem.zig",
|
|
|
|
"special/compiler_rt/comparetf2.zig",
|
|
|
|
"special/compiler_rt/fixuint.zig",
|
|
|
|
"special/compiler_rt/fixunsdfdi.zig",
|
|
|
|
"special/compiler_rt/fixunsdfsi.zig",
|
|
|
|
"special/compiler_rt/fixunsdfti.zig",
|
|
|
|
"special/compiler_rt/fixunssfdi.zig",
|
|
|
|
"special/compiler_rt/fixunssfsi.zig",
|
|
|
|
"special/compiler_rt/fixunssfti.zig",
|
|
|
|
"special/compiler_rt/fixunstfdi.zig",
|
|
|
|
"special/compiler_rt/fixunstfsi.zig",
|
|
|
|
"special/compiler_rt/fixunstfti.zig",
|
|
|
|
"special/compiler_rt/index.zig",
|
|
|
|
"special/compiler_rt/udivmod.zig",
|
|
|
|
"special/compiler_rt/udivmoddi4.zig",
|
|
|
|
"special/compiler_rt/udivmodti4.zig",
|
|
|
|
"special/compiler_rt/udivti3.zig",
|
|
|
|
"special/compiler_rt/umodti3.zig",
|
|
|
|
"special/panic.zig",
|
|
|
|
"special/test_runner.zig",
|
|
|
|
};
|
|
|
|
for (stdlib_files) |stdlib_file| {
|
|
|
|
const src_path = %%os.path.join(b.allocator, "std", stdlib_file);
|
|
|
|
const dest_path = %%os.path.join(b.allocator, "lib", "zig", "std", stdlib_file);
|
|
|
|
b.installFile(src_path, dest_path);
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 09:55:16 +00:00
|
|
|
|
|
|
|
fn nextValue(index: &usize, build_info: []const u8) -> []const u8 {
|
|
|
|
const start = *index;
|
|
|
|
while (build_info[*index] != '\n' and build_info[*index] != '\r') : (*index += 1) { }
|
|
|
|
const result = build_info[start..*index];
|
|
|
|
*index += 1;
|
|
|
|
return result;
|
|
|
|
}
|