[llvm-dev] How to use mainline clang/llvm with CMake

Rakur Moski via llvm-dev llvm-dev at lists.llvm.org
Fri Nov 6 09:53:40 PST 2020


Dear all, I had managed to find answers to my question.


The following is my CMakeLists.txt

‐‐‐‐‐‐‐ CMakeLists.txt ‐‐‐‐‐‐‐
#
# TOOLCHAIN_PATH=/Users/user/workspace/github/clang_build/manual/llvm-project/_install
# or
# TOOLCHAIN_PATH=/Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr
#

project(${PROJECT_NAME} LANGUAGES CXX)
cmake_minimum_required(VERSION 3.19)

set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/bin/clang++)
string(APPEND CMAKE_CXX_FLAGS  " -v")

string(APPEND CMAKE_EXE_LINKER_FLAGS  " -L${TOOLCHAIN_PATH}/lib")
string(APPEND CMAKE_EXE_LINKER_FLAGS  " -Wl,-rpath,${TOOLCHAIN_PATH}/lib")
string(APPEND CMAKE_EXE_LINKER_FLAGS  " -v")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(seq_vector seq_vector.cpp)

‐‐‐‐‐‐‐ End of CMakeLists.txt ‐‐‐‐‐‐‐




Now, I can see my include paths as

‐‐‐‐‐‐‐ Excerpt from compilation ‐‐‐‐‐‐‐
#include "..." search starts here:
#include <...> search starts here:
 /Users/user/workspace/github/clang_build/manual/llvm-project/_install2/bin/../include/c++/v1
 /Users/user/workspace/github/clang_build/manual/llvm-project/_install2/lib/clang/12.0.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks (framework directory)
End of search list.
‐‐‐‐‐‐‐ End of Excerpt from compilation ‐‐‐‐‐‐‐




The rpath on executable is set automatically through CMake

‐‐‐‐‐‐‐ Excerpt from compilation ‐‐‐‐‐‐‐
otool -L /Users/user/workspace/github/clang_build/manual/llvm-project/example/_build/seq_vector
/Users/user/workspace/github/clang_build/manual/llvm-project/example/_build/seq_vector:
	@rpath/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
‐‐‐‐‐‐‐ End of Excerpt from compilation ‐‐‐‐‐‐‐




And the newly built libraries are loaded when running the executable

‐‐‐‐‐‐‐ Excerpt from compilation ‐‐‐‐‐‐‐

dyld: loaded: <91A3ABCC-8A41-3CF8-A788-2527F5CC5211> /Users/user/workspace/github/clang_build/manual/llvm-project/example/_build/seq_vector
dyld: loaded: <0DD22FCB-0487-3FC1-8EAC-CCE7A5338C73> /Users/user/workspace/github/clang_build/manual/llvm-project/_install2/lib/libc++.1.dylib
dyld: loaded: <0A6C8BA1-30FD-3D10-83FD-FF29E221AFFE> /usr/lib/libSystem.B.dylib
dyld: loaded: <152484F8-8B54-3D3B-97D8-C2C9C938858B> /Users/user/workspace/github/clang_build/manual/llvm-project/_install2/lib/../lib/libc++abi.1.dylib
‐‐‐‐‐‐‐ End of Excerpt from compilation ‐‐‐‐‐‐‐




I found lot of the required information through clang documentation and searching through mailing list; As I had to experiment several options, I lost track of reference posts to a solid documentation.

Thank you all!
Rakur.


‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Thursday, November 5, 2020 11:30 PM, Rakur Moski via llvm-dev <llvm-dev at lists.llvm.org> wrote:

> Dear all, I am not sure whether this is a right place to ask basic questions about usage of CLang/LLVM, but also not sure if there is any other mailing list for such.
> Kindly point me to such a list in such case.
>
> I had built mainline CLang/LLVM on OSX and tried to use it with CMake. I had override following variables in CMake:
>
> CMAKE_CXX_COMPILER=${LLVM_INSTALL_PATH}/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/bin/clang++
> CMAKE_CXX_STANDARD_LIBRARIES=${LLVM_INSTALL_PATH}/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib
> CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES=${LLVM_INSTALL_PATH}/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/include/c++/v1
>
> Though the compilation works, the link stage fails, as I couldn't set the path to newly built LLVM libcxx/libcxxabi.
>
> Could some one please tell what is the correct way to use mainline LLVM in CMake?
>
> Thanks.
> Rakur.
>
> PS: 
> Complete verbose compile logs are available at https://pastebin.com/p3U2EQ1h
> Im adding excerpt of compile/link commands and output logs below in this mail too.
>
> It produces the following compilation command:
> /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/bin/clang++  -isystem /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/include/c++/v1 -g -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -std=c++17 -o CMakeFiles/seq_vector.dir/seq_vector.cpp.o -c /Users/user/workspace/github/clang_build/manual/llvm-project/example/seq_vector.cpp
>
> Verbose compile command:
> "/Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/bin/clang-12" -cc1 -triple x86_64-apple-macosx10.15.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all --mrelax-relocations -disable-free -main-file-name seq_vector.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-rounding-math -munwind-tables -target-sdk-version=10.15.6 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -target-linker-version 609 -v -resource-dir /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib/clang/12.0.0 -isystem /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/include/c++/v1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -stdlib=libc++ -internal-isystem /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/bin/../include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/local/include -internal-isystem /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib/clang/12.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /Users/user/workspace/github/clang_build/manual/llvm-project/example/_build/cmake/Debug -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -o CMakeFiles/seq_vector.dir/seq_vector.cpp.o -x c++ /Users/user/workspace/github/clang_build/manual/llvm-project/example/seq_vector.cpp
>
> But unfortunately, the link stage fails:
> /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/bin/clang++ -g -v -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/seq_vector.dir/seq_vector.cpp.o -o seq_vector  /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib
>
> Verbose link command:
> "/usr/bin/ld" -demangle -lto_library /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 10.15.0 10.15.6 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o seq_vector -search_paths_first -headerpad_max_install_names CMakeFiles/seq_vector.dir/seq_vector.cpp.o /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib -lc++ -lSystem /Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib/clang/12.0.0/lib/darwin/libclang_rt.osx.a
>
> Error:
> ld: can't map file, errno=22 file '/Users/user/workspace/github/clang_build/manual/llvm-project/_install/Toolchains/LLVM12.0.0git.xctoolchain/usr/lib' for architecture x86_64
>
> -----
> I had built Clang with following command:
>
> cmake \
> -H./llvm-project/llvm \
> -B./llvm-project/_build \
> -DCMAKE_INSTALL_PREFIX=./llvm-project/_install \
> -G "Ninja" \
> -DCMAKE_BUILD_TYPE=Release \
> \
> -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi;compiler-rt;clang-tools-extra;libunwind;lld;pstl;" \
> -DLLVM_TARGETS_TO_BUILD="X86" \
> -DCMAKE_OSX_ARCHITECTURES="x86_64" \
> -DLLVM_CREATE_XCODE_TOOLCHAIN=ON \
> -DDEFAULT_SYSROOT="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"


More information about the llvm-dev mailing list