2017-12-26 16:44:08 -08:00
|
|
|
const builtin = @import("builtin");
|
2017-12-11 20:34:59 -08:00
|
|
|
const std = @import("std");
|
|
|
|
const Builder = std.build.Builder;
|
2017-04-18 22:13:15 -07:00
|
|
|
const tests = @import("test/tests.zig");
|
2017-12-11 20:34:59 -08: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-18 22:13:15 -07:00
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
pub fn build(b: &Builder) !void {
|
2017-10-21 14:31:06 -07:00
|
|
|
const mode = b.standardReleaseOptions();
|
|
|
|
|
2017-11-07 00:22:27 -08:00
|
|
|
var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
|
|
|
|
|
2018-01-16 22:50:35 -08:00
|
|
|
const rel_zig_exe = try os.path.relative(b.allocator, b.build_root, b.zig_exe);
|
2017-11-07 00:22:27 -08:00
|
|
|
var docgen_cmd = b.addCommand(null, b.env_map, [][]const u8 {
|
|
|
|
docgen_exe.getOutputPath(),
|
2018-01-16 22:50:35 -08:00
|
|
|
rel_zig_exe,
|
2017-11-07 00:22:27 -08:00
|
|
|
"doc/langref.html.in",
|
2018-01-08 21:07:01 -08:00
|
|
|
os.path.join(b.allocator, b.cache_root, "langref.html") catch unreachable,
|
2017-11-07 00:22:27 -08:00
|
|
|
});
|
|
|
|
docgen_cmd.step.dependOn(&docgen_exe.step);
|
|
|
|
|
|
|
|
const docs_step = b.step("docs", "Build documentation");
|
|
|
|
docs_step.dependOn(&docgen_cmd.step);
|
|
|
|
|
2018-01-03 15:25:17 -08:00
|
|
|
const test_step = b.step("test", "Run all the tests");
|
|
|
|
|
2018-01-03 18:32:50 -08:00
|
|
|
// find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library
|
2018-01-11 23:12:11 -08:00
|
|
|
const build_info = try b.exec([][]const u8{b.zig_exe, "BUILD_INFO"});
|
2018-01-03 18:32:50 -08:00
|
|
|
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);
|
2018-01-04 19:46:26 -08:00
|
|
|
const dia_guids_lib = nextValue(&index, build_info);
|
2018-01-03 18:32:50 -08:00
|
|
|
|
2018-01-08 21:07:01 -08:00
|
|
|
const llvm = findLLVM(b, llvm_config_exe) catch unreachable;
|
2018-01-03 18:32:50 -08:00
|
|
|
|
|
|
|
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);
|
2017-12-26 16:44:08 -08:00
|
|
|
}
|
2018-01-03 18:32:50 -08:00
|
|
|
} 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);
|
2017-12-26 16:44:08 -08:00
|
|
|
|
2018-01-04 12:30:22 -08:00
|
|
|
if (exe.target.getOs() == builtin.Os.linux) {
|
2018-01-11 23:12:11 -08:00
|
|
|
const libstdcxx_path_padded = try b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"});
|
2018-01-03 18:32:50 -08:00
|
|
|
const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next();
|
2018-03-20 10:48:25 -07:00
|
|
|
if (mem.eql(u8, libstdcxx_path, "libstdc++.a")) {
|
|
|
|
warn(
|
|
|
|
\\Unable to determine path to libstdc++.a
|
|
|
|
\\On Fedora, install libstdc++-static and try again.
|
|
|
|
\\
|
|
|
|
);
|
|
|
|
return error.RequiredLibraryNotFound;
|
|
|
|
}
|
2018-01-03 18:32:50 -08:00
|
|
|
exe.addObjectFile(libstdcxx_path);
|
2017-12-26 16:44:08 -08:00
|
|
|
|
2018-01-03 18:32:50 -08:00
|
|
|
exe.linkSystemLibrary("pthread");
|
2018-01-04 12:30:22 -08:00
|
|
|
} else if (exe.target.isDarwin()) {
|
|
|
|
exe.linkSystemLibrary("c++");
|
2018-01-03 18:32:50 -08:00
|
|
|
}
|
2017-10-21 14:31:06 -07:00
|
|
|
|
2018-01-04 19:46:26 -08:00
|
|
|
if (dia_guids_lib.len != 0) {
|
|
|
|
exe.addObjectFile(dia_guids_lib);
|
|
|
|
}
|
|
|
|
|
2018-03-10 15:23:08 -08:00
|
|
|
if (exe.target.getOs() != builtin.Os.windows) {
|
|
|
|
exe.linkSystemLibrary("xml2");
|
|
|
|
}
|
2018-01-03 18:32:50 -08:00
|
|
|
exe.linkSystemLibrary("c");
|
2018-01-03 15:25:17 -08:00
|
|
|
|
2018-01-03 18:32:50 -08:00
|
|
|
b.default_step.dependOn(&exe.step);
|
2018-01-04 20:30:03 -08:00
|
|
|
|
|
|
|
const skip_self_hosted = b.option(bool, "skip-self-hosted", "Main test suite skips building self hosted compiler") ?? false;
|
|
|
|
if (!skip_self_hosted) {
|
|
|
|
test_step.dependOn(&exe.step);
|
|
|
|
}
|
2018-01-04 20:43:46 -08:00
|
|
|
const verbose_link_exe = b.option(bool, "verbose-link", "Print link command for self hosted compiler") ?? false;
|
|
|
|
exe.setVerboseLink(verbose_link_exe);
|
2017-10-21 14:31:06 -07:00
|
|
|
|
2018-01-03 18:32:50 -08:00
|
|
|
b.installArtifact(exe);
|
|
|
|
installStdLib(b, std_files);
|
|
|
|
installCHeaders(b, c_header_files);
|
2017-10-21 14:31:06 -07:00
|
|
|
|
2017-04-18 22:13:15 -07:00
|
|
|
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
|
2017-09-17 11:43:51 -07:00
|
|
|
const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") ?? false;
|
2017-04-18 22:13:15 -07:00
|
|
|
|
2017-11-07 00:22:27 -08:00
|
|
|
test_step.dependOn(docs_step);
|
|
|
|
|
2017-04-30 15:56:24 -07:00
|
|
|
test_step.dependOn(tests.addPkgTests(b, test_filter,
|
2017-09-17 11:43:51 -07:00
|
|
|
"test/behavior.zig", "behavior", "Run the behavior tests",
|
|
|
|
with_lldb));
|
2017-04-19 01:12:22 -07:00
|
|
|
|
2017-04-30 15:56:24 -07:00
|
|
|
test_step.dependOn(tests.addPkgTests(b, test_filter,
|
2017-09-17 11:43:51 -07:00
|
|
|
"std/index.zig", "std", "Run the standard library tests",
|
|
|
|
with_lldb));
|
2017-04-19 23:26:36 -07:00
|
|
|
|
2017-10-02 22:15:07 -07:00
|
|
|
test_step.dependOn(tests.addPkgTests(b, test_filter,
|
2017-09-17 11:43:51 -07:00
|
|
|
"std/special/compiler_rt/index.zig", "compiler-rt", "Run the compiler_rt tests",
|
|
|
|
with_lldb));
|
2017-08-18 10:51:16 -07:00
|
|
|
|
2017-04-30 15:56:24 -07: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));
|
2018-01-24 22:46:12 -08:00
|
|
|
test_step.dependOn(tests.addRuntimeSafetyTests(b, test_filter));
|
2017-11-24 11:56:05 -08:00
|
|
|
test_step.dependOn(tests.addTranslateCTests(b, test_filter));
|
2018-01-22 19:24:07 -08:00
|
|
|
test_step.dependOn(tests.addGenHTests(b, test_filter));
|
2017-04-18 22:13:15 -07:00
|
|
|
}
|
2017-12-11 20:34:59 -08:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) void {
|
2017-12-11 20:34:59 -08:00
|
|
|
for (dep.libdirs.toSliceConst()) |lib_dir| {
|
|
|
|
lib_exe_obj.addLibPath(lib_dir);
|
|
|
|
}
|
2017-12-23 17:21:57 -08:00
|
|
|
for (dep.system_libs.toSliceConst()) |lib| {
|
2017-12-11 20:34:59 -08:00
|
|
|
lib_exe_obj.linkSystemLibrary(lib);
|
|
|
|
}
|
2017-12-23 17:21:57 -08:00
|
|
|
for (dep.libs.toSliceConst()) |lib| {
|
|
|
|
lib_exe_obj.addObjectFile(lib);
|
|
|
|
}
|
2017-12-11 20:34:59 -08:00
|
|
|
for (dep.includes.toSliceConst()) |include_path| {
|
|
|
|
lib_exe_obj.addIncludeDir(include_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn addCppLib(b: &Builder, lib_exe_obj: &std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) void {
|
2018-01-03 01:55:16 -08:00
|
|
|
const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib";
|
2018-01-08 21:07:01 -08:00
|
|
|
lib_exe_obj.addObjectFile(os.path.join(b.allocator, cmake_binary_dir, "zig_cpp",
|
|
|
|
b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())) catch unreachable);
|
2017-12-26 16:44:08 -08:00
|
|
|
}
|
|
|
|
|
2017-12-11 20:34:59 -08:00
|
|
|
const LibraryDep = struct {
|
|
|
|
libdirs: ArrayList([]const u8),
|
|
|
|
libs: ArrayList([]const u8),
|
2017-12-23 17:21:57 -08:00
|
|
|
system_libs: ArrayList([]const u8),
|
2017-12-11 20:34:59 -08:00
|
|
|
includes: ArrayList([]const u8),
|
|
|
|
};
|
|
|
|
|
2018-01-31 19:48:40 -08:00
|
|
|
fn findLLVM(b: &Builder, llvm_config_exe: []const u8) !LibraryDep {
|
2018-01-11 23:12:11 -08:00
|
|
|
const libs_output = try b.exec([][]const u8{llvm_config_exe, "--libs", "--system-libs"});
|
|
|
|
const includes_output = try b.exec([][]const u8{llvm_config_exe, "--includedir"});
|
|
|
|
const libdir_output = try b.exec([][]const u8{llvm_config_exe, "--libdir"});
|
2017-12-11 20:34:59 -08:00
|
|
|
|
|
|
|
var result = LibraryDep {
|
|
|
|
.libs = ArrayList([]const u8).init(b.allocator),
|
2017-12-23 17:21:57 -08:00
|
|
|
.system_libs = ArrayList([]const u8).init(b.allocator),
|
2017-12-11 20:34:59 -08:00
|
|
|
.includes = ArrayList([]const u8).init(b.allocator),
|
|
|
|
.libdirs = ArrayList([]const u8).init(b.allocator),
|
|
|
|
};
|
|
|
|
{
|
2018-01-03 01:55:16 -08:00
|
|
|
var it = mem.split(libs_output, " \r\n");
|
2017-12-11 20:34:59 -08:00
|
|
|
while (it.next()) |lib_arg| {
|
|
|
|
if (mem.startsWith(u8, lib_arg, "-l")) {
|
2018-01-08 21:07:01 -08:00
|
|
|
try result.system_libs.append(lib_arg[2..]);
|
2017-12-23 17:21:57 -08:00
|
|
|
} else {
|
2017-12-23 18:19:48 -08:00
|
|
|
if (os.path.isAbsolute(lib_arg)) {
|
2018-01-08 21:07:01 -08:00
|
|
|
try result.libs.append(lib_arg);
|
2017-12-23 18:19:48 -08:00
|
|
|
} else {
|
2018-01-08 21:07:01 -08:00
|
|
|
try result.system_libs.append(lib_arg);
|
2017-12-23 18:19:48 -08:00
|
|
|
}
|
2017-12-11 20:34:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2018-01-03 01:55:16 -08:00
|
|
|
var it = mem.split(includes_output, " \r\n");
|
2017-12-11 20:34:59 -08:00
|
|
|
while (it.next()) |include_arg| {
|
|
|
|
if (mem.startsWith(u8, include_arg, "-I")) {
|
2018-01-08 21:07:01 -08:00
|
|
|
try result.includes.append(include_arg[2..]);
|
2017-12-11 20:34:59 -08:00
|
|
|
} else {
|
2018-01-08 21:07:01 -08:00
|
|
|
try result.includes.append(include_arg);
|
2017-12-11 20:34:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2018-01-03 01:55:16 -08:00
|
|
|
var it = mem.split(libdir_output, " \r\n");
|
2017-12-11 20:34:59 -08:00
|
|
|
while (it.next()) |libdir| {
|
|
|
|
if (mem.startsWith(u8, libdir, "-L")) {
|
2018-01-08 21:07:01 -08:00
|
|
|
try result.libdirs.append(libdir[2..]);
|
2017-12-11 20:34:59 -08:00
|
|
|
} else {
|
2018-01-08 21:07:01 -08:00
|
|
|
try result.libdirs.append(libdir);
|
2017-12-11 20:34:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-12-22 21:29:39 -08:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn installStdLib(b: &Builder, stdlib_files: []const u8) void {
|
2018-01-03 16:39:04 -08:00
|
|
|
var it = mem.split(stdlib_files, ";");
|
|
|
|
while (it.next()) |stdlib_file| {
|
2018-01-08 21:07:01 -08:00
|
|
|
const src_path = os.path.join(b.allocator, "std", stdlib_file) catch unreachable;
|
|
|
|
const dest_path = os.path.join(b.allocator, "lib", "zig", "std", stdlib_file) catch unreachable;
|
2017-12-22 21:29:39 -08:00
|
|
|
b.installFile(src_path, dest_path);
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 01:55:16 -08:00
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
pub fn installCHeaders(b: &Builder, c_header_files: []const u8) void {
|
2018-01-03 16:39:04 -08:00
|
|
|
var it = mem.split(c_header_files, ";");
|
|
|
|
while (it.next()) |c_header_file| {
|
2018-01-08 21:07:01 -08:00
|
|
|
const src_path = os.path.join(b.allocator, "c_headers", c_header_file) catch unreachable;
|
|
|
|
const dest_path = os.path.join(b.allocator, "lib", "zig", "include", c_header_file) catch unreachable;
|
2018-01-03 16:39:04 -08:00
|
|
|
b.installFile(src_path, dest_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn nextValue(index: &usize, build_info: []const u8) []const u8 {
|
2018-01-03 01:55:16 -08:00
|
|
|
const start = *index;
|
2018-01-03 19:38:13 -08:00
|
|
|
while (true) : (*index += 1) {
|
|
|
|
switch (build_info[*index]) {
|
|
|
|
'\n' => {
|
|
|
|
const result = build_info[start..*index];
|
|
|
|
*index += 1;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
'\r' => {
|
|
|
|
const result = build_info[start..*index];
|
|
|
|
*index += 2;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
else => continue,
|
|
|
|
}
|
|
|
|
}
|
2018-01-03 01:55:16 -08:00
|
|
|
}
|