Skip to content

[cmake] only require a CXX compiler when tests are build #4357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions build/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ project(zstd
VERSION "${ZSTD_FULL_VERSION}"
LANGUAGES C # Main library is in C
ASM # And ASM
CXX # Testing contributed code also utilizes CXX
)

message(STATUS "ZSTD VERSION: ${zstd_VERSION}")
Expand All @@ -54,12 +53,6 @@ endif()

include(GNUInstallDirs)

#-----------------------------------------------------------------------------
# Add extra compilation flags
#-----------------------------------------------------------------------------
include(AddZstdCompilationFlags)
ADD_ZSTD_COMPILATION_FLAGS()

# Always hide XXHash symbols
add_definitions(-DXXH_NAMESPACE=ZSTD_)

Expand Down Expand Up @@ -123,6 +116,19 @@ if (MSVC)
option(ZSTD_USE_STATIC_RUNTIME "LINK TO STATIC RUN-TIME LIBRARIES" OFF)
endif ()

# Enable C++ support for testing.
set(ZSTD_ENABLE_CXX ${ZSTD_BUILD_TESTS})

if(ZSTD_ENABLE_CXX)
enable_language(CXX)
endif()

#-----------------------------------------------------------------------------
# Add extra compilation flags
#-----------------------------------------------------------------------------
include(AddZstdCompilationFlags)
ADD_ZSTD_COMPILATION_FLAGS(ON ZSTD_ENABLE_CXX ON) # C CXX LD

#-----------------------------------------------------------------------------
# External dependencies
#-----------------------------------------------------------------------------
Expand Down
34 changes: 17 additions & 17 deletions build/cmake/CMakeModules/AddZstdCompilationFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function(EnableCompilerFlag _flag _C _CXX _LD)
endif ()
endfunction()

macro(ADD_ZSTD_COMPILATION_FLAGS)
macro(ADD_ZSTD_COMPILATION_FLAGS _C _CXX _LD)
# We set ZSTD_HAS_NOEXECSTACK if we are certain we've set all the required
# compiler flags to mark the stack as non-executable.
set(ZSTD_HAS_NOEXECSTACK false)
Expand All @@ -63,26 +63,26 @@ macro(ADD_ZSTD_COMPILATION_FLAGS)
# EnableCompilerFlag("-std=c99" true false) # Set C compilation to c99 standard
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND MSVC)
# clang-cl normally maps -Wall to -Weverything.
EnableCompilerFlag("/clang:-Wall" true true false)
EnableCompilerFlag("/clang:-Wall" _C _CXX false)
else ()
EnableCompilerFlag("-Wall" true true false)
EnableCompilerFlag("-Wall" _C _CXX false)
endif ()
EnableCompilerFlag("-Wextra" true true false)
EnableCompilerFlag("-Wundef" true true false)
EnableCompilerFlag("-Wshadow" true true false)
EnableCompilerFlag("-Wcast-align" true true false)
EnableCompilerFlag("-Wcast-qual" true true false)
EnableCompilerFlag("-Wstrict-prototypes" true false false)
EnableCompilerFlag("-Wextra" _C _CXX false)
EnableCompilerFlag("-Wundef" _C _CXX false)
EnableCompilerFlag("-Wshadow" _C _CXX false)
EnableCompilerFlag("-Wcast-align" _C _CXX false)
EnableCompilerFlag("-Wcast-qual" _C _CXX false)
EnableCompilerFlag("-Wstrict-prototypes" _C false false)
# Enable asserts in Debug mode
if (CMAKE_BUILD_TYPE MATCHES "Debug")
EnableCompilerFlag("-DDEBUGLEVEL=1" true true false)
EnableCompilerFlag("-DDEBUGLEVEL=1" _C _CXX false)
endif ()
# Add noexecstack flags
# LDFLAGS
EnableCompilerFlag("-Wl,-z,noexecstack" false false true)
EnableCompilerFlag("-Wl,-z,noexecstack" false false _LD)
# CFLAGS & CXXFLAGS
EnableCompilerFlag("-Qunused-arguments" true true false)
EnableCompilerFlag("-Wa,--noexecstack" true true false)
EnableCompilerFlag("-Qunused-arguments" _C _CXX false)
EnableCompilerFlag("-Wa,--noexecstack" _C _CXX false)
# NOTE: Using 3 nested ifs because the variables are sometimes
# empty if the condition is false, and sometimes equal to false.
# This implicitly converts them to truthy values. There may be
Expand All @@ -99,15 +99,15 @@ macro(ADD_ZSTD_COMPILATION_FLAGS)

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

# UNICODE SUPPORT
EnableCompilerFlag("/D_UNICODE" true true false)
EnableCompilerFlag("/DUNICODE" true true false)
EnableCompilerFlag("/D_UNICODE" _C _CXX false)
EnableCompilerFlag("/DUNICODE" _C _CXX false)
# Enable asserts in Debug mode
if (CMAKE_BUILD_TYPE MATCHES "Debug")
EnableCompilerFlag("/DDEBUGLEVEL=1" true true false)
EnableCompilerFlag("/DDEBUGLEVEL=1" _C _CXX false)
endif ()
endif ()

Expand Down
Loading