authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-03 04:55:16-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-03 04:55:49-05:00
log36ff26609b1d81a253053e7d7785392fae240dab
tree5226307e3861ac8d9c944ffa611a84f6205a55a6
parent6281a511e130aeb4f7c777de9b90c60d7c8c6f34

fix self hosted compiler on windows


8 files changed, 54 insertions(+), 21 deletions(-)

build.zig+31-12
......@@ -36,23 +36,33 @@ pub fn build(b: &Builder) {
3636 if (findLLVM(b)) |llvm| {
3737 // find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library
3838 const build_info = b.exec([][]const u8{b.zig_exe, "BUILD_INFO"});
39 var build_info_it = mem.split(build_info, "\n");
40 const cmake_binary_dir = ??build_info_it.next();
41 const cxx_compiler = ??build_info_it.next();
39 var index: usize = 0;
40 const cmake_binary_dir = nextValue(&index, build_info);
41 const cxx_compiler = nextValue(&index, build_info);
42 const lld_include_dir = nextValue(&index, build_info);
43 const lld_libraries = nextValue(&index, build_info);
4244
4345 var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
4446 exe.setBuildMode(mode);
4547 exe.addIncludeDir("src");
4648 exe.addIncludeDir(cmake_binary_dir);
47 addCppLib(b, exe, cmake_binary_dir, "libzig_cpp");
48 addCppLib(b, exe, cmake_binary_dir, "libembedded_lld_elf");
49 addCppLib(b, exe, cmake_binary_dir, "libembedded_lld_coff");
50 addCppLib(b, exe, cmake_binary_dir, "libembedded_lld_lib");
49 addCppLib(b, exe, cmake_binary_dir, "zig_cpp");
50 if (lld_include_dir.len != 0) {
51 exe.addIncludeDir(lld_include_dir);
52 var it = mem.split(lld_libraries, ";");
53 while (it.next()) |lib| {
54 exe.addObjectFile(lib);
55 }
56 } else {
57 addCppLib(b, exe, cmake_binary_dir, "embedded_lld_elf");
58 addCppLib(b, exe, cmake_binary_dir, "embedded_lld_coff");
59 addCppLib(b, exe, cmake_binary_dir, "embedded_lld_lib");
60 }
5161 dependOnLib(exe, llvm);
5262
5363 if (!exe.target.isWindows()) {
5464 const libstdcxx_path_padded = b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"});
55 const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\n").next();
65 const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next();
5666 exe.addObjectFile(libstdcxx_path);
5767
5868 exe.linkSystemLibrary("pthread");
......@@ -114,8 +124,9 @@ fn dependOnLib(lib_exe_obj: &std.build.LibExeObjStep, dep: &const LibraryDep) {
114124}
115125
116126fn addCppLib(b: &Builder, lib_exe_obj: &std.build.LibExeObjStep, cmake_binary_dir: []const u8, lib_name: []const u8) {
127 const lib_prefix = if (lib_exe_obj.target.isWindows()) "" else "lib";
117128 lib_exe_obj.addObjectFile(%%os.path.join(b.allocator, cmake_binary_dir, "zig_cpp",
118 b.fmt("{}{}", lib_name, lib_exe_obj.target.libFileExt())));
129 b.fmt("{}{}{}", lib_prefix, lib_name, lib_exe_obj.target.libFileExt())));
119130}
120131
121132const LibraryDep = struct {
......@@ -150,7 +161,7 @@ fn findLLVM(b: &Builder) -> ?LibraryDep {
150161 .libdirs = ArrayList([]const u8).init(b.allocator),
151162 };
152163 {
153 var it = mem.split(libs_output, " \n");
164 var it = mem.split(libs_output, " \r\n");
154165 while (it.next()) |lib_arg| {
155166 if (mem.startsWith(u8, lib_arg, "-l")) {
156167 %%result.system_libs.append(lib_arg[2..]);
......@@ -164,7 +175,7 @@ fn findLLVM(b: &Builder) -> ?LibraryDep {
164175 }
165176 }
166177 {
167 var it = mem.split(includes_output, " \n");
178 var it = mem.split(includes_output, " \r\n");
168179 while (it.next()) |include_arg| {
169180 if (mem.startsWith(u8, include_arg, "-I")) {
170181 %%result.includes.append(include_arg[2..]);
......@@ -174,7 +185,7 @@ fn findLLVM(b: &Builder) -> ?LibraryDep {
174185 }
175186 }
176187 {
177 var it = mem.split(libdir_output, " \n");
188 var it = mem.split(libdir_output, " \r\n");
178189 while (it.next()) |libdir| {
179190 if (mem.startsWith(u8, libdir, "-L")) {
180191 %%result.libdirs.append(libdir[2..]);
......@@ -310,3 +321,11 @@ pub fn installStdLib(b: &Builder) {
310321 b.installFile(src_path, dest_path);
311322 }
312323}
324
325fn nextValue(index: &usize, build_info: []const u8) -> []const u8 {
326 const start = *index;
327 while (build_info[*index] != '\n' and build_info[*index] != '\r') : (*index += 1) { }
328 const result = build_info[start..*index];
329 *index += 1;
330 return result;
331}
src-self-hosted/c.zig+1
......@@ -1,4 +1,5 @@
11pub use @cImport({
2 @cInclude("inttypes.h");
23 @cInclude("config.h");
34 @cInclude("zig_llvm.h");
45});
src/config.h.in+2
......@@ -27,5 +27,7 @@
2727// Used for communicating build information to self hosted build.
2828#define ZIG_CMAKE_BINARY_DIR "@CMAKE_BINARY_DIR@"
2929#define ZIG_CXX_COMPILER "@CMAKE_CXX_COMPILER@"
30#define ZIG_LLD_INCLUDE_PATH "@LLD_INCLUDE_DIRS@"
31#define ZIG_LLD_LIBRARIES "@LLD_LIBRARIES@"
3032
3133#endif
src/link.cpp+4-2
......@@ -426,8 +426,7 @@ static void construct_linker_job_coff(LinkJob *lj) {
426426 if (g->is_static) {
427427 Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str);
428428 lj->args.append(buf_ptr(cmt_lib_name));
429 }
430 else {
429 } else {
431430 Buf *msvcrt_lib_name = buf_sprintf("msvcrt%s.lib", d_str);
432431 lj->args.append(buf_ptr(msvcrt_lib_name));
433432 }
......@@ -452,6 +451,9 @@ static void construct_linker_job_coff(LinkJob *lj) {
452451 // }
453452 //}
454453 //lj->args.append(get_libc_static_file(g, "crtbegin.o"));
454
455 // msvcrt depends on kernel32
456 lj->args.append("kernel32.lib");
455457 } else {
456458 lj->args.append("-NODEFAULTLIB");
457459 if (!is_library) {
src/main.cpp+1-1
......@@ -267,7 +267,7 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
267267
268268int main(int argc, char **argv) {
269269 if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {
270 printf("%s\n%s\n", ZIG_CMAKE_BINARY_DIR, ZIG_CXX_COMPILER);
270 printf("%s\n%s\n%s\n%s\n", ZIG_CMAKE_BINARY_DIR, ZIG_CXX_COMPILER, ZIG_LLD_INCLUDE_PATH, ZIG_LLD_LIBRARIES);
271271 return 0;
272272 }
273273
std/buffer.zig+5-4
......@@ -112,11 +112,12 @@ pub const Buffer = struct {
112112 // TODO: remove, use OutStream for this
113113 pub fn appendByteNTimes(self: &Buffer, byte: u8, count: usize) -> %void {
114114 var prev_size: usize = self.len();
115 %return self.resize(prev_size + count);
115 const new_size = prev_size + count;
116 %return self.resize(new_size);
116117
117 var i: usize = 0;
118 while (i < count) : (i += 1) {
119 self.list.items[prev_size + i] = byte;
118 var i: usize = prev_size;
119 while (i < new_size) : (i += 1) {
120 self.list.items[i] = byte;
120121 }
121122 }
122123
std/os/index.zig+2-2
......@@ -851,7 +851,7 @@ pub fn makePath(allocator: &Allocator, full_path: []const u8) -> %void {
851851 // march end_index backward until next path component
852852 while (true) {
853853 end_index -= 1;
854 if (resolved_path[end_index] == '/')
854 if (os.path.isSep(resolved_path[end_index]))
855855 break;
856856 }
857857 continue;
......@@ -864,7 +864,7 @@ pub fn makePath(allocator: &Allocator, full_path: []const u8) -> %void {
864864 // march end_index forward until next path component
865865 while (true) {
866866 end_index += 1;
867 if (end_index == resolved_path.len or resolved_path[end_index] == '/')
867 if (end_index == resolved_path.len or os.path.isSep(resolved_path[end_index]))
868868 break;
869869 }
870870 }
std/os/path.zig+8
......@@ -22,6 +22,14 @@ pub const delimiter = if (is_windows) delimiter_windows else delimiter_posix;
2222
2323const is_windows = builtin.os == builtin.Os.windows;
2424
25pub fn isSep(byte: u8) -> bool {
26 if (is_windows) {
27 return byte == '/' or byte == '\\';
28 } else {
29 return byte == '/';
30 }
31}
32
2533/// Naively combines a series of paths with the native path seperator.
2634/// Allocates memory for the result, which must be freed by the caller.
2735pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {