Commit 72ed051b authored by Michael Maltese's avatar Michael Maltese Committed by Brad King

CMakeDetermineCompilerId: check with and without user-specified flags

Clang may raise an error when passed a `-march=` option that doesn't
correspond to the current target triple.  CMake cannot pass the target
triple when determining the compiler id because it doesn't know how yet,
but it does pass along user-specified flags.  This breaks when those
user-specified flags include `-march=`.  Fix this use case by also
trying to find the compiler id without the user-specified flags.

Fixes: #16587
parent 0f4dae0a
...@@ -93,8 +93,8 @@ if(NOT CMAKE_ASM${ASM_DIALECT}_COMPILER_ID) ...@@ -93,8 +93,8 @@ if(NOT CMAKE_ASM${ASM_DIALECT}_COMPILER_ID)
set(CMAKE_ASM${ASM_DIALECT}_COMPILER_ID_VENDOR_REGEX_ARMCC "(ARM Compiler)|(ARM Assembler)") set(CMAKE_ASM${ASM_DIALECT}_COMPILER_ID_VENDOR_REGEX_ARMCC "(ARM Compiler)|(ARM Assembler)")
include(CMakeDetermineCompilerId) include(CMakeDetermineCompilerId)
CMAKE_DETERMINE_COMPILER_ID_VENDOR(ASM${ASM_DIALECT}) set(userflags)
CMAKE_DETERMINE_COMPILER_ID_VENDOR(ASM${ASM_DIALECT} "${userflags}")
endif() endif()
if(CMAKE_ASM${ASM_DIALECT}_COMPILER_ID) if(CMAKE_ASM${ASM_DIALECT}_COMPILER_ID)
......
...@@ -24,16 +24,21 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) ...@@ -24,16 +24,21 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
# Try building with no extra flags and then try each set # Try building with no extra flags and then try each set
# of helper flags. Stop when the compiler is identified. # of helper flags. Stop when the compiler is identified.
foreach(flags ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS_FIRST} foreach(userflags "${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" "")
"" foreach(testflags ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS_FIRST}
${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS}) ""
CMAKE_DETERMINE_COMPILER_ID_BUILD("${lang}" "${flags}" "${src}") ${CMAKE_${lang}_COMPILER_ID_TEST_FLAGS})
CMAKE_DETERMINE_COMPILER_ID_MATCH_VENDOR("${lang}" "${COMPILER_${lang}_PRODUCED_OUTPUT}") CMAKE_DETERMINE_COMPILER_ID_BUILD("${lang}" "${testflags}" "${userflags}" "${src}")
if(CMAKE_${lang}_COMPILER_ID) CMAKE_DETERMINE_COMPILER_ID_MATCH_VENDOR("${lang}" "${COMPILER_${lang}_PRODUCED_OUTPUT}")
break() if(CMAKE_${lang}_COMPILER_ID)
endif() break()
foreach(file ${COMPILER_${lang}_PRODUCED_FILES}) endif()
CMAKE_DETERMINE_COMPILER_ID_CHECK("${lang}" "${CMAKE_${lang}_COMPILER_ID_DIR}/${file}" "${src}") foreach(file ${COMPILER_${lang}_PRODUCED_FILES})
CMAKE_DETERMINE_COMPILER_ID_CHECK("${lang}" "${CMAKE_${lang}_COMPILER_ID_DIR}/${file}" "${src}")
endforeach()
if(CMAKE_${lang}_COMPILER_ID)
break()
endif()
endforeach() endforeach()
if(CMAKE_${lang}_COMPILER_ID) if(CMAKE_${lang}_COMPILER_ID)
break() break()
...@@ -42,7 +47,9 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) ...@@ -42,7 +47,9 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
# If the compiler is still unknown, try to query its vendor. # If the compiler is still unknown, try to query its vendor.
if(CMAKE_${lang}_COMPILER AND NOT CMAKE_${lang}_COMPILER_ID) if(CMAKE_${lang}_COMPILER AND NOT CMAKE_${lang}_COMPILER_ID)
CMAKE_DETERMINE_COMPILER_ID_VENDOR(${lang}) foreach(userflags "${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" "")
CMAKE_DETERMINE_COMPILER_ID_VENDOR(${lang} "${userflags}")
endforeach()
endif() endif()
if (COMPILER_QNXNTO AND CMAKE_${lang}_COMPILER_ID STREQUAL "GNU") if (COMPILER_QNXNTO AND CMAKE_${lang}_COMPILER_ID STREQUAL "GNU")
...@@ -66,7 +73,9 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) ...@@ -66,7 +73,9 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
endif() endif()
if(CMAKE_GENERATOR STREQUAL "Ninja" AND MSVC_${lang}_ARCHITECTURE_ID) if(CMAKE_GENERATOR STREQUAL "Ninja" AND MSVC_${lang}_ARCHITECTURE_ID)
CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX(${lang}) foreach(userflags "${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST}" "")
CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX(${lang} "${userflags}")
endforeach()
else() else()
set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "") set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "")
endif() endif()
...@@ -127,7 +136,7 @@ endfunction() ...@@ -127,7 +136,7 @@ endfunction()
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# Function to build the compiler id source file and look for output # Function to build the compiler id source file and look for output
# files. # files.
function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src) function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags userflags src)
# Create a clean working directory. # Create a clean working directory.
file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR}) file(REMOVE_RECURSE ${CMAKE_${lang}_COMPILER_ID_DIR})
file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}) file(MAKE_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR})
...@@ -137,7 +146,7 @@ function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src) ...@@ -137,7 +146,7 @@ function(CMAKE_DETERMINE_COMPILER_ID_BUILD lang testflags src)
# Construct a description of this test case. # Construct a description of this test case.
set(COMPILER_DESCRIPTION set(COMPILER_DESCRIPTION
"Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1} "Compiler: ${CMAKE_${lang}_COMPILER} ${CMAKE_${lang}_COMPILER_ID_ARG1}
Build flags: ${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} Build flags: ${userflags}
Id flags: ${testflags} ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS} Id flags: ${testflags} ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS}
") ")
...@@ -327,7 +336,7 @@ Id flags: ${testflags} ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS} ...@@ -327,7 +336,7 @@ Id flags: ${testflags} ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS}
execute_process( execute_process(
COMMAND "${CMAKE_${lang}_COMPILER}" COMMAND "${CMAKE_${lang}_COMPILER}"
${CMAKE_${lang}_COMPILER_ID_ARG1} ${CMAKE_${lang}_COMPILER_ID_ARG1}
${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} ${userflags}
${testflags} ${testflags}
${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS} ${CMAKE_${lang}_COMPILER_ID_FLAGS_ALWAYS}
"${src}" "${src}"
...@@ -615,7 +624,7 @@ endfunction() ...@@ -615,7 +624,7 @@ endfunction()
# set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output") # set(CMAKE_${lang}_COMPILER_ID_VENDOR_REGEX_${vendor} "Some Vendor Output")
# We try running the compiler with the flag for each vendor and # We try running the compiler with the flag for each vendor and
# matching its regular expression in the output. # matching its regular expression in the output.
function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang) function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang userflags)
if(NOT CMAKE_${lang}_COMPILER_ID_DIR) if(NOT CMAKE_${lang}_COMPILER_ID_DIR)
# We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID() # We get here when this function is called not from within CMAKE_DETERMINE_COMPILER_ID()
...@@ -633,7 +642,7 @@ function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang) ...@@ -633,7 +642,7 @@ function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang)
execute_process( execute_process(
COMMAND "${CMAKE_${lang}_COMPILER}" COMMAND "${CMAKE_${lang}_COMPILER}"
${CMAKE_${lang}_COMPILER_ID_ARG1} ${CMAKE_${lang}_COMPILER_ID_ARG1}
${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} ${userflags}
${flags} ${flags}
WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR} WORKING_DIRECTORY ${CMAKE_${lang}_COMPILER_ID_DIR}
OUTPUT_VARIABLE output ERROR_VARIABLE output OUTPUT_VARIABLE output ERROR_VARIABLE output
...@@ -661,7 +670,7 @@ function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang) ...@@ -661,7 +670,7 @@ function(CMAKE_DETERMINE_COMPILER_ID_VENDOR lang)
endforeach() endforeach()
endfunction() endfunction()
function(CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX lang) function(CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX lang userflags)
# Run this MSVC-compatible compiler to detect what the /showIncludes # Run this MSVC-compatible compiler to detect what the /showIncludes
# option displays. We can use a C source even with the C++ compiler # option displays. We can use a C source even with the C++ compiler
# because MSVC-compatible compilers handle both and show the same output. # because MSVC-compatible compilers handle both and show the same output.
...@@ -671,7 +680,7 @@ function(CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX lang) ...@@ -671,7 +680,7 @@ function(CMAKE_DETERMINE_MSVC_SHOWINCLUDES_PREFIX lang)
execute_process( execute_process(
COMMAND "${CMAKE_${lang}_COMPILER}" COMMAND "${CMAKE_${lang}_COMPILER}"
${CMAKE_${lang}_COMPILER_ID_ARG1} ${CMAKE_${lang}_COMPILER_ID_ARG1}
${CMAKE_${lang}_COMPILER_ID_FLAGS_LIST} ${userflags}
/nologo /showIncludes /c main.c /nologo /showIncludes /c main.c
WORKING_DIRECTORY ${showdir} WORKING_DIRECTORY ${showdir}
OUTPUT_VARIABLE out OUTPUT_VARIABLE out
......
...@@ -8,7 +8,6 @@ file(MAKE_DIRECTORY ${MY_BINARY_DIR}) ...@@ -8,7 +8,6 @@ file(MAKE_DIRECTORY ${MY_BINARY_DIR})
set(CMAKE_MyLang_COMPILER ${CMAKE_COMMAND}) set(CMAKE_MyLang_COMPILER ${CMAKE_COMMAND})
set(CMAKE_MyLang_COMPILER_ID_ARG1) set(CMAKE_MyLang_COMPILER_ID_ARG1)
set(CMAKE_MyLang_COMPILER_ID_FLAGS_LIST)
set(CMAKE_MyLang_COMPILER_ID_DIR ${MY_BINARY_DIR}) set(CMAKE_MyLang_COMPILER_ID_DIR ${MY_BINARY_DIR})
file(WRITE "${MY_BINARY_DIR}/BogusVendor.cmake" "message(\"This is a BogusVendor compiler\")") file(WRITE "${MY_BINARY_DIR}/BogusVendor.cmake" "message(\"This is a BogusVendor compiler\")")
...@@ -22,7 +21,8 @@ set(CMAKE_MyLang_COMPILER_ID_VENDOR_FLAGS_MyVendor -P MyVendor.cmake) ...@@ -22,7 +21,8 @@ set(CMAKE_MyLang_COMPILER_ID_VENDOR_FLAGS_MyVendor -P MyVendor.cmake)
set(CMAKE_MyLang_COMPILER_ID_VENDOR_REGEX_MyVendor MyVendor) set(CMAKE_MyLang_COMPILER_ID_VENDOR_REGEX_MyVendor MyVendor)
set(CMAKE_BINARY_DIR ${MY_BINARY_DIR}) set(CMAKE_BINARY_DIR ${MY_BINARY_DIR})
cmake_determine_compiler_id_vendor(MyLang) set(userflags)
cmake_determine_compiler_id_vendor(MyLang "${userflags}")
if("${CMAKE_MyLang_COMPILER_ID}" STREQUAL "MyVendor") if("${CMAKE_MyLang_COMPILER_ID}" STREQUAL "MyVendor")
message(STATUS "Found MyVendor compiler id!") message(STATUS "Found MyVendor compiler id!")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment