Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osx: error: unable to create target: 'Unable to find target for this triple (no targets are registered)' #5055

Closed
timotheecour opened this issue Apr 15, 2020 · 17 comments
Labels
Milestone

Comments

@timotheecour
Copy link
Contributor

timotheecour commented Apr 15, 2020

Summary of this issue


on OSX 10.15.2 (19C57)
brew install zig
zig version
0.6.0

zig build-exe --c-source thelloc.c --library c

error: unable to create target: 'Unable to find target for this triple (no targets are registered)'
1 error generated.

The following command failed:
/usr/local/Cellar/zig/0.6.0/bin/zig clang -c -nostdinc -fno-spell-checking -target x86_64-unknown-macosx-gnu -isystem /usr/local/Cellar/zig/0.6.0/lib/zig/include -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Xclang -target-cpu -Xclang skylake -Xclang -target-feature -Xclang -3dnow,-3dnowa,+64bit,+adx,+aes,+avx,+avx2,-avx512bf16,-avx512bitalg,-avx512bw,-avx512cd,-avx512dq,-avx512er,-avx512f,-avx512ifma,-avx512pf,-avx512vbmi,-avx512vbmi2,-avx512vl,-avx512vnni,-avx512vp2intersect,-avx512vpopcntdq,+bmi,+bmi2,-branchfusion,-cldemote,+clflushopt,-clwb,-clzero,+cmov,+cx16,+cx8,-enqcmd,+ermsb,+f16c,-false-deps-lzcnt-tzcnt,+false-deps-popcnt,-fast-11bytenop,-fast-15bytenop,-fast-bextr,+fast-gather,-fast-hops,-fast-lzcnt,+fast-scalar-fsqrt,-fast-scalar-shift-masks,+fast-shld-rotate,+fast-variable-shuffle,+fast-vector-fsqrt,-fast-vector-shift-masks,+fma,-fma4,+fsgsbase,+fxsr,-gfni,-idivl-to-divb,+idivq-to-divl,+invpcid,-lea-sp,-lea-uses-ag,-lwp,+lzcnt,+macrofusion,+merge-to-threeway-branch,+mmx,+movbe,-movdir64b,-movdiri,-mpx,-mwaitx,+nopl,-pad-short-functions,+pclmul,-pconfig,-pku,+popcnt,-prefer-128-bit,-prefer-256-bit,-prefer-mask-registers,-prefetchwt1,+prfchw,-ptwrite,-rdpid,+rdrnd,+rdseed,-retpoline,-retpoline-external-thunk,-retpoline-indirect-branches,-retpoline-indirect-calls,+rtm,+sahf,+sgx,-sha,-shstk,+slow-3ops-lea,-slow-incdec,-slow-lea,-slow-pmaddwd,-slow-pmulld,-slow-shld,-slow-two-mem-ops,-slow-unaligned-mem-16,-slow-unaligned-mem-32,-soft-float,+sse,-sse-unaligned-mem,+sse2,+sse3,+sse4.1,+sse4.2,-sse4a,+ssse3,-tbm,-use-aa,-use-glm-div-sqrt-costs,-vaes,-vpclmulqdq,+vzeroupper,-waitpkg,-wbnoinvd,+x87,-xop,+xsave,+xsavec,+xsaveopt,+xsaves -MD -MV -MF zig-cache/tmp/twKKHmKNIRM8-thelloc.o.d -g -fno-omit-frame-pointer -fsanitize=undefined -fsanitize-trap=undefined -D_DEBUG -Og -fstack-protector-strong --param ssp-buffer-size=4 -fPIC -o zig-cache/tmp/twKKHmKNIRM8-thelloc.o thelloc.c
// thelloc.c
#include <stdio.h>
int main (int argc, char *argv[]) {
  printf("hello\n");
  return 0;
}

