test_compile_features.cmake 1.68 KB
Newer Older
1

Praetorius, Simon's avatar
Praetorius, Simon committed
2
macro(test_compile_features RESULT_VAR TARGET SCOPE) # feature1 feature2 feature3 ...)
3 4 5 6
    set(_FEATURES "")
    foreach(FEATURE ${ARGN})
        set(_FEATURES "${_FEATURES} ${FEATURE}")
    endforeach()
Praetorius, Simon's avatar
Praetorius, Simon committed
7
    set(_DIR ${CMAKE_BINARY_DIR}/test_cxx11)
8 9 10

    # test only of variable RESULT_VAR not yet set manually or by a
    # previous call to this macro
Praetorius, Simon's avatar
Praetorius, Simon committed
11
    if (NOT DEFINED ${RESULT_VAR})
12 13 14 15 16
        if (EXISTS "${BASE_DIR}/cmake3/dummy.cc")
          set(DUMMY_FILE ${BASE_DIR}/cmake3/dummy.cc)
        else ()
          set(DUMMY_FILE ${AMDIS_DIR}/dummy.cc)
        endif ()
17

Praetorius, Simon's avatar
Praetorius, Simon committed
18 19 20 21 22
        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)
23
            add_library(test_cxx ${DUMMY_FILE})
Praetorius, Simon's avatar
Praetorius, Simon committed
24 25 26 27 28 29 30 31 32 33 34
            target_compile_features(test_cxx PRIVATE ${_FEATURES})")

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

        execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory ${_DIR})
    elseif (${RESULT_VAR})
        set(_ERR_CODE "0")
    else ()
        set(_ERR_CODE "1")
35 36
    endif ()

Praetorius, Simon's avatar
Praetorius, Simon committed
37
    if ("${_ERR_CODE}" STREQUAL "0")
38 39 40 41 42 43 44
        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})

Praetorius, Simon's avatar
Praetorius, Simon committed
45 46 47 48
    unset(_ERR_CODE)
    unset(_DIR)
    unset(_FEATURES)
endmacro(test_compile_features)