Move compiler options into src/cmake/DawnCompilerExtraFlags cmake module
Change-Id: I4d27f1277d863192a52736658c0c63613b52e321
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/194906
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Jaswant Panchumarti <jaswant.panchumarti@kitware.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c7bfb7a..d006d49 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -268,6 +268,11 @@
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
+################################################################################
+# Include utility CMake modules
+################################################################################
+include(DawnCompilerExtraFlags)
+
message(STATUS "Dawn build D3D11 backend: ${DAWN_ENABLE_D3D11}")
message(STATUS "Dawn build D3D12 backend: ${DAWN_ENABLE_D3D12}")
message(STATUS "Dawn build Metal backend: ${DAWN_ENABLE_METAL}")
@@ -323,70 +328,6 @@
find_package(Python3 REQUIRED)
message(STATUS "Dawn: using python at ${Python3_EXECUTABLE}")
-################################################################################
-# common_compile_options - sets compiler and linker options common for dawn and
-# tint on the given target
-################################################################################
-function(common_compile_options TARGET)
- if (COMPILER_IS_LIKE_GNU)
- target_compile_options(${TARGET} PRIVATE
- -fno-exceptions
- -fno-rtti
-
- -Wno-deprecated-builtins
- -Wno-unknown-warning-option
- -Wno-switch-default
- )
- if (${DAWN_WERROR})
- target_compile_options(${TARGET} PRIVATE -Werror)
- endif()
-
- set(SANITIZER_OPTIONS "")
- if (${DAWN_ENABLE_MSAN})
- list(APPEND SANITIZER_OPTIONS "-fsanitize=memory")
- endif()
- if (${DAWN_ENABLE_ASAN})
- list(APPEND SANITIZER_OPTIONS "-fsanitize=address")
- endif()
- if (${DAWN_ENABLE_TSAN})
- list(APPEND SANITIZER_OPTIONS "-fsanitize=thread")
- endif()
- if (${DAWN_ENABLE_UBSAN})
- list(APPEND SANITIZER_OPTIONS "-fsanitize=undefined" "-fsanitize=float-divide-by-zero")
- endif()
- if (SANITIZER_OPTIONS)
- target_compile_options(${TARGET} PUBLIC ${SANITIZER_OPTIONS})
- target_link_options(${TARGET} PUBLIC ${SANITIZER_OPTIONS})
- endif()
- endif(COMPILER_IS_LIKE_GNU)
-
- if(MSVC)
- target_compile_options(${TARGET} PUBLIC /utf-8)
- endif()
-
- if (DAWN_EMIT_COVERAGE)
- target_compile_definitions(${TARGET} PRIVATE "DAWN_EMIT_COVERAGE")
- if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
- target_compile_options(${TARGET} PRIVATE "--coverage")
- target_link_options(${TARGET} PRIVATE "gcov")
- elseif(COMPILER_IS_CLANG OR COMPILER_IS_CLANG_CL)
- target_compile_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
- target_link_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
- else()
- message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
- endif()
- endif(DAWN_EMIT_COVERAGE)
-endfunction()
-
-if (${DAWN_ENABLE_TSAN})
- add_compile_options(-stdlib=libc++)
- add_link_options(-stdlib=libc++)
-endif()
-
-################################################################################
-# Dawn's public and internal "configs"
-################################################################################
-
set(IS_DEBUG_BUILD 0)
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
if ((NOT ${build_type} STREQUAL "RELEASE") AND (NOT ${build_type} STREQUAL "RELWITHDEBINFO"))
diff --git a/src/cmake/DawnCompilerExtraFlags.cmake b/src/cmake/DawnCompilerExtraFlags.cmake
new file mode 100644
index 0000000..8133040
--- /dev/null
+++ b/src/cmake/DawnCompilerExtraFlags.cmake
@@ -0,0 +1,101 @@
+# Copyright 2024 The Dawn & Tint Authors
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# 1. Redistributions of source code must retain the above copyright notice, this
+# list of conditions and the following disclaimer.
+#
+# 2. Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# 3. Neither the name of the copyright holder nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+if (${DAWN_ENABLE_TSAN})
+ add_compile_options(-stdlib=libc++)
+ add_link_options(-stdlib=libc++)
+endif ()
+
+################################################################################
+# common_compile_options - sets compiler and linker options common for dawn
+################################################################################
+function(common_compile_options target)
+ if (COMPILER_IS_LIKE_GNU)
+ target_compile_options(${target}
+ PRIVATE
+ "-fno-exceptions"
+ "-fno-rtti"
+
+ "-Wno-deprecated-builtins"
+ "-Wno-unknown-warning-option"
+ "-Wno-switch-default"
+ )
+ if (${DAWN_WERROR})
+ target_compile_options(${target}
+ PRIVATE "-Werror")
+ endif ()
+ endif ()
+
+ if (MSVC)
+ target_compile_options(${target}
+ PUBLIC "/utf-8")
+ endif ()
+
+ if (COMPILER_IS_LIKE_GNU)
+ set(SANITIZER_OPTIONS "")
+ if (${DAWN_ENABLE_MSAN})
+ list(APPEND SANITIZER_OPTIONS "-fsanitize=memory")
+ endif ()
+ if (${DAWN_ENABLE_ASAN})
+ list(APPEND SANITIZER_OPTIONS "-fsanitize=address")
+ endif ()
+ if (${DAWN_ENABLE_TSAN})
+ list(APPEND SANITIZER_OPTIONS "-fsanitize=thread")
+ endif ()
+ if (${DAWN_ENABLE_UBSAN})
+ list(APPEND SANITIZER_OPTIONS
+ "-fsanitize=undefined"
+ "-fsanitize=float-divide-by-zero")
+ endif ()
+ if (SANITIZER_OPTIONS)
+ target_compile_options(${target}
+ PUBLIC ${SANITIZER_OPTIONS})
+ target_link_options(${target}
+ PUBLIC ${SANITIZER_OPTIONS})
+ endif ()
+ endif ()
+
+ option(DAWN_EMIT_COVERAGE "Emit code coverage information" OFF)
+ if (DAWN_EMIT_COVERAGE)
+ target_compile_definitions(${target}
+ PRIVATE "DAWN_EMIT_COVERAGE")
+ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
+ target_compile_options(${target}
+ PRIVATE "--coverage")
+ target_link_options(${target}
+ PRIVATE "gcov")
+ elseif (COMPILER_IS_CLANG OR COMPILER_IS_CLANG_CL)
+ target_compile_options(${target}
+ PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
+ target_link_options(${target}
+ PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
+ else()
+ message(FATAL_ERROR
+ "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
+ endif ()
+ endif ()
+endfunction()