Commit d008e209e7

Andrew Kelley <superjoe30@gmail.com>
2018-01-04 21:30:22
self-hosted compiler works on macos
1 parent e1c03d9
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
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");
 
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);
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");
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