ziglang / zig-bootstrap

take off every zig
375 stars 90 forks source link

x86_64-macos-gnu #38

Closed andrewrk closed 3 years ago

andrewrk commented 4 years ago

This is failing because of https://github.com/ziglang/zig/issues/5828. The workaround is to use native instead of macos.

andrewrk commented 3 years ago

I see this when cross compiling from linux:

-- Check for working C compiler: /home/andy/dev/bootstrap-zig/out/host/bin/zig
-- Check for working C compiler: /home/andy/dev/bootstrap-zig/out/host/bin/zig -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/andy/dev/bootstrap-zig/out/host/bin/zig
-- Check for working CXX compiler: /home/andy/dev/bootstrap-zig/out/host/bin/zig -- broken
CMake Error at /nix/store/acw9iism5wr6mxaq54h306bbxnl27n63-cmake-3.16.3/share/cmake-3.16/Modules/CMakeTestCXXCompiler.cmake:53 (message):
  The C++ compiler

    "/home/andy/dev/bootstrap-zig/out/host/bin/zig"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/andy/dev/bootstrap-zig/out/build-llvm-x86_64-macos-gnu/CMakeFiles/CMakeTmp

    Run Build Command(s):/nix/store/dp6y0n9cba79wwc54n1brg7xbjsq5hka-gnumake-4.2.1/bin/make cmTC_6606a/fast && /nix/store/dp6y0n9cba79wwc54n1brg7xbjsq5hka-gnumake-4.2.1/bin/make -f CMakeFiles/cmTC_6606a.dir/build.make CMakeFiles/cmTC_6606a.dir/build
    make[1]: Entering directory '/home/andy/dev/bootstrap-zig/out/build-llvm-x86_64-macos-gnu/CMakeFiles/CMakeTmp'
    Building CXX object CMakeFiles/cmTC_6606a.dir/testCXXCompiler.cxx.o
    /home/andy/dev/bootstrap-zig/out/host/bin/zig  c++ -fno-sanitize=all -target x86_64-macos-gnu -mcpu=baseline     -o CMakeFiles/cmTC_6606a.dir/testCXXCompiler.cxx.o -c /home/andy/dev/bootstrap-zig/out/build-llvm-x86_64-macos-gnu/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
    Linking CXX executable cmTC_6606a
    /nix/store/acw9iism5wr6mxaq54h306bbxnl27n63-cmake-3.16.3/bin/cmake -E cmake_link_script CMakeFiles/cmTC_6606a.dir/link.txt --verbose=1
    /home/andy/dev/bootstrap-zig/out/host/bin/zig  c++ -fno-sanitize=all -target x86_64-macos-gnu -mcpu=baseline    -Wl,-headerpad_max_install_names   CMakeFiles/cmTC_6606a.dir/testCXXCompiler.cxx.o  -o cmTC_6606a 
    warning: unsupported linker arg: -headerpad_max_install_names
    In file included from /home/andy/dev/bootstrap-zig/out/host/lib/zig/libcxx/src/algorithm.cpp:9:
    In file included from /home/andy/dev/bootstrap-zig/out/host/lib/zig/libcxx/include/algorithm:643:
    In file included from /home/andy/dev/bootstrap-zig/out/host/lib/zig/libcxx/include/memory:681:
    In file included from /home/andy/dev/bootstrap-zig/out/host/lib/zig/libcxx/include/atomic:571:
    In file included from /home/andy/dev/bootstrap-zig/out/host/lib/zig/libcxx/include/__threading_support:27:
    In file included from /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/pthread.h:22:
    In file included from /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/sched.h:25:
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:139:3: error: 
    # error
      ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:145:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __DEV_T_TYPE __dev_t;        /* Type of device numbers.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:145:24: error: expected ';' after top level declarator
    __STD_TYPE __DEV_T_TYPE __dev_t;        /* Type of device numbers.  */
                           ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:146:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __UID_T_TYPE __uid_t;        /* Type of user identifications.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:147:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __GID_T_TYPE __gid_t;        /* Type of group identifications.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:148:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __INO_T_TYPE __ino_t;        /* Type of file serial numbers.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:149:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __INO64_T_TYPE __ino64_t;    /* Type of file serial numbers (LFS).*/
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:149:26: error: expected ';' after top level declarator
    __STD_TYPE __INO64_T_TYPE __ino64_t;    /* Type of file serial numbers (LFS).*/
                             ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:150:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __MODE_T_TYPE __mode_t;      /* Type of file attribute bitmasks.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:151:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __NLINK_T_TYPE __nlink_t;    /* Type of file link counts.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:151:26: error: expected ';' after top level declarator
    __STD_TYPE __NLINK_T_TYPE __nlink_t;    /* Type of file link counts.  */
                             ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:152:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __OFF_T_TYPE __off_t;        /* Type of file sizes and offsets.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:153:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __OFF64_T_TYPE __off64_t;    /* Type of file sizes and offsets (LFS).  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:153:26: error: expected ';' after top level declarator
    __STD_TYPE __OFF64_T_TYPE __off64_t;    /* Type of file sizes and offsets (LFS).  */
                             ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:154:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __PID_T_TYPE __pid_t;        /* Type of process identifications.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:155:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __FSID_T_TYPE __fsid_t;      /* Type of file system IDs.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:156:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __CLOCK_T_TYPE __clock_t;    /* Type of CPU usage counts.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:157:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __RLIM_T_TYPE __rlim_t;      /* Type for resource measurement.  */
    ^
    /home/andy/dev/bootstrap-zig/out/host/lib/zig/libc/include/generic-glibc/bits/types.h:158:1: error: unknown type name '__STD_TYPE'
    __STD_TYPE __RLIM64_T_TYPE __rlim64_t;  /* Type for resource measurement (LFS).  */
    ^
    fatal error: too many errors emitted, stopping now [-ferror-limit=]
    20 errors generated.
