• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1include(CheckCXXCompilerFlag)
2include(CheckCCompilerFlag)
3
4function(EnableCompilerFlag _flag _C _CXX)
5    string(REGEX REPLACE "\\+" "PLUS" varname "${_flag}")
6    string(REGEX REPLACE "[^A-Za-z0-9]+" "_" varname "${varname}")
7    string(REGEX REPLACE "^_+" "" varname "${varname}")
8    string(TOUPPER "${varname}" varname)
9    if (_C)
10        CHECK_C_COMPILER_FLAG(${_flag} C_FLAG_${varname})
11        if (C_FLAG_${varname})
12            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flag}" PARENT_SCOPE)
13        endif ()
14    endif ()
15    if (_CXX)
16        CHECK_CXX_COMPILER_FLAG(${_flag} CXX_FLAG_${varname})
17        if (CXX_FLAG_${varname})
18            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flag}" PARENT_SCOPE)
19        endif ()
20    endif ()
21endfunction()
22
23macro(ADD_ZSTD_COMPILATION_FLAGS)
24    if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" OR MINGW) #Not only UNIX but also WIN32 for MinGW
25        #Set c++11 by default
26        EnableCompilerFlag("-std=c++11" false true)
27        #Set c99 by default
28        EnableCompilerFlag("-std=c99" true false)
29        EnableCompilerFlag("-Wall" true true)
30        EnableCompilerFlag("-Wextra" true true)
31        EnableCompilerFlag("-Wundef" true true)
32        EnableCompilerFlag("-Wshadow" true true)
33        EnableCompilerFlag("-Wcast-align" true true)
34        EnableCompilerFlag("-Wcast-qual" true true)
35        EnableCompilerFlag("-Wstrict-prototypes" true false)
36        # Enable asserts in Debug mode
37        if (CMAKE_BUILD_TYPE MATCHES "Debug")
38            EnableCompilerFlag("-DDEBUGLEVEL=1" true true)
39        endif ()
40    elseif (MSVC) # Add specific compilation flags for Windows Visual
41
42        set(ACTIVATE_MULTITHREADED_COMPILATION "ON" CACHE BOOL "activate multi-threaded compilation (/MP flag)")
43        if (CMAKE_GENERATOR MATCHES "Visual Studio" AND ACTIVATE_MULTITHREADED_COMPILATION)
44            EnableCompilerFlag("/MP" true true)
45        endif ()
46
47        # UNICODE SUPPORT
48        EnableCompilerFlag("/D_UNICODE" true true)
49        EnableCompilerFlag("/DUNICODE" true true)
50        # Enable asserts in Debug mode
51        if (CMAKE_BUILD_TYPE MATCHES "Debug")
52            EnableCompilerFlag("/DDEBUGLEVEL=1" true true)
53        endif ()
54    endif ()
55
56    # Remove duplicates compilation flags
57    foreach (flag_var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
58             CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
59             CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
60             CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
61        if( ${flag_var} )
62            separate_arguments(${flag_var})
63            string(REPLACE ";" " " ${flag_var} "${${flag_var}}")
64        endif()
65    endforeach ()
66
67    if (MSVC AND ZSTD_USE_STATIC_RUNTIME)
68        foreach (flag_var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
69                 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
70                 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
71                 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
72            if ( ${flag_var} )
73                string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
74            endif()
75        endforeach ()
76    endif ()
77
78endmacro()
79