likewise if I do nim c -f --cc:zig hello.nim (which was my original use case, see nim-lang/Nim#13757)

note

  • which clang
    /usr/bin/clang

  • clang -v
    Apple clang version 11.0.3 (clang-1103.0.32.29)
    Target: x86_64-apple-darwin19.2.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin

  • brew test zig works but that doesnt' help

  • I also tried specifying a target explicitly via -target ... but nothing worked

@ghost
Copy link

ghost commented Apr 15, 2020

What if you try to download zig from the website and try with that?

@timotheecour
Copy link
Contributor Author

timotheecour commented Apr 15, 2020

great,

here's what works:

  • download and unzip https://github.com/ziglang/zig/releases/download/0.6.0/zig-macos-x86_64-0.6.0.tar.xz
  • export PATH=/Users/timothee/Downloads/zig-macos-x86_64-0.6.0/:$PATH
  • zig build-exe --c-source /Users/timothee/git_clone//nim//timn/src/timn/examples/thelloc.c --library c
  • nim c -f --cc:zig -d:nimEmulateOverflowChecks $timn_D/tests/nim/all/t10536.nim
  • zig build-exe -target x86_64-windows-gnu --c-source /Users/timothee/git_clone//nim//timn/src/timn/examples/thelloc.c --library c

here's what doesn't work: brew zig

  • brew install zig
  • brew install --build-from-source zig (after brew uninstall zig)

here's what doesn't work: cross compile from nim

  • nim c -f --cc:zig --zig.options.linker:"-target x86_64-windows-gnu" --zig.options.always:"-target x86_64-windows-gnu" $timn_D/tests/nim/all/t10536.nim
    fatal error: 'sys/mman.h' file not found

@ghost
Copy link

ghost commented Apr 15, 2020

@timotheecour about the last one which doesn't work (about cross-compiling)
#4897

@andrewrk
Copy link
Member

here's what doesn't work: brew zig

This is a homebrew issue and you should file it on the homebrew issue tracker

@andrewrk
Copy link
Member

* fatal error: 'sys/mman.h' file not found

The next step to troubleshooting this one is to try to reproduce it without nim

@timotheecour
Copy link
Contributor Author

timotheecour commented Apr 15, 2020

This is a homebrew issue and you should file it on the homebrew issue tracker

will do; EDIT: Homebrew/homebrew-core#53198

The next step to troubleshooting this one is to try to reproduce it without nim

will do

but please keep this issue open for now :)

@andrewrk
Copy link
Member

but please keep this issue open for now :)

Of course :)

Generally we keep issues open in the zig issue tracker for upstream/downstream issues, even if we're waiting on a third party to change something.

@timotheecour
Copy link
Contributor Author

@andrewrk it appears instructions to build from source in https://github.com/ziglang/zig don't work and have the same issue, which is why the homebrew formuala also fails: https://github.com/Homebrew/homebrew-core/blob/master/Formula/zig.rb

so this seems like a zig issue, not a homebrew issue:

git clone https://github.com/ziglang/zig
cd zig
brew install cmake llvm
brew outdated llvm || brew upgrade llvm
mkdir build
cd build
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DZIG_PREFER_CLANG_CPP_DYLIB=ON
make install # i did instead `make install DESTDIR=/tmp/d01` here

/tmp/d01/Users/timothee/git_clone/zig/build/bin/zig build-exe --c-source /Users/timothee/git_clone//nim//timn/src/timn/examples/thelloc.c --library c
error: unable to create target: 'Unable to find target for this triple (no targets are registered)'

@timotheecour
Copy link
Contributor Author

timotheecour commented Apr 16, 2020

@andrewrk update:

# note: refer to upstream instructions
...
CACHE_BASENAME="llvm+clang+lld-$LLVMVER-$ARCH-macosx-gcc9-release"
wget -nv "https://ziglang.org/builds/$CACHE_BASENAME.tar.xz"
tar xf "$CACHE_BASENAME.tar.xz"
cd $ZIGDIR
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$PREFIX -DCMAKE_INSTALL_PREFIX=$(pwd)/release -DZIG_STATIC=ON
make $JOBS install

