yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Jay KwakAllow a compiler warning on MacOS (#7561)7a6a455f1

master
6.6 KiB232 linesraw
1#
2# Given a list of flags, add those which the C++ compiler supports to the target
3#
4include(CheckCXXCompilerFlag)
5function(add_supported_cxx_flags target)
6    cmake_parse_arguments(ARG "PRIVATE;PUBLIC;INTERFACE" "" "" ${ARGN})
7    set(flags ${ARG_UNPARSED_ARGUMENTS})
8    if(ARG_PRIVATE)
9        set(private PRIVATE)
10    endif()
11    if(ARG_PUBLIC)
12        set(public PUBLIC)
13    endif()
14    if(ARG_INTERFACE)
15        set(interface INTERFACE)
16    endif()
17
18    foreach(flag ${flags})
19        # remove the `no-` prefix from warnings because gcc doesn't treat it as an
20        # error on its own
21        string(REGEX REPLACE "\\-Wno\\-(.+)" "-W\\1" flag_to_test "${flag}")
22        string(
23            REGEX REPLACE
24            "[^a-zA-Z0-9]+"
25            "_"
26            test_name
27            "CXXFLAG_${flag_to_test}"
28        )
29        check_cxx_compiler_flag("${flag_to_test}" ${test_name})
30        if(${test_name})
31            target_compile_options(
32                ${target}
33                ${private}
34                ${public}
35                ${interface}
36                ${flag}
37            )
38        endif()
39    endforeach()
40endfunction()
41
42#
43# Given a list of linker flags, add those which the compiler supports to the
44# target
45#
46include(CheckLinkerFlag)
47function(add_supported_cxx_linker_flags target)
48    cmake_parse_arguments(ARG "PRIVATE;PUBLIC;INTERFACE;BEFORE" "" "" ${ARGN})
49    set(flags ${ARG_UNPARSED_ARGUMENTS})
50    if(ARG_BEFORE)
51        set(before BEFORE)
52    endif()
53    if(ARG_PRIVATE)
54        set(private PRIVATE)
55    endif()
56    if(ARG_PUBLIC)
57        set(public PUBLIC)
58    endif()
59    if(ARG_INTERFACE)
60        set(interface INTERFACE)
61    endif()
62
63    foreach(flag ${flags})
64        string(
65            REGEX REPLACE
66            "[^a-zA-Z0-9]+"
67            "_"
68            test_name
69            "CXXLINKFLAG_${flag}"
70        )
71        check_linker_flag(CXX "${flag}" ${test_name})
72        if(${test_name})
73            target_link_options(
74                ${target}
75                ${before}
76                ${private}
77                ${public}
78                ${interface}
79                ${flag}
80            )
81        endif()
82    endforeach()
83endfunction()
84
85#
86# Add our default compiler flags to a target
87#
88# Pass USE_EXTRA_WARNINGS to enable -WExtra or /W3
89#
90function(set_default_compile_options target)
91    cmake_parse_arguments(
92        ARG
93        "USE_EXTRA_WARNINGS;USE_FEWER_WARNINGS"
94        ""
95        ""
96        ${ARGN}
97    )
98
99    set(warning_flags)
100    if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
101        list(
102            APPEND
103            warning_flags
104            -Wall
105            # Disabled warnings:
106            -Wno-switch
107            -Wno-parentheses
108            -Wno-unused-local-typedefs
109            -Wno-class-memaccess
110            -Wno-assume
111            -Wno-reorder
112            -Wno-invalid-offsetof
113            -Wno-newline-eof
114            -Wno-return-std-move
115            # Allowed warnings:
116            # If a function returns an address/reference to a local, we want it to
117            # produce an error, because it probably means something very bad.
118            -Werror=return-local-addr
119            # Allow unused variables with a pattern of `if (auto v = as<...>(...))`.
120            # This pattern is very common in Slang code base.
121            -Wno-error=unused-but-set-variable
122            # Some warnings which are on by default in MSVC
123            -Wnarrowing
124        )
125        if(ARG_USE_EXTRA_WARNINGS)
126            list(APPEND warning_flags -Wextra)
127        endif()
128        if(ARG_USE_FEWER_WARNINGS)
129            list(
130                APPEND
131                warning_flags
132                -Wno-class-memaccess
133                -Wno-unused-variable
134                -Wno-unused-parameter
135                -Wno-sign-compare
136                -Wno-unused-function
137                -Wno-unused-value
138                -Wno-unused-but-set-variable
139                -Wno-implicit-fallthrough
140                -Wno-missing-field-initializers
141                -Wno-strict-aliasing
142                -Wno-maybe-uninitialized
143            )
144        endif()
145    elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
146        list(APPEND warning_flags)
147        if(ARG_USE_EXTRA_WARNINGS)
148            list(APPEND warning_flags /W4)
149        elseif(ARG_USE_FEWER_WARNINGS)
150            list(APPEND warning_flags /W0)
151        else()
152            list(APPEND warning_flags /W2)
153        endif()
154    endif()
155
156    add_supported_cxx_flags(${target} PRIVATE ${warning_flags})
157
158    if(NOT WIN32)
159        # these options are for ELF specific and not for Windows
160        add_supported_cxx_linker_flags(
161            ${target}
162            PRIVATE
163            # Don't assume that symbols will be resolved at runtime
164            "-Wl,--no-undefined"
165            # No reason not to do this? Useful when using split debug info
166            "-Wl,--build-id"
167        )
168    endif()
169
170    set_target_properties(
171        ${target}
172        PROPERTIES # -fvisibility=hidden
173            CXX_VISIBILITY_PRESET
174            hidden
175            C_VISIBILITY_PRESET
176            hidden
177            VISIBILITY_INLINES_HIDDEN
178            ON
179            # C++ standard
180            CXX_STANDARD
181            20
182            # pic
183            POSITION_INDEPENDENT_CODE
184            ON
185    )
186
187    target_compile_definitions(
188        ${target}
189        PRIVATE # Add _DEBUG depending on the build configuration
190            $<$<CONFIG:Debug>:_DEBUG>
191            # For including windows.h in a way that minimized namespace
192            # pollution. Although we define these here, we still set them
193            # manually in any header files which may be included by another
194            # project
195            WIN32_LEAN_AND_MEAN
196            VC_EXTRALEAN
197            NOMINMAX
198            # Use multi-byte character set on Windows
199            UNICODE
200            _UNICODE
201    )
202
203    #
204    # Settings dependent on config options
205    #
206
207    target_compile_definitions(
208        ${target}
209        PRIVATE
210            SLANG_ENABLE_DXIL_SUPPORT=$<BOOL:${SLANG_ENABLE_DXIL}>
211            $<$<BOOL:${SLANG_ENABLE_FULL_DEBUG_VALIDATION}>:SLANG_ENABLE_FULL_IR_VALIDATION>
212            $<$<BOOL:${SLANG_ENABLE_IR_BREAK_ALLOC}>:SLANG_ENABLE_IR_BREAK_ALLOC>
213            $<$<BOOL:${SLANG_ENABLE_DX_ON_VK}>:SLANG_CONFIG_DX_ON_VK>
214            $<$<STREQUAL:${SLANG_LIB_TYPE},STATIC>:STB_IMAGE_STATIC>
215    )
216
217    if(SLANG_ENABLE_ASAN)
218        add_supported_cxx_flags(
219            ${target}
220            PRIVATE
221            /fsanitize=address
222            -fsanitize=address
223        )
224        add_supported_cxx_linker_flags(
225            ${target}
226            BEFORE
227            PUBLIC
228            /INCREMENTAL:NO
229            -fsanitize=address
230        )
231    endif()
232endfunction()