Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set specific CMAKE_C_OUTPUT_EXTENSION for cross-compiling configurations with CMAKE?

I am trying to setup a toolchain file for cross compilation with CMake 3.12.0 version.

My object files have a different extensions than .obj on Windows and .o in UNIX.

Thus, I set my CMAKE_LANG_OUTPUT_EXTENSION to .src.

Unfortunately, this variable is overwritten by CMakeCInformation.cmake file in these lines:

# some compilers use different extensions (e.g. sdcc uses .rel)
# so set the extension here first so it can be overridden by the compiler specific file
if(UNIX)
  set(CMAKE_C_OUTPUT_EXTENSION .o)
else()
  set(CMAKE_C_OUTPUT_EXTENSION .obj)
endif()

If I comment these lines my configurations will work and the right object extension will be used.

I think my toolchain file is configured so that CMake will not execute its internal compiler checks.

This is how my toolchain file entry lines look:

SET(CMAKE_SYSTEM_NAME Generic)

INCLUDE(CMakeForceCompiler)

SET(CMAKE_C_COMPILER_FORCED TRUE)
SET(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
#other compiler configuration lines here 
SET(CMAKE_C_OUTPUT_EXTENSION .src)
SET(CMAKE_ASM_OUTPUT_EXTENSION .o)
SET(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)
SET(CMAKE_ASM_OUTPUT_EXTENSION_REPLACE 1)

I know CMakeForceCompiler is depreciated and CMAKE_TRY_COMPILE_TARGET_TYPE should be used that is why both are there.

I am telling CMake about my toolchain file using -DCMAKE_TOOLCHAIN_FILE

Can you please help me figure out what I am doing wrong?

EDIT: I was also trying to CACHE the value of CMAKE_C_OUTPUT_EXTENSION. At least for me this didn't work.

like image 784
John Smith Avatar asked Aug 14 '18 13:08

John Smith


1 Answers

Add SET(CMAKE_C_OUTPUT_EXTENSION .src) in the CMakeLists.txt file after the project command not in the toolchain file. This should get you the desired behavior (as it should override the values set by CMakeCInformation and any other module scripts).

The toolchain file is used for setting basic toolchain information on where the compiler is and some basic settings. Other variables need to be setup afterwards by custom compiler or platform files that can be included via CMAKE_USER_MAKE_RULES_OVERRIDE.

From CMake issue 18713: "This issue has been reported before and it was mentioned that the toolchain file isn't where these kinds of things should be set for more complicated toolchains."

CMake issue 398139 "Toolchain files should not be setting things like this. ... The output extension should be specified by compiler or platform information files that are loaded by CMakeCInformation after the defaults here are set."

like image 119
fdk1342 Avatar answered Oct 26 '22 04:10

fdk1342