Fix Clang-Cl compile on Windows (#7308)

Introduce a MSVC_LIKE variable in the CMake scripts, set that variable to
true only if the compiler is either MSVC or tries to emulate the MSVC
command line, and test that variable when setting compiler arguments.

Tested with cmake .. -G Ninja -DCMAKE_C_COMPILER:PATH="clang-cl.exe" -DCMAKE_CXX_COMPILER:PATH="clang-cl.exe" -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_CPP17=ON

Co-authored-by: Kaiyi Li <kaiyili@google.com>
This commit is contained in:
06393993
2022-08-05 22:08:36 -07:00
committed by GitHub
parent 28e858c855
commit 5f6672be44
2 changed files with 11 additions and 6 deletions

View File

@@ -75,6 +75,11 @@ option(FLATBUFFERS_SKIP_MONSTER_EXTRA
"Skip generating monster_extra.fbs that contains non-supported numerical\" "Skip generating monster_extra.fbs that contains non-supported numerical\"
types." OFF) types." OFF)
set(MSVC_LIKE OFF)
if(MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
set(MSVC_LIKE ON)
endif()
if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
message(WARNING message(WARNING
"Cannot build tests without building the compiler. Tests will be disabled.") "Cannot build tests without building the compiler. Tests will be disabled.")
@@ -92,7 +97,7 @@ if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT)
include(CheckCXXSymbolExists) include(CheckCXXSymbolExists)
set(FLATBUFFERS_LOCALE_INDEPENDENT 0) set(FLATBUFFERS_LOCALE_INDEPENDENT 0)
if(MSVC) if(MSVC_LIKE)
check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L) check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
check_cxx_symbol_exists(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L) check_cxx_symbol_exists(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
else() else()
@@ -335,7 +340,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# We shouldn't rely on any compiler-extensions to make things work. # We shouldn't rely on any compiler-extensions to make things work.
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
if(MSVC) if(MSVC_LIKE)
target_compile_options(ProjectConfig target_compile_options(ProjectConfig
INTERFACE INTERFACE
/W4 /W4
@@ -343,7 +348,7 @@ if(MSVC)
/wd4512 # C4512: assignment operator could not be generated /wd4512 # C4512: assignment operator could not be generated
/wd4316 # C4316: object allocated on the heap may not be aligned /wd4316 # C4316: object allocated on the heap may not be aligned
/wd4456 # C4456: hides previous local declaration /wd4456 # C4456: hides previous local declaration
$<$<CXX_COMPILER_ID:CLANG>: $<$<CXX_COMPILER_ID:Clang>:
/D_CRT_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS
> >
) )
@@ -425,7 +430,7 @@ if(FLATBUFFERS_BUILD_FLATC)
target_link_libraries(flatc PRIVATE $<BUILD_INTERFACE:ProjectConfig>) target_link_libraries(flatc PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
target_compile_options(flatc target_compile_options(flatc
PUBLIC PUBLIC
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<CONFIG:Release>>: $<$<AND:$<BOOL:${MSVC_LIKE}>,$<CONFIG:Release>>:
/MT /MT
> >
) )

View File

@@ -5,8 +5,8 @@ message(STATUS "Conan FlatBuffers Wrapper")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup() conan_basic_setup()
if (WIN32 AND MSVC AND FLATBUFFERS_BUILD_SHAREDLIB) if (WIN32 AND MSVC_LIKE AND FLATBUFFERS_BUILD_SHAREDLIB)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
endif(WIN32 AND MSVC AND FLATBUFFERS_BUILD_SHAREDLIB) endif(WIN32 AND MSVC_LIKE AND FLATBUFFERS_BUILD_SHAREDLIB)
include(${CMAKE_SOURCE_DIR}/CMakeListsOriginal.txt) include(${CMAKE_SOURCE_DIR}/CMakeListsOriginal.txt)