Forráskód Böngészése

CMake: Add custom compiler flags for MSVC

Max Bruckner 8 éve
szülő
commit
e174831819
2 módosított fájl, 39 hozzáadás és 30 törlés
  1. 38 29
      CMakeLists.txt
  2. 1 1
      README.md

+ 38 - 29
CMakeLists.txt

@@ -16,37 +16,46 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT
 set(custom_compiler_flags)
 
 include(CheckCCompilerFlag)
-option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON)
+option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags" ON)
 if (ENABLE_CUSTOM_COMPILER_FLAGS)
-    list(APPEND custom_compiler_flags
-        -std=c89
-        -pedantic
-        -Wall
-        -Wextra
-        -Werror
-        -Wstrict-prototypes
-        -Wwrite-strings
-        -Wshadow
-        -Winit-self
-        -Wcast-align
-        -Wformat=2
-        -Wmissing-prototypes
-        -Wstrict-overflow=2
-        -Wcast-qual
-        -Wundef
-        -Wswitch-default
-        -Wconversion
-        -Wc++-compat
-        -fstack-protector-strong
-        -Wcomma
-        -Wdouble-promotion
-        -Wparentheses
-        -Wformat-overflow
-        -Wunused-macros
-        -Wmissing-variable-declarations
-        -Wused-but-marked-unused
-        -Wswitch-enum
+    if (("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU"))
+        list(APPEND custom_compiler_flags
+            -std=c89
+            -pedantic
+            -Wall
+            -Wextra
+            -Werror
+            -Wstrict-prototypes
+            -Wwrite-strings
+            -Wshadow
+            -Winit-self
+            -Wcast-align
+            -Wformat=2
+            -Wmissing-prototypes
+            -Wstrict-overflow=2
+            -Wcast-qual
+            -Wundef
+            -Wswitch-default
+            -Wconversion
+            -Wc++-compat
+            -fstack-protector-strong
+            -Wcomma
+            -Wdouble-promotion
+            -Wparentheses
+            -Wformat-overflow
+            -Wunused-macros
+            -Wmissing-variable-declarations
+            -Wused-but-marked-unused
+            -Wswitch-enum
+        )
+    elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
+        list(APPEND custom_compiler_flags
+            /GS
+            /Za
+            /sdl
+            /W4
         )
+    endif()
 endif()
 
 option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF)

+ 1 - 1
README.md

@@ -85,7 +85,7 @@ You can change the build process with a list of different options that you can p
 * `-DENABLE_CJSON_TEST=On`: Enable building the tests. (on by default)
 * `-DENABLE_CJSON_UTILS=On`: Enable building cJSON_Utils. (off by default)
 * `-DENABLE_TARGET_EXPORT=On`: Enable the export of CMake targets. Turn off if it makes problems. (on by default)
-* `-DENABLE_CUSTOM_COMPILER_FLAGS=On`: Enable custom compiler flags (currently for Clang and GCC). Turn off if it makes problems. (on by default)
+* `-DENABLE_CUSTOM_COMPILER_FLAGS=On`: Enable custom compiler flags (currently for Clang, GCC and MSVC). Turn off if it makes problems. (on by default)
 * `-DENABLE_VALGRIND=On`: Run tests with [valgrind](http://valgrind.org). (off by default)
 * `-DENABLE_SANITIZERS=On`: Compile cJSON with [AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) and [UndefinedBehaviorSanitizer](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html) enabled (if possible). (off by default)
 * `-DBUILD_SHARED_LIBS=On`: Build the shared libraries. (on by default)