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.

compor
  • 2,239
  • 1
  • 19
  • 29
John Smith
  • 777
  • 2
  • 14
  • 37
  • I've never done this, so hence only posting this as a comment. If you see in [CMakeCInformation](https://github.com/Kitware/CMake/blob/master/Modules/CMakeCInformation.cmake) 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. – compor Aug 17 '18 at 09:27
  • Is `CMAKE_LANG_OUTPUT_EXTENSION` in the third line of your question supposed to be `CMAKE_C_OUTPUT_EXTENSION` (typo)? – Ghasem Naddaf Aug 22 '18 at 20:52
  • is the example given [here](https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/CrossCompiling#the-toolchain-file) 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. – Ghasem Naddaf Aug 23 '18 at 00:19

1 Answers1

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

fdk1342
  • 3,274
  • 1
  • 16
  • 17