Павел
Павел

Reputation: 123

cmake: multiple compilers for different targets with single build command

I have read the answers in the questions:

  1. Using CMake with multiple compilers for the same language
  2. CMake: how to change compiler for individual target

But I still don't understand the proper solution for cmake. The answer in (2) works only with Ninja, not cmake. The answer in (1) concerns the basic concept without example. So for me, as a beginner in cmake, the solution using process_execute() is unclear in the terms of implementation.

Is it possible to create a top-level CMakeLists.txt with process_execute(), which can be used in the follow way:

  1. Single cmake command in shell.
  2. Single make command after cmake command for the project build.
  3. Multiple targets with different compilers in one build.

What is the simple scheme of a such solution in this case?

ADDITION

My project consists from libA and libB. The libA is а backend, which emits code. The libB is a runtime support library for emitted code. I want to build the whole project by a standard "single" build command (cmake && make && make install). Of course the cmake command inlcudes parameters.

The libA may emits code for some processors. The libA may works in a cross mode. The libB is used for linking with emitted code. That is why the libB as a runtime library must be compiled in native mode.

When the libA is building in cross mode the build process must uses a one compiler for the libA, and a second compiler for the libB. In my opinion the splitting of the build process into two separated commands ("cmake1 && make1" for libA and "cmake2 && make2" for libB) is a "logical" error.

Also it is useful if the single build command may builds a multiple number of version libB for different processors.

ADDITION2

possible solution: using add_subdirectory()

Upvotes: 0

Views: 761

Answers (2)

Павел
Павел

Reputation: 123

The idea of a simplest solution for me via my current cmake user level. (Based on The canonical way ...)

libb.cmake:

set(CMAKE_C_COMPILER ${CC_ARCH})
add_library(libb_${X_ARCH} static ${libb_SOURCE})

targets/x86_64/CMakeLists.txt:

inlcude(../../libb.cmake)

targets/mips64el/CMakeLists.txt:

inlcude(../../libb.cmake)

CMakeLists.txt

...
foreach ( X_ARCH IN LISTS ARCHS_TO_BUILD)
    set(CC_ARCH ...)
    add_subdirectory(targets/${X_ARCH})        
endforeach()

So a follow build command causes the using different compilers at a one build. (The specification of (cross-)compilers is taking out of context.) The using a multiple number of compilers becomes possible by add_subdirectory().

$ cmake -DARCHS_TO_BUILD="x86_64;mips64el" && make

File 'libb.cmake' works as the macro definition and files 'targets/*/CMakeLists.txt' work as the instance of definitions.

Upvotes: 0

Listen Cheer
Listen Cheer

Reputation: 11

since CMAKE_<LANG>_COMPILER is singleton variable for each lang, we can never assign different compilers to targets with same language in "One CMake Progress". Thus the cmake is nearly no more than a script launcher.

But there is a little things cmake can do. cmake is also a build-system based on dependencies. so you may achieve what you want by declare custom_target, see add_custom_target. which i thought is better than output-insensitive process_execute。

Upvotes: 1

Related Questions