[CMake] how to setup cmake_c/xx_flags per toolchain?

Michael Hertling mhertling at online.de
Fri Jan 6 21:10:05 EST 2012


On 01/07/2012 12:56 AM, Paweł Sikora wrote:
> Hi,
> 
> i'm trying to setup a toolchain file for cross-compilation with target specfic
> options and afaics cmake dosen't use flags from such file:
> 
> $ cat CMakeLists.txt 
> cmake_minimum_required( VERSION 2.8.7 )
> project( test CXX )
> add_executable( main main.cpp )
> 
> $ cat CMakeToolchain-x86_64-gnu-linux.txt
> set( CMAKE_SYSTEM_NAME Linux )
> set( CMAKE_SYSTEM_VERSION 1 )
> set( CMAKE_CXX_COMPILER /local/devel/toolchain46/x86_64-gnu-linux/bin/x86_64-gnu-linux-g++ )
> set( CMAKE_CXX_FLAGS "-Wall -O1 -gdwarf-4 -g2 -std=gnu++0x" )
> set( CMAKE_FIND_ROOT_PATH /usr )
> 
> $ LANG=C sh -x ./build.sh
> + rm -rf build
> + mkdir build
> + cd build
> + cmake ../ -DCMAKE_TOOLCHAIN_FILE=../CMakeToolchain-x86_64-gnu-linux.txt
> -- The CXX compiler identification is GNU
> -- Check for working CXX compiler: /local/devel/toolchain46/x86_64-gnu-linux/bin/x86_64-gnu-linux-g++
> -- Check for working CXX compiler: /local/devel/toolchain46/x86_64-gnu-linux/bin/x86_64-gnu-linux-g++ -- works
> -- Detecting CXX compiler ABI info
> -- Detecting CXX compiler ABI info - done
> -- Configuring done
> -- Generating done
> (...)
> [100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
> /local/devel/toolchain46/x86_64-gnu-linux/bin/x86_64-gnu-linux-g++     -o CMakeFiles/main.dir/main.cpp.o -c /home/users/pluto/src/cmake-cross-build/main.cpp
>                                                                    ^^^ no cxx flags.
> 
> is it a bug or a feature?
> 
> BR,
> Paweł.

Try SET(CMAKE_CXX_FLAGS "..." CACHE STRING "...") in the toolchain
file, and cf. [1]. AFAIK, the toolchain file is processed first,
but later in CMakeCXXInformation.cmake:

# Load compiler-specific information.
IF(CMAKE_CXX_COMPILER_ID)
  INCLUDE(Compiler/${CMAKE_CXX_COMPILER_ID}-CXX OPTIONAL)
ENDIF(CMAKE_CXX_COMPILER_ID)

This loads Compiler/GNU-CXX.cmake and subsequently Compiler/GNU.cmake
which empties CMAKE_CXX_FLAGS_INIT. Again later in the same file:

# add the flags to the cache based
# on the initial values computed in the platform/*.cmake files
# use _INIT variables so that this only happens the first time
# and you can set these flags in the cmake cache
SET(CMAKE_CXX_FLAGS_INIT "$ENV{CXXFLAGS} ${CMAKE_CXX_FLAGS_INIT}")
# avoid just having a space as the initial value for the cache
IF(CMAKE_CXX_FLAGS_INIT STREQUAL " ")
  SET(CMAKE_CXX_FLAGS_INIT)
ENDIF(CMAKE_CXX_FLAGS_INIT STREQUAL " ")
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_INIT}" CACHE STRING
     "Flags used by the compiler during all build types.")

If CMAKE_CXX_FLAGS is not cached by that time, the SET() command sets
this variable to CMAKE_CXX_FLAGS_INIT, both in the cache *and* in the
current scope, thus overwriting the value from the toolchain file and
having CMAKE_CXX_FLAGS be empty. Already writing it to the cache in
the toolchain file makes the incriminated SET() command a no-op, so
the desired value will survive. However, I'm not completely sure if
this is the intended way to handle language flags when they are to
be preset in a toolchain file.

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg33240.html


More information about the CMake mailing list