test_compile_features.cmake 1.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

macro(test_compile_features RESULT_VAR TARGET SCOPE) # feature1 feature2 feature3 ...)
    set(_FEATURES "")
    foreach(FEATURE ${ARGN})
        set(_FEATURES "${_FEATURES} ${FEATURE}")
    endforeach()

    # test only of variable RESULT_VAR not yet set manually or by a
    # previous call to this macro
    if (DEFINED ${RESULT_VAR})
        return ()
    endif ()

    set(_DIR ${BASE_DIR}/cmake3/tmp)

    message(STATUS "Test for cxx11 features for target ${TARGET}")
    execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${_DIR})
    file(WRITE ${_DIR}/CMakeLists.txt "
        cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
        project(TestCxx LANGUAGES CXX)
        add_library(test_cxx ${BASE_DIR}/cmake3/dummy.cc)
        target_compile_features(test_cxx PRIVATE ${_FEATURES})")

    execute_process(COMMAND ${CMAKE_COMMAND} ${_DIR}
        WORKING_DIRECTORY ${_DIR}
        RESULT_VARIABLE _RES_VAR OUTPUT_QUIET)

    execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${_DIR})

    if ("${_RES_VAR}" STREQUAL "0")
        set(${RESULT_VAR} true CACHE BOOL "Enable C++11 compiler features")
        target_compile_features(${TARGET} ${SCOPE} ${ARGN})
    else ()
        set(${RESULT_VAR} false CACHE BOOL "Enable C++11 compiler features")
    endif ()
    mark_as_advanced(${RESULT_VAR})

    unset(_RES_VAR)
    unset(_DIR)
    unset(_FEATURES)
endmacro(test_compile_features)