Browse Source

CMake: Add warnings by default. Add option for treating warnings as errors.

Michael Ragazzon 5 years ago
parent
commit
a64ed41ea2
4 changed files with 45 additions and 28 deletions
  1. 1 2
      .appveyor.yml
  2. 2 5
      .travis.yml
  3. 37 13
      CMakeLists.txt
  4. 5 8
      Tests/CMakeLists.txt

+ 1 - 2
.appveyor.yml

@@ -4,7 +4,6 @@ matrix:
   fast_finish: true
 environment:
   FREETYPE_VER: 2.10.1
-  VS_CXXFLAGS: /DWIN32 /D_WINDOWS /W4 /GR /EHsc /permissive- /w44062
   RUN_MINGW: false
   matrix:
     - RUN_MINGW: true
@@ -25,7 +24,7 @@ install:
     mkdir Build-Dynamic, Build-Static
     
     cd Build-Dynamic
-    cmake -G "%VS_GENERATOR%" -DBUILD_SHARED_LIBS=ON -DBUILD_SAMPLES=ON -DCMAKE_CXX_FLAGS="%VS_CXXFLAGS%" ..
+    cmake -G "%VS_GENERATOR%" -DBUILD_SHARED_LIBS=ON -DBUILD_SAMPLES=ON ..
 
     cd ../Build-Static
     cmake -G "%VS_GENERATOR%" -DBUILD_SHARED_LIBS=OFF -DBUILD_SAMPLES=OFF ..

+ 2 - 5
.travis.yml

@@ -1,9 +1,6 @@
 sudo: false
 dist: bionic
 language: c++
-env:
-  global:
-    - CXXFLAGS="-pedantic -Wall -Wextra"
 
 cache: 
   apt: true
@@ -17,7 +14,7 @@ matrix:
       compiler: clang
     - os: linux
       compiler: clang
-      env: BUILD_TESTING="ON"
+      env: BUILD_TESTING="ON" WARNINGS_AS_ERRORS="ON"
       addons:
         apt:
           packages:
@@ -91,7 +88,7 @@ install:
   - cmake --version
   - cd "$TRAVIS_BUILD_DIR"
   - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then cmake -DNO_THIRDPARTY_CONTAINERS=ON -DENABLE_PRECOMPILED_HEADERS=OFF -G Xcode .; fi
-  - if [[ "$TRAVIS_OS_NAME" != "osx" ]]; then cmake -DBUILD_LUA_BINDINGS=ON -DBUILD_SAMPLES=ON -DBUILD_TESTING=${BUILD_TESTING:-OFF} -DDISABLE_RTTI_AND_EXCEPTIONS=${DISABLE_RTTI_AND_EXCEPTIONS:-OFF} -DNO_THIRDPARTY_CONTAINERS=${NO_THIRDPARTY_CONTAINERS:-OFF} -DNO_FONT_INTERFACE_DEFAULT=${NO_FONT_INTERFACE_DEFAULT:-OFF} -G Ninja .; fi
+  - if [[ "$TRAVIS_OS_NAME" != "osx" ]]; then cmake -DBUILD_LUA_BINDINGS=ON -DBUILD_SAMPLES=ON -DBUILD_TESTING=${BUILD_TESTING:-OFF} -DWARNINGS_AS_ERRORS=${WARNINGS_AS_ERRORS:-OFF} -DDISABLE_RTTI_AND_EXCEPTIONS=${DISABLE_RTTI_AND_EXCEPTIONS:-OFF} -DNO_THIRDPARTY_CONTAINERS=${NO_THIRDPARTY_CONTAINERS:-OFF} -DNO_FONT_INTERFACE_DEFAULT=${NO_FONT_INTERFACE_DEFAULT:-OFF} -G Ninja .; fi
 
 before_script:
   - if [[ "$VALGRIND_SAMPLES" == "1" ]] && [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0; fi

+ 37 - 13
CMakeLists.txt

@@ -230,6 +230,26 @@ elseif (ENABLE_PRECOMPILED_HEADERS)
 	set(PRECOMPILED_HEADERS_ENABLED ON)
 endif()
 
+
+option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors." OFF)
+mark_as_advanced(WARNINGS_AS_ERRORS)
+
+macro(add_common_target_options NAME)
+	if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
+		target_compile_options(${NAME} PRIVATE -Wall -pedantic -Wextra)
+		
+		if(WARNINGS_AS_ERRORS)
+			target_compile_options(${NAME} PRIVATE -Werror)
+		endif()
+	elseif(MSVC)
+		target_compile_options(${NAME} PRIVATE /MP /W4 /w44062 /permissive-)
+		
+		if(WARNINGS_AS_ERRORS)
+			target_compile_options(${NAME} PRIVATE /WX)
+		endif()
+	endif()
+endmacro()
+
 #===================================
 # Find dependencies ================
 #===================================
@@ -288,10 +308,8 @@ foreach(library ${LIBRARIES})
 	   SOVERSION ${PROJECT_VERSION_MAJOR}
 	)
 	
