Halsafar
Halsafar

Reputation: 2640

Compiling clang from source with custom gcc - set default target

Problem

When I compile gcc I am setting --target=x86_64-mytoolchain-linux-gnu. Adjust for arm64 accordingly.

I compile clang from source like so:

mkdir build
cd build
cmake .. \
-DCMAKE_INSTALL_PREFIX="/opt/clang" \
-DCMAKE_PREFIX_PATH="/opt/clang" \
-DCMAKE_BUILD_TYPE=Release \
-DGCC_INSTALL_PREFIX="/opt/gcc/" \
-DLLVM_INCLUDE_TESTS=FALSE \
-DLLVM_TARGETS_TO_BUILD="x86" \
-DLLVM_ENABLE_LTO=ON \
-DCMAKE_C_COMPILER="/opt/gcc/bin/gcc" \
-DCMAKE_CXX_COMPILER="/opt/gcc/bin/g++" \
-DCMAKE_C_FLAGS="${CFLAGS}" \
-DCMAKE_CXX_FLAGS="${CPPFLAGS}" \
-DCMAKE_C_LINK_FLAGS="${LDFLAGS}" \
-DCMAKE_CXX_LINK_FLAGS="${LDFLAGS} -L$/opt/gcc/lib64 -Wl,-rpath,$/opt/gcc/lib64"

The output of clang -v:

clang version 9.0.1 
Target: x86_64-unknown-linux-gnu
Thread model: posix

The default target does not match my custom GCC target.

I cannot compile any C++ without manually specifying -target x86_64-mytoolchain-linux-gnu. If I specify that then it automatically finds all the appropriate includes and libs. Everything works as expected.

Question

When I am compiling clang from source how can I specify the target that it will use by default? It seems the only control I have is 'x86'.

Upvotes: 0

Views: 1421

Answers (1)

Halsafar
Halsafar

Reputation: 2640

Turns out the default target is dictated by LLVM during its compilation.

Specify this when building LLVM:

-DLLVM_DEFAULT_TARGET_TRIPLE="x86_64-mytoolchain-linux-gnu"

Now when compiling clang, the output of clang -v:

clang version 9.0.1 
Target: x86_64-mytoolchain-linux-gnu
Thread model: posix

Upvotes: 1

Related Questions