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

1

7

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.

Esperance answered 14/8, 2018 at 13:43 Comment(3)
I've never done this, so hence only posting this as a comment. If you see in CMakeCInformation and follow the include commands, maybe you can define a custom platform file where you override those values, name and place it in a dir structure that follows the required conventions for inclusion and, lastly, update the CMAKE_MODULE_PATH with the root location for the include to actually work.Thaxter
Is CMAKE_LANG_OUTPUT_EXTENSION in the third line of your question supposed to be CMAKE_C_OUTPUT_EXTENSION (typo)?Antemundane
is the example given here useful? I think you should remove SET(CMAKE_C_COMPILER_FORCED TRUE) and add CMAKE_FORCE_C_COMPILER with the desired compiler name and id for cross-compiling.Antemundane
S
2

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."

Sikorski answered 29/11, 2018 at 20:5 Comment(1)
gitlab.kitware.com/cmake/cmake/issues/18713 documents this issue.Sikorski

© 2022 - 2024 — McMap. All rights reserved.