Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a compile flag to cmake initial compiler test?

I'm trying to use CMake for building a project which uses the MSPGCC cross-compiler for a MSP430 microcontroller. To successfully compile any simple program with it, we need to pass a compile flag indicating the target processor, or else it fails like this:

$ msp430-gcc -o test test.c
In file included from test.c:1:0:
/usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/include/msp430.h:813:2: warning: #warning Unable to identify and include MCU header, use -mmcu=MCU [-Wcpp]
/usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld: cannot open linker script file memory.x: No such file or directory
collect2: ld returned 1 exit status

Hence, if I indicate the processor using the -mmcu switch it works fine. The problem is, although I'm already specifying this in my CMakeLists.txt file:

cmake_minimum_required (VERSION 2.6)

project (test-project C)

set (SOURCES
  test.c
  )

add_executable (test-project ${SOURCES})
set (CPU_FLAG "-mmcu=msp430f148")

set (CMAKE_C_FLAGS ${CPU_FLAG})
set (CMAKE_EXE_LINKER_FLAGS ${CPU_FLAG})

CMake complains the compiler failed the test to compile a simple program, which I bet is happening because it is probably not using the -mmcu switch (note the message about not being able to open linker script file memory.x):

$ cd ~/git/test-project
$ mkdir build
$ cd build
$ cmake -DCMAKE_TOOLCHAIN_FILE=../msp430.cmake ..
-- The C compiler identification is GNU 4.6.3
-- Check for working C compiler: /usr/local/bin/msp430-gcc
-- Check for working C compiler: /usr/local/bin/msp430-gcc -- broken
CMake Error at /usr/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler "/usr/local/bin/msp430-gcc" is not able to compile a simple
  test program.

  It fails with the following output:

   Change Dir: /home/claudio/git/test-project/build/CMakeFiles/CMakeTmp



  Run Build Command:/usr/bin/gmake "cmTryCompileExec2889462763/fast"

  /usr/bin/gmake -f CMakeFiles/cmTryCompileExec2889462763.dir/build.make
  CMakeFiles/cmTryCompileExec2889462763.dir/build

  gmake[1]: Entering directory
  `/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp'

  /usr/bin/cmake -E cmake_progress_report
  /home/claudio/git/test-project/build/CMakeFiles/CMakeTmp/CMakeFiles 1

  Building C object
  CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o

  /usr/local/bin/msp430-gcc -o
  CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o -c
  /home/claudio/git/test-project/build/CMakeFiles/CMakeTmp/testCCompiler.c

  Linking C executable cmTryCompileExec2889462763

  /usr/bin/cmake -E cmake_link_script
  CMakeFiles/cmTryCompileExec2889462763.dir/link.txt --verbose=1

  /usr/local/bin/msp430-gcc
  CMakeFiles/cmTryCompileExec2889462763.dir/testCCompiler.c.o -o
  cmTryCompileExec2889462763 -rdynamic

  /usr/local/lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld: cannot open
  linker script file memory.x: No such file or directory

  collect2: ld returned 1 exit status

  gmake[1]: Leaving directory
  `/home/claudio/git/test-project/build/CMakeFiles/CMakeTmp'

  gmake[1]: *** [cmTryCompileExec2889462763] Error 1

  gmake: *** [cmTryCompileExec2889462763/fast] Error 2





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)

-- Configuring incomplete, errors occurred!

Just for the record, my toolchain file is as follows, and my PATH variable allows it to find the compiler binaries at /usr/local/bin:

# the name of the target operating system
#SET(CMAKE_SYSTEM_NAME Linux)

# which C and C++ compiler to use
SET(CMAKE_C_COMPILER   msp430-gcc)
SET(CMAKE_CXX_COMPILER msp430-g++) 

# here is the target environment located
SET(CMAKE_FIND_ROOT_PATH  /usr/local/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)

All that said, can anyone tell me how to check which compile flags CMake is using to carry the compiler test, and how can we pass custom flags (like -mmcu, for instance) so it doesn't fail it?

like image 505
Claudio Avatar asked Oct 22 '12 13:10

Claudio


People also ask

Does CMake need compiler?

CMake was written in C++, requires only a C++ compiler to build, and precompiled binaries are available for most systems. Scripting it yourself also typically means you will not be generating native Xcode or Visual Studio workspaces, making Mac and Windows builds limited.

Does CMake work with GCC?

CMake generates files for other build systems. These can be Makefiles, Ninja files or projects files for IDEs like Visual Studio or Eclipse. The build files contain calls to compilers like GCC, Clang, or cl.exe. If you have several compilers installed, you can choose one.


2 Answers

According to the Docs:

http://www.cmake.org/Wiki/CMake_Cross_Compiling#The_toolchain_file

you should use the CMakeForceCompiler thing

INCLUDE(CMakeForceCompiler)

# this one is important
SET(CMAKE_SYSTEM_NAME eCos)

# specify the cross compiler
CMAKE_FORCE_C_COMPILER(arm-elf-gcc GNU)
CMAKE_FORCE_CXX_COMPILER(arm-elf-g++ GNU)

# where is the target environment 
SET(CMAKE_FIND_ROOT_PATH  /home/alex/src/ecos/install )

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

(copy&paste from the docs)

Using it fine here for my MSP430 too

like image 73
duselbaer Avatar answered Oct 19 '22 21:10

duselbaer


The other answer here is outdated.

The toolchain file should include the required flags in the appropriate CMAKE_<KIND>_FLAGS(_<CONFIG>)_INIT variables, like so:

set(CMAKE_C_FLAGS_INIT "-mmcu=msp430f148")
set(CMAKE_CXX_FLAGS_INIT "-mmcu=msp430f148")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-mmcu=msp430f148")

CMake's compiler detection routines will use these flags when compiling a test executable. The full list is available in the CMake variables documentation, but the full list (at time of writing) is:

  • CMAKE_<LANG>_FLAGS_<CONFIG>_INIT
  • CMAKE_<LANG>_FLAGS_INIT
  • CMAKE_EXE_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_EXE_LINKER_FLAGS_INIT
  • CMAKE_MODULE_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_MODULE_LINKER_FLAGS_INIT
  • CMAKE_SHARED_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_SHARED_LINKER_FLAGS_INIT
  • CMAKE_STATIC_LINKER_FLAGS_<CONFIG>_INIT
  • CMAKE_STATIC_LINKER_FLAGS_INIT

Where <CONFIG> is any ALL CAPS config name including, but not limited to, DEBUG, RELWITHDEBINFO, MINSIZEREL, and RELEASE. The non-<CONFIG> variants apply to all configurations and are augmented by the <CONFIG> variants.

And, where <LANG> is any of the languages known to enable_language(), currently including CXX, C, CUDA, OBJC, OBJCXX, Fortran, HIP, ISPC, and ASM. The current list may be found in the enable_language documentation.

like image 25
Alex Reinking Avatar answered Oct 19 '22 22:10

Alex Reinking