2525# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626
2727function (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 ()
4048endfunction ()
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 "\n gcc 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 ()
4991endfunction ()
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
0 commit comments