[compiler-rt] Explicitly set dependency on libcxx for MemProfUnitTest

MemProfUnitTest now depends on libcxx but the dependency is not
explicitly expressed in build system, causing build races. This patch
addresses this issue.

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

GitOrigin-RevId: 692131f41aa1a8b4e1f7606546d206201ae2e55c
diff --git a/lib/memprof/tests/CMakeLists.txt b/lib/memprof/tests/CMakeLists.txt
index 4c12c1a..1dfd783 100644
--- a/lib/memprof/tests/CMakeLists.txt
+++ b/lib/memprof/tests/CMakeLists.txt
@@ -30,6 +30,10 @@
   list(APPEND MEMPROF_UNITTEST_LINK_FLAGS -pthread)
 endif()
 
+foreach(lib ${SANITIZER_TEST_CXX_LIBRARIES})
+  list(APPEND MEMPROF_UNITTEST_LINK_FLAGS -l${lib})
+endforeach()
+
 if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST MEMPROF_SUPPORTED_ARCH)
   # MemProf unit tests are only run on the host machine.
   set(arch ${COMPILER_RT_DEFAULT_TARGET_ARCH})
@@ -45,7 +49,11 @@
     $<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>)
   set_target_compile_flags(MemProfUnitTests ${MEMPROF_UNITTEST_CFLAGS})
   set_target_link_flags(MemProfUnitTests ${MEMPROF_UNITTEST_LINK_FLAGS})
-  target_link_libraries(MemProfUnitTests dl) 
+  target_link_libraries(MemProfUnitTests dl)
+
+  if (TARGET cxx-headers OR HAVE_LIBCXX)
+    add_dependencies(MemProfUnitTests cxx-headers)
+  endif()
 
   set_target_properties(MemProfUnitTests PROPERTIES
     RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})