Skip to content

Commit b11377f

Browse files
committed
fix #4405
1 parent 49fe2ec commit b11377f

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

.github/workflows/cmake-tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
name: "CMake Cross-Platform (Spaces in Path)"
4545
runs-on: ${{ matrix.os }}
4646
strategy:
47+
fail-fast: false
4748
matrix:
4849
include:
4950
- os: ubuntu-latest
@@ -77,6 +78,7 @@ jobs:
7778
name: "CMake Windows VS2022 (${{ matrix.name }})"
7879
runs-on: ${{ matrix.runner }}
7980
strategy:
81+
fail-fast: false
8082
matrix:
8183
include:
8284
- generator: "Visual Studio 17 2022"

build/cmake/CMakeModules/AddZstdCompilationFlags.cmake

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ function(EnableCompilerFlag _flag _C _CXX _LD)
2222
endif ()
2323
endif ()
2424
if (_CXX)
25-
CHECK_CXX_COMPILER_FLAG(${_flag} CXX_FLAG_${varname})
26-
if (CXX_FLAG_${varname})
27-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}" PARENT_SCOPE)
25+
# Only check CXX flags if CXX language is enabled
26+
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
27+
if("CXX" IN_LIST languages)
28+
CHECK_CXX_COMPILER_FLAG(${_flag} CXX_FLAG_${varname})
29+
if (CXX_FLAG_${varname})
30+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}" PARENT_SCOPE)
31+
endif ()
2832
endif ()
2933
endif ()
3034
if (_LD)
@@ -54,19 +58,24 @@ macro(ADD_ZSTD_COMPILATION_FLAGS _C _CXX _LD)
5458
# compiler flags to mark the stack as non-executable.
5559
set(ZSTD_HAS_NOEXECSTACK false)
5660

57-
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" OR MINGW) #Not only UNIX but also WIN32 for MinGW
61+
# Detect clang-cl: it identifies as Clang but should be treated as MSVC
62+
# Use C compiler info since CXX might not be enabled yet
63+
set(CLANG_CL_DETECTED FALSE)
64+
if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND (MSVC OR CMAKE_C_SIMULATE_ID STREQUAL "MSVC"))
65+
set(CLANG_CL_DETECTED TRUE)
66+
message(STATUS "Detected clang-cl compiler - using MSVC-compatible flags")
67+
endif()
68+
69+
# Handle GNU/Clang compilers (but not clang-cl which is handled in MSVC section)
70+
# Use C compiler ID since CXX might not be enabled yet
71+
if ((CMAKE_C_COMPILER_ID MATCHES "GNU|Clang" OR MINGW) AND NOT CLANG_CL_DETECTED)
5872
# It's possible to select the exact standard used for compilation.
5973
# It's not necessary, but can be employed for specific purposes.
6074
# Note that zstd source code is compatible with both C++98 and above
6175
# and C-gnu90 (c90 + long long + variadic macros ) and above
6276
# EnableCompilerFlag("-std=c++11" false true) # Set C++ compilation to c++11 standard
6377
# EnableCompilerFlag("-std=c99" true false) # Set C compilation to c99 standard
64-
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND MSVC)
65-
# clang-cl normally maps -Wall to -Weverything.
66-
EnableCompilerFlag("/clang:-Wall" _C _CXX false)
67-
else ()
68-
EnableCompilerFlag("-Wall" _C _CXX false)
69-
endif ()
78+
EnableCompilerFlag("-Wall" _C _CXX false)
7079
EnableCompilerFlag("-Wextra" _C _CXX false)
7180
EnableCompilerFlag("-Wundef" _C _CXX false)
7281
EnableCompilerFlag("-Wshadow" _C _CXX false)
@@ -95,16 +104,26 @@ macro(ADD_ZSTD_COMPILATION_FLAGS _C _CXX _LD)
95104
endif()
96105
endif()
97106
endif()
98-
elseif (MSVC) # Add specific compilation flags for Windows Visual
107+
endif()
108+
109+
if (MSVC OR CLANG_CL_DETECTED) # Add specific compilation flags for Windows Visual (including clang-cl)
99110

100111
set(ACTIVATE_MULTITHREADED_COMPILATION "ON" CACHE BOOL "activate multi-threaded compilation (/MP flag)")
101112
if (CMAKE_GENERATOR MATCHES "Visual Studio" AND ACTIVATE_MULTITHREADED_COMPILATION)
102113
EnableCompilerFlag("/MP" _C _CXX false)
103114
endif ()
104115

105-
# UNICODE SUPPORT
106-
EnableCompilerFlag("/D_UNICODE" _C _CXX false)
107-
EnableCompilerFlag("/DUNICODE" _C _CXX false)
116+
# For clang-cl, use clang-specific warning flags with MSVC syntax
117+
if (CLANG_CL_DETECTED)
118+
EnableCompilerFlag("/clang:-Wall" _C _CXX false)
119+
EnableCompilerFlag("/clang:-Wextra" _C _CXX false)
120+
# Don't set UNICODE flags for clang-cl as they cause RC issues
121+
else()
122+
# UNICODE SUPPORT (only for regular MSVC, not clang-cl)
123+
EnableCompilerFlag("/D_UNICODE" _C _CXX false)
124+
EnableCompilerFlag("/DUNICODE" _C _CXX false)
125+
endif()
126+
108127
# Enable asserts in Debug mode
109128
if (CMAKE_BUILD_TYPE MATCHES "Debug")
110129
EnableCompilerFlag("/DDEBUGLEVEL=1" _C _CXX false)

build/cmake/CMakeModules/ZstdOptions.cmake

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,15 @@ if(MSVC)
5858
option(ZSTD_USE_STATIC_RUNTIME "Link to static runtime libraries" OFF)
5959
endif()
6060

61-
# C++ support (needed for tests)
62-
set(ZSTD_ENABLE_CXX ${ZSTD_BUILD_TESTS})
61+
# C++ support (needed for tests and required for MSVC builds)
62+
if(MSVC)
63+
# MSVC builds require CXX language support for proper project generation
64+
set(ZSTD_ENABLE_CXX ON)
65+
else()
66+
# For other compilers, only enable CXX when tests are built
67+
set(ZSTD_ENABLE_CXX ${ZSTD_BUILD_TESTS})
68+
endif()
69+
6370
if(ZSTD_ENABLE_CXX)
6471
enable_language(CXX)
6572
endif()

build/cmake/lib/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ endif ()
100100
if (MSVC)
101101
set(MSVC_RESOURCE_DIR ${ZSTD_SOURCE_DIR}/build/VS2010/libzstd-dll)
102102
set(PlatformDependResources ${MSVC_RESOURCE_DIR}/libzstd-dll.rc)
103+
# Set specific properties for RC files only on Windows to avoid C/C++ flag contamination
104+
if(WIN32)
105+
set_source_files_properties(${PlatformDependResources} PROPERTIES
106+
LANGUAGE RC
107+
COMPILE_DEFINITIONS "" # Clear any inherited definitions
108+
COMPILE_FLAGS "/I \"${LIBRARY_DIR}\" /D_WIN32 /DWIN32")
109+
endif()
103110
endif ()
104111

105112
# Explicitly set the language to C for all files, including ASM files.
@@ -118,7 +125,6 @@ endmacro ()
118125

119126
# Define directories containing the library's public headers
120127
set(PUBLIC_INCLUDE_DIRS ${LIBRARY_DIR})
121-
set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} /I \"${LIBRARY_DIR}\"")
122128
# Split project to static and shared libraries build
123129
set(library_targets)
124130
if (ZSTD_BUILD_SHARED)

0 commit comments

Comments
 (0)