[SCUDO] add cmake options for custom sysroot

These options will allow the SCUDO standalone to be built with custom
headers. Specifically, this patch will enable building with the
LLVM-libc headers.

Reviewed By: abrachet

Differential Revision: https://reviews.llvm.org/D135702

GitOrigin-RevId: b9663ebbf825eed3fc45fbb7d4773271ee2ca259
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fc7607e..7f4737f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -57,6 +57,13 @@
 mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN)
 option(COMPILER_RT_ENABLE_CET "Build Compiler RT with CET enabled" OFF)
 
+option(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH "Set custom sysroot for building SCUDO standalone" OFF)
+mark_as_advanced(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH)
+option(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED "Build SCUDO standalone for shared libraries" ON)
+mark_as_advanced(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED)
+option(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC "Build SCUDO standalone with LLVM's libc headers" OFF)
+mark_as_advanced(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC)
+
 if(FUCHSIA)
   set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF)
 else()
diff --git a/lib/scudo/standalone/CMakeLists.txt b/lib/scudo/standalone/CMakeLists.txt
index 4b586bc..d7d1c32 100644
--- a/lib/scudo/standalone/CMakeLists.txt
+++ b/lib/scudo/standalone/CMakeLists.txt
@@ -38,6 +38,10 @@
 append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SCUDO_LINK_FLAGS)
 append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none SCUDO_LINK_FLAGS)
 
+if(COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH)
+  list(APPEND SCUDO_CFLAGS "--sysroot=${COMPILER_RT_SCUDO_STANDALONE_SYSROOT_PATH}")
+endif()
+
 if(ANDROID)
   list(APPEND SCUDO_CFLAGS -fno-emulated-tls)
 
@@ -137,6 +141,14 @@
 
 set(SCUDO_LINK_LIBS ${COMPILER_RT_UNWINDER_LINK_LIBS})
 
+if(COMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC)
+  include_directories(${COMPILER_RT_BINARY_DIR}/../libc/include/)
+
+  set(SCUDO_DEPS libc-headers)
+
+  list(APPEND SCUDO_CFLAGS "-ffreestanding")
+endif()
+
 append_list_if(COMPILER_RT_HAS_LIBPTHREAD -pthread SCUDO_LINK_FLAGS)
 
 append_list_if(FUCHSIA zircon SCUDO_LINK_LIBS)
@@ -150,17 +162,20 @@
     ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
     SOURCES ${SCUDO_SOURCES}
     ADDITIONAL_HEADERS ${SCUDO_HEADERS}
-    CFLAGS ${SCUDO_CFLAGS})
+    CFLAGS ${SCUDO_CFLAGS}
+    DEPS ${SCUDO_DEPS})
   add_compiler_rt_object_libraries(RTScudoStandaloneCWrappers
     ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
     SOURCES ${SCUDO_SOURCES_C_WRAPPERS}
     ADDITIONAL_HEADERS ${SCUDO_HEADERS}
-    CFLAGS ${SCUDO_CFLAGS})
+    CFLAGS ${SCUDO_CFLAGS}
+    DEPS ${SCUDO_DEPS})
   add_compiler_rt_object_libraries(RTScudoStandaloneCxxWrappers
     ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
     SOURCES ${SCUDO_SOURCES_CXX_WRAPPERS}
     ADDITIONAL_HEADERS ${SCUDO_HEADERS}
-    CFLAGS ${SCUDO_CFLAGS})
+    CFLAGS ${SCUDO_CFLAGS}
+    DEPS ${SCUDO_DEPS})
 
   add_compiler_rt_runtime(clang_rt.scudo_standalone
     STATIC
@@ -168,6 +183,7 @@
     SOURCES ${SCUDO_SOURCES} ${SCUDO_SOURCES_C_WRAPPERS}
     ADDITIONAL_HEADERS ${SCUDO_HEADERS}
     CFLAGS ${SCUDO_CFLAGS}
+    DEPS ${SCUDO_DEPS}
     OBJECT_LIBS ${SCUDO_OBJECT_LIBS}
     PARENT_TARGET scudo_standalone)
   add_compiler_rt_runtime(clang_rt.scudo_standalone_cxx
@@ -176,18 +192,22 @@
     SOURCES ${SCUDO_SOURCES_CXX_WRAPPERS}
     ADDITIONAL_HEADERS ${SCUDO_HEADERS}
     CFLAGS ${SCUDO_CFLAGS}
+    DEPS ${SCUDO_DEPS}
     PARENT_TARGET scudo_standalone)
 
-  add_compiler_rt_runtime(clang_rt.scudo_standalone
-    SHARED
-    ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
-    SOURCES ${SCUDO_SOURCES} ${SCUDO_SOURCES_C_WRAPPERS} ${SCUDO_SOURCES_CXX_WRAPPERS}
-    ADDITIONAL_HEADERS ${SCUDO_HEADERS}
-    CFLAGS ${SCUDO_CFLAGS}
-    OBJECT_LIBS ${SCUDO_OBJECT_LIBS}
-    LINK_FLAGS ${SCUDO_LINK_FLAGS}
-    LINK_LIBS ${SCUDO_LINK_LIBS}
-    PARENT_TARGET scudo_standalone)
+  if(COMPILER_RT_SCUDO_STANDALONE_BUILD_SHARED)
+    add_compiler_rt_runtime(clang_rt.scudo_standalone
+      SHARED
+      ARCHS ${SCUDO_STANDALONE_SUPPORTED_ARCH}
+      SOURCES ${SCUDO_SOURCES} ${SCUDO_SOURCES_C_WRAPPERS} ${SCUDO_SOURCES_CXX_WRAPPERS}
+      ADDITIONAL_HEADERS ${SCUDO_HEADERS}
+      CFLAGS ${SCUDO_CFLAGS}
+      DEPS ${SCUDO_DEPS}
+      OBJECT_LIBS ${SCUDO_OBJECT_LIBS}
+      LINK_FLAGS ${SCUDO_LINK_FLAGS}
+      LINK_LIBS ${SCUDO_LINK_LIBS}
+      PARENT_TARGET scudo_standalone)
+  endif()
 
   add_subdirectory(benchmarks)
   if(COMPILER_RT_INCLUDE_TESTS)