CMake: how to change the compiler for a separate purpose

I have a built-in project using cross-compiler. I would like to introduce a Google test compiled using my own GCC compiler. In addition, create multiple unit test targets using the CTC compiler.

Briefly:
I have 3 different goals and compile them with three different compilers. How to express it in CMakeLists.txt ? I have tried SET_TARGET_PROPERTIES;
but impossible to install CXX variablewith this command!

+14
source share
3 answers

, . -, GCC, avr-gcc.

, CMakeLists.txt, , , .

-:

macro(use_host_compiler)
  if (${CURRENT_COMPILER} STREQUAL "NATIVE")
    # Save current native flags
    set(NATIVE_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "GCC flags for the native compiler." FORCE)

    # Change compiler
    set(CMAKE_SYSTEM_NAME ${CMAKE_HOST_SYSTEM_NAME})
    set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
    set(CMAKE_C_COMPILER ${HOST_C_COMPILER})
    set(CMAKE_C_FLAGS ${HOST_C_FLAGS})
    set(CURRENT_COMPILER "HOST" CACHE STRING "Which compiler we are using." FORCE)
  endif()
endmacro()


macro(use_native_compiler)
  if (CMAKE_CROSSCOMPILING AND ${CURRENT_COMPILER} STREQUAL "HOST")
    # Save current host flags
    set(HOST_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "GCC flags for the host compiler." FORCE)

    # Change compiler
    set(CMAKE_SYSTEM_NAME ${NATIVE_SYSTEM_NAME})
    set(CMAKE_SYSTEM_PROCESSOR ${NATIVE_SYSTEM_PROCESSOR})
    set(CMAKE_C_COMPILER ${NATIVE_C_COMPILER})
    set(CMAKE_C_FLAGS ${NATIVE_C_FLAGS})
    set(CURRENT_COMPILER "NATIVE" CACHE STRING "Which compiler we are using." FORCE)
  endif()
endmacro()

CMakeLists.txt script ( toolchain) , :

  • CURRENT_COMPILER
  • HOST_C_COMPILER
  • HOST_C_FLAGS
  • NATIVE_SYSTEM_NAME
  • NATIVE_C_COMPILER
  • NATIVE_C_FLAGS

, CMAKE_C_COMPILER ( ) - , , .


:

use_host_compiler()
add_executable(foo foo.c) # Compiled with your host (computer) compiler.
use_native_compiler()
add_executable(bar bar.c) # Compiled with your native compiler (e.g. `avr-gcc`).
+10

.

cmake " ". CMAKE__COMPILER.

, AnthonyD973, , , , , . custom_command .

0

CMake - make. , . , CMake .

, , CMake, CMake: script, CMake.

-7

All Articles