CMake AMRCC + custom linker

∥☆過路亽.° 提交于 2019-12-01 11:54:29

问题


I'm trying to use cmake in a project which is compiled using armcc, but use a custom proprietary linker (not armlink).

I've changed the variables in the toolchain.cmake file as following:

unset (CMAKE_LINKER CACHE)
set (CMAKE_LINKER "my_linker" CACHE STRING "" FORCE)

unset (CMAKE_ARMCC_LINKER CACHE)
set (CMAKE_ARMCC_LINKER "my_linker" CACHE STRING "" FORCE)

unset (CMAKE_EXE_LINKER_FLAGS CACHE )
set (CMAKE_EXE_LINKER_FLAGS "-flag1 -flag2" CACHE STRING "" FORCE)

unset (CMAKE_C_LINK_EXECUTABLE)
set (CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> <CMAKE_EXE_LINKER_FLAGS> <OBJECTS> <LINK_LIBRARIES> -o <TARGET>")

unset (CMAKE_CXX_LINK_EXECUTABLE)
set (CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_LINKER> <CMAKE_EXE_LINKER_FLAGS> <OBJECTS> <LINK_LIBRARIES> -o <TARGET>")

But when cmake tries to check my compiler suite, it fails in the linking step:

-- Check for working C compiler: armcc.exe -- broken
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
  The C compiler "armcc.exe" is not able to compile a simple test program.

... (compiling commands that worked hidden here)

Linking C executable cmTC_c08ef.elf

"C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_c08ef.dir\link.txt --verbose=1

my_linker -flag1 -flag2 CMakeFiles/cmTC_c08ef.dir/testCCompiler.o -o cmTC_c08ef.elf --list cmTC_c08ef.map

The problem is the --list cmTC_c08ef.map at the end of command line (which does not exist in the toolchain.cmake file).

In order to make it work, I need to change the file <cmake_install_dir>\Modules\Compiler\ARMCC.cmake as following:

  set(CMAKE_${lang}_LINK_EXECUTABLE      "<CMAKE_LINKER> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES> <OBJECTS> -o <TARGET> --list <TARGET_BASE>.map")
#  set(CMAKE_${lang}_LINK_EXECUTABLE      "<CMAKE_LINKER> <CMAKE_${lang}_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES> <OBJECTS> -o <TARGET>")

There is a better approach to solve this issue or it is the only way?

Edit: Apparently this is a bug in the cmake's armcc support, so I will keep my change in the ARMCC.cmake file.


回答1:


That's exactly the use case of a relatively new (version 3.6) global CMake variable named CMAKE_TRY_COMPILE_TARGET_TYPE. So just add the following to your toolchain file:

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

Edit: If your custom linker also gives you trouble outside the compiler check, simply split your toolchain into 2 files (toolchain.cmake to be read before and makerules.cmake after the CMake's compiler detection):

toolchain.cmake

[...]
set (CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_FILE}/makerules.cmake")

makerules.cmake

set (CMAKE_C_LINK_EXECUTABLE "<CMAKE_LINKER> <CMAKE_EXE_LINKER_FLAGS> <OBJECTS> <LINK_LIBRARIES> -o <TARGET>")
set (CMAKE_CXX_LINK_EXECUTABLE "<CMAKE_LINKER> <CMAKE_EXE_LINKER_FLAGS> <OBJECTS> <LINK_LIBRARIES> -o <TARGET>")

Refrences

  • CMAKE_USER_MAKE_RULES_OVERRIDE
  • Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake
  • CMake: In which Order are Files parsed (Cache, Toolchain, …)?



回答2:


Apparently this is a bug in the cmake's armcc support, so I will keep my change in the ARMCC.cmake file.



来源:https://stackoverflow.com/questions/38359796/cmake-amrcc-custom-linker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!