2015-01-06 21:31:30 +03:00
|
|
|
cmake_minimum_required(VERSION 3.1.0)
|
2013-10-21 18:59:40 +04:00
|
|
|
project(WriteCompilerDetectionHeader)
|
|
|
|
|
|
|
|
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
|
|
|
|
|
|
|
include(WriteCompilerDetectionHeader)
|
|
|
|
|
|
|
|
get_property(cxx_known_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES)
|
2013-11-04 04:15:43 +04:00
|
|
|
get_property(c_known_features GLOBAL PROPERTY CMAKE_C_KNOWN_FEATURES)
|
2013-10-21 18:59:40 +04:00
|
|
|
|
|
|
|
write_compiler_detection_header(
|
|
|
|
FILE "${CMAKE_CURRENT_BINARY_DIR}/test_compiler_detection.h"
|
|
|
|
PREFIX TEST
|
2015-01-19 23:49:06 +03:00
|
|
|
COMPILERS GNU Clang AppleClang MSVC SunPro
|
2013-10-21 18:59:40 +04:00
|
|
|
VERSION 3.1
|
|
|
|
PROLOG "// something"
|
|
|
|
EPILOG "// more"
|
|
|
|
FEATURES
|
2013-11-04 04:15:43 +04:00
|
|
|
${cxx_known_features} ${c_known_features}
|
2013-10-21 18:59:40 +04:00
|
|
|
)
|
|
|
|
|
2013-11-04 04:15:43 +04:00
|
|
|
if (NOT CMAKE_CXX_COMPILE_FEATURES AND NOT CMAKE_C_COMPILE_FEATURES)
|
2013-10-21 18:59:40 +04:00
|
|
|
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp"
|
|
|
|
"int main(int,char**) { return 0; }\n"
|
|
|
|
)
|
|
|
|
add_executable(WriteCompilerDetectionHeader "${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp")
|
|
|
|
|
2015-09-30 16:53:52 +03:00
|
|
|
if((UNIX OR NOT CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
|
|
|
AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
|
2015-01-18 16:59:40 +03:00
|
|
|
include(CheckCXXSourceCompiles)
|
|
|
|
check_cxx_source_compiles("#include \"${CMAKE_CURRENT_BINARY_DIR}/test_compiler_detection.h\"\nint main() { return 0; }\n"
|
|
|
|
file_include_works
|
|
|
|
)
|
|
|
|
if (file_include_works)
|
|
|
|
message(SEND_ERROR "Inclusion of ${CMAKE_CURRENT_BINARY_DIR}/test_compiler_detection.h was expected to cause an error, but did not.")
|
|
|
|
endif()
|
2013-10-21 18:59:40 +04:00
|
|
|
endif()
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
2014-06-05 15:16:56 +04:00
|
|
|
string(REGEX REPLACE "^([0-9]+)\\.[0-9]+\\.[0-9]+.*" "\\1" COMPILER_VERSION_MAJOR "${CMAKE_CXX_COMPILER_VERSION}")
|
|
|
|
string(REGEX REPLACE "^[0-9]+\\.([0-9]+)\\.[0-9]+.*" "\\1" COMPILER_VERSION_MINOR "${CMAKE_CXX_COMPILER_VERSION}")
|
|
|
|
string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" COMPILER_VERSION_PATCH "${CMAKE_CXX_COMPILER_VERSION}")
|
|
|
|
|
2013-10-21 18:59:40 +04:00
|
|
|
macro(set_defines target true_defs false_defs)
|
|
|
|
set(defines)
|
|
|
|
foreach(def ${true_defs})
|
|
|
|
list(APPEND defines ${def}=1)
|
|
|
|
endforeach()
|
|
|
|
foreach(def ${false_defs})
|
|
|
|
list(APPEND defines ${def}=0)
|
|
|
|
endforeach()
|
|
|
|
target_compile_definitions(${target}
|
|
|
|
PRIVATE
|
|
|
|
${defines}
|
2014-06-05 15:16:56 +04:00
|
|
|
EXPECTED_COMPILER_VERSION_MAJOR=${COMPILER_VERSION_MAJOR}
|
|
|
|
EXPECTED_COMPILER_VERSION_MINOR=${COMPILER_VERSION_MINOR}
|
|
|
|
EXPECTED_COMPILER_VERSION_PATCH=${COMPILER_VERSION_PATCH}
|
2013-10-21 18:59:40 +04:00
|
|
|
)
|
|
|
|
endmacro()
|
|
|
|
|
2015-01-06 21:31:30 +03:00
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
|
|
|
|
OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang"
|
2015-01-16 21:26:37 +03:00
|
|
|
OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
|
|
|
|
OR CMAKE_CXX_COMPILER_ID STREQUAL "SunPro")
|
2013-10-21 18:59:40 +04:00
|
|
|
# False for C++98 mode.
|
|
|
|
list(APPEND false_defs EXPECTED_COMPILER_CXX_DELEGATING_CONSTRUCTORS)
|
|
|
|
list(APPEND false_defs EXPECTED_COMPILER_CXX_VARIADIC_TEMPLATES)
|
|
|
|
endif()
|
|
|
|
|
2015-01-19 23:49:06 +03:00
|
|
|
# for msvc the compiler version determines which c++11 features are available.
|
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
|
|
|
if(";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";cxx_delegating_constructors;")
|
|
|
|
list(APPEND true_defs EXPECTED_COMPILER_CXX_DELEGATING_CONSTRUCTORS)
|
|
|
|
list(APPEND true_defs EXPECTED_COMPILER_CXX_VARIADIC_TEMPLATES)
|
|
|
|
else()
|
|
|
|
list(APPEND false_defs EXPECTED_COMPILER_CXX_DELEGATING_CONSTRUCTORS)
|
|
|
|
list(APPEND false_defs EXPECTED_COMPILER_CXX_VARIADIC_TEMPLATES)
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2015-01-06 21:31:30 +03:00
|
|
|
if (CMAKE_C_COMPILER_ID STREQUAL "GNU"
|
|
|
|
OR CMAKE_C_COMPILER_ID STREQUAL "Clang"
|
|
|
|
OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
|
2014-11-24 23:47:48 +03:00
|
|
|
add_executable(C_undefined c_undefined.c)
|
2015-01-19 21:43:04 +03:00
|
|
|
set_property(TARGET C_undefined PROPERTY C_STANDARD 90)
|
2014-11-24 23:47:48 +03:00
|
|
|
target_compile_options(C_undefined PRIVATE -Werror=undef)
|
2015-02-03 23:47:45 +03:00
|
|
|
|
|
|
|
add_executable(WriteCompilerDetectionHeader_C main.c)
|
|
|
|
set_property(TARGET WriteCompilerDetectionHeader_C PROPERTY C_STANDARD 90)
|
|
|
|
set_defines(WriteCompilerDetectionHeader_C "EXPECTED_COMPILER_C_FUNCTION_PROTOTYPES" "EXPECTED_COMPILER_C_RESTRICT")
|
|
|
|
|
|
|
|
add_executable(WriteCompilerDetectionHeader_C_multi main_multi.c)
|
|
|
|
set_property(TARGET WriteCompilerDetectionHeader_C_multi PROPERTY C_STANDARD 90)
|
|
|
|
set_defines(WriteCompilerDetectionHeader_C_multi "EXPECTED_COMPILER_C_FUNCTION_PROTOTYPES" "EXPECTED_COMPILER_C_RESTRICT")
|
|
|
|
target_include_directories(WriteCompilerDetectionHeader_C_multi PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/compiler_multi_files)
|
2014-11-24 23:47:48 +03:00
|
|
|
endif()
|
|
|
|
|
2013-10-21 18:59:40 +04:00
|
|
|
add_executable(WriteCompilerDetectionHeader main.cpp)
|
|
|
|
set_property(TARGET WriteCompilerDetectionHeader PROPERTY CXX_STANDARD 98)
|
|
|
|
set_defines(WriteCompilerDetectionHeader "${true_defs}" "${false_defs}")
|
|
|
|
|
2014-10-31 02:41:53 +03:00
|
|
|
write_compiler_detection_header(
|
|
|
|
FILE "${CMAKE_CURRENT_BINARY_DIR}/compiler_multi_files/multi_file_compiler_detection.h"
|
|
|
|
PREFIX MULTI
|
|
|
|
OUTPUT_FILES_VAR multi_files
|
|
|
|
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/compiler_multi_files/compiler_support"
|
2015-01-19 23:49:06 +03:00
|
|
|
COMPILERS GNU Clang AppleClang MSVC SunPro
|
2014-10-31 02:41:53 +03:00
|
|
|
VERSION 3.1
|
|
|
|
FEATURES
|
|
|
|
${cxx_known_features} ${c_known_features}
|
|
|
|
)
|
|
|
|
|
|
|
|
add_executable(multi_files multi_files.cpp)
|
|
|
|
set_property(TARGET multi_files PROPERTY CXX_STANDARD 98)
|
|
|
|
target_include_directories(multi_files PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/compiler_multi_files)
|
|
|
|
set_defines(multi_files "${true_defs}" "${false_defs}")
|
|
|
|
|
2013-10-21 18:59:40 +04:00
|
|
|
if(MSVC)
|
|
|
|
return() # MSVC has only one mode.
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Since GNU 4.7
|
|
|
|
if (";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";cxx_delegating_constructors;")
|
|
|
|
list(APPEND true_defs EXPECTED_COMPILER_CXX_DELEGATING_CONSTRUCTORS)
|
|
|
|
list(REMOVE_ITEM false_defs EXPECTED_COMPILER_CXX_DELEGATING_CONSTRUCTORS)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Since GNU 4.4
|
|
|
|
if (";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";cxx_variadic_templates;")
|
|
|
|
list(APPEND true_defs EXPECTED_COMPILER_CXX_VARIADIC_TEMPLATES)
|
|
|
|
list(REMOVE_ITEM false_defs EXPECTED_COMPILER_CXX_VARIADIC_TEMPLATES)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
add_executable(WriteCompilerDetectionHeader_11 main.cpp)
|
|
|
|
set_property(TARGET WriteCompilerDetectionHeader_11 PROPERTY CXX_STANDARD 11)
|
|
|
|
set_defines(WriteCompilerDetectionHeader_11 "${true_defs}" "${false_defs}")
|
2014-10-31 02:41:53 +03:00
|
|
|
|
|
|
|
add_executable(multi_files_11 multi_files.cpp)
|
|
|
|
set_property(TARGET multi_files_11 PROPERTY CXX_STANDARD 11)
|
|
|
|
target_include_directories(multi_files_11 PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/compiler_multi_files)
|
|
|
|
set_defines(multi_files_11 "${true_defs}" "${false_defs}")
|
2015-02-03 23:47:45 +03:00
|
|
|
|
|
|
|
if (CMAKE_C_COMPILER_ID STREQUAL "GNU"
|
|
|
|
OR CMAKE_C_COMPILER_ID STREQUAL "Clang"
|
|
|
|
OR CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
|
|
|
|
add_executable(WriteCompilerDetectionHeader_C11 main.c)
|
|
|
|
set_property(TARGET WriteCompilerDetectionHeader_C11 PROPERTY C_STANDARD 11)
|
|
|
|
set_defines(WriteCompilerDetectionHeader_C11 "EXPECTED_COMPILER_C_FUNCTION_PROTOTYPES;EXPECTED_COMPILER_C_RESTRICT" "")
|
|
|
|
|
|
|
|
add_executable(WriteCompilerDetectionHeader_C11_multi main_multi.c)
|
|
|
|
set_property(TARGET WriteCompilerDetectionHeader_C11_multi PROPERTY C_STANDARD 11)
|
|
|
|
set_defines(WriteCompilerDetectionHeader_C11_multi "EXPECTED_COMPILER_C_FUNCTION_PROTOTYPES;EXPECTED_COMPILER_C_RESTRICT" "")
|
|
|
|
target_include_directories(WriteCompilerDetectionHeader_C11_multi PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/compiler_multi_files)
|
|
|
|
endif()
|