Debian Codegraph @

prboom2/cmake/DsdaTargetFeatures.cmake

raw
1 include_guard()
2
3 include(DsdaHelpers)
4
5 # Internal functions, these should not be called from outside this module
6
7 function(dsda_internal_setup_warnings_msvc result_var)
8 set(${result_var}
9 "/W3"
10 PARENT_SCOPE
11 )
12 endfunction()
13
14 function(dsda_internal_setup_warnings_gnu result_var)
15 set(GNU_WARNINGS
16 "-Wall"
17 "-Wwrite-strings"
18 "-Wundef"
19 "-Wtype-limits"
20 "-Wcast-qual"
21 "-Wpointer-arith"
22 "-Wno-unused-function"
23 "-Wno-switch"
24 "-Wno-sign-compare"
25 "-Wno-missing-field-initializers"
26 )
27 set(GNU_C_WARNINGS
28 -Wabsolute-value
29 -Wno-pointer-sign
30 -Wdeclaration-after-statement
31 -Wbad-function-cast
32 -Wno-strict-prototypes
33 )
34 if(CMAKE_C_COMPILER_ID STREQUAL "GNU"
35 OR (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 18)
36 OR (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 17)
37 )
38 list(APPEND GNU_WARNINGS "-Wno-format-truncation")
39 endif()
40 if(CMAKE_C_COMPILER_ID MATCHES "Clang")
41 list(APPEND GNU_WARNINGS
42 "-Wno-tautological-constant-out-of-range-compare"
43 "-Wno-tautological-unsigned-enum-zero-compare"
44 "-Wno-misleading-indentation"
45 )
46 endif()
47
48 set(GNU_WARNINGS_SET ${GNU_WARNINGS} ${GNU_C_WARNINGS})
49
50 include(CheckCCompilerFlag)
51 check_c_compiler_flag("${GNU_WARNINGS_SET}" DSDA_SUPPORTS_GNU_WARNINGS)
52
53 if(NOT DSDA_SUPPORTS_GNU_WARNINGS)
54 message(AUTHOR_WARNING
55 "The default set of warnings is not supported.\n"
56 "Set 'DSDA_ENABLED_WARNINGS' manually to a list of warnings you wish to enable."
57 )
58 set(SUPPORTED_GNU_WARNINGS "")
59 else()
60 set(SUPPORTED_GNU_WARNINGS ${GNU_WARNINGS})
61 foreach(c_warning IN LISTS GNU_C_WARNINGS)
62 list(APPEND
63 SUPPORTED_GNU_WARNINGS
64 $<$<COMPILE_LANGUAGE:C>:${c_warning}>
65 )
66 endforeach()
67 endif()
68
69 set(${result_var}
70 "${SUPPORTED_GNU_WARNINGS}"
71 PARENT_SCOPE
72 )
73 endfunction()
74
75 function(dsda_internal_setup_warnings)
76 if(MSVC)
77 dsda_internal_setup_warnings_msvc(PLATFORM_WARNINGS)
78 else()
79 dsda_internal_setup_warnings_gnu(PLATFORM_WARNINGS)
80 endif()
81 set(DSDA_ENABLED_WARNINGS
82 "${PLATFORM_WARNINGS}"
83 CACHE STRING
84 "List of compiler warnings"
85 )
86 endfunction()
87
88 function(dsda_internal_check_fast_math_flag)
89 if(MSVC)
90 set(FAST_MATH_FLAG "/fp:fast")
91 else()
92 set(FAST_MATH_FLAG "-ffast-math")
93 endif()
94 include(CheckCCompilerFlag)
95 check_c_compiler_flag("${FAST_MATH_FLAG}" DSDA_SUPPORTS_FAST_MATH)
96
97 if(NOT DSDA_SUPPORTS_FAST_MATH)
98 message(AUTHOR_WARNING
99 "Could not detect the flag to enable fast math.\n"
100 "Set 'DSDA_FAST_MATH_FLAG' manually to a list of flags needed to enable it."
101 )
102 set(FAST_MATH_FLAG "")
103 endif()
104
105 set(DSDA_FAST_MATH_FLAG
106 "${FAST_MATH_FLAG}"
107 CACHE STRING
108 "Flags to enable fast math"
109 )
110 endfunction()
111
112 # Public functions
113
114 function(dsda_target_set_warnings tgt)
115 dsda_fail_if_invalid_target(${tgt})
116
117 if(NOT DEFINED CACHE{DSDA_ENABLED_WARNINGS})
118 dsda_internal_setup_warnings()
119 endif()
120
121 target_compile_options(${tgt}
122 PRIVATE
123 ${DSDA_ENABLED_WARNINGS}
124 )
125 endfunction()
126
127 function(dsda_target_silence_deprecation tgt)
128 dsda_fail_if_invalid_target(${tgt})
129
130 if(WIN32)
131 target_compile_definitions(${tgt}
132 PRIVATE
133 "_CRT_SECURE_NO_WARNINGS" # Warnings for not using "secure" (_s) functions
134 "_CRT_NONSTDC_NO_WARNINGS" # Warnings for using POSIX names of functions
135 )
136 elseif(APPLE)
137 target_compile_definitions(${tgt}
138 PRIVATE
139 "GL_SILENCE_DEPRECATION" # Calling GL functions is deprecated on macOS
140 )
141 endif()
142 endfunction()
143
144 function(dsda_target_enable_fast_math tgt)
145 dsda_fail_if_invalid_target(${tgt})
146
147 if(NOT DEFINED CACHE{DSDA_FAST_MATH_FLAG})
148 dsda_internal_check_fast_math_flag()
149 endif()
150
151 target_compile_options(${tgt}
152 PRIVATE
153 ${DSDA_FAST_MATH_FLAG}
154 )
155 endfunction()