|
| 1 | +# Daemon BSD Source Code |
| 2 | +# Copyright (c) 2024, 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 <organization> 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 <COPYRIGHT HOLDER> 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 | +# Determine compiler |
| 29 | +################################################################################ |
| 30 | + |
| 31 | +# FIXME: Force -W#pragma-messages and -Wno-error |
| 32 | +# In case there is -Wno-#pragma-messages or -Werror in CFLAGS/CXXFLAGS |
| 33 | + |
| 34 | +function(detect_daemon_compiler lang) |
| 35 | + set(C_NAME "C") |
| 36 | + set(CXX_NAME "C++") |
| 37 | + set(C_EXT ".c") |
| 38 | + set(CXX_EXT ".cpp") |
| 39 | + |
| 40 | + try_compile(BUILD_RESULT |
| 41 | + "${CMAKE_BINARY_DIR}" |
| 42 | + "${DAEMON_DIR}/cmake/DaemonCompiler/DaemonCompiler${${lang}_EXT}" |
| 43 | + CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} |
| 44 | + OUTPUT_VARIABLE BUILD_LOG |
| 45 | + ) |
| 46 | + |
| 47 | + get_filename_component(compiler_basename "${CMAKE_${lang}_COMPILER}" NAME) |
| 48 | + |
| 49 | + if (NOT BUILD_RESULT) |
| 50 | + message(WARNING "Failed to build DaemonCompiler${${lang}_EXT}, relying on CMake builtin detection.") |
| 51 | + set(compiler_name "Unknown") |
| 52 | + else() |
| 53 | + set(BUILD_LOG "\n${BUILD_LOG}\n") |
| 54 | + string(REGEX REPLACE "\n[^\n]*<REPORT<" "\n" BUILD_LOG "${BUILD_LOG}") |
| 55 | + string(REGEX REPLACE ">REPORT>[^\n]*\n" "\n" BUILD_LOG "${BUILD_LOG}") |
| 56 | + |
| 57 | + string(REGEX REPLACE ".*\nDAEMON_COMPILER_NAME=([^\n]*)\n.*" "\\1" |
| 58 | + compiler_name "${BUILD_LOG}") |
| 59 | + |
| 60 | + foreach(name GCC;Clang;generic;${compiler_name}) |
| 61 | + set(compatibility_regex ".*\nDAEMON_COMPILER_${name}_COMPATIBILITY=([^\n]*)\n.*") |
| 62 | + if ("${BUILD_LOG}" MATCHES ${compatibility_regex}) |
| 63 | + string(REGEX REPLACE ${compatibility_regex} "\\1" |
| 64 | + compiler_${name}_compatibility "${BUILD_LOG}") |
| 65 | + endif() |
| 66 | + |
| 67 | + set(version_regex ".*\nDAEMON_COMPILER_${name}_VERSION=([^\n]*)\n.*") |
| 68 | + if ("${BUILD_LOG}" MATCHES ${version_regex}) |
| 69 | + string(REGEX REPLACE ${version_regex} "\\1" |
| 70 | + compiler_${name}_version "${BUILD_LOG}") |
| 71 | + endif() |
| 72 | + |
| 73 | + set(version_string_regex ".*\nDAEMON_COMPILER_${name}_VERSION_STRING=([^\n]*)\n.*") |
| 74 | + if ("${BUILD_LOG}" MATCHES ${version_string_regex}) |
| 75 | + string(REGEX REPLACE ${version_string_regex} "\\1" |
| 76 | + compiler_${name}_version_string "${BUILD_LOG}") |
| 77 | + endif() |
| 78 | + |
| 79 | + set(DAEMON_${lang}_COMPILER_${name}_VERSION |
| 80 | + "${compiler_${name}_version}" |
| 81 | + PARENT_SCOPE) |
| 82 | + |
| 83 | + set(DAEMON_${lang}_COMPILER_${name}_COMPATIBILITY |
| 84 | + "${compiler_${name}_compatibility}" |
| 85 | + PARENT_SCOPE) |
| 86 | + endforeach() |
| 87 | + endif() |
| 88 | + |
| 89 | + if (compiler_name STREQUAL "Unknown") |
| 90 | + if (CMAKE_${lang}_COMPILER_ID) |
| 91 | + set(compiler_name "${CMAKE_${lang}_COMPILER_ID}") |
| 92 | + # Compiler version is done below. |
| 93 | + else() |
| 94 | + message(WARNING "Unknown ${${lang}_NAME} compiler") |
| 95 | + endif() |
| 96 | + endif() |
| 97 | + |
| 98 | + # AOCC |
| 99 | + if (compiler_Clang_version_string) |
| 100 | + set(aocc_version_regex ".*CLANG: AOCC_([^ )]+).*") |
| 101 | + if (compiler_Clang_version_string MATCHES ${aocc_version_regex}) |
| 102 | + set(compiler_name "AOCC") |
| 103 | + string(REGEX REPLACE ${aocc_version_regex} "\\1" |
| 104 | + compiler_AOCC_version "${compiler_Clang_version_string}") |
| 105 | + string(REGEX REPLACE "(.*)-Build.*" "\\1" |
| 106 | + compiler_AOCC_version "${compiler_AOCC_version}") |
| 107 | + endif() |
| 108 | + endif() |
| 109 | + |
| 110 | + # Zig |
| 111 | + if (compiler_Clang_version_string) |
| 112 | + set(zig_version_regex ".*[(]https://github.com/ziglang/zig-bootstrap .*[)]") |
| 113 | + if (compiler_Clang_version_string MATCHES ${zig_version_regex}) |
| 114 | + set(compiler_name "Zig") |
| 115 | + endif() |
| 116 | + |
| 117 | + # Parse “zig version” |
| 118 | + execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" version |
| 119 | + OUTPUT_VARIABLE CUSTOM_${lang}_ZIG_OUTPUT |
| 120 | + RESULT_VARIABLE CUSTOM_${lang}_ZIG_RETURN_CODE |
| 121 | + ERROR_QUIET |
| 122 | + OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 123 | + |
| 124 | + if (NOT CUSTOM_${lang}_ZIG_RETURN_CODE) # Success |
| 125 | + set(compiler_Zig_version "${CUSTOM_${lang}_ZIG_OUTPUT}") |
| 126 | + endif() |
| 127 | + endif() |
| 128 | + |
| 129 | + # Compilers that use underlying Clang version as their own version. |
| 130 | + foreach(name in AppleClang) |
| 131 | + if (compiler_name STREQUAL "${name}") |
| 132 | + set(compiler_${name}_version "${compiler_Clang_version}") |
| 133 | + endif() |
| 134 | + endforeach() |
| 135 | + |
| 136 | + # Compilers that write the version number at the beginning of the VERSION string. |
| 137 | + set(string_version_regex "([^ ]+).*") |
| 138 | + foreach(name in PNaCl) |
| 139 | + if (compiler_name STREQUAL "${name}") |
| 140 | + if (compiler_generic_version_string) |
| 141 | + if (compiler_generic_version_string MATCHES ${string_version_regex}) |
| 142 | + string(REGEX REPLACE ${string_version_regex} "\\1" |
| 143 | + compiler_${name}_version "${compiler_generic_version_string}") |
| 144 | + endif() |
| 145 | + endif() |
| 146 | + endif() |
| 147 | + endforeach() |
| 148 | + |
| 149 | + if (compiler_ARMClang_version_string) |
| 150 | + # There is no __armclang_patchlevel__ so we should parse __armclang_version__ to get it. |
| 151 | + if (compiler_ARMClang_version_string MATCHES ${string_version_regex}) |
| 152 | + string(REGEX REPLACE ${string_version_regex} "\\1" |
| 153 | + compiler_ARMClang_version "${compiler_ARMClang_version_string}") |
| 154 | + endif() |
| 155 | + endif() |
| 156 | + |
| 157 | + if (compiler_ICX_version) |
| 158 | + # 20240000 becomes 2024.0.0 |
| 159 | + string(REGEX REPLACE "(....)(..)(..)" "\\1.\\2.\\3" |
| 160 | + compiler_ICX_version "${compiler_ICX_version}") |
| 161 | + string(REGEX REPLACE "\\.0" "." |
| 162 | + compiler_ICX_version "${compiler_ICX_version}") |
| 163 | + endif() |
| 164 | + |
| 165 | + if (compiler_${compiler_name}_version) |
| 166 | + set(compiler_version "${compiler_${compiler_name}_version}") |
| 167 | + elseif (CMAKE_${lang}_COMPILER_VERSION) |
| 168 | + set(compiler_version "${CMAKE_${lang}_COMPILER_VERSION}") |
| 169 | + else() |
| 170 | + set(compiler_version "Unknown") |
| 171 | + message(WARNING "Unknown ${${lang}_NAME} compiler version") |
| 172 | + endif() |
| 173 | + |
| 174 | + set(DAEMON_${lang}_COMPILER_BASENAME "${compiler_basename}" PARENT_SCOPE) |
| 175 | + set(DAEMON_${lang}_COMPILER_NAME "${compiler_name}" PARENT_SCOPE) |
| 176 | + set(DAEMON_${lang}_COMPILER_VERSION "${compiler_version}" PARENT_SCOPE) |
| 177 | +endfunction() |
| 178 | + |
| 179 | +message(STATUS "CMake generator: ${CMAKE_GENERATOR}") |
| 180 | + |
| 181 | +foreach(lang C;CXX) |
| 182 | + set(C_NAME "C") |
| 183 | + set(CXX_NAME "C++") |
| 184 | + |
| 185 | + if (MSVC) |
| 186 | + # Let CMake do the job, it does it very well, |
| 187 | + # and there is probably no variant to take care about. |
| 188 | + set(DAEMON_${lang}_COMPILER_NAME "${CMAKE_${lang}_COMPILER_ID}") |
| 189 | + set(DAEMON_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}") |
| 190 | + get_filename_component(DAEMON_${lang}_COMPILER_BASENAME "${CMAKE_${lang}_COMPILER}" NAME) |
| 191 | + else() |
| 192 | + detect_daemon_compiler(${lang}) |
| 193 | + |
| 194 | + if (DAEMON_${lang}_COMPILER_Clang_COMPATIBILITY) |
| 195 | + if (NOT DAEMON_${lang}_COMPILER_NAME STREQUAL "Clang") |
| 196 | + set(DAEMON_${lang}_COMPILER_EXTENDED_VERSION |
| 197 | + "${DAEMON_${lang}_COMPILER_VERSION}/clang-${DAEMON_${lang}_COMPILER_Clang_VERSION}") |
| 198 | + endif() |
| 199 | + elseif (DAEMON_${lang}_COMPILER_GCC_COMPATIBILITY) |
| 200 | + if (NOT DAEMON_${lang}_COMPILER_NAME STREQUAL "GCC") |
| 201 | + # Almost all compilers on Earth pretend to be GCC compatible. |
| 202 | + # So we first have to check it's really a GCC variant. |
| 203 | + # Parse “<compiler> -v” |
| 204 | + execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -v |
| 205 | + ERROR_VARIABLE CUSTOM_${lang}_GCC_OUTPUT |
| 206 | + RESULT_VARIABLE CUSTOM_${lang}_GCC_RETURN_CODE |
| 207 | + OUTPUT_QUIET) |
| 208 | + |
| 209 | + if (NOT CUSTOM_${lang}_GCC_RETURN_CODE) # Success |
| 210 | + # The existence of this string tells us it's a GCC variant. |
| 211 | + # The version in this string is the same as __VERSION__, |
| 212 | + # the version of the GCC variant, not the version of the upstream |
| 213 | + # GCC we are looking for. |
| 214 | + if ("${CUSTOM_${lang}_GCC_OUTPUT}" MATCHES "\ngcc version ") |
| 215 | + set(DAEMON_${lang}_COMPILER_EXTENDED_VERSION |
| 216 | + "${DAEMON_${lang}_COMPILER_VERSION}/gcc-${DAEMON_${lang}_COMPILER_GCC_VERSION}") |
| 217 | + endif() |
| 218 | + endif() |
| 219 | + endif() |
| 220 | + endif() |
| 221 | + endif() |
| 222 | + |
| 223 | + if (NOT DAEMON_${lang}_COMPILER_EXTENDED_VERSION) |
| 224 | + set(DAEMON_${lang}_COMPILER_EXTENDED_VERSION "${DAEMON_${lang}_COMPILER_VERSION}") |
| 225 | + endif() |
| 226 | + |
| 227 | + set(DAEMON_${lang}_COMPILER_STRING |
| 228 | + "${DAEMON_${lang}_COMPILER_NAME} ${DAEMON_${lang}_COMPILER_EXTENDED_VERSION} ${DAEMON_${lang}_COMPILER_BASENAME}") |
| 229 | + |
| 230 | + message(STATUS "Detected ${${lang}_NAME} compiler: ${DAEMON_${lang}_COMPILER_STRING}") |
| 231 | + |
| 232 | + add_definitions(-DDAEMON_${lang}_COMPILER_${DAEMON_${lang}_COMPILER_NAME}=1) |
| 233 | + |
| 234 | + # Preprocessor definitions containing '#' may not be passed on the compiler |
| 235 | + # command line because many compilers do not support it. |
| 236 | + string(REGEX REPLACE "\#" "~" DAEMON_DEFINE_${lang}_COMPILER_STRING "${DAEMON_${lang}_COMPILER_STRING}") |
| 237 | + |
| 238 | + # Quotes cannot be part of the define as support for them is not reliable. |
| 239 | + add_definitions(-DDAEMON_${lang}_COMPILER_STRING=${DAEMON_DEFINE_${lang}_COMPILER_STRING}) |
| 240 | +endforeach() |
0 commit comments