Skip to content

Commit

Permalink
self-hosted compiler works on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Jan 4, 2018
1 parent e1c03d9 commit d008e20
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
3 changes: 1 addition & 2 deletions README.md
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion build.zig
Expand Up @@ -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");
Expand Down
15 changes: 1 addition & 14 deletions ci/travis_osx_script
Expand Up @@ -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
2 changes: 2 additions & 0 deletions 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");

Expand Down
21 changes: 17 additions & 4 deletions std/os/index.zig
Expand Up @@ -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());
Expand All @@ -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"),
}
}
Expand All @@ -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);
Expand Down

0 comments on commit d008e20

Please sign in to comment.