however, when I don't use pre-built llvm+friends (obviously a non-starter for many cases including homebrew), nothing i tried works:

but even after fixing those paths nothing works:

./release2/bin/llvm-config --libnames
llvm-config: error: component libraries and shared library
llvm-config: error: missing: /Users/timothee/build/D20200415T142759_1/llvm-project-10.0.0/build_gcc9/Release/lib/libgtest.a
llvm-config: error: missing: /Users/timothee/build/D20200415T142759_1/llvm-project-10.0.0/build_gcc9/Release/lib/libgtest_main.a
llvm-config: error: missing: /Users/timothee/build/D20200415T142759_1/llvm-project-10.0.0/build_gcc9/Release/lib/libLLVMTestingSupport.a

if I manually copy those 3 missing libs from build output to release dir (as hinted here http://lists.llvm.org/pipermail/llvm-bugs/2017-June/056284.html), and rerun building zig, it now gives:

Undefined symbols for architecture x86_64:
  "_Z3_ast_to_string", referenced from:
      (anonymous namespace)::Z3Expr::print(llvm::raw_ostream&) const in libLLVMSupport.a(Z3Solver.cpp.o)

which is odd

building without -DZIG_STATIC=ON also doesn't work

I tried different combinations of -DZIG_PREFER_CLANG_CPP_DYLIB=ON and -DZIG_STATIC=ON, or using homebrew built llvm, nothing works.

[update] building (locally) using exact same instructions as https://github.com/ziglang/zig/blob/master/ci/azure/macos_script#L18 (with HAVE_CACHE="false") fails

I'm not running into the above mentioned libLLVMTestingSupport.a issue but I am running again into the other one:

Undefined symbols for architecture x86_64:
  "_Z3_ast_to_string", referenced from:
      (anonymous namespace)::Z3Expr::print(llvm::raw_ostream&) const in libLLVMSupport.a(Z3Solver.cpp.o)
      llvm::SMTExpr::dump() const in libLLVMSupport.a(Z3Solver.cpp.o)

@timotheecour
Copy link
Contributor Author

timotheecour commented Apr 16, 2020

EDIT: finally i can build from source a working zig, but via gcc only:

gcc: works

I follow instructions from https://github.com/ziglang/zig/blob/master/ci/azure/macos_script#L18 (with HAVE_CACHE="false" to avoid prebuilt libs) and change

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 27bc6a04f..889eff11f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -359,14 +359,17 @@ endif()

 if(ZIG_STATIC)
     if(APPLE)
-        set(EXE_LDFLAGS "${EXE_LDFLAGS} -static-libgcc -static-libstdc++")
+        # set(EXE_LDFLAGS "${EXE_LDFLAGS} -static-libgcc -static-libstdc++")
+        set(EXE_LDFLAGS "${EXE_LDFLAGS} -static-libgcc -static-libstdc++ -lz3 -L/usr/local/opt/z3/lib ")
     elseif(MINGW)
         set(EXE_LDFLAGS "${EXE_LDFLAGS} -static-libgcc -static-libstdc++ -Wl,-Bstatic, -lwinpthread -lz3 -lz -lgomp")
     elseif(NOT MSVC)
         set(EXE_LDFLAGS "${EXE_LDFLAGS} -static")
     endif()
 else()
-    if(MINGW)
+    if(APPLE)
+        set(EXE_LDFLAGS "${EXE_LDFLAGS} -lz3 -L/usr/local/opt/z3/lib ")
+    elseif(MINGW)
         set(EXE_LDFLAGS "${EXE_LDFLAGS}")
     endif()
 endif()

and then it works both with and without -DZIG_STATIC=ON (but otool -L for -DZIG_STATIC=ON shows /usr/local/opt/z3/lib/libz3.dylib in my setup because of -lz3 -L/usr/local/opt/z3/lib, but linking against static would fail due to gcc vs clang abi issue)

clang: gives non-working zig

this is painful though as it requires building llvm+friends from source using gcc; isn't there a way to use clang? if not I don't see how this could be packaged in homebrew easily (which builds llvm via clang)

EDIT: i tried this:
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$PREFIX -DCMAKE_INSTALL_PREFIX=$(pwd)/release -DZIG_STATIC=ON -DZIG_PREFER_CLANG_CPP_DYLIB=ON
and also modified

if(ZIG_STATIC)                                                                                                                                                                                                                                                                                  llvm: add polly gpgpu and make rtti optional (https://github.com/Homebrew/homebrew-core/pull/28340)
    if(APPLE)
      set(EXE_LDFLAGS "${EXE_LDFLAGS} -lz3 -L/usr/local/opt/z3/lib ") # if clang
...

and it does build zig, but then has same issue as top post

@andrewrk
Copy link
Member

This is a problem with homebrew's LLVM/Clang. It's missing targets. There is nothing zig can do about this. What your system is claiming to fulfill Zig's dependency actually does not fulfill Zig's dependency.

And so the homebrew zig package as well, is broken. Zig is not intended to be linked against an LLVM/Clang that is missing targets.

Your options are to build llvm/clang/lld from source, download a prebuilt tarball, or convince homebrew to improve their packages.

@andrewrk andrewrk added this to the 0.7.0 milestone Apr 17, 2020
@andrewrk andrewrk pinned this issue Apr 17, 2020
@timotheecour
Copy link
Contributor Author

timotheecour commented Apr 17, 2020

@andrewrk
TLDR: no, it's a zig bug IMO.

This is a problem with homebrew's LLVM/Clang. It's missing targets. There is nothing zig can do about this

I don't think this is the case, and I think it's a bug with zig instead.

  • zig targets works as expected
  • --print-supported-cpus (which is reporting llvm::TargetRegistry::lookupTarget + friends) shows x86_64-unknown-macosx-gnu and works as expected:
    zig build-exe --c-source --print-supported-cpus thelloc.c shows:
clang version 10.0.0 Target: x86_64-unknown-macosx-gnu Thread model: posix InstalledDir: /usr/bin Available CPUs for this target: amdfam10 (etc)

'Unable to find target for this triple (no targets are registered)'

this actually means there are 0 targets (not just missing ones but 0). However, when I instrument zig code, I find that:
when I run zig build-exe --c-source --print-supported-cpus thelloc.c:

  • ./src/zig_clang_cc1_main.cpp cc1_main gets eventually called
  • right before Success = ExecuteCompilerInvocation(Clang.get());, instrumenting zig code to print PrintSupportedCPUs shows valid targets
  • then zig binary invokes itself in a subprocess, and when it invokes llvm::TargetRegistry::lookupTarget that fails.
  • so the bug could be that zig fails to properly pass arguments to clang in that subprocess; note that the arguments are passed (IIUC) via bool Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv, Diags);

proof: see lldb stacktrace here: https://gist.github.com/timotheecour/cce51bf06f3b12b1db2b15f4cdc31ef5

Your options are to build llvm/clang/lld from source, download a prebuilt tarball, or convince homebrew to improve their packages.

no, as I explained here #5055 (comment) I have tried building llvm/clang/lld from source using clang as compiler (using same approach as I did for building llvm/clang/lld from source using gcc as compiler), and that gave exactly same errors

@andrewrk
Copy link
Member

andrewrk commented Apr 17, 2020

This diff makes zig link against libLLVM.dylib, just like the (working) clang-10 binary from the brew package.

diff --git a/cmake/Findclang.cmake b/cmake/Findclang.cmake
index 5f5bef820..534d740c0 100644
--- a/cmake/Findclang.cmake
+++ b/cmake/Findclang.cmake
@@ -18,7 +18,7 @@ find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
 )
 
 if(ZIG_PREFER_CLANG_CPP_DYLIB)
