[CMake] Support for cross compiling

Alexander Neundorf a.neundorf-work at gmx.net
Wed Apr 16 15:18:31 EDT 2008


On Wednesday 16 April 2008, Mark Jonas wrote:
> Hi,
>
> I am currently testing if CMake would be a usable build system for my
> embedded C, C++ and assembly language project.
> My first target compiler will be a cross gcc but I need to get CMake
> to work with IAR and Green Hills compilers, too.
>
> I found CMAKE_FORCE_C_COMPILER() and CMAKE_FORCE_CXX_COMPILER() to
> force a different compiler. But how do I tell CMake about different
> archivers, assemblers and linkers?

You are using cmake 2.6, right ?
Here's the wiki page:
http://www.cmake.org/Wiki/CMake_Cross_Compiling

So you have to create a separate "toolchain"-file for each crosscompile setup.
Further down on that page there are links ot examples for some different 
targest/toolchains:
http://www.cmake.org/Wiki/CMake_Cross_Compiling#Information_how_to_set_up_various_cross_compiling_toolchains

For supporting a new compiler (as IAR and Green Hills):
this shouldn't be a big problem.
At first cmake has to detect which compiler it is, this is done by compiling 
CMake/Modules/CMakeCInformation.c.in.
Have a look at that file, you will see how it is supposed to work.

Once the compiler has been determined, cmake tries to use compiler-specific 
files, as eg. CMake/Modules/Platform/Generic-SDCC-C.cmake, 
Linux-SunPro-C.cmake etc.
These filenames are constructed from the name of the target system, then the 
name of the compiler, then the language.
If no such file is found, the generic settings are used (which will probably 
be largely ok also for IAR and GHS).

What is the operating system of the targets ?

> Is this a reasonable way to teach CMake to handle assembly language files?
>
> FOREACH(src ${ASM_SRCS})
>    GET_FILENAME_COMPONENT(SRC_NAME ${src} NAME)
>    SET(ASM_SRC ${CMAKE_CURRENT_SOURCE_DIR}/${src})
>    SET(ASM_OBJ ${CMAKE_CURRENT_BINARY_DIR}/${SRC_NAME}.o)
>    STRING(TOUPPER ${CMAKE_BUILD_TYPE} BTYPE)
>    SET(ASM_FLAGS ${CMAKE_C_FLAGS})
>    SET(ASM_FLAGS ${ASM_FLAGS} ${CMAKE_C_FLAGS_${BTYPE}})
>    SEPARATE_ARGUMENTS(ASM_FLAGS)
>    ADD_CUSTOM_COMMAND(
>       OUTPUT ${ASM_OBJ}
>       DEPENDS ${ASM_SRC}
>       COMMAND ${MY_ASM_COMPILER}
>       ARGS -c ${ASM_FLAGS} -o ${ASM_OBJ} ${ASM_SRC}
>    )
>    SET(ASM_OBJS ${ASM_OBJS} ${ASM_OBJ})
> ENDFOREACH(src)
>
> ADD_EXECUTABLE( ${EXENAME} ${SRCS} ${ASM_OBJS} )
>
> Can this be done better?
>
> Is there already assembly language support in CMake and I did not look
> closely enough?

Yes.
I really have to document this somewhat better:
http://www.cmake.org/pipermail/cmake/2007-July/014929.html

All files related to assembler are below CMake/Modules, so if you grep 
for "ASM" there you will find all relevant files.

I'd happily help you with this.

Alex


More information about the CMake mailing list