-	if (MSVC)
-		target_compile_options(${NAME} PUBLIC "/MP")
-	endif(MSVC)
-	
+	add_common_target_options(${NAME})
+
 	install(TARGETS ${NAME}
 		EXPORT RmlUiTargets
 		LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@@ -381,6 +399,8 @@ if(BUILD_LUA_BINDINGS)
 	   VERSION ${PROJECT_VERSION}
 	   SOVERSION ${PROJECT_VERSION_MAJOR}
 	)
+	
+	add_common_target_options(${NAME})
 
 	install(TARGETS ${NAME}
 		EXPORT RmlUiTargets
@@ -418,18 +438,18 @@ endif()
 #===================================
 
 if(NOT BUILD_FRAMEWORK)
-target_link_libraries(RmlCore ${CORE_LINK_LIBS})
-target_link_libraries(RmlDebugger RmlCore)
-else(NOT BUILD_FRAMEWORK)
-target_link_libraries(RmlUi ${CORE_LINK_LIBS})
-endif(NOT BUILD_FRAMEWORK)
+	target_link_libraries(RmlCore ${CORE_LINK_LIBS})
+	target_link_libraries(RmlDebugger RmlCore)
+else()
+	target_link_libraries(RmlUi ${CORE_LINK_LIBS})
+endif()
 
 if(BUILD_LUA_BINDINGS)
 	if(NOT BUILD_FRAMEWORK)
 		target_link_libraries(RmlLua RmlCore ${LUA_BINDINGS_LINK_LIBS})
-	else(NOT BUILD_FRAMEWORK)
+	else()
 		target_link_libraries(RmlLua RmlUi ${LUA_BINDINGS_LINK_LIBS})
-	endif(NOT BUILD_FRAMEWORK)
+	endif()
 endif()
 
 
@@ -454,6 +474,8 @@ macro(bl_sample NAME)
 	else()
 		add_executable(${NAME} ${${NAME}_SRC_FILES} ${${NAME}_HDR_FILES} )
 	endif()
+	
+	add_common_target_options(${NAME})
 
 	target_link_libraries(${NAME} ${ARGN})
 endmacro()
@@ -481,8 +503,8 @@ endif(NOT BUILD_FRAMEWORK)
 	find_package(OpenGL REQUIRED)
 		   
 	if(OPENGL_FOUND)
-	include_directories(${OPENGL_INCLUDE_DIR})
-	list(APPEND sample_LIBRARIES ${OPENGL_LIBRARIES})
+		include_directories(${OPENGL_INCLUDE_DIR})
+		list(APPEND sample_LIBRARIES ${OPENGL_LIBRARIES})
 	endif()
 	
 	# Set up required system libraries
@@ -526,6 +548,8 @@ endif(NOT BUILD_FRAMEWORK)
 	if (WIN32)
 		target_link_libraries(shell PUBLIC shlwapi)
 	endif()
+	
+	add_common_target_options(shell)
 
 	# Build and install the basic samples
 	foreach(sample ${samples})

+ 5 - 8
Tests/CMakeLists.txt

@@ -16,18 +16,15 @@ set_property(TARGET nanobench::nanobench PROPERTY INTERFACE_INCLUDE_DIRECTORIES
 #===================================
 file(GLOB RmlTest_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/Source/*.cpp)
 add_executable(Tests ${RmlTest_SRC_FILES})
-target_link_libraries(Tests RmlCore doctest::doctest nanobench::nanobench)
+target_link_libraries(Tests RmlCore RmlDebugger doctest::doctest nanobench::nanobench)
 
 set_target_properties(Tests PROPERTIES CXX_STANDARD 14)
 
 # Enable compiler warnings
-if (NOT TEST_INSTALLED_VERSION)
-  if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
-    target_compile_options(RmlCore PUBLIC -Wall -pedantic -Wextra -Werror)
-  elseif(MSVC)
-    target_compile_options(RmlCore PUBLIC /W4 /WX /permissive-)
-    target_compile_definitions(Tests PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
-  endif()
+add_common_target_options(Tests)
+
+if(MSVC)
+	target_compile_definitions(Tests PUBLIC DOCTEST_CONFIG_USE_STD_HEADERS)
 endif()
 
 # Add tests using doctest's discovery module