-  find_library(CLANG_CPP_DYLIB
+  find_library(CLANG_LIBRARIES
     NAMES
       clang-cpp-10.0
       clang-cpp100
@@ -31,9 +31,7 @@ if(ZIG_PREFER_CLANG_CPP_DYLIB)
   )
 endif()
 
-if(CLANG_CPP_DYLIB)
-  set(CLANG_LIBRARIES ${CLANG_CPP_DYLIB})
-else()
+if(NOT CLANG_LIBRARIES)
   macro(FIND_AND_ADD_CLANG_LIB _libname_)
     string(TOUPPER ${_libname_} _prettylibname_)
     find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}
diff --git a/cmake/Findllvm.cmake b/cmake/Findllvm.cmake
index 7d6a4b04d..5228710dc 100644
--- a/cmake/Findllvm.cmake
+++ b/cmake/Findllvm.cmake
@@ -7,7 +7,30 @@
 # LLVM_LIBRARIES
 # LLVM_LIBDIRS
 
-if("${ZIG_TARGET_TRIPLE}" STREQUAL "native")
+find_path(LLVM_INCLUDE_DIRS NAMES llvm/IR/IRBuilder.h
+  PATHS
+    /usr/lib/llvm/10/include
+    /usr/lib/llvm-10/include
+    /usr/lib/llvm-10.0/include
+    /usr/local/llvm10/include
+    /usr/local/llvm100/include
+    /mingw64/include
+)
+
+if(ZIG_PREFER_CLANG_CPP_DYLIB)
+  find_library(LLVM_LIBRARIES
+    NAMES
+      LLVM-10.0
+      LLVM-10
+      LLVM-100
+      LLVM
+    PATHS
+      ${LLVM_LIBDIRS}
+      /usr/lib/llvm-10/lib
+      /usr/local/llvm10/lib
+      /usr/local/llvm100/lib
+  )
+elseif("${ZIG_TARGET_TRIPLE}" STREQUAL "native")
   find_program(LLVM_CONFIG_EXE
       NAMES llvm-config-10 llvm-config-10.0 llvm-config100 llvm-config10 llvm-config
       PATHS
@@ -106,11 +129,6 @@ if("${ZIG_TARGET_TRIPLE}" STREQUAL "native")
     string(REPLACE " " ";" LLVM_LIBDIRS "${LLVM_LIBDIRS_SPACES}")
   endif()
 
-  execute_process(
-      COMMAND ${LLVM_CONFIG_EXE} --includedir
-      OUTPUT_VARIABLE LLVM_INCLUDE_DIRS
-      OUTPUT_STRIP_TRAILING_WHITESPACE)
-
   set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS})
 
   if(NOT LLVM_LIBRARIES)
