[CMake] CMAKE_C_FLAGS and CMAKE_C_COMPILER cross compilation woe

Thomas Taranowski tom at baringforge.com
Tue May 14 13:02:56 EDT 2013


I am running into a situation when cross compiling where I cannot get
cmake to emit the write CFLAGS for my cross-compilation toolchain
correctly.

If I force the C compiler, via the CMakeForceCompiler mechanism, the
CMAKE_C_FLAGS do not get passed to gcc during the build.

Example make VERBOSE=true snapshot.  Note there are no CFLAGS, only
include paths:
/usr/local/msp430/bin/msp430-gcc
-I/home/tom/src/t3gtb.1/t3_i2c_mm/public
-I/home/tom/src/t3gtb.1/t3_i2c_mm/src/msp430
-I/home/tom/src/t3gtb.1/t3_i2c_mm/src    -o
CMakeFiles/t3_i2c_mm_support.dir/src/msp430/t3g_bsp.c.obj   -c
/home/tom/src/t3gtb.1/t3_i2c_mm/src/msp430/t3g_bsp.c


Now, I can eliminate the need to force by passing in a -mmcu option
(required for this processor to find the correct link map), which
allows the TRY_COMPILE process to pass.  So I set:

set(CMAKE_C_COMPILER ${CROSS_COMPILE}gcc ${CMAKE_C_FLAGS})

This works to allow cmake to do it's thing and figure out what it
needs to about the toolchain.  However, it seems as though the cflags
is now fixed, because the include_directories directive has absolutely
no effect.  It's as if cmake thinks that if I pass in some additional
flags to the CMAKE_C_COMPILER, that it can no longer append anything
to cflags.

Example output from make VERBOSE=true.  Note the flags now exist, but
no include paths:

/usr/local/msp430/bin/msp430-gcc  -mmcu=msp430fr5739
-D__MSP430FR5739__ -ffunction-sections -fdata-sections -DGCC_MSP430
-Wall -g -std=c99  -mmcu=msp430fr5739
CMakeFiles/test_manager_main.dir/src/test_manager_main.c.obj  -o
test_manager_main


My ideal solution would be to get cmake to allow for appending include
directories and other flags to the compiler invocation, even though I
specified the magic -mmcu to allow the gcc to compile for my target.
Any ideas on what I'm doing wrong, or an alternate path to try that
I've overlooked?  A simplified version of my CMakeLists.txt is pasted
below.

set(CMAKE_SYSTEM_NAME Generic)

# Base directory for  the msp toolchain
set(MSPBASE $ENV{MSPBASE})

# the name of the target operating system
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR "msp430")
set(CMAKE_CROSSCOMPILING TRUE)
set(CROSS_COMPILE "msp430-")

# which C and C++ compiler to use
set(CMAKE_C_FLAGS "-mmcu=msp430fr5969 -D__MSP430FR5969__
-ffunction-sections -fdata-sections -DGCC_MSP430 -Wall -g -std=c99")
set(CMAKE_C_COMPILER ${CROSS_COMPILE}gcc ${CMAKE_C_FLAGS})
set(CMAKE_LINKER ${CROSS_COMPILE}gcc)

set(CMAKE_CXX_FLAGS "-mmcu=msp430fr5969 -D__MSP430FR5969__
-ffunction-sections -fdata-sections -DGCC_MSP430 -Wall -fno-exceptions
-fno-rtti -fno-enforce-eh-specs -fnostdlib")
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++ ${CMAKE_CXX_FLAGS})
set(CMAKE_EXE_LINKER_FLAGS "-mmcu=msp430fr5969")

set(CMAKE_FIND_ROOT_PATH /usr/local/msp430/msp430)

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)



Thanks,
Tom


More information about the CMake mailing list