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

homebrew and llvm 10 packages in apt.llvm.org are broken with undefined reference to getPollyPluginInfo #4799

Closed
heidezomp opened this issue Mar 24, 2020 · 34 comments
Labels
upstream An issue with a third party project that Zig uses.
Milestone

Comments

@heidezomp
Copy link
Contributor

Building with the shiny new llvm10 fails:

[ 98%] Linking CXX executable zig0
/usr/lib/llvm-10/lib/libclangCodeGen.a(BackendUtil.cpp.o): In function `(anonymous namespace)::EmitAssemblyHelper::EmitAssemblyWithNewPassManager(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >)':
BackendUtil.cpp:(.text._ZN12_GLOBAL__N_118EmitAssemblyHelper30EmitAssemblyWithNewPassManagerEN5clang13BackendActionESt10unique_ptrIN4llvm17raw_pwrite_streamESt14default_deleteIS5_EE+0x16df): undefined reference to `getPollyPluginInfo()'
collect2: error: ld returned 1 exit status
CMakeFiles/zig0.dir/build.make:163: recipe for target 'zig0' failed

I have installed llvm10 from the official apt repository:

deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main

Might this be a problem with my installation?

@andrewrk
Copy link
Member

andrewrk commented Mar 24, 2020

It's a problem with apt.llvm.org. They're aware and will hopefully fix it soon. In the meantime, you'll have to use the same workaround that the CI uses, or build llvm+clang+lld 10 from source.


homebrew workaround

@andrewrk andrewrk added the upstream An issue with a third party project that Zig uses. label Mar 24, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Mar 24, 2020
@andrewrk andrewrk changed the title Building zig fails during linking zig0 llvm 10 packgaes in apt.llvm.org are broken with undefined reference to getPollyPluginInfo Mar 24, 2020
@andrewrk andrewrk changed the title llvm 10 packgaes in apt.llvm.org are broken with undefined reference to getPollyPluginInfo llvm 10 packages in apt.llvm.org are broken with undefined reference to getPollyPluginInfo Mar 24, 2020
@andrewrk
Copy link
Member

@yrashk
Copy link
Sponsor

yrashk commented Mar 26, 2020

I am having the same issue with an attempt to combine this LLVM 10 (release) PR in Nixpkgs NixOS/nixpkgs#83350 and the following change to zig's derivation:

diff --git a/pkgs/development/compilers/zig/default.nix b/pkgs/development/compilers/zig/default.nix
index 961e28da82e..ab43c862227 100644
--- a/pkgs/development/compilers/zig/default.nix
+++ b/pkgs/development/compilers/zig/default.nix
@@ -1,18 +1,18 @@
 { stdenv, fetchFromGitHub, cmake, llvmPackages, libxml2, zlib }:
 
 stdenv.mkDerivation rec {
-  version = "0.5.0";
+  version = "0d43543";
   pname = "zig";
 
   src = fetchFromGitHub {
     owner = "ziglang";
     repo = pname;
     rev = version;
-    sha256 = "0xyl0riakh6kwb3yvxihb451kqs4ai4q0aygqygnlb2rlr1dn1zb";
+    sha256 = "144igsf104dlwf8rvxg0dk6brlv73ywhq35r5nnljra81h52wzf9";
   };
 
   nativeBuildInputs = [ cmake ];
-  buildInputs = [ llvmPackages.clang-unwrapped llvmPackages.llvm libxml2 zlib ];
+  buildInputs = [ llvmPackages.clang-unwrapped llvmPackages.llvm llvmPackages.lld libxml2 zlib ];
 
   preBuild = ''
     export HOME=$TMPDIR;
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 30bc0af360e..f0ab0586955 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -15147,7 +15147,7 @@ in
   zmqpp = callPackage ../development/libraries/zmqpp { };
 
   zig = callPackage ../development/compilers/zig {
-    llvmPackages = llvmPackages_9;
+    llvmPackages = llvmPackages_10;
   };
 
   zimlib = callPackage ../development/libraries/zimlib { };

@yrashk
Copy link
Sponsor

yrashk commented Mar 28, 2020

I was able to devise a quick-and-dirty workaround to make it possible to build master with Nixpkgs (with NixOS/nixpkgs#83350 merged)

diff --git a/pkgs/development/compilers/zig/default.nix b/pkgs/development/compilers/zig/default.nix
index 961e28da82e..ed521a0e70a 100644
--- a/pkgs/development/compilers/zig/default.nix
+++ b/pkgs/development/compilers/zig/default.nix
@@ -1,18 +1,25 @@
 { stdenv, fetchFromGitHub, cmake, llvmPackages, libxml2, zlib }:
 
 stdenv.mkDerivation rec {
-  version = "0.5.0";
+  version = "547ba8eb203fa84d207eb38d8d64c0f7f3162101";
   pname = "zig";
 
   src = fetchFromGitHub {
     owner = "ziglang";
     repo = pname;
     rev = version;
-    sha256 = "0xyl0riakh6kwb3yvxihb451kqs4ai4q0aygqygnlb2rlr1dn1zb";
+    sha256 = "1qvaky2h4j0bss7qjcwiz7q3jhrwlhbjyjgg91zjp9rz5sx2a24k";
   };
 
   nativeBuildInputs = [ cmake ];
-  buildInputs = [ llvmPackages.clang-unwrapped llvmPackages.llvm libxml2 zlib ];
+  buildInputs = [ llvmPackages.clang-unwrapped llvmPackages.llvm llvmPackages.lld libxml2 zlib ];
+
+  patches = [ ./llvm10_poly.patch ] ;
+
+  postPatch = ''
+    export llvm_extras=-Wl,${llvmPackages.llvm}/lib/LLVMPolly.so
+    substituteAllInPlace CMakeLists.txt
+  '';
 
   preBuild = ''
     export HOME=$TMPDIR;
diff --git a/pkgs/development/compilers/zig/llvm10_poly.patch b/pkgs/development/compilers/zig/llvm10_poly.patch
new file mode 100644
index 00000000000..873c825dfbc
--- /dev/null
+++ b/pkgs/development/compilers/zig/llvm10_poly.patch
@@ -0,0 +1,10 @@
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 97608cddf..e451c0711 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -369,6 +369,7 @@ target_link_libraries(zig_cpp LINK_PUBLIC
+     ${CLANG_LIBRARIES}
+     ${LLD_LIBRARIES}
+     ${LLVM_LIBRARIES}
++    @llvm_extras@
+ )
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 30bc0af360e..f0ab0586955 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -15147,7 +15147,7 @@ in
   zmqpp = callPackage ../development/libraries/zmqpp { };
 
   zig = callPackage ../development/compilers/zig {
-    llvmPackages = llvmPackages_9;
+    llvmPackages = llvmPackages_10;
   };
 
   zimlib = callPackage ../development/libraries/zimlib { };

@josephg
Copy link

josephg commented Mar 30, 2020

I run into the same problem installing zig --HEAD from homebrew on macos:

 $ brew install zig --HEAD
Updating Homebrew...
==> Cloning https://github.com/ziglang/zig.git
Cloning into '/Users/josephg/Library/Caches/Homebrew/zig--git'...
Updating files: 100% (9367/9367), done.
==> Checking out branch master
Already on 'master'
Your branch is up to date with 'origin/master'.
==> cmake .
==> make install
Last 15 lines from /Users/josephg/Library/Logs/Homebrew/zig/02.make:
[ 98%] Building CXX object CMakeFiles/zig0.dir/src/stage2.cpp.o
/usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang++  -DNDEBUG -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names   CMakeFiles/zig0.dir/src/main.cpp.o CMakeFiles/zig0.dir/src/stage2.cpp.o  -o zig0 [...]
Undefined symbols for architecture x86_64:
  "getPollyPluginInfo()", referenced from:
      (anonymous namespace)::EmitAssemblyHelper::EmitAssemblyWithNewPassManager(clang::BackendAction, std::__1::unique_ptr<llvm::raw_pwrite_stream, std::__1::default_delete<llvm::raw_pwrite_stream> >) in libclangCodeGen.a(BackendUtil.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [zig0] Error 1
make[1]: *** [CMakeFiles/zig0.dir/all] Error 2
make: *** [all] Error 2

@fengb
Copy link
Contributor

fengb commented Mar 31, 2020

Homebrew fix: Homebrew/homebrew-core#52352

@yrashk
Copy link
Sponsor

yrashk commented Apr 1, 2020

LLVM 10 release is in Nixpkgs now NixOS/nixpkgs#83350

@josephg
Copy link

josephg commented Apr 2, 2020

:/ That homebrew fix didn't work (see the issue), and adding "-Wl,/usr/local/opt/llvm/lib/libPolly.so" gives me this error:

$ make 
Configuring zig version 0.5.0+f6d384450
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/josephg/3rdparty/zig/build
[ 76%] Built target embedded_softfloat
[ 80%] Built target zig_cpp
[ 82%] Built target opt_c_util
[ 97%] Built target zigcompiler
[ 98%] Linking CXX executable zig0
ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file '/usr/local/opt/llvm/lib/LLVMPolly.so' for architecture x86_64
clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [zig0] Error 1
make[1]: *** [CMakeFiles/zig0.dir/all] Error 2
make: *** [all] Error 2

EDIT

Linking to all the static versions of the library worked. In CMakeLists.txt:

target_link_libraries(zig_cpp LINK_PUBLIC
    ${CLANG_LIBRARIES}
    ${LLD_LIBRARIES}
    ${LLVM_LIBRARIES}
    "-Wl,/usr/local/opt/llvm/lib/libPolly.a"
    "-Wl,/usr/local/opt/llvm/lib/libPollyPPCG.a"
    "-Wl,/usr/local/opt/llvm/lib/libPollyISL.a"
)

@timmyjose
Copy link

timmyjose commented Apr 2, 2020

@josephg
Thank you so much! It has been frustrating waiting for the Homebrew team to fix the issue. I was making do with the latest binary from master. This should work nicely till that issue is fixed upstream. Cheers!

@andrewrk andrewrk changed the title llvm 10 packages in apt.llvm.org are broken with undefined reference to getPollyPluginInfo llvm 10 packages in apt.llvm.org and homebrew are broken with undefined reference to getPollyPluginInfo Apr 2, 2020
@andrewrk andrewrk pinned this issue Apr 6, 2020
@Bo98
Copy link

Bo98 commented Apr 9, 2020

I've been asked a few times about this over at Homebrew from people coming over from here. Just to make everyone aware, this is a comment I made on the matter before: Homebrew/homebrew-core#52352 (comment) (after the bullet points).

I've looked at the issue more since then, so let me clarify a few things:

  • This is not specific to Homebrew or apt.llvm.org.
  • LLVM links Polly into Clang libraries by default. This is not a "misconfiguration". It is intentional.
  • As such, Polly is a required dependency of clangCodeGen when Polly is built. If you link clangCodeGen you must link Polly.

To answer some questions I've been asked:

Why does Polly not automatically get linked when you link clangCodeGen?

Because clangCodeGen is a static library. This is an inherent attribute of static libraries.

Why does zig not detect to link Polly itself?

Because zig provides a manual list of what to link when it comes to Clang:

FIND_AND_ADD_CLANG_LIB(clangFrontendTool)
FIND_AND_ADD_CLANG_LIB(clangCodeGen)
FIND_AND_ADD_CLANG_LIB(clangFrontend)
FIND_AND_ADD_CLANG_LIB(clangDriver)
FIND_AND_ADD_CLANG_LIB(clangSerialization)
FIND_AND_ADD_CLANG_LIB(clangSema)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerFrontend)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCheckers)
FIND_AND_ADD_CLANG_LIB(clangStaticAnalyzerCore)
FIND_AND_ADD_CLANG_LIB(clangAnalysis)
FIND_AND_ADD_CLANG_LIB(clangASTMatchers)
FIND_AND_ADD_CLANG_LIB(clangAST)
FIND_AND_ADD_CLANG_LIB(clangParse)
FIND_AND_ADD_CLANG_LIB(clangSema)
FIND_AND_ADD_CLANG_LIB(clangBasic)
FIND_AND_ADD_CLANG_LIB(clangEdit)
FIND_AND_ADD_CLANG_LIB(clangLex)
FIND_AND_ADD_CLANG_LIB(clangARCMigrate)
FIND_AND_ADD_CLANG_LIB(clangRewriteFrontend)
FIND_AND_ADD_CLANG_LIB(clangRewrite)
FIND_AND_ADD_CLANG_LIB(clangCrossTU)
FIND_AND_ADD_CLANG_LIB(clangIndex)

How do you detect whether Polly should be linked?

Let CMake handle the linking work for you and use Clang's provided CMake modules. They can be found in <LLVM prefix>/lib/cmake/clang.

It handles what should be linked. For example, lib/cmake/clang/ClangTargets.cmake provides a target called clangCodeGen lists Polly in its INTERFACE_LINK_LIBRARIES:

# Create imported target clangCodeGen
add_library(clangCodeGen STATIC IMPORTED)

set_target_properties(clangCodeGen PROPERTIES
  INTERFACE_LINK_LIBRARIES "clangAnalysis;clangAST;clangASTMatchers;clangBasic;clangFrontend;clangLex;clangSerialization;LLVM;Polly"
)

There is a ClangConfig.cmake to allow the above to be used after a find_package - don't include the file directly.

A similar system for LLVM libraries also exists.

Is there another way?

Use the clang-cpp shared library instead of static libraries. Shared libraries avoid a lot of problems that static libraries have. There is a clang-cpp target in the CMake files too so you can combine the two solutions.

There's a big move in some of the community to stop using Clang static libraries and start using clang-cpp. For example, over at Fedora: https://fedoraproject.org/wiki/Changes/Stop-Shipping-Individual-Component-Libraries-In-clang-lib-Package. Fedora plan to remove Clang static libraries from the clang-libs package in Fedora 32.

What about llvm-config?

llvm-config is not for Clang libraries.

I strongly discourage using llvm-config. It is known to be buggy (borderline broken in fact) on macOS, and is not maintained very much upstream. Upstream strongly dissuades its use inside CMake as the CMake modules are much superior: https://github.com/llvm/llvm-project/blob/llvmorg-10.0.0/clang/CMakeLists.txt#L17-L21

@andrewrk
Copy link
Member

andrewrk commented Apr 9, 2020

Thanks for the clarification @Bo98. It's a bit of a problem that LLVM forces downstream projects to use cmake in order to properly link against it, but that's not Homebrew's fault. I'm looking into this and will attempt to make zig's cmake configuration compatible with Homebrew, and stop sending people your way. Thanks again for stopping by.

@andrewrk andrewrk changed the title llvm 10 packages in apt.llvm.org and homebrew are broken with undefined reference to getPollyPluginInfo llvm 10: undefined reference to getPollyPluginInfo Apr 9, 2020
@andrewrk andrewrk removed the upstream An issue with a third party project that Zig uses. label Apr 9, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.6.0 Apr 9, 2020
@Bo98
Copy link

Bo98 commented Apr 9, 2020

It's a bit of a problem that LLVM forces downstream projects

Yeah, it caught out Mesa too I heard.

They don't use CMake, so they had to go with the clang-cpp shared library option: https://github.com/mesa3d/mesa/commit/ff1a3a00cb37d84ab9a563f0aa241714876f56b4.

@andrewrk
Copy link
Member

I've opened #4992 to solve this. Now it's just the O(N) task of testing it on all the various systems and putting in workarounds to fix the breakage.

@andrewrk
Copy link
Member

@Bo98 I tried taking your advice and going full cmake-based configuration. That work is in #4992. It successfully built against the Homebrew package.

However, the apt.llvm.org packaging problem persists (and is reproduced by an LLVM dev), and there are other show-stopping problems with cmake-based config.

So I've made the much less invasive change: #4993
This also solved building against Homebrew on my macOS laptop, and I expect the CI tests to pass. So unless you have any objections, this is what you're going to see with the 0.6.0 release on Monday.

I'm not aware of any other patches or issues downstream, but please let me know and I would be happy to work with you on getting that patch count (and other unwanted workarounds) down to zero.

@andrewrk andrewrk changed the title llvm 10: undefined reference to getPollyPluginInfo llvm 10 packages in apt.llvm.org are broken with undefined reference to getPollyPluginInfo Apr 10, 2020
@timmyjose
Copy link

timmyjose commented Jun 23, 2020

Does this mean that we will have to wait for llvm to fix it in their next major release? Also, having read the relevant threads, will it still be possible to get a working build if I build clang+lld from source? I've been stuck at the last working revision (448f8c2eb8335e1c8fc0dd76c3ed728b80487a63) for some time now. :-)

@heidezomp
Copy link
Contributor Author

heidezomp commented Jul 2, 2020

As for the apt.llvm.org packages, I just got an update of llvm-toolchain-10 (1:10.0.1~++20200626113502+77d76b71d7d-1~exp1~20200626214123.189) that seems to fix linking to Polly; manually adding "-Wl,/usr/lib/llvm-10/lib/LLVMPolly.so" to the libraries in CMakeLists.txt isn't required anymore and a clean checkout/build of Zig now builds and runs fine on my system.

@andrewrk andrewrk reopened this Jul 3, 2020
andrewrk added a commit that referenced this issue Jul 3, 2020
to work around #4799 until a newer llvm version is released.
kubkon pushed a commit to kubkon/zig that referenced this issue Jul 3, 2020
kubkon pushed a commit to kubkon/zig that referenced this issue Jul 3, 2020
to work around ziglang#4799 until a newer llvm version is released.
@frett27
Copy link

frett27 commented Jul 5, 2020

Thank's @andrewrk , cmake -DZIG_WORKAROUND_POLLY_SO:STRING=/usr/lib/llvm-10/lib/LLVMPolly.so ..
Worked also for me on ubuntu 20.04 LTS

@jbsolomon-fw
Copy link

jbsolomon-fw commented Jul 13, 2020

EDIT: Never mind, it works using the macOS-specific workaround flag.

Doesn't work on macOS 10.15.4 using LLVM 10.0.0:

ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file '/usr/local/opt/llvm/lib/LLVMPolly.so' for architecture x86_64

@hazeycode
Copy link
Sponsor Contributor

hazeycode commented Aug 15, 2020

Works for me with LLVM 10.0.1 on MacOS

@jbsolomon-fw
Copy link

jbsolomon-fw commented Aug 17, 2020

I'm getting a new and different error on the new (10.0.1) homebrew release of LLVM :D

ld: library not found for -llibxml2.tbd

@Bo98
Copy link

Bo98 commented Aug 17, 2020 via email

@kubkon
Copy link
Member

kubkon commented Aug 18, 2020

I'm getting a new and different error on the new (10.0.1) homebrew release of LLVM :D

ld: library not found for -llibxml2.tbd

I can confirm that this is indeed the problem with llvm-config. In particular, it comes from running:

$ llvm-config --system-libs
> -lz -lcurses -lm -llibxml2.tbd

@andrewrk
Copy link
Member

😢

@kubkon
Copy link
Member

kubkon commented Aug 18, 2020

A temporary (and super hacky hack hack that makes me cringe) solution to the problem of

ld: library not found for -llibxml2.tbd

is to:

  1. Create a symbolic link /usr/local/lib/liblibxml2.tbd.dylib -> /usr/lib/libxml2.2.dylib
  2. Add /usr/local/lib to your dyld search path if it's not already there. The easiest way is to simply add LINK_FLAGS "-L/usr/local/lib" to zig0 and zig target properties in CMakeLists.txt:
set_target_properties(zig0 PROPERTIES
    COMPILE_FLAGS ${EXE_CFLAGS}
    LINK_FLAGS ${EXE_LDFLAGS}
    LINK_FLAGS "-L/usr/local/lib"
)

This should get everyone with LLVM 10.0.1 installed via Homebrew on macOS going for now.

@kubkon
Copy link
Member

kubkon commented Aug 18, 2020

I think we can mark this particular issue as resolved (to do with polly), and unfortunately, instead, open a new issue to do with the latest error:

ld: library not found for -llibxml2.tbd

which also seems an upstream issue...

EDIT: I've now created an issue for this #6087.

@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
@kubkon
Copy link
Member

kubkon commented Oct 13, 2020

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

Just a heads up that at the time of writing, homebrew still doesn't feature LLVM updated to version 11, so anyone developing on a macOS might either wait a little, or install LLVM from source.

@andrewrk
Copy link
Member

Or use the bootstrap repository.

@kubkon
Copy link
Member

kubkon commented Oct 13, 2020

Just an FYI that I'm working hard on updating LLVM to 11 in homebrew in Homebrew/homebrew-core#62798

@kivikakk
Copy link
Contributor

kivikakk commented Dec 1, 2020

Note for future readers: this one got merged, so brew install llvm and then cmake .. -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) from your build directory and you're good to go! You may need to remove an old CMakeCache.txt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

Successfully merging a pull request may close this issue.