@@ -124,15 +142,6 @@ else()
   # to support more complicated setups. We also assume the experimental target
   # AVR is enabled.
 
-  find_path(LLVM_INCLUDE_DIRS NAMES llvm/IR/IRBuilder.h
-    PATHS
-        /usr/lib/llvm/10/include
-        /usr/lib/llvm-10/include
-        /usr/lib/llvm-10.0/include
-        /usr/local/llvm100/include
-        /usr/local/llvm10/include
-        /mingw64/include)
-
   macro(FIND_AND_ADD_LLVM_LIB _libname_)
     string(TOUPPER ${_libname_} _prettylibname_)
     find_library(LLVM_${_prettylibname_}_LIB NAMES ${_libname_}

However this causes, at runtime,

: CommandLine Error: Option 'mc-relax-all' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options

Because of static linking LLD combined with dynamic linking LLVM+Clang. The brew package does not provide a dynamic library for LLD, so Zig cannot solve this. Upstream LLVM needs to fix it, and homebrew needs to do one of these things:

  • patch LLD to allow being static linked into a binary that has dynamically linked LLVM+Clang, or
  • patch clang to not depend on polly, or
  • patch llvm-config to properly report polly with --libfiles. I believe this is what upstream is doing to fix the bug.

Welcome to C++ dependency hell.

I cannot reproduce your problem, following the directions to build from source. It works for me when I build everything from source.

My new suggestion is now reverting all the way back to #4799 (comment).

@Bo98, the ball is back in your court. I'm sending zig users to the homebrew issue tracker.

@andrewrk
Copy link
Member

I gave homebrew a suggestion of how to fix this: Homebrew/homebrew-core#53198 (comment)

@timotheecour
Copy link
Contributor Author

timotheecour commented Apr 18, 2020

@andrewrk
thanks!!! this helps a lot.

  • I can finally build a working zig from source using default clang built homebrew llvm (without having to rebuild llvm+friends using gcc)
as of 986aa42:
git clone https://github.com/ziglang/zig && cd zig
# apply patch from https://github.com/Homebrew/homebrew-core/issues/53198#issuecomment-615499405
mkdir build && cd build
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DCMAKE_INSTALL_PREFIX=$(pwd)/release
make -j install
./release/bin/zig build-exe --c-source /pathto/tzig.c

here's what doesn't work yet

  • adding -DZIG_STATIC=ON gives:
[ 97%] Linking CXX static library zig_cpp/libzigcompiler.a
[ 97%] Built target zigcompiler
Scanning dependencies of target zig0
[ 97%] Building CXX object CMakeFiles/zig0.dir/src/stage2.cpp.o
[ 98%] Building CXX object CMakeFiles/zig0.dir/src/main.cpp.o
[ 99%] Linking CXX executable zig0
clang: error: unsupported option '-static-libgcc'

=> clearly, '-static-libgcc' should be somthg else

  • IMO you should allow passing a flag to avoid having to patch eg -DZIG_WORKAROUND_5055 which would add the polly libs in target_link_libraries:
cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DCMAKE_INSTALL_PREFIX=$(pwd)/release -DZIG_WORKAROUND_5055

=> makes life easy and wouldn't break things like brew install zig --HEAD etc

  • is there a better way than hardcoding paths in cmake/Findclang.cmake eg /usr/lib/llvm-10.0/include ? (eg asking llvm-config or similar (yes i know it has some bugs, but it's working for simple things at least)

I cannot reproduce your problem, following the directions to build from source. It works for me when I build everything from source.

are you referring to no targets are registered ? looks like I wasn't the only one with this problem though (eg 1 upvote at least)
in any case above instructions now work

@Bo98
Copy link

Bo98 commented Apr 18, 2020

I'm happy to apply whatever workaround is needed in the zig formula until upstream makes changes.

Just to answer some points:

Because of static linking LLD combined with dynamic linking LLVM+Clang.

Ah, I did not realise Zig used LLD. I wouldn't have suggested using shared libraries at all for that case since, for some reason, LLD isn't available as a shared library at the moment.

The brew package does not provide a dynamic library for LLD

Neither does upstream. I argue they should, but that's out of my hands.

patch LLD to allow being static linked into a binary that has dynamically linked LLVM+Clang,

Not really trivial to do. The proper fix would be upstream providing a LLD shared library.

patch clang to not depend on polly

This would unfortunately just shift the problem. Some users would ask for Polly support to come back.

patch llvm-config to properly report polly with --libfiles. I believe this is what upstream is doing to fix the bug.

Yeah, upstream adjusting llvm-config seems like this is the easiest approach in the short term for 10.0.1. This is more a missing feature as llvm-config hasn't even reported Clang libraries ever before, let alone anything else like Polly, but there's no reason why it shouldn't.

If we are going to rely on llvm-config even more though instead of CMake, I will need to also patch the bugs it has on macOS. Hopefully that will make it for 10.0.1 too.

Any patching of llvm-config is likely to be non-trivial and involve several patches so it would likely end up being a case of waiting for 10.0.1 for the case of llvm-config. However, in the meantime before 10.0.1 arrives, I'm happy to apply some of the workarounds mentioned to the Homebrew formula. I'll revisit this tomorrow.

@data-man data-man mentioned this issue Aug 31, 2020
@andrewrk
Copy link
Member

LLVM 11 was released today and I just merged the llvm11 branch into master.

@andrewrk andrewrk unpinned this issue Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants