dxc: build with asan or ubsan if enabled

Also make it possible to enable more than one sanitizer for Dawn targets, as it possible for DXC.

Change-Id: I1bf28f782524f72986c8e7c1331781eecd406e57
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/180521
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d6a3008..5a02630 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -387,18 +387,22 @@
       -Wno-switch-default
     )
 
+    set(SANITIZER_OPTIONS "")
     if (${DAWN_ENABLE_MSAN})
-      target_compile_options(${TARGET} PUBLIC -fsanitize=memory)
-      target_link_options(${TARGET} PUBLIC -fsanitize=memory)
-    elseif (${DAWN_ENABLE_ASAN})
-      target_compile_options(${TARGET} PUBLIC -fsanitize=address)
-      target_link_options(${TARGET} PUBLIC -fsanitize=address)
-    elseif (${DAWN_ENABLE_TSAN})
-      target_compile_options(${TARGET} PUBLIC -fsanitize=thread)
-      target_link_options(${TARGET} PUBLIC -fsanitize=thread)
-    elseif (${DAWN_ENABLE_UBSAN})
-      target_compile_options(${TARGET} PUBLIC -fsanitize=undefined -fsanitize=float-divide-by-zero)
-      target_link_options(${TARGET} PUBLIC -fsanitize=undefined -fsanitize=float-divide-by-zero)
+      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)
 
diff --git a/third_party/CMakeLists.txt b/third_party/CMakeLists.txt
index b21bbc4..a693fce 100644
--- a/third_party/CMakeLists.txt
+++ b/third_party/CMakeLists.txt
@@ -170,6 +170,14 @@
     set(LLVM_ENABLE_EH ON)
     set(CLANG_CL OFF)
 
+    if (DAWN_ENABLE_ASAN AND DAWN_ENABLE_UBSAN)
+        set(LLVM_USE_SANITIZER "Address;Undefined")
+    elseif(DAWN_ENABLE_ASAN)
+        set(LLVM_USE_SANITIZER "Address")
+    elseif(DAWN_ENABLE_UBSAN)
+        set(LLVM_USE_SANITIZER "Undefined")
+    endif()
+
     # Cache variables -- these are *not* scoped to this function
     set(LLVM_TARGETS_TO_BUILD "None" CACHE STRING "" FORCE)
     set(LLVM_DEFAULT_TARGET_TRIPLE "dxil-ms-dx" CACHE STRING "" FORCE)