banach-space / clang-tutor

A collection of out-of-tree Clang plugins for teaching and learning
The Unlicense
692 stars 62 forks source link

Fix building against LLVM/Clang configured with BUILD_SHARED_LIBS=ON #16

Open banach-space opened 3 years ago

banach-space commented 3 years ago

The tools from clang-tutor fail to build when using Clang/LLVM that was build with BUILD_SHARED_LIBS set. This is the error that I see on Ubuntu 16.04:

[7/9] Linking CXX executable bin/ct-la-commenter
FAILED: : && /usr/bin/clang++-11 -Wall    -fdiagnostics-color=always -fno-rtti -fvisibility-inlines-hidden -g  tools/CMakeFiles/ct-la-commenter.dir/LACommenterMain.cpp.o tools/CMakeFiles/ct-la-commenter.dir/__/lib/LACommenter.cpp.o -o bin/ct-la-commenter  -Wl,-rpath,/home/andwar02/work/release-11/build/release/lib  /home/bs/release-11/build/release/lib/libclangTooling.so.11  -Wl,-rpath-link,/home/bs/release-11/build/release/lib && :
/usr/bin/ld: tools/CMakeFiles/ct-la-commenter.dir/LACommenterMain.cpp.o: undefined reference to symbol '_ZN5clang14FrontendAction22shouldEraseOutputFilesEv'
/home/bs/work/release-11/build/release/lib/libclangFrontend.so.11: error adding symbols: DSO missing from command line
clang: error: linker command failed with exit code 1 (use -v to see invocation)

SOLUTION 1 A solution was submitted by @xgupta here. It went through a few iterations. The original approach looked like this:

$ git diff
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 3673d81..6a33776 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -34,6 +34,14 @@ foreach( tool ${CLANG_TUTOR_TOOLS} )
     target_link_libraries(
       ${tool}
       clangTooling
+      clangFrontend
+      clangSerialization
+      clangRewrite
+      clangASTMatchers
+      clangAST
+      clangBasic
+      LLVMFrontendOpenMP
+      LLVMSupport
     )

     # Configure include directories for 'tool'

However, we discovered that that didn't work well with pre-build binary packages from Ubuntu 16.04. This is the error that I was getting at run-time:

 bin/ct-la-commenter <clang-tutor-dir>/test/LACBool.cpp 2>&1
: CommandLine Error: Option 'help-list' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options
Aborted

So this was regression. It's hard to fix as it's not clear how the packages from Ubuntu are built and how to reproduce this with manually built packages.

SOLUTION 2 @xgupta kindly submitted an updated solution that is based on Clang's reference example. However, for this to work clang-tutor needs access to:

This would normally be achieved with include(AddClang) in one of the CMake scripts. However, currently AddClang is not copied/installed in the build directory. A fix has been proposed here. One can also work around this by pointing clang-tutor to Clang's source directory (i.e. the location of AddClang). But that would mean that Ubuntu's pre-build packages alone are no longer sufficient to build the project.

SUMMARY Both approaches break clang-tutor when using Ubuntu's pre-build LLVM/Clang packages. I think that SOLUTION 2 would be more canonical, but we may need for D94533 to be merged.

banach-space commented 3 years ago

Looking at the definition of add_clang_executable, it feels that add_llvm_executable should be sufficient for us.

Similarly, clang_target_link_libraries could be replaced with target_link_libraries. So, include(AddLLVM) could be replaced with include(AddClang), which means that this is doable :)