andrewrk commented 3 years ago

I get a bit further with https://github.com/ziglang/zig/pull/7035:

-- Install configuration: "Release"
-- Installing: /home/andy/dev/bootstrap-zig/out/host/bin/zig
-- Set runtime path of "/home/andy/dev/bootstrap-zig/out/host/bin/zig" to ""
-- Installing: /home/andy/dev/bootstrap-zig/out/host/lib
-- The ASM compiler identification is unknown
-- Found assembler: /home/andy/dev/bootstrap-zig/out/host/bin/zig
-- Warning: Did not find file Compiler/-ASM
-- clang project is enabled
-- clang-tools-extra project is disabled
-- compiler-rt project is disabled
-- debuginfo-tests project is disabled
-- libc project is disabled
-- libclc project is disabled
-- libcxx project is disabled
-- libcxxabi project is disabled
-- libunwind project is disabled
-- lld project is enabled
-- lldb project is disabled
-- mlir project is disabled
-- openmp project is disabled
-- parallel-libs project is disabled
-- polly project is disabled
-- pstl project is disabled
-- flang project is disabled
-- Native target architecture is X86
-- Threads enabled.
-- Doxygen disabled.
-- Go bindings disabled.
-- Could NOT find OCaml (missing: OCAMLFIND OCAML_VERSION OCAML_STDLIB_PATH) 
-- OCaml bindings disabled.
-- LLVM host triple: x86_64-unknown-linux-gnu
-- LLVM default target triple: x86_64-macos-gnu
-- Building with -fPIC
-- Constructing LLVMBuild project information
-- Setting native build dir to /home/andy/dev/bootstrap-zig/out/build-llvm-x86_64-macos-gnu-haswell/NATIVE
-- Targeting AArch64
-- Targeting AMDGPU
-- Targeting ARM
-- Targeting AVR
-- Targeting BPF
-- Targeting Hexagon
-- Targeting Lanai
-- Targeting Mips
-- Targeting MSP430
-- Targeting NVPTX
-- Targeting PowerPC
-- Targeting RISCV
-- Targeting Sparc
-- Targeting SystemZ
-- Targeting WebAssembly
-- Targeting X86
-- Targeting XCore
-- Clang version: 11.0.0
-- LLD version: 11.0.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/andy/dev/bootstrap-zig/out/build-llvm-x86_64-macos-gnu-haswell
[  0%] Built target LLVMDemangle
[  0%] Building CXX object lib/Support/CMakeFiles/LLVMSupport.dir/TimeProfiler.cpp.o
/home/andy/dev/bootstrap-zig/llvm/lib/Support/TimeProfiler.cpp:36:8: error: thread-local storage is not supported for the current target
static LLVM_THREAD_LOCAL TimeTraceProfiler *TimeTraceProfilerInstance = nullptr;
       ^
