CMake AMRCC + custom linker

2019-02-20 12:07发布

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.

2条回答
贪生不怕死
2楼-- · 2019-02-20 12:46

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

查看更多
Lonely孤独者°
3楼-- · 2019-02-20 12:47

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

查看更多
登录 后发表回答