|
| 1 | +# Daemon BSD Source Code |
| 2 | +# Copyright (c) 2024-2026, Daemon Developers |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are met: |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# * Neither the name of the Daemon developers nor the |
| 13 | +# names of its contributors may be used to endorse or promote products |
| 14 | +# derived from this software without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +# DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY |
| 20 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +################################################################################ |
| 28 | +# Compiler detection. |
| 29 | +################################################################################ |
| 30 | + |
| 31 | +# When adding a new compiler, look at all the places YOKAI_C_COMPILER |
| 32 | +# and YOKAI_CXX_COMPILER are used. |
| 33 | + |
| 34 | +function(yokai_detect_compiler lang) |
| 35 | + set(C_NAME "C") |
| 36 | + set(CXX_NAME "C++") |
| 37 | + set(C_EXT ".c") |
| 38 | + set(CXX_EXT ".cpp") |
| 39 | + |
| 40 | + get_filename_component(compiler_basename "${CMAKE_${lang}_COMPILER}" NAME) |
| 41 | + |
| 42 | + yokai_run_detection("${lang}" "COMPILER" "Compiler${${lang}_EXT}" "GCC;Clang;generic") |
| 43 | + |
| 44 | + if (compiler_name STREQUAL "Unknown") |
| 45 | + if (CMAKE_${lang}_COMPILER_ID) |
| 46 | + set(compiler_name "${CMAKE_${lang}_COMPILER_ID}") |
| 47 | + # Compiler version is done below. |
| 48 | + else() |
| 49 | + message(WARNING "Unknown ${${lang}_NAME} compiler") |
| 50 | + endif() |
| 51 | + endif() |
| 52 | + |
| 53 | + # AOCC |
| 54 | + if (compiler_Clang_version_string) |
| 55 | + set(aocc_version_regex ".*CLANG: AOCC_([^ )]+).*") |
| 56 | + if (compiler_Clang_version_string MATCHES ${aocc_version_regex}) |
| 57 | + set(compiler_name "AOCC") |
| 58 | + string(REGEX REPLACE ${aocc_version_regex} "\\1" |
| 59 | + compiler_AOCC_version "${compiler_Clang_version_string}") |
| 60 | + string(REGEX REPLACE "(.*)-Build.*" "\\1" |
| 61 | + compiler_AOCC_version "${compiler_AOCC_version}") |
| 62 | + endif() |
| 63 | + endif() |
| 64 | + |
| 65 | + # Zig |
| 66 | + if (compiler_Clang_version_string) |
| 67 | + set(zig_version_regex ".*[(]https://github.com/ziglang/zig-bootstrap .*[)]") |
| 68 | + if (compiler_Clang_version_string MATCHES ${zig_version_regex}) |
| 69 | + set(compiler_name "Zig") |
| 70 | + endif() |
| 71 | + |
| 72 | + # Parse “zig version” |
| 73 | + execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" version |
| 74 | + OUTPUT_VARIABLE CUSTOM_${lang}_ZIG_OUTPUT |
| 75 | + RESULT_VARIABLE CUSTOM_${lang}_ZIG_RETURN_CODE |
| 76 | + ERROR_QUIET |
| 77 | + OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 78 | + |
| 79 | + if (NOT CUSTOM_${lang}_ZIG_RETURN_CODE) # Success |
| 80 | + set(compiler_Zig_version "${CUSTOM_${lang}_ZIG_OUTPUT}") |
| 81 | + endif() |
| 82 | + endif() |
| 83 | + |
| 84 | + # Compilers that use the underlying Clang version as their own version. |
| 85 | + foreach(name in AppleClang) |
| 86 | + if (compiler_name STREQUAL "${name}") |
| 87 | + set(compiler_${name}_version "${compiler_Clang_version}") |
| 88 | + endif() |
| 89 | + endforeach() |
| 90 | + |
| 91 | + # Compilers that write the version number at the beginning of the VERSION string. |
| 92 | + set(string_version_regex "([^ ]+).*") |
| 93 | + foreach(name in PNaCl) |
| 94 | + if (compiler_name STREQUAL "${name}") |
| 95 | + if (compiler_generic_version_string) |
| 96 | + if (compiler_generic_version_string MATCHES ${string_version_regex}) |
| 97 | + string(REGEX REPLACE ${string_version_regex} "\\1" |
| 98 | + compiler_${name}_version "${compiler_generic_version_string}") |
| 99 | + endif() |
| 100 | + endif() |
| 101 | + endif() |
| 102 | + endforeach() |
| 103 | + |
| 104 | + if (compiler_ARMClang_version_string) |
| 105 | + # There is no __armclang_patchlevel__ so we should parse __armclang_version__ to get it. |
| 106 | + if (compiler_ARMClang_version_string MATCHES ${string_version_regex}) |
| 107 | + string(REGEX REPLACE ${string_version_regex} "\\1" |
| 108 | + compiler_ARMClang_version "${compiler_ARMClang_version_string}") |
| 109 | + endif() |
| 110 | + endif() |
| 111 | + |
| 112 | + if (compiler_ICX_version) |
| 113 | + # 20240000 becomes 2024.0.0 |
| 114 | + string(REGEX REPLACE "(....)(..)(..)" "\\1.\\2.\\3" |
| 115 | + compiler_ICX_version "${compiler_ICX_version}") |
| 116 | + string(REGEX REPLACE "\\.0" "." |
| 117 | + compiler_ICX_version "${compiler_ICX_version}") |
| 118 | + endif() |
| 119 | + |
| 120 | + if (compiler_${compiler_name}_version) |
| 121 | + set(compiler_version "${compiler_${compiler_name}_version}") |
| 122 | + elseif (CMAKE_${lang}_COMPILER_VERSION) |
| 123 | + set(compiler_version "${CMAKE_${lang}_COMPILER_VERSION}") |
| 124 | + else() |
| 125 | + set(compiler_version "Unknown") |
| 126 | + message(WARNING "Unknown ${${lang}_NAME} compiler version") |
| 127 | + endif() |
| 128 | + |
| 129 | + set(YOKAI_${lang}_COMPILER_BASENAME "${compiler_basename}" PARENT_SCOPE) |
| 130 | + set(YOKAI_${lang}_COMPILER_NAME "${compiler_name}" PARENT_SCOPE) |
| 131 | + set(YOKAI_${lang}_COMPILER_NAME_UPPER "${compiler_name_upper}" PARENT_SCOPE) |
| 132 | + set(YOKAI_${lang}_COMPILER_VERSION "${compiler_version}" PARENT_SCOPE) |
| 133 | +endfunction() |
| 134 | + |
| 135 | +message(STATUS "CMake generator: ${CMAKE_GENERATOR}") |
| 136 | + |
| 137 | +foreach(lang C;CXX) |
| 138 | + set(C_NAME "C") |
| 139 | + set(CXX_NAME "C++") |
| 140 | + |
| 141 | + if (MSVC) |
| 142 | + set(YOKAI_${lang}_COMPILER_MSVC_COMPATIBILITY ON) |
| 143 | + endif() |
| 144 | + |
| 145 | + # When MSVC is ON, ${CMAKE_${lang}_COMPILER_ID} can be either MSVC |
| 146 | + # or Clang (clang-cl), Only MSVC is true MSVC. |
| 147 | + if (MSVC AND "${CMAKE_${lang}_COMPILER_ID}" STREQUAL "MSVC") |
| 148 | + set(YOKAI_${lang}_COMPILER_NAME "MSVC") |
| 149 | + set(YOKAI_${lang}_COMPILER_NAME_UPPER "MSVC") |
| 150 | + |
| 151 | + # Let CMake do the job, it does it well, |
| 152 | + set(YOKAI_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}") |
| 153 | + |
| 154 | + get_filename_component(YOKAI_${lang}_COMPILER_BASENAME "${CMAKE_${lang}_COMPILER}" NAME) |
| 155 | + else() |
| 156 | + yokai_detect_compiler(${lang}) |
| 157 | + |
| 158 | + if (YOKAI_${lang}_COMPILER_CLANG_COMPATIBILITY) |
| 159 | + if (NOT YOKAI_${lang}_COMPILER_NAME STREQUAL "Clang") |
| 160 | + set(YOKAI_${lang}_COMPILER_EXTENDED_VERSION |
| 161 | + "${YOKAI_${lang}_COMPILER_VERSION}/Clang_${YOKAI_${lang}_COMPILER_CLANG_VERSION}") |
| 162 | + endif() |
| 163 | + elseif (YOKAI_${lang}_COMPILER_GCC_COMPATIBILITY) |
| 164 | + if (NOT YOKAI_${lang}_COMPILER_NAME STREQUAL "GCC") |
| 165 | + # Almost all compilers on Earth pretend to be GCC compatible. |
| 166 | + # So we first have to check it's really a GCC variant. |
| 167 | + # Parse “<compiler> -v” |
| 168 | + execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -v |
| 169 | + ERROR_VARIABLE CUSTOM_${lang}_GCC_OUTPUT |
| 170 | + RESULT_VARIABLE CUSTOM_${lang}_GCC_RETURN_CODE |
| 171 | + OUTPUT_QUIET) |
| 172 | + |
| 173 | + if (NOT CUSTOM_${lang}_GCC_RETURN_CODE) # Success |
| 174 | + # The existence of this string tells us it's a GCC variant. |
| 175 | + # The version in this string is the same as __VERSION__, |
| 176 | + # the version of the GCC variant, not the version of the upstream |
| 177 | + # GCC we are looking for. |
| 178 | + if ("${CUSTOM_${lang}_GCC_OUTPUT}" MATCHES "\ngcc version ") |
| 179 | + set(YOKAI_${lang}_COMPILER_EXTENDED_VERSION |
| 180 | + "${YOKAI_${lang}_COMPILER_VERSION}/GCC_${YOKAI_${lang}_COMPILER_GCC_VERSION}") |
| 181 | + endif() |
| 182 | + endif() |
| 183 | + endif() |
| 184 | + endif() |
| 185 | + endif() |
| 186 | + |
| 187 | + if (NOT YOKAI_${lang}_COMPILER_EXTENDED_VERSION) |
| 188 | + set(YOKAI_${lang}_COMPILER_EXTENDED_VERSION "${YOKAI_${lang}_COMPILER_VERSION}") |
| 189 | + endif() |
| 190 | + |
| 191 | + set(YOKAI_${lang}_COMPILER_STRING |
| 192 | + "${YOKAI_${lang}_COMPILER_NAME}_${YOKAI_${lang}_COMPILER_EXTENDED_VERSION}:${YOKAI_${lang}_COMPILER_BASENAME}") |
| 193 | + |
| 194 | + if (CMAKE_CXX_COMPILER_ARG1) |
| 195 | + set(YOKAI_${lang}_COMPILER_STRING "${YOKAI_${lang}_COMPILER_STRING}:${CMAKE_CXX_COMPILER_ARG1}") |
| 196 | + endif() |
| 197 | + |
| 198 | + message(STATUS "Detected ${${lang}_NAME} compiler: ${YOKAI_${lang}_COMPILER_STRING}") |
| 199 | + |
| 200 | + # Makes possible to do that in C++ code: |
| 201 | + # > if defined(YOKAI_CXX_COMPILER_CLANG) |
| 202 | + set(compiler_var_name "YOKAI_${lang}_COMPILER_${YOKAI_${lang}_COMPILER_NAME_UPPER}") |
| 203 | + add_definitions(-D${compiler_var_name}) |
| 204 | + |
| 205 | + # Makes possible to do that in CMake code: |
| 206 | + # > if (YOKAI_CXX_COMPILER_CLANG) |
| 207 | + set("${compiler_var_name}" ON) |
| 208 | + |
| 209 | + if (YOKAI_SOURCE_GENERATOR) |
| 210 | + # Add printable string to the executable. |
| 211 | + yokai_add_buildinfo("char*" "YOKAI_${lang}_COMPILER_STRING" "\"${YOKAI_${lang}_COMPILER_STRING}\"") |
| 212 | + endif() |
| 213 | +endforeach() |
0 commit comments