From 5f6672be446127661f9e80605e94a89b4ad22331 Mon Sep 17 00:00:00 2001 From: 06393993 <33996375+06393993@users.noreply.github.com> Date: Fri, 5 Aug 2022 22:08:36 -0700 Subject: [PATCH] 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 --- CMakeLists.txt | 13 +++++++++---- conan/CMakeLists.txt | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e7ab8762..2cff05159 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,11 @@ option(FLATBUFFERS_SKIP_MONSTER_EXTRA "Skip generating monster_extra.fbs that contains non-supported numerical\" 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) message(WARNING "Cannot build tests without building the compiler. Tests will be disabled.") @@ -92,7 +97,7 @@ if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT) include(CheckCXXSymbolExists) 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(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L) else() @@ -335,7 +340,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # We shouldn't rely on any compiler-extensions to make things work. set(CMAKE_CXX_EXTENSIONS OFF) -if(MSVC) +if(MSVC_LIKE) target_compile_options(ProjectConfig INTERFACE /W4 @@ -343,7 +348,7 @@ if(MSVC) /wd4512 # C4512: assignment operator could not be generated /wd4316 # C4316: object allocated on the heap may not be aligned /wd4456 # C4456: hides previous local declaration - $<$: + $<$: /D_CRT_SECURE_NO_WARNINGS > ) @@ -425,7 +430,7 @@ if(FLATBUFFERS_BUILD_FLATC) target_link_libraries(flatc PRIVATE $) target_compile_options(flatc PUBLIC - $<$,$>: + $<$,$>: /MT > ) diff --git a/conan/CMakeLists.txt b/conan/CMakeLists.txt index d32a01327..65f902876 100644 --- a/conan/CMakeLists.txt +++ b/conan/CMakeLists.txt @@ -5,8 +5,8 @@ message(STATUS "Conan FlatBuffers Wrapper") include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) 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) -endif(WIN32 AND MSVC AND FLATBUFFERS_BUILD_SHAREDLIB) +endif(WIN32 AND MSVC_LIKE AND FLATBUFFERS_BUILD_SHAREDLIB) include(${CMAKE_SOURCE_DIR}/CMakeListsOriginal.txt)