Reputation: 98
I'm trying to write a CMake function that creates an object library of LLVM bitcode files. Here is what I have so far
function(build_llvm_lib OUTPUT SRC)
if(NOT LLVM_FOUND)
message(FATAL_ERROR "LLVM build requested but LLVM not found")
endif()
set(SRCS ${SRC} ${ARGN})
set(CMAKE_C_OUTPUT_EXTENSION ".bc")
set(CMAKE_CXX_OUTPUT_EXTENSION ".bc")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".bc")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -emit-llvm")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -emit-llvm")
set(CMAKE_C_COMPILER ${LLVM_TOOLS_BINARY_DIR}/clang)
set(CMAKE_CXX_COMPILER ${LLVM_TOOLS_BINARY_DIR}/clang++)
set(CMAKE_AR ${LLVM_TOOLS_BINARY_DIR}/llvm-ar)
set(CMAKE_RANLIB ${LLVM_TOOLS_BINARY_DIR}/llvm-ranlib)
add_library(${OUTPUT} OBJECT ${SRCS})
endfunction(build_llvm_lib)
However, the problem that I'm having is that even though I am setting the CMake variables like CMAKE_CXX_FLAGS
and CMAKE_CXX_OUTPUT_EXTENSION
these don't seem to be having any affect (i.e. they are being ignore) when CMake is run. When I look at the generated Makefile these settings don't appear and when I run make a regular object file is built rather than an LLVM bitcode file.
I'm kind of a CMake newb, so can someone please explain what's going on here? Is there some sort of misunderstanding I'm making here with CMake functions?
Upvotes: 3
Views: 973
Reputation: 65870
While all CMake variables may have different values when you create different targets, CMake expects some of variables be the same for all targets. Examples of those variables:
CMAKE_<LANG>_COMPILER
CMAKE_AR
CMAKE_RANLIB
CMAKE_<LANG>_OUTPUT_EXTENSION
This means that you cannot use different compilers for different targets in the same project. See, e.g. this question.
Variables CMAKE_<LANG>_FLAGS also should be defined globally, for all targets.
Only CMake variables and functions which are propagated to the target's property have per-target meaning.
For example, you may define additional compiler options for concrete targets using commands add_compile_options
, add_definitions
or by manually setting target's properties COMPILE_FLAGS or COMPILE_DEFINITIONS.
In your build_llvm_lib
function body only CMAKE_STATIC_LIBRARY_SUFFIX variable probably has per-target nature: while it is not stated in the documentation, I suspect it just sets SUFFIX property for newly-defined targets.
Upvotes: 3