diff --git a/README.markdown b/README.markdown index 7a96a92..49ac3ed 100644 --- a/README.markdown +++ b/README.markdown @@ -241,6 +241,20 @@ To run the targets: cmake --build [] --target doxygen ``` +#### `noa_add_default_options` + +Configure a target with an opinionated set of strict compiler options. + +```cmake +noa_add_default_options([visibility] [target]) +``` + +For example: + +```cmake +noa_add_default_options(PUBLIC my_lib) +```` + #### `noa_sanitizer` Provides a unified interface for setting up a set of compiler sanitizers diff --git a/cmake/noa.cmake b/cmake/noa.cmake index 71b9247..383b647 100644 --- a/cmake/noa.cmake +++ b/cmake/noa.cmake @@ -4,6 +4,7 @@ include("${NOA_DIRECTORY}/variables.cmake") include("${NOA_DIRECTORY}/defaults.cmake") include("${NOA_DIRECTORY}/library.cmake") include("${NOA_DIRECTORY}/compiler/sanitizer.cmake") +include("${NOA_DIRECTORY}/compiler/options.cmake") include("${NOA_DIRECTORY}/options/enum.cmake") include("${NOA_DIRECTORY}/commands/copy-file.cmake") include("${NOA_DIRECTORY}/targets/clang-format.cmake") diff --git a/cmake/noa/compiler/options.cmake b/cmake/noa/compiler/options.cmake new file mode 100644 index 0000000..200955c --- /dev/null +++ b/cmake/noa/compiler/options.cmake @@ -0,0 +1,74 @@ +function(noa_add_default_options visibility target) + if(NOA_COMPILER_MSVC) + # See https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category + target_compile_options("${target}" ${visibility} + /options:strict + /permissive- + /W4 + /WL + /MP + /sdl) + else() + target_compile_options("${target}" ${visibility} + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wdouble-promotion + -Wconversion + -Wunused-parameter + -Wtrigraphs + -Wunreachable-code + -Wmissing-braces + -Wparentheses + -Wswitch + -Wunused-function + -Wunused-label + -Wunused-parameter + -Wunused-variable + -Wunused-value + -Wempty-body + -Wuninitialized + -Wshadow + -Wconversion + -Wenum-conversion + -Wfloat-conversion + -Wimplicit-fallthrough + -Wsign-compare + # TODO: Enable this flag for safety + -Wno-sign-conversion + -Wunknown-pragmas + -Wnon-virtual-dtor + -Woverloaded-virtual + -Winvalid-offsetof + + # Assume that signed arithmetic overflow of addition, subtraction and + # multiplication wraps around using twos-complement representation + # See https://users.cs.utah.edu/~regehr/papers/overflow12.pdf + # See https://www.postgresql.org/message-id/1689.1134422394@sss.pgh.pa.us + -fwrapv) + endif() + + if(NOA_COMPILER_LLVM) + target_compile_options("${target}" ${visibility} + -Wbool-conversion + -Wint-conversion + -Wpointer-sign + -Wconditional-uninitialized + -Wconstant-conversion + -Wnon-literal-null-conversion + -Wshorten-64-to-32 + -Wdeprecated-implementations + -Winfinite-recursion + -Wnewline-eof + -Wfour-char-constants + -Wselector + -Wundeclared-selector + -Wdocumentation + -Wmove + -Wc++11-extensions + -Wcomma + -Wno-exit-time-destructors + -Wrange-loop-analysis) + endif() +endfunction()