| 1 |
|
| 2 |
# Copyright Catch2 Authors |
| 3 |
# Distributed under the Boost Software License, Version 1.0. |
| 4 |
# (See accompanying file LICENSE.txt or copy at |
| 5 |
# https://www.boost.org/LICENSE_1_0.txt) |
| 6 |
|
| 7 |
# SPDX-License-Identifier: BSL-1.0 |
| 8 |
|
| 9 |
include(CheckCXXCompilerFlag) |
| 10 |
function(add_cxx_flag_if_supported_to_targets flagname targets) |
| 11 |
string(MAKE_C_IDENTIFIER ${flagname} flag_identifier ) |
| 12 |
check_cxx_compiler_flag("${flagname}" HAVE_FLAG_${flag_identifier}) |
| 13 |
|
| 14 |
if (HAVE_FLAG_${flag_identifier}) |
| 15 |
foreach(target ${targets}) |
| 16 |
target_compile_options(${target} PRIVATE ${flagname}) |
| 17 |
endforeach() |
| 18 |
endif() |
| 19 |
endfunction() |
| 20 |
|
| 21 |
# Assumes that it is only called for development builds, where warnings |
| 22 |
# and Werror is desired, so it also enables Werror. |
| 23 |
function(add_warnings_to_targets targets) |
| 24 |
LIST(LENGTH targets TARGETS_LEN) |
| 25 |
# For now we just assume 2 possibilities: msvc and msvc-like compilers, |
| 26 |
# and other. |
| 27 |
if (MSVC) |
| 28 |
foreach(target ${targets}) |
| 29 |
# Force MSVC to consider everything as encoded in utf-8 |
| 30 |
target_compile_options( ${target} PRIVATE /utf-8 ) |
| 31 |
# Enable Werror equivalent |
| 32 |
if (CATCH_ENABLE_WERROR) |
| 33 |
target_compile_options( ${target} PRIVATE /WX ) |
| 34 |
endif() |
| 35 |
|
| 36 |
# MSVC is currently handled specially |
| 37 |
if ( CMAKE_CXX_COMPILER_ID MATCHES "MSVC" ) |
| 38 |
STRING(REGEX REPLACE "/W[0-9]" "/W4" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) # override default warning level |
| 39 |
target_compile_options( ${target} PRIVATE /w44265 /w44061 /w44062 /w45038 ) |
| 40 |
endif() |
| 41 |
endforeach() |
| 42 |
|
| 43 |
endif() |
| 44 |
|
| 45 |
if (NOT MSVC) |
| 46 |
set(CHECKED_WARNING_FLAGS |
| 47 |
"-Wabsolute-value" |
| 48 |
"-Wall" |
| 49 |
"-Wc++20-compat" |
| 50 |
"-Wcall-to-pure-virtual-from-ctor-dtor" |
| 51 |
"-Wcast-align" |
| 52 |
"-Wcatch-value" |
| 53 |
"-Wdangling" |
| 54 |
"-Wdeprecated" |
| 55 |
"-Wdeprecated-register" |
| 56 |
"-Wexceptions" |
| 57 |
"-Wexit-time-destructors" |
| 58 |
"-Wextra" |
| 59 |
"-Wextra-semi" |
| 60 |
"-Wfloat-equal" |
| 61 |
"-Wglobal-constructors" |
| 62 |
"-Winit-self" |
| 63 |
"-Wmisleading-indentation" |
| 64 |
"-Wmismatched-new-delete" |
| 65 |
"-Wmismatched-return-types" |
| 66 |
"-Wmismatched-tags" |
| 67 |
"-Wmissing-braces" |
| 68 |
"-Wmissing-declarations" |
| 69 |
"-Wmissing-noreturn" |
| 70 |
"-Wmissing-prototypes" |
| 71 |
"-Wmissing-variable-declarations" |
| 72 |
"-Wnull-dereference" |
| 73 |
"-Wold-style-cast" |
| 74 |
"-Woverloaded-virtual" |
| 75 |
"-Wparentheses" |
| 76 |
"-Wpedantic" |
| 77 |
"-Wredundant-decls" |
| 78 |
"-Wreorder" |
| 79 |
"-Wreturn-std-move" |
| 80 |
"-Wshadow" |
| 81 |
"-Wstrict-aliasing" |
| 82 |
"-Wsuggest-destructor-override" |
| 83 |
"-Wsuggest-override" |
| 84 |
"-Wundef" |
| 85 |
"-Wuninitialized" |
| 86 |
"-Wunneeded-internal-declaration" |
| 87 |
"-Wunreachable-code-aggressive" |
| 88 |
"-Wunused" |
| 89 |
"-Wunused-function" |
| 90 |
"-Wunused-parameter" |
| 91 |
"-Wvla" |
| 92 |
"-Wweak-vtables" |
| 93 |
|
| 94 |
# This is a useful warning, but our tests sometimes rely on |
| 95 |
# functions being present, but not picked (e.g. various checks |
| 96 |
# for stringification implementation ordering). |
| 97 |
# Ergo, we should use it every now and then, but we cannot |
| 98 |
# enable it by default. |
| 99 |
# "-Wunused-member-function" |
| 100 |
) |
| 101 |
foreach(warning ${CHECKED_WARNING_FLAGS}) |
| 102 |
add_cxx_flag_if_supported_to_targets(${warning} "${targets}") |
| 103 |
endforeach() |
| 104 |
|
| 105 |
if (CATCH_ENABLE_WERROR) |
| 106 |
foreach(target ${targets}) |
| 107 |
# Enable Werror equivalent |
| 108 |
target_compile_options( ${target} PRIVATE -Werror ) |
| 109 |
endforeach() |
| 110 |
endif() |
| 111 |
endif() |
| 112 |
endfunction() |
| 113 |
|
| 114 |
# Adds flags required for reproducible build to the target |
| 115 |
# Currently only supports GCC and Clang |
| 116 |
function(add_build_reproducibility_settings target) |
| 117 |
# Make the build reproducible on versions of g++ and clang that supports -ffile-prefix-map |
| 118 |
if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) |
| 119 |
add_cxx_flag_if_supported_to_targets("-ffile-prefix-map=${CATCH_DIR}/=" "${target}") |
| 120 |
endif() |
| 121 |
endfunction() |