I am trying to apply Link Time Optimization with LLVM on a CMake Project, that creates a shared library. My question is pretty much the same as this one:
Switching between GCC and Clang/LLVM using CMake.
However, the answers do not seem to be applicable anymore, since llvm-ld
is not present in the new versions. On the command line, I run the following commands to get LTO (Assuming there are only 2 .cpp
files):
Compile to byte code:
clang++ -c FirstClass.cpp -O3 -flto -o FirstClass.bc
clang++ -c SecondClass.cpp -O3 -flto -o SecondClass.bc
Link byte code:
llvm-link FirstClass.bc SecondClass.bc -o unoptimized.bc
Optimize byte code:
opt -O3 unoptimized.bc -o optimized.bc
Convert byte code to shared object:
clang++ -shared optimized.bc -o libTest.so
Could somebody please tell me how to have CMake run the additional steps?
add_custom_command
of cmake... Something likeadd_custom_command(OUTPUT libTest.so COMMAND clang++ -shared optimized.bc -o libTest.so MAIN_DEPENDENCY optimized.bc)
See #13470999 – George