Skip to content

Commit

Permalink
Added support for the CXX_STANDARD and CXX_EXTENSIONS properties.
Browse files Browse the repository at this point in the history
Also extended the tests to treat warnings (about preprocessor
problems) as errors, and added two new tests about the c++
standard version.

With these changes, the c parts of the mixed-force tests are
failing.

Likely the mixed test is also failing to use the pch for the c
parts, but does so silently.
  • Loading branch information
dutow committed Mar 14, 2018
1 parent 6803af3 commit ba8ee37
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 3 deletions.
19 changes: 16 additions & 3 deletions PrecompiledHeader.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,20 @@ function(export_all_flags _filename)
set(_compile_definitions "$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions},\n-D>\n>")
set(_compile_flags "$<$<BOOL:${_compile_flags}>:$<JOIN:${_compile_flags},\n>\n>")
set(_compile_options "$<$<BOOL:${_compile_options}>:$<JOIN:${_compile_options},\n>\n>")
file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}\n")

set(_cxx_standard "$<TARGET_PROPERTY:${_target},CXX_STANDARD>")
set(_cxx_extensions "$<TARGET_PROPERTY:${_target},CXX_EXTENSIONS>")
set(_has_extensions "$<OR:$<STREQUAL:${_cxx_extensions},>,$<BOOL:${_cxx_extensions}>>") # CXX_EXTENSION default to true
set(_cxx_standard_check "$<$<AND:$<STREQUAL:${_cxx_standard},17>,$<NOT:${_has_extensions}>>:${CMAKE_CXX17_STANDARD_COMPILE_OPTION}>")
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},14>,$<NOT:${_has_extensions}>>:${CMAKE_CXX14_STANDARD_COMPILE_OPTION}>")
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},11>,$<NOT:${_has_extensions}>>:${CMAKE_CXX11_STANDARD_COMPILE_OPTION}>")
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},98>,$<NOT:${_has_extensions}>>:${CMAKE_CXX98_STANDARD_COMPILE_OPTION}>")
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},17>,${_has_extensions}>:${CMAKE_CXX17_EXTENSION_COMPILE_OPTION}>")
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},14>,${_has_extensions}>:${CMAKE_CXX14_EXTENSION_COMPILE_OPTION}>")
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},11>,${_has_extensions}>:${CMAKE_CXX11_EXTENSION_COMPILE_OPTION}>")
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},98>,${_has_extensions}>:${CMAKE_CXX98_EXTENSION_COMPILE_OPTION}>")

file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}${_cxx_standard_check}\n")
endfunction()

function(add_precompiled_header _target _input)
Expand Down Expand Up @@ -147,7 +160,7 @@ function(add_precompiled_header _target _input)
endif()
endif(MSVC)

if(CMAKE_COMPILER_IS_GNUCXX)
if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU|Clang")
get_filename_component(_name ${_input} NAME)
set(_pch_header "${CMAKE_CURRENT_SOURCE_DIR}/${_input}")
set(_pch_binary_dir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch")
Expand Down Expand Up @@ -210,5 +223,5 @@ function(add_precompiled_header _target _input)
OBJECT_DEPENDS "${_object_depends}")
endif()
endforeach()
endif(CMAKE_COMPILER_IS_GNUCXX)
endif(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU|Clang")
endfunction()
2 changes: 2 additions & 0 deletions test/c-force/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.c)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-c-force test-c-force.c test-pch.h ${pch_source})
add_precompiled_header(test-c-force test-pch.h FORCEINCLUDE)
add_test(test-c-force test-c-force)
2 changes: 2 additions & 0 deletions test/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.c)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-c test-c.c test-pch.h ${pch_source})
add_precompiled_header(test-c test-pch.h)
add_test(test-c test-c)
2 changes: 2 additions & 0 deletions test/cxx-force/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-cxx-force test-cxx-force.cpp test-pch.h ${pch_source})
add_precompiled_header(test-cxx-force test-pch.h FORCEINCLUDE)
add_test(test-cxx-force test-cxx-force)
2 changes: 2 additions & 0 deletions test/cxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-cxx test-cxx.cpp test-pch.h ${pch_source})
add_precompiled_header(test-cxx test-pch.h)
add_test(test-cxx test-cxx)
15 changes: 15 additions & 0 deletions test/cxx11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0058 NEW)
enable_testing()
project(test-cxx11)
include(../../PrecompiledHeader.cmake)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_CXX_STANDARD 11)
add_executable(test-cxx11 test-cxx.cpp test-pch.h ${pch_source})
add_precompiled_header(test-cxx11 test-pch.h)
add_test(test-cxx11 test-cxx11)
5 changes: 5 additions & 0 deletions test/cxx11/test-cxx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "test-pch.h"
#ifndef PCH
#error Missing precompiled header
#endif
int main() { return !(PCH == atoi(getenv("EXPECTED_PCH"))); }
1 change: 1 addition & 0 deletions test/cxx11/test-pch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "test-pch.h"
16 changes: 16 additions & 0 deletions test/cxx11/test-pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define PCH 1
#ifdef __cplusplus
#include <cstdlib>
#include <iostream>

#if __cplusplus < 201100L
#error This PCH has to be compiled in C++14 mode
#endif

#if __cplusplus > 201112L
#error This PCH has to be compiled in C++14 mode
#endif

#else
#include <stdlib.h>
#endif
16 changes: 16 additions & 0 deletions test/cxx14_noext/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.3)
cmake_policy(SET CMP0058 NEW)
enable_testing()
project(test-cxx14_noext)
include(../../PrecompiledHeader.cmake)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_EXTENSIONS OFF)
add_executable(test-cxx14_noext test-cxx.cpp test-pch.h ${pch_source})
add_precompiled_header(test-cxx14_noext test-pch.h)
add_test(test-cxx14_noext test-cxx14_noext)
5 changes: 5 additions & 0 deletions test/cxx14_noext/test-cxx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "test-pch.h"
#ifndef PCH
#error Missing precompiled header
#endif
int main() { return !(PCH == atoi(getenv("EXPECTED_PCH"))); }
1 change: 1 addition & 0 deletions test/cxx14_noext/test-pch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "test-pch.h"
16 changes: 16 additions & 0 deletions test/cxx14_noext/test-pch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#define PCH 1
#ifdef __cplusplus
#include <cstdlib>
#include <iostream>

#if __cplusplus < 201400L
#error This PCH has to be compiled in C++14 mode
#endif

#if __cplusplus > 201412L
#error This PCH has to be compiled in C++14 mode
#endif

#else
#include <stdlib.h>
#endif
2 changes: 2 additions & 0 deletions test/heavy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-heavy test-heavy.cpp test-pch.h ${pch_source})
add_precompiled_header(test-heavy test-pch.h FORCEINCLUDE)
add_test(test-heavy test-heavy)
2 changes: 2 additions & 0 deletions test/mixed-force/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.c test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-mixed-force test-mixed-force.c test-mixed-force.cpp test-pch.h ${pch_source})
add_precompiled_header(test-mixed-force test-pch.h FORCEINCLUDE)
add_test(test-mixed-force test-mixed-force)
2 changes: 2 additions & 0 deletions test/mixed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
set(pch_source test-pch.c test-pch.cpp)
endif()
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
add_executable(test-mixed test-mixed.c test-mixed.cpp test-pch.h ${pch_source})
add_precompiled_header(test-mixed test-pch.h)
add_test(test-mixed test-mixed)

0 comments on commit ba8ee37

Please sign in to comment.