Debian Codegraph @

builds/cmake/OJ-misc.cmake

raw
1
2 # ninja/parallel builds disable output colors, forcing them here.
3 # Needs to be at global scope to catch all targets.
4 # cmake has this build-in since 3.24
5 if(CMAKE_VERSION VERSION_LESS "3.24")
6 option(CMAKE_COLOR_DIAGNOSTICS "Always produce ANSI-colored output" OFF)
7 if(CMAKE_COLOR_DIAGNOSTICS)
8 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
9 add_compile_options("-fdiagnostics-color=always")
10 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
11 add_compile_options("-fcolor-diagnostics")
12 endif()
13 endif()
14 endif()
15
16
17 # compiler cache
18 option(WANT_CCACHE "Use ccache to speed up rebuilds" OFF)
19 if(WANT_CCACHE)
20 if(NOT FOUND_CCACHE)
21 find_program(CCACHE_EXECUTABLE ccache)
22
23 if(CCACHE_EXECUTABLE)
24 message(STATUS "Using ccache as CXX compiler launcher")
25 set(FOUND_CCACHE 1 CACHE INTERNAL "ccache has been found")
26 endif()
27 endif()
28
29 if(CCACHE_EXECUTABLE)
30 set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}")
31 endif()
32 endif()
33
34 # warnings
35 if(MSVC)
36 add_compile_options(/W4)
37 else()
38 add_compile_options(-Wall -Wextra)
39 endif()
40
41 # global scope, since ASAN needs to catch all targets
42 option(WANT_ASAN "build with address sanitizer" OFF)
43 if(WANT_ASAN)
44 message(STATUS "Building with address sanitizer")
45 add_compile_options(-fno-omit-frame-pointer -fsanitize=address)
46 add_link_options(-fno-omit-frame-pointer -fsanitize=address)
47 endif()
48
49 # formatting and static analysis
50 file(GLOB_RECURSE ALL_SRC
51 ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
52 ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h)
53
54 if(NOT FOUND_ASTYLE)
55 find_program(ASTYLE_EXECUTABLE astyle)
56
57 if(ASTYLE_EXECUTABLE)
58 message(STATUS "Found source code formatter: astyle")
59 set(FOUND_ASTYLE 1 CACHE INTERNAL "Astyle has been found")
60 endif()
61 endif()
62 if(ASTYLE_EXECUTABLE)
63 list(APPEND ASTYLE_ARGS
64 --suffix=none
65 --style=attach
66 --indent=tab=4
67 --pad-oper
68 --pad-header
69 --unpad-paren
70 --max-code-length=100
71 --break-after-logical
72 --attach-closing-while
73 --align-pointer=type
74 --align-reference=name
75 --indent-classes
76 --indent-preproc-block
77 --indent-switches
78 --min-conditional-indent=0)
79
80 add_custom_target(format
81 COMMAND ${ASTYLE_EXECUTABLE} ${ASTYLE_ARGS} ${ALL_SRC}
82 COMMENT "Running astyle to format source code"
83 VERBATIM)
84 endif()
85
86 if(NOT FOUND_CPPCHECK)
87 find_program(CPPCHECK_EXECUTABLE cppcheck)
88 if(CPPCHECK_EXECUTABLE)
89 message(STATUS "Found static analysis tool: cppcheck")
90 set(FOUND_CPPCHECK 1 CACHE INTERNAL "Cppcheck has been found")
91 endif()
92 endif()
93 if(CPPCHECK_EXECUTABLE)
94 list(APPEND CPPCHECK_ARGS
95 --enable=warning,style,performance,portability,unusedFunction
96 --std=c++14
97 --language=c++
98 -I${CMAKE_CURRENT_SOURCE_DIR}/src
99 -U__SYMBIAN32__ -UUIQ3 -UENABLE_JJ2 # unmaintained
100 -UGP2X -UWIZ -UDINGOO -UCAANOO -UGAMESHELL -U__riscos__ # contributed
101 -UPSP -U__vita__ -U__3DS__ -U__wii__ -U__SWITCH__ # homebrew
102 #--enable=information
103 )
104
105 add_custom_target(cppcheck
106 COMMAND ${CPPCHECK_EXECUTABLE} ${CPPCHECK_ARGS} ${ALL_SRC}
107 COMMENT "Running cppcheck for static analysis"
108 USES_TERMINAL VERBATIM)
109 endif()
110
111 if(NOT FOUND_ASTYLE AND NOT FOUND_CPPCHECK)
112 message(STATUS "No source code formatter or static analysis tool enabled.")
113 endif()