Skip to content

Commit 4c02ffd

Browse files
committed
cmake/CustomCompiler: improve version discovery of underlying clang and gcc
- Add a quick clang version discovery if CMake already detected the compiler as Clang but we know it's something else. - Add a gcc version parsing code for the case CMake did not already detected the compiler as GCC while we know it's something else.
1 parent f1e72ef commit 4c02ffd

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

cmake/CustomCompiler.cmake

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

2727
function(detect_custom_clang_version lang file)
28+
# If CMake already detected the compiler as Clang but we know
29+
# it's something else.
30+
if(CUSTOM_${lang}_COMPILER_ID AND CMAKE_${lang}_COMPILER_ID STREQUAL "Clang")
31+
set(CUSTOM_${lang}_CLANG_VERSION "${CMAKE_${lang}_COMPILER_VERSION}")
32+
set(CUSTOM_${lang}_CLANG_VERSION "${CUSTOM_${lang}_CLANG_VERSION}" PARENT_SCOPE)
33+
return()
34+
endif()
35+
2836
# Parse “<compiler> -E -dM <source file>”
2937
execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" ${CUSTOM_${lang}_COMPILER_SUBCOMMAND} -E -dM "${file}"
3038
OUTPUT_VARIABLE CUSTOM_${lang}_CLANG_OUTPUT
@@ -39,12 +47,46 @@ function(detect_custom_clang_version lang file)
3947
endif()
4048
endfunction()
4149

42-
function(detect_custom_gcc_version lang)
43-
# This will only set the GCC version if the custom compiler is detected
44-
# as GCC by CMake.
50+
function(detect_custom_gcc_version lang file)
51+
# If CMake already detected the compiler as GCC but we know
52+
# it's something else.
4553
if(CUSTOM_${lang}_COMPILER_ID AND CMAKE_${lang}_COMPILER_ID STREQUAL "GNU")
4654
set(CUSTOM_${lang}_GCC_VERSION "${CMAKE_${lang}_COMPILER_VERSION}")
4755
set(CUSTOM_${lang}_GCC_VERSION "${CUSTOM_${lang}_GCC_VERSION}" PARENT_SCOPE)
56+
return()
57+
endif()
58+
59+
# Almost all compilers on Earth define __GNUC__, __GNUC_MINOR__, and __GNUC_PATCHLEVEL__,
60+
# So we first have to check it's really a GCC variant.
61+
# Parse “<compiler> -v”
62+
execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -v
63+
ERROR_VARIABLE CUSTOM_${lang}_GCC_OUTPUT
64+
RESULT_VARIABLE CUSTOM_${lang}_RETURN_CODE
65+
OUTPUT_QUIET)
66+
67+
if (NOT CUSTOM_${lang}_RETURN_CODE) # Success
68+
# The existence of this string tells us it's a GCC variant.
69+
# The version in this string is the same as __VERSION__,
70+
# the version of the GCC variant, not the version of the upstream
71+
# GCC we are looking for.
72+
if ("${CUSTOM_${lang}_GCC_OUTPUT}" MATCHES "\ngcc version ")
73+
# Parse “<compiler> -E -dM <source file>”
74+
# No subcommand implemented for now, there may be no usage.
75+
execute_process(COMMAND "${CMAKE_${lang}_COMPILER}" -E -dM "${file}"
76+
OUTPUT_VARIABLE CUSTOM_${lang}_GCC_OUTPUT
77+
RESULT_VARIABLE CUSTOM_${lang}_RETURN_CODE
78+
ERROR_QUIET)
79+
80+
if (NOT CUSTOM_${lang}_RETURN_CODE) # Success
81+
string(REGEX REPLACE ".*#define __GNUC__ ([^ \n]+).*" "\\1" CUSTOM_${lang}_GCC_MAJOR "${CUSTOM_${lang}_GCC_OUTPUT}")
82+
string(REGEX REPLACE ".*#define __GNUC_MINOR__ ([^ \n]+).*" "\\1" CUSTOM_${lang}_GCC_MINOR "${CUSTOM_${lang}_GCC_OUTPUT}")
83+
string(REGEX REPLACE ".*#define __GNUC_PATCHLEVEL__ ([^ \n]+).*" "\\1" CUSTOM_${lang}_GCC_PATCHLEVEL "${CUSTOM_${lang}_GCC_OUTPUT}")
84+
85+
set(CUSTOM_${lang}_GCC_VERSION "${CUSTOM_${lang}_GCC_MAJOR}.${CUSTOM_${lang}_GCC_MINOR}.${CUSTOM_${lang}_GCC_PATCHLEVEL}")
86+
set(CUSTOM_${lang}_GCC_VERSION "${CUSTOM_${lang}_GCC_VERSION}" PARENT_SCOPE)
87+
return()
88+
endif()
89+
endif()
4890
endif()
4991
endfunction()
5092

@@ -149,7 +191,7 @@ function(detect_custom_compiler lang)
149191

150192
detect_custom_compiler_id_version("${lang}" "${COMPILER_TEST_FILE}")
151193
detect_custom_clang_version("${lang}" "${COMPILER_TEST_FILE}")
152-
detect_custom_gcc_version("${lang}")
194+
detect_custom_gcc_version("${lang}" "${COMPILER_TEST_FILE}")
153195

154196
file(REMOVE "${COMPILER_TEST_FILE}")
155197

cmake/DaemonCompiler.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ foreach(lang C;CXX)
7474
detect_daemon_compiler("${lang}")
7575
endforeach()
7676

77-
if (CMAKE_COMPILER_IS_GNUCXX)
77+
if (CMAKE_COMPILER_IS_GNUCXX OR "${CUSTOM_CXX_GCC_VERSION}")
7878
set(DAEMON_COMPILER_IS_GNUCXX ON)
7979
endif()
8080

0 commit comments

Comments
 (0)