[cmake] Forward RTTI setting to glslang

Fixes CMake UBSan build on FYI.

Also slightly refactors DAWN_ENABLE_RTTI and how it's set and forwarded.

Tested locally (there doesn't seem to be a way to test on CQ).

Bug: b:434977307
Change-Id: I187d82cb902ac400c6fe30c2084a069be57af550
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/266395
Commit-Queue: Kai Ninomiya <kainino@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Reviewed-by: Brandon Jones <bajones@chromium.org>
Reviewed-by: Kaylee Lubick <kjlubick@google.com>
Auto-Submit: Kai Ninomiya <kainino@chromium.org>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ef35888..2b4acbe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -139,7 +139,12 @@
 option(DAWN_ENABLE_TSAN "Enable thread sanitizer" OFF)
 option(DAWN_ENABLE_MSAN "Enable memory sanitizer" OFF)
 option(DAWN_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
-option(DAWN_ENABLE_RTTI "Enable RTTI (needed when using some sanitizers)" OFF)
+option(DAWN_ENABLE_RTTI "Enable RTTI (implicitly enabled if UBSan is enabled)" OFF)
+if (DAWN_ENABLE_UBSAN)
+    # -fsanitize=undefined includes -fsanitize=vptr which does not work without RTTI. Force enable
+    # it by shadowing the option with a normal variable (so we don't overwrite it in CMakeCache.txt)
+    set(DAWN_ENABLE_RTTI ON)
+endif()
 
 option(DAWN_ENABLE_D3D11 "Enable compilation of the D3D11 backend" ${ENABLE_D3D11})
 option(DAWN_ENABLE_D3D12 "Enable compilation of the D3D12 backend" ${ENABLE_D3D12})
@@ -209,11 +214,6 @@
 endif()
 option(DAWN_FETCH_DEPENDENCIES "Use fetch_dawn_dependencies.py as an alternative to using depot_tools" OFF)
 
-# -fsanitizer=undefined includes -fsanitizer=vptr which does not work without rtti
-if (DAWN_ENABLE_UBSAN AND NOT DAWN_ENABLE_RTTI)
-    message(FATAL_ERROR "DAWN_ENABLE_UBSAN requires DAWN_ENABLE_RTTI to support some of the sanitizers")
-endif()
-
 if (DAWN_ENABLE_OPENGLES OR DAWN_ENABLE_DESKTOP_GL)
   set(TINT_DEFAULT_GLSL ON)
 else()
@@ -290,10 +290,11 @@
 message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
 message(STATUS "")
 
-message(STATUS "Dawn build with ASAN: ${DAWN_ENABLE_ASAN}")
-message(STATUS "Dawn build with TSAN: ${DAWN_ENABLE_TSAN}")
-message(STATUS "Dawn build with MSAN: ${DAWN_ENABLE_MSAN}")
-message(STATUS "Dawn build with UBSAN: ${DAWN_ENABLE_UBSAN}")
+message(STATUS "Dawn build with ASan: ${DAWN_ENABLE_ASAN}")
+message(STATUS "Dawn build with TSan: ${DAWN_ENABLE_TSAN}")
+message(STATUS "Dawn build with MSan: ${DAWN_ENABLE_MSAN}")
+message(STATUS "Dawn build with UBSan: ${DAWN_ENABLE_UBSAN}")
+message(STATUS "Dawn build with RTTI: ${DAWN_ENABLE_RTTI}")
 message(STATUS "DAWN Werror: ${DAWN_WERROR}")
 message(STATUS "Dawn enable install: ${DAWN_ENABLE_INSTALL}")
 message(STATUS "Dawn allow system component fallback: ${DAWN_FORCE_SYSTEM_COMPONENT_LOAD}")
diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt
index 132e6a7..368dc97 100644
--- a/third_party/CMakeLists.txt
+++ b/third_party/CMakeLists.txt
@@ -120,6 +120,9 @@
     return()
 endif()
 
+# Force ENABLE_RTTI in spirv-tools and glslang.
+set(ENABLE_RTTI ${DAWN_ENABLE_RTTI} CACHE BOOL "" FORCE)
+
 if (NOT TARGET SPIRV-Headers)
     set(SPIRV_HEADERS_SKIP_EXAMPLES ON CACHE BOOL "" FORCE)
     set(SPIRV_HEADERS_SKIP_INSTALL ON CACHE BOOL "" FORCE)
@@ -138,9 +141,6 @@
     set(SPIRV_SKIP_EXECUTABLES ON CACHE BOOL "" FORCE)
     set(SKIP_SPIRV_TOOLS_INSTALL ON CACHE BOOL "" FORCE)
     set(SPIRV_WERROR OFF CACHE BOOL OFF FORCE)
-    # ENABLE_RTTI is defined in spirv-tools's CMakeLists.txt
-    set(ENABLE_RTTI ${DAWN_ENABLE_RTTI} CACHE BOOL "" FORCE)
-
     message(STATUS "Dawn: using SPIRV-Tools at ${DAWN_SPIRV_TOOLS_DIR}")
     add_subdirectory(${DAWN_SPIRV_TOOLS_DIR} "${CMAKE_CURRENT_BINARY_DIR}/spirv-tools" EXCLUDE_FROM_ALL)
 endif()