cmake/Modules/CMakeCompilerIdDetection.cmake

158 lines
4.7 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2015-04-27 22:25:09 +02:00
function(_readFile file)
include(${file})
get_filename_component(name ${file} NAME_WE)
string(REGEX REPLACE "-.*" "" CompilerId ${name})
set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE)
set(_compiler_id_simulate_${CompilerId} ${_compiler_id_simulate} PARENT_SCOPE)
set(_compiler_id_pp_test_${CompilerId} ${_compiler_id_pp_test} PARENT_SCOPE)
endfunction()
function(compiler_id_detection outvar lang)
2021-09-14 00:13:48 +02:00
if (NOT "x${lang}" STREQUAL "xFortran" AND NOT "x${lang}" STREQUAL "xCSharp"
AND NOT "x${lang}" STREQUAL "xISPC")
2015-04-27 22:25:09 +02:00
file(GLOB lang_files
"${CMAKE_ROOT}/Modules/Compiler/*-DetermineCompiler.cmake")
set(nonlang CXX)
2021-09-14 00:13:48 +02:00
if ("x${lang}" STREQUAL "xCXX")
2015-04-27 22:25:09 +02:00
set(nonlang C)
endif()
file(GLOB nonlang_files
"${CMAKE_ROOT}/Modules/Compiler/*-${nonlang}-DetermineCompiler.cmake")
list(REMOVE_ITEM lang_files ${nonlang_files})
endif()
set(files ${lang_files})
if (files)
foreach(file ${files})
_readFile(${file})
endforeach()
set(options ID_STRING VERSION_STRINGS ID_DEFINE PLATFORM_DEFAULT_COMPILER)
set(oneValueArgs PREFIX)
cmake_parse_arguments(CID "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if (CID_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Unrecognized arguments: \"${CID_UNPARSED_ARGUMENTS}\"")
endif()
# Order is relevant here. For example, compilers which pretend to be
# GCC must appear before the actual GCC.
2021-09-14 00:13:48 +02:00
if ("x${lang}" STREQUAL "xCXX")
2015-04-27 22:25:09 +02:00
list(APPEND ordered_compilers
Comeau
)
endif()
list(APPEND ordered_compilers
Intel
2021-09-14 00:13:48 +02:00
IntelLLVM
2015-04-27 22:25:09 +02:00
PathScale
Embarcadero
Borland
Watcom
OpenWatcom
SunPro
HP
Compaq
zOS
2019-11-11 23:01:05 +01:00
XLClang
2015-04-27 22:25:09 +02:00
XL
VisualAge
2021-09-14 00:13:48 +02:00
NVHPC
2015-04-27 22:25:09 +02:00
PGI
Cray
TI
2021-09-14 00:13:48 +02:00
FujitsuClang
2015-04-27 22:25:09 +02:00
Fujitsu
2019-11-11 23:01:05 +01:00
GHS
2015-04-27 22:25:09 +02:00
)
2021-09-14 00:13:48 +02:00
if ("x${lang}" STREQUAL "xC")
2015-04-27 22:25:09 +02:00
list(APPEND ordered_compilers
TinyCC
2016-07-09 11:21:54 +02:00
Bruce
2015-04-27 22:25:09 +02:00
)
endif()
list(APPEND ordered_compilers
SCO
2019-11-11 23:01:05 +01:00
ARMCC
2015-04-27 22:25:09 +02:00
AppleClang
2019-11-11 23:01:05 +01:00
ARMClang
2021-09-14 00:13:48 +02:00
)
list(APPEND ordered_compilers
2015-04-27 22:25:09 +02:00
Clang
GNU
MSVC
ADSP
IAR
)
2021-09-14 00:13:48 +02:00
if ("x${lang}" STREQUAL "xC")
2015-04-27 22:25:09 +02:00
list(APPEND ordered_compilers
SDCC
)
endif()
2021-09-14 00:13:48 +02:00
if("x${lang}" STREQUAL "xCUDA")
2020-08-30 11:54:41 +02:00
set(ordered_compilers NVIDIA Clang)
2017-04-14 19:02:05 +02:00
endif()
2015-04-27 22:25:09 +02:00
if(CID_ID_DEFINE)
foreach(Id ${ordered_compilers})
2017-07-20 19:35:53 +02:00
string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_${Id} 0\n")
2015-04-27 22:25:09 +02:00
endforeach()
2019-11-11 23:01:05 +01:00
# Hard-code definitions for compilers that are no longer supported.
string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_MIPSpro 0\n")
2015-04-27 22:25:09 +02:00
endif()
set(pp_if "#if")
if (CID_VERSION_STRINGS)
2017-07-20 19:35:53 +02:00
string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n/* Version number components: V=Version, R=Revision, P=Patch
2015-04-27 22:25:09 +02:00
Version date components: YYYY=Year, MM=Month, DD=Day */\n")
endif()
foreach(Id ${ordered_compilers})
if (NOT _compiler_id_pp_test_${Id})
message(FATAL_ERROR "No preprocessor test for \"${Id}\"")
endif()
set(id_content "${pp_if} ${_compiler_id_pp_test_${Id}}\n")
if (CID_ID_STRING)
set(PREFIX ${CID_PREFIX})
string(CONFIGURE "${_compiler_id_simulate_${Id}}" SIMULATE_BLOCK @ONLY)
2016-10-30 18:24:19 +01:00
string(APPEND id_content "# define ${CID_PREFIX}COMPILER_ID \"${Id}\"${SIMULATE_BLOCK}")
2015-04-27 22:25:09 +02:00
endif()
if (CID_ID_DEFINE)
2016-10-30 18:24:19 +01:00
string(APPEND id_content "# undef ${CID_PREFIX}COMPILER_IS_${Id}\n")
string(APPEND id_content "# define ${CID_PREFIX}COMPILER_IS_${Id} 1\n")
2015-04-27 22:25:09 +02:00
endif()
if (CID_VERSION_STRINGS)
set(PREFIX ${CID_PREFIX})
set(MACRO_DEC DEC)
set(MACRO_HEX HEX)
string(CONFIGURE "${_compiler_id_version_compute_${Id}}" VERSION_BLOCK @ONLY)
2016-10-30 18:24:19 +01:00
string(APPEND id_content "${VERSION_BLOCK}\n")
2015-04-27 22:25:09 +02:00
endif()
2017-07-20 19:35:53 +02:00
string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${id_content}")
2015-04-27 22:25:09 +02:00
set(pp_if "#elif")
endforeach()
if (CID_PLATFORM_DEFAULT_COMPILER)
set(platform_compiler_detection "
/* These compilers are either not known or too old to define an
identification macro. Try to identify the platform and guess that
it is the native compiler. */
#elif defined(__hpux) || defined(__hpua)
# define ${CID_PREFIX}COMPILER_ID \"HP\"
#else /* unknown compiler */
# define ${CID_PREFIX}COMPILER_ID \"\"")
endif()
2017-07-20 19:35:53 +02:00
string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${platform_compiler_detection}\n#endif")
2015-04-27 22:25:09 +02:00
endif()
set(${outvar} ${CMAKE_${lang}_COMPILER_ID_CONTENT} PARENT_SCOPE)
endfunction()