From ba71a0746f31a2f5f9af992f569a9418133a9a38 Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson@google.com>
Date: Mon, 26 Oct 2020 13:36:01 -0700
Subject: [PATCH] [MemProf] Decouple memprof build from
 COMPILER_RT_BUILD_SANITIZERS

The MemProf compiler-rt support relies on some of the support only built
when COMPILER_RT_BUILD_SANITIZERS was enabled. This showed up in some
initial bot failures, and I addressed those by making the memprof
runtime build also conditional on COMPILER_RT_BUILD_SANITIZERS
(3ed77ecd0a5d5e5c33770f0f9d3d75cf2f80c80b). However, this resulted in
another inconsistency with how the tests were set up that was hit by
Chromium:
  https://bugs.chromium.org/p/chromium/issues/detail?id=1142191

Undo the original bot fix and address this with a more comprehensive fix
that enables memprof to be built even when COMPILER_RT_BUILD_SANITIZERS
is disabled, by also building the necessary pieces under
COMPILER_RT_BUILD_MEMPROF.

Tested by configuring with a similar command as to what was used in the
failing Chromium configure. I reproduced the Chromium failure, as well
as the original bot failure I tried to fix in
3ed77ecd0a5d5e5c33770f0f9d3d75cf2f80c80b, with that fix reverted.
Confirmed it now works.

Differential Revision: https://reviews.llvm.org/D90190
---
 compiler-rt/include/CMakeLists.txt | 8 +++++++-
 compiler-rt/lib/CMakeLists.txt     | 9 +++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/include/CMakeLists.txt b/compiler-rt/include/CMakeLists.txt
index de4cf3655610..b00e8caa1ddd 100644
--- a/compiler-rt/include/CMakeLists.txt
+++ b/compiler-rt/include/CMakeLists.txt
@@ -5,7 +5,6 @@ if (COMPILER_RT_BUILD_SANITIZERS)
     sanitizer/common_interface_defs.h
     sanitizer/coverage_interface.h
     sanitizer/dfsan_interface.h
-    sanitizer/memprof_interface.h
     sanitizer/hwasan_interface.h
     sanitizer/linux_syscall_hooks.h
     sanitizer/lsan_interface.h
@@ -21,6 +20,12 @@ if (COMPILER_RT_BUILD_SANITIZERS)
     )
 endif(COMPILER_RT_BUILD_SANITIZERS)
 
+if (COMPILER_RT_BUILD_MEMPROF)
+  set(MEMPROF_HEADERS
+    sanitizer/memprof_interface.h
+    )
+endif(COMPILER_RT_BUILD_MEMPROF)
+
 if (COMPILER_RT_BUILD_XRAY)
   set(XRAY_HEADERS
     xray/xray_interface.h
@@ -38,6 +43,7 @@ endif(COMPILER_RT_BUILD_PROFILE)
 set(COMPILER_RT_HEADERS
   ${SANITIZER_HEADERS}
   ${FUZZER_HEADERS}
+  ${MEMPROF_HEADERS}
   ${XRAY_HEADERS}
   ${PROFILE_HEADERS})
 
diff --git a/compiler-rt/lib/CMakeLists.txt b/compiler-rt/lib/CMakeLists.txt
index aa7591f0d9b4..c1c6880a4720 100644
--- a/compiler-rt/lib/CMakeLists.txt
+++ b/compiler-rt/lib/CMakeLists.txt
@@ -9,7 +9,7 @@ include(SanitizerUtils)
 #
 #TODO: Refactor sanitizer_common into smaller pieces (e.g. flag parsing, utils).
 if (COMPILER_RT_HAS_SANITIZER_COMMON AND
-    (COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_XRAY))
+    (COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_XRAY OR COMPILER_RT_BUILD_MEMPROF))
   add_subdirectory(sanitizer_common)
 endif()
 
@@ -34,9 +34,11 @@ function(compiler_rt_build_runtime runtime)
   endif()
 endfunction()
 
-if(COMPILER_RT_BUILD_SANITIZERS)
+if(COMPILER_RT_BUILD_SANITIZERS OR COMPILER_RT_BUILD_MEMPROF)
   compiler_rt_build_runtime(interception)
+endif()
 
+if(COMPILER_RT_BUILD_SANITIZERS)
   if(COMPILER_RT_HAS_SANITIZER_COMMON)
     add_subdirectory(stats)
     add_subdirectory(lsan)
@@ -60,8 +62,7 @@ if(COMPILER_RT_BUILD_LIBFUZZER)
   compiler_rt_build_runtime(fuzzer)
 endif()
 
-if(COMPILER_RT_BUILD_MEMPROF AND COMPILER_RT_HAS_SANITIZER_COMMON AND
-   COMPILER_RT_BUILD_SANITIZERS)
+if(COMPILER_RT_BUILD_MEMPROF AND COMPILER_RT_HAS_SANITIZER_COMMON)
   compiler_rt_build_runtime(memprof)
 endif()