internal/CheckCompilerFlag: rely on common configuration

Extract configuration step from the CheckCompilerFlag command
to enable to share it with future CheckLinkerFlag implementation
This commit is contained in:
Marc Chevrier
2022-01-12 10:51:19 +01:00
parent 50cfe54402
commit 660e0d80ae
4 changed files with 81 additions and 66 deletions

View File

@@ -2,71 +2,15 @@
# file Copyright.txt or https://cmake.org/licensing for details.
include_guard(GLOBAL)
include(Internal/CheckFlagCommonConfig)
include(Internal/CheckSourceCompiles)
include(CMakeCheckCompilerFlagCommonPatterns)
cmake_policy(PUSH)
cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST
function(CMAKE_CHECK_COMPILER_FLAG _lang _flag _var)
if(_lang STREQUAL "C")
set(_lang_src "int main(void) { return 0; }")
set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C"
FAIL_REGEX "-Werror=.* argument .* is not valid for C")
elseif(_lang STREQUAL "CXX")
set(_lang_src "int main() { return 0; }")
set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+"
FAIL_REGEX "-Werror=.* argument .* is not valid for C\\+\\+")
elseif(_lang STREQUAL "CUDA")
set(_lang_src "__host__ int main() { return 0; }")
set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+" # Host GNU
FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif(_lang STREQUAL "Fortran")
set(_lang_src " program test\n stop\n end program")
set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Fortran")
elseif(_lang STREQUAL "HIP")
set(_lang_src "__host__ int main() { return 0; }")
set(_lang_fail_regex FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif(_lang STREQUAL "OBJC")
set(_lang_src [=[
#ifndef __OBJC__
# error "Not an Objective-C compiler"
#endif
int main(void) { return 0; }]=])
set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C" # GNU
FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif(_lang STREQUAL "OBJCXX")
set(_lang_src [=[
#ifndef __OBJC__
# error "Not an Objective-C++ compiler"
#endif
int main(void) { return 0; }]=])
set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C\\+\\+" # GNU
FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif(_lang STREQUAL "ISPC")
set(_lang_src "float func(uniform int32, float a) { return a / 2.25; }")
else()
message (SEND_ERROR "check_compiler_flag: ${_lang}: unknown language.")
return()
endif()
get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
if (NOT _lang IN_LIST _supported_languages)
message (SEND_ERROR "check_compiler_flag: ${_lang}: needs to be enabled before use.")
return()
endif()
cmake_check_flag_common_init("check_compiler_flag" ${_lang} _lang_src _lang_fail_regex)
set(CMAKE_REQUIRED_DEFINITIONS ${_flag})
# Normalize locale during test compilation.
set(_locale_vars LC_ALL LC_MESSAGES LANG)
foreach(v IN LISTS _locale_vars)
set(_locale_vars_saved_${v} "$ENV{${v}}")
set(ENV{${v}} C)
endforeach()
check_compiler_flag_common_patterns(_common_patterns)
cmake_check_source_compiles(${_lang}
"${_lang_src}"
@@ -75,9 +19,5 @@ int main(void) { return 0; }]=])
${_common_patterns}
)
foreach(v IN LISTS _locale_vars)
set(ENV{${v}} ${_locale_vars_saved_${v}})
endforeach()
endfunction ()
cmake_policy(POP)
cmake_check_flag_common_finish()
endfunction()

View File

@@ -0,0 +1,75 @@
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
# Do NOT include this module directly into any of your code. It is meant as
# a library for Check*CompilerFlag.cmake and Check*LinkerFlag.cma modules.
# It's content may change in any way between releases.
include_guard(GLOBAL)
cmake_policy(PUSH)
cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST
macro(CMAKE_CHECK_FLAG_COMMON_INIT _FUNC _LANG _SRC _PATTERNS)
if("${_LANG}" STREQUAL "C")
set(${_SRC} "int main(void) { return 0; }")
set(${_PATTERNS} FAIL_REGEX "command[ -]line option .* is valid for .* but not for C"
FAIL_REGEX "-Werror=.* argument .* is not valid for C")
elseif("${_LANG}" STREQUAL "CXX")
set(${_SRC} "int main() { return 0; }")
set(${_PATTERNS} FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+"
FAIL_REGEX "-Werror=.* argument .* is not valid for C\\+\\+")
elseif("${_LANG}" STREQUAL "CUDA")
set(${_SRC} "__host__ int main() { return 0; }")
set(${_PATTERNS} FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+" # Host GNU
FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif("${_LANG}" STREQUAL "Fortran")
set(${_SRC} " program test\n stop\n end program")
set(${_PATTERNS} FAIL_REGEX "command[ -]line option .* is valid for .* but not for Fortran")
elseif("${_LANG}" STREQUAL "HIP")
set(${_SRC} "__host__ int main() { return 0; }")
set(${_PATTERNS} FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif("${_LANG}" STREQUAL "OBJC")
set(${_SRC} [=[
#ifndef __OBJC__
# error "Not an Objective-C compiler"
#endif
int main(void) { return 0; }]=])
set(${_PATTERNS} FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C" # GNU
FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif("${_LANG}" STREQUAL "OBJCXX")
set(${_SRC} [=[
#ifndef __OBJC__
# error "Not an Objective-C++ compiler"
#endif
int main(void) { return 0; }]=])
set(${_PATTERNS} FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C\\+\\+" # GNU
FAIL_REGEX "argument unused during compilation: .*") # Clang
elseif("${_LANG}" STREQUAL "ISPC")
set(${_SRC} "float func(uniform int32, float a) { return a / 2.25; }")
else()
message (SEND_ERROR "${_FUNC}: ${_LANG}: unknown language.")
return()
endif()
get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
if (NOT "${_LANG}" IN_LIST _supported_languages)
message (SEND_ERROR "${_FUNC}: ${_LANG}: needs to be enabled before use.")
return()
endif()
# Normalize locale during test compilation.
set(_locale_vars LC_ALL LC_MESSAGES LANG)
foreach(v IN LISTS _locale_vars)
set(_CMAKE_CHECK_FLAG_COMMON_CONFIG_locale_vars_saved_${v} "$ENV{${v}}")
set(ENV{${v}} C)
endforeach()
endmacro()
macro(CMAKE_CHECK_FLAG_COMMON_FINISH)
foreach(v IN LISTS _CFCC_locale_vars)
set(ENV{${v}} ${_CMAKE_CHECK_FLAG_COMMON_CONFIG_locale_vars_saved_${v}})
endforeach()
endmacro()
cmake_policy(POP)

View File

@@ -1,2 +1,2 @@
CMake Error at .*CheckCompilerFlag\.cmake:[0-9]+ \(message\):
CMake Error at .*CheckFlagCommonConfig\.cmake:[0-9]+ \(message\):
check_compiler_flag: FAKE_LANG: unknown language.

View File

@@ -1,2 +1,2 @@
CMake Error at .*CheckCompilerFlag\.cmake:[0-9]+ \(message\):
CMake Error at .*CheckFlagCommonConfig\.cmake:[0-9]+ \(message\):
check_compiler_flag: C: needs to be enabled before use.