CMake/Modules/Compiler/MSVC-C.cmake
Brad King 48aac247e9 Compile with explicit language flag when source LANGUAGE property is set
This change was originally made by commit 74b1c9fc8e (Explicitly specify
language flag when source LANGUAGE property is set, 2020-06-01,
v3.19.0-rc1~722^2), but it was reverted by commit 30aa715fac (Revert
"specify language flag when source LANGUAGE property is set",
2020-11-19) to restore compatibility with pre-3.19 behavior.

Implement the change again, but add policy CMP0119 to make this change
while preserving compatibility with existing projects.

Note that the `Compiler/{Clang,Intel,MSVC}-CXX` modules do not need to
specify `-TP` for their MSVC-like variants because we already use the
flag in `CMAKE_CXX_COMPILE_OBJECT`.  Similarly for `Compiler/XL-CXX`
and `Platform/Windows-Embarcadero`.

Note also that this does not seem possible to implement for XL C.
Even with `-qsourcetype=c`, `xlc` complains about an unknown suffix:
`1501-218 (W) file /.../AltExtC.zzz contains an incorrect file suffix`.
It returns non-zero even with `-qsuppress=1501-218`.

Co-Author: Robert Maynard <robert.maynard@kitware.com>
Fixes: #14516, #20716
2020-12-02 11:39:11 -05:00

66 lines
2.5 KiB
CMake

# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
include(Compiler/CMakeCommonCompilerMacros)
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.27)
set(CMAKE_C90_STANDARD_COMPILE_OPTION "")
set(CMAKE_C90_EXTENSION_COMPILE_OPTION "")
set(CMAKE_C99_STANDARD_COMPILE_OPTION "")
set(CMAKE_C99_EXTENSION_COMPILE_OPTION "")
set(CMAKE_C11_STANDARD_COMPILE_OPTION "-std:c11")
set(CMAKE_C11_EXTENSION_COMPILE_OPTION "-std:c11")
__compiler_check_default_language_standard(C 19.27 99)
else()
# MSVC has no specific options to set C language standards, but set them as
# empty strings anyways so the feature test infrastructure can at least check
# to see if they are defined.
set(CMAKE_C90_STANDARD_COMPILE_OPTION "")
set(CMAKE_C90_EXTENSION_COMPILE_OPTION "")
set(CMAKE_C99_STANDARD_COMPILE_OPTION "")
set(CMAKE_C99_EXTENSION_COMPILE_OPTION "")
set(CMAKE_C11_STANDARD_COMPILE_OPTION "")
set(CMAKE_C11_EXTENSION_COMPILE_OPTION "")
# There is no meaningful default for this
set(CMAKE_C_STANDARD_DEFAULT "")
endif()
set(CMAKE_C_COMPILE_OPTIONS_EXPLICIT_LANGUAGE -TC)
set(CMAKE_C_CLANG_TIDY_DRIVER_MODE "cl")
# There are no C compiler modes so we hard-code the known compiler supported
# features. Override the default macro for this special case. Pretend that
# all language standards are available so that at least compilation
# can be attempted.
macro(cmake_record_c_compile_features)
list(APPEND CMAKE_C_COMPILE_FEATURES
c_std_90
c_std_99
c_std_11
c_function_prototypes
)
list(APPEND CMAKE_C90_COMPILE_FEATURES c_std_90 c_function_prototypes)
list(APPEND CMAKE_C99_COMPILE_FEATURES c_std_99)
list(APPEND CMAKE_C11_COMPILE_FEATURES c_std_11)
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 14.0)
list(APPEND CMAKE_C_COMPILE_FEATURES c_variadic_macros)
list(APPEND CMAKE_C99_COMPILE_FEATURES c_variadic_macros)
endif()
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.27)
list(APPEND CMAKE_C_COMPILE_FEATURES c_restrict)
list(APPEND CMAKE_C99_COMPILE_FEATURES c_restrict)
endif()
if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.28)
list(APPEND CMAKE_C_COMPILE_FEATURES c_static_assert)
list(APPEND CMAKE_C11_COMPILE_FEATURES c_static_assert)
endif()
set(_result 0) # expected by cmake_determine_compile_features
endmacro()
# /JMC "Just My Code" is only supported by MSVC 19.05 onward.
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 19.05)
set(CMAKE_C_COMPILE_OPTIONS_JMC "-JMC")
endif()