/home/andy/dev/bootstrap-zig/llvm/include/llvm/Support/Compiler.h:543:27: note: expanded from macro 'LLVM_THREAD_LOCAL'
#define LLVM_THREAD_LOCAL __thread
                          ^
1 error generated.
make[2]: *** [lib/Support/CMakeFiles/LLVMSupport.dir/build.make:1298: lib/Support/CMakeFiles/LLVMSupport.dir/TimeProfiler.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:8913: lib/Support/CMakeFiles/LLVMSupport.dir/all] Error 2
make: *** [Makefile:152: all] Error 2

With a native build (with ZIG_SYSTEM_LINKER_HACK=1) I got all the way to the end, and then failure:

[ 98%] Linking CXX shared module ../../../../../../lib/CheckerDependencyHandlingAnalyzerPlugin.dylib
warning: unsupported linker arg: -headerpad_max_install_names
warning: unsupported linker arg: -dead_strip
warning: unsupported linker arg: -exported_symbols_list
warning: unsupported linker arg: /Users/andy/dev/zig-bootstrap/out/build-llvm-x86_64-native-gnu-native/tools/clang/lib/Analysis/plugins/CheckerDependencyHandling/CheckerDependencyHandlingAnalyzerPlugin.exports
warning: unsupported linker arg: -flat_namespace
warning: unsupported linker arg: -undefined
warning: unsupported linker arg: suppress
[100%] Linking CXX shared module ../../../../../../lib/SampleAnalyzerPlugin.dylib
warning: unsupported linker arg: -headerpad_max_install_names
warning: unsupported linker arg: -dead_strip
warning: unsupported linker arg: -exported_symbols_list
warning: unsupported linker arg: /Users/andy/dev/zig-bootstrap/out/build-llvm-x86_64-native-gnu-native/tools/clang/lib/Analysis/plugins/SampleAnalyzer/SampleAnalyzerPlugin.exports
warning: unsupported linker arg: -flat_namespace
warning: unsupported linker arg: -undefined
warning: unsupported linker arg: suppress
warning: unexpected LD stderr: ld: warning: directory not found for option '-L/usr/local/lib64'
ld: warning: directory not found for option '-L/usr/lib/x86_64-macos-gnu'
ld: warning: directory not found for option '-L/lib64'
ld: warning: directory not found for option '-L/lib'
ld: warning: directory not found for option '-L/usr/lib64'
ld: warning: directory not found for option '-L/lib/x86_64-macos-gnu'
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

ld: warning: directory not found for option '-L/usr/local/lib64'
ld: warning: directory not found for option '-L/usr/lib/x86_64-macos-gnu'
ld: warning: directory not found for option '-L/lib64'
ld: warning: directory not found for option '-L/lib'
ld: warning: directory not found for option '-L/usr/lib64'
ld: warning: directory not found for option '-L/lib/x86_64-macos-gnu'
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
error: LDReportedFailure
make[2]: *** [lib/CheckerDependencyHandlingAnalyzerPlugin.dylib] Error 1
make[1]: *** [tools/clang/lib/Analysis/plugins/CheckerDependencyHandling/CMakeFiles/CheckerDependencyHandlingAnalyzerPlugin.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
warning: unexpected LD stderr: ld: warning: directory not found for option '-L/usr/local/lib64'
ld: warning: directory not found for option '-L/usr/lib/x86_64-macos-gnu'
ld: warning: directory not found for option '-L/lib64'
ld: warning: directory not found for option '-L/lib'
ld: warning: directory not found for option '-L/usr/lib64'
ld: warning: directory not found for option '-L/lib/x86_64-macos-gnu'
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

ld: warning: directory not found for option '-L/usr/local/lib64'
ld: warning: directory not found for option '-L/usr/lib/x86_64-macos-gnu'
ld: warning: directory not found for option '-L/lib64'
ld: warning: directory not found for option '-L/lib'
ld: warning: directory not found for option '-L/usr/lib64'
ld: warning: directory not found for option '-L/lib/x86_64-macos-gnu'
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
error: LDReportedFailure
make[2]: *** [lib/SampleAnalyzerPlugin.dylib] Error 1
make[1]: *** [tools/clang/lib/Analysis/plugins/SampleAnalyzer/CMakeFiles/SampleAnalyzerPlugin.dir/all] Error 2
make: *** [all] Error 2
daurnimator commented 3 years ago

error: thread-local storage is not supported for the current target

Not sure if this is helpful, but you classically get similar errors when compiling programs with an old OSX target. e.g. if you set MACOSX_DEPLOYMENT_TARGET to 10.5, then __thread will be missing (it is only available in 10.7 and above)

andrewrk commented 3 years ago

Nice, I believe that clue is going to lead us towards a solution:

So let me try a patch where we pass the option to clang.

andrewrk commented 3 years ago

With this patch, it's working :-)

diff --git a/src/Compilation.zig b/src/Compilation.zig
index 65fa296ca..5438bb742 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -613,6 +613,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
         cache.hash.addBytes(options.target.cpu.model.name);
         cache.hash.add(options.target.cpu.features.ints);
         cache.hash.add(options.target.os.tag);
+        cache.hash.add(options.target.os.getVersionRange());
         cache.hash.add(options.is_native_os);
         cache.hash.add(options.target.abi);
         cache.hash.add(ofmt);
@@ -642,7 +643,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
             hash.addOptionalBytes(root_pkg.root_src_directory.path);
             hash.add(valgrind);
             hash.add(single_threaded);
-            hash.add(options.target.os.getVersionRange());
             hash.add(dll_export_fns);
             hash.add(options.is_test);
             hash.add(options.is_compiler_rt_or_libc);
@@ -1855,6 +1855,33 @@ pub fn addCCArgs(
                 try argv.append("-Wno-pragma-pack");
             }

+            // Pass the proper -m<os>-version-min argument for darwin.
+            switch (target.os.tag) {
+                .macos => {
+                    const ver = target.os.version_range.semver.min;
+                    try argv.append(try std.fmt.allocPrint(arena, "-mmacos-version-min={d}.{d}.{d}", .{
+                        ver.major, ver.minor, ver.patch,
+                    }));
+                },
+                .ios, .tvos, .watchos => switch (target.cpu.arch) {
+                    .i386, .x86_64 => {
+                        const ver = target.os.version_range.semver.min;
+                        try argv.append(try std.fmt.allocPrint(
+                            arena,
+                            "-m{s}-simulator-version-min={d}.{d}.{d}",
+                            .{ @tagName(target.os.tag), ver.major, ver.minor, ver.patch },
+                        ));
+                    },
+                    else => {
+                        const ver = target.os.version_range.semver.min;
+                        try argv.append(try std.fmt.allocPrint(arena, "-m{s}-version-min={d}.{d}.{d}", .{
+                            @tagName(target.os.tag), ver.major, ver.minor, ver.patch,
+                        }));
+                    },
+                },
+                else => {},
+            }
+
             if (!comp.bin_file.options.strip) {
                 try argv.append("-g");
             }

It's still building, the next question is what happens when it gets to the link step.

andrewrk commented 3 years ago

It worked :D

[100%] Linking CXX executable zig
warning: unsupported linker arg: -headerpad_max_install_names
[100%] Built target zig
Install the project...
-- Install configuration: "Release"
-- Installing: /home/andy/dev/bootstrap-zig/out/zig-x86_64-macos-gnu-haswell/bin/zig
-- Installing: /home/andy/dev/bootstrap-zig/out/zig-x86_64-macos-gnu-haswell/lib

now to see if the executable is viable when copied over to a macos

andrewrk commented 3 years ago

OK now we're hitting https://github.com/ziglang/zig/issues/3295

kubkon commented 3 years ago

This one, for some weird reason, is hanging for me during second pass at the linking stage. Anyhow, @andrewrk if you've got the temp artefacts lying around, you might wanna retry this with latest Zig master since we've landed cross-compilation post-mortem LLD fixups. This should fix missing __tlv_boostrap dyld error message. It might reveal other problems with LLD generated final Zig binary though.

I'll give this one a go again tomorrow on x86_64 MBP.

kubkon commented 3 years ago

Good news, x86_64-macos-gnu fails the same way #44 does. Bad news, it's still the linker messing up the MachO when cross-compiling C++.

kubkon commented 3 years ago

For those tracking this issue, zld is now successfully able to bootstrap x86_64 macOS too, and the relevant changes are incoming in #56!