Skip to content

Commit ba8ee37

Browse files
committed
Added support for the CXX_STANDARD and CXX_EXTENSIONS properties.
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.
1 parent 6803af3 commit ba8ee37

File tree

16 files changed

+105
-3
lines changed

16 files changed

+105
-3
lines changed

PrecompiledHeader.cmake

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,20 @@ function(export_all_flags _filename)
7272
set(_compile_definitions "$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions},\n-D>\n>")
7373
set(_compile_flags "$<$<BOOL:${_compile_flags}>:$<JOIN:${_compile_flags},\n>\n>")
7474
set(_compile_options "$<$<BOOL:${_compile_options}>:$<JOIN:${_compile_options},\n>\n>")
75-
file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}\n")
75+
76+
set(_cxx_standard "$<TARGET_PROPERTY:${_target},CXX_STANDARD>")
77+
set(_cxx_extensions "$<TARGET_PROPERTY:${_target},CXX_EXTENSIONS>")
78+
set(_has_extensions "$<OR:$<STREQUAL:${_cxx_extensions},>,$<BOOL:${_cxx_extensions}>>") # CXX_EXTENSION default to true
79+
set(_cxx_standard_check "$<$<AND:$<STREQUAL:${_cxx_standard},17>,$<NOT:${_has_extensions}>>:${CMAKE_CXX17_STANDARD_COMPILE_OPTION}>")
80+
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},14>,$<NOT:${_has_extensions}>>:${CMAKE_CXX14_STANDARD_COMPILE_OPTION}>")
81+
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},11>,$<NOT:${_has_extensions}>>:${CMAKE_CXX11_STANDARD_COMPILE_OPTION}>")
82+
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},98>,$<NOT:${_has_extensions}>>:${CMAKE_CXX98_STANDARD_COMPILE_OPTION}>")
83+
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},17>,${_has_extensions}>:${CMAKE_CXX17_EXTENSION_COMPILE_OPTION}>")
84+
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},14>,${_has_extensions}>:${CMAKE_CXX14_EXTENSION_COMPILE_OPTION}>")
85+
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},11>,${_has_extensions}>:${CMAKE_CXX11_EXTENSION_COMPILE_OPTION}>")
86+
set(_cxx_standard_check "${_cxx_standard_check}$<$<AND:$<STREQUAL:${_cxx_standard},98>,${_has_extensions}>:${CMAKE_CXX98_EXTENSION_COMPILE_OPTION}>")
87+
88+
file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}${_cxx_standard_check}\n")
7689
endfunction()
7790

7891
function(add_precompiled_header _target _input)
@@ -147,7 +160,7 @@ function(add_precompiled_header _target _input)
147160
endif()
148161
endif(MSVC)
149162

150-
if(CMAKE_COMPILER_IS_GNUCXX)
163+
if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU|Clang")
151164
get_filename_component(_name ${_input} NAME)
152165
set(_pch_header "${CMAKE_CURRENT_SOURCE_DIR}/${_input}")
153166
set(_pch_binary_dir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch")
@@ -210,5 +223,5 @@ function(add_precompiled_header _target _input)
210223
OBJECT_DEPENDS "${_object_depends}")
211224
endif()
212225
endforeach()
213-
endif(CMAKE_COMPILER_IS_GNUCXX)
226+
endif(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU|Clang")
214227
endfunction()

test/c-force/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ if(MSVC)
77
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
88
set(pch_source test-pch.c)
99
endif()
10+
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
11+
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
1012
add_executable(test-c-force test-c-force.c test-pch.h ${pch_source})
1113
add_precompiled_header(test-c-force test-pch.h FORCEINCLUDE)
1214
add_test(test-c-force test-c-force)

test/c/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ if(MSVC)
77
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
88
set(pch_source test-pch.c)
99
endif()
10+
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
11+
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
1012
add_executable(test-c test-c.c test-pch.h ${pch_source})
1113
add_precompiled_header(test-c test-pch.h)
1214
add_test(test-c test-c)

test/cxx-force/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ if(MSVC)
77
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
88
set(pch_source test-pch.cpp)
99
endif()
10+
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
11+
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
1012
add_executable(test-cxx-force test-cxx-force.cpp test-pch.h ${pch_source})
1113
add_precompiled_header(test-cxx-force test-pch.h FORCEINCLUDE)
1214
add_test(test-cxx-force test-cxx-force)

test/cxx/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ if(MSVC)
77
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
88
set(pch_source test-pch.cpp)
99
endif()
10+
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
11+
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
1012
add_executable(test-cxx test-cxx.cpp test-pch.h ${pch_source})
1113
add_precompiled_header(test-cxx test-pch.h)
1214
add_test(test-cxx test-cxx)

test/cxx11/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.3)
2+
cmake_policy(SET CMP0058 NEW)
3+
enable_testing()
4+
project(test-cxx11)
5+
include(../../PrecompiledHeader.cmake)
6+
if(MSVC)
7+
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
8+
set(pch_source test-pch.cpp)
9+
endif()
10+
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
11+
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
12+
set(CMAKE_CXX_STANDARD 11)
13+
add_executable(test-cxx11 test-cxx.cpp test-pch.h ${pch_source})
14+
add_precompiled_header(test-cxx11 test-pch.h)
15+
add_test(test-cxx11 test-cxx11)

test/cxx11/test-cxx.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "test-pch.h"
2+
#ifndef PCH
3+
#error Missing precompiled header
4+
#endif
5+
int main() { return !(PCH == atoi(getenv("EXPECTED_PCH"))); }

test/cxx11/test-pch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "test-pch.h"

test/cxx11/test-pch.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define PCH 1
2+
#ifdef __cplusplus
3+
#include <cstdlib>
4+
#include <iostream>
5+
6+
#if __cplusplus < 201100L
7+
#error This PCH has to be compiled in C++14 mode
8+
#endif
9+
10+
#if __cplusplus > 201112L
11+
#error This PCH has to be compiled in C++14 mode
12+
#endif
13+
14+
#else
15+
#include <stdlib.h>
16+
#endif

test/cxx14_noext/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.3)
2+
cmake_policy(SET CMP0058 NEW)
3+
enable_testing()
4+
project(test-cxx14_noext)
5+
include(../../PrecompiledHeader.cmake)
6+
if(MSVC)
7+
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
8+
set(pch_source test-pch.cpp)
9+
endif()
10+
set(CMAKE_CXX_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
11+
set(CMAKE_C_FLAGS "${CMAKH_CXX_FLAGS} -Werror")
12+
set(CMAKE_CXX_STANDARD 14)
13+
set(CMAKE_CXX_EXTENSIONS OFF)
14+
add_executable(test-cxx14_noext test-cxx.cpp test-pch.h ${pch_source})
15+
add_precompiled_header(test-cxx14_noext test-pch.h)
16+
add_test(test-cxx14_noext test-cxx14_noext)

0 commit comments

Comments
 (0)