From d008e209e7446311bda1b555284978dfabb91308 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 4 Jan 2018 15:30:22 -0500 Subject: [PATCH] self-hosted compiler works on macos --- README.md | 3 +-- build.zig | 4 +++- ci/travis_osx_script | 15 +-------------- std/c/darwin.zig | 2 ++ std/os/index.zig | 21 +++++++++++++++++---- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a05d8a1a7..084d62a24 100644 --- a/README.md +++ b/README.md @@ -154,8 +154,7 @@ make install `ZIG_LIBC_LIB_DIR` and `ZIG_LIBC_STATIC_LIB_DIR` are unused. ``` -brew install cmake -brew install llvm@5 +brew install cmake llvm@5 brew outdated llvm@5 || brew upgrade llvm@5 mkdir build cd build diff --git a/build.zig b/build.zig index d78a9de37..b8b47cda2 100644 --- a/build.zig +++ b/build.zig @@ -66,12 +66,14 @@ pub fn build(b: &Builder) { } dependOnLib(exe, llvm); - if (!exe.target.isWindows()) { + if (exe.target.getOs() == builtin.Os.linux) { 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"); + } else if (exe.target.isDarwin()) { + exe.linkSystemLibrary("c++"); } exe.linkSystemLibrary("c"); diff --git a/ci/travis_osx_script b/ci/travis_osx_script index a55132395..db3992aa8 100755 --- a/ci/travis_osx_script +++ b/ci/travis_osx_script @@ -9,17 +9,4 @@ cmake .. -DCMAKE_PREFIX_PATH=/usr/local/opt/llvm@5/ -DCMAKE_INSTALL_PREFIX=$(pwd make VERBOSE=1 make install -# TODO: we run the tests separately because when run all together there is some -# mysterious issue where after N child process spawns it crashes. I've been -# unable to reproduce the issue on my macbook - it only happens on Travis. -# ./zig build --build-file ../build.zig test - -./zig build --build-file ../build.zig test-behavior --verbose -./zig build --build-file ../build.zig test-std --verbose -./zig build --build-file ../build.zig test-compiler-rt --verbose -./zig build --build-file ../build.zig test-compare-output --verbose -./zig build --build-file ../build.zig test-build-examples --verbose -./zig build --build-file ../build.zig test-compile-errors --verbose -./zig build --build-file ../build.zig test-asm-link --verbose -./zig build --build-file ../build.zig test-debug-safety --verbose -./zig build --build-file ../build.zig test-translate-c --verbose +./zig build --build-file ../build.zig test diff --git a/std/c/darwin.zig b/std/c/darwin.zig index 433463fde..006dcc066 100644 --- a/std/c/darwin.zig +++ b/std/c/darwin.zig @@ -1,4 +1,6 @@ extern "c" fn __error() -> &c_int; +pub extern "c" fn _NSGetExecutablePath(buf: &u8, bufsize: &u32) -> c_int; + pub use @import("../os/darwin_errno.zig"); diff --git a/std/os/index.zig b/std/os/index.zig index 96d374503..952108ffc 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -1553,10 +1553,12 @@ pub fn openSelfExe() -> %io.File { pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 { switch (builtin.os) { Os.linux => { - @compileError("TODO: selfExePath for linux"); + // If the currently executing binary has been deleted, + // the file path looks something like `/a/b/c/exe (deleted)` + return readLink(allocator, "/proc/self/exe"); }, Os.windows => { - var out_path = %return Buffer.initSize(allocator, 256); + var out_path = %return Buffer.initSize(allocator, 0xff); %defer out_path.deinit(); while (true) { const dword_len = %return math.cast(windows.DWORD, out_path.len()); @@ -1571,9 +1573,20 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 { out_path.shrink(copied_amt); return out_path.toOwnedSlice(); } - %return out_path.resize(out_path.len() * 2); + const new_len = (%return math.shlExact(out_path.len(), 1)) | 0b1; + %return out_path.resize(new_len); } }, + Os.darwin, Os.macosx, Os.ios => { + var u32_len: u32 = 0; + const ret1 = c._NSGetExecutablePath(undefined, &u32_len); + assert(ret1 != 0); + const bytes = %return allocator.alloc(u8, u32_len); + %defer allocator.free(bytes); + const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len); + assert(ret2 == 0); + return bytes; + }, else => @compileError("Unsupported OS"), } } @@ -1592,7 +1605,7 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 { const dir = path.dirname(full_exe_path); return allocator.shrink(u8, full_exe_path, dir.len); }, - Os.windows => { + Os.windows, Os.darwin, Os.macosx, Os.ios => { const self_exe_path = %return selfExePath(allocator); %defer allocator.free(self_exe_path); const dirname = os.path.dirname(self_exe_path);