From f56aeef645a1fc798e677c0ff4fd2b349e068e9f Mon Sep 17 00:00:00 2001
From: Xinliang David Li <davidxl@google.com>
Date: Fri, 19 Feb 2016 17:52:28 +0000
Subject: [PATCH] [PGO] Enable profile-rt testing  on all supported targets

Differential Revision: http://reviews.llvm.org/D17361

llvm-svn: 261344
---
 compiler-rt/test/profile/CMakeLists.txt  | 29 ++++++++++++++++++++----
 compiler-rt/test/profile/lit.cfg         | 20 +++++++++++++---
 compiler-rt/test/profile/lit.site.cfg.in |  2 ++
 3 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/compiler-rt/test/profile/CMakeLists.txt b/compiler-rt/test/profile/CMakeLists.txt
index 28fb35a9f6f8..bfbb0c05027c 100644
--- a/compiler-rt/test/profile/CMakeLists.txt
+++ b/compiler-rt/test/profile/CMakeLists.txt
@@ -1,16 +1,35 @@
 set(PROFILE_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 set(PROFILE_LIT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
 
+set(PROFILE_TESTSUITES)
 set(PROFILE_TEST_DEPS ${SANITIZER_COMMON_LIT_TEST_DEPS})
 if(NOT COMPILER_RT_STANDALONE_BUILD)
   list(APPEND PROFILE_TEST_DEPS profile llvm-profdata)
 endif()
 
-configure_lit_site_cfg(
-  ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
-  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
-  )
+set(PROFILE_TEST_ARCH ${PROFILE_SUPPORTED_ARCH})
+if(APPLE)
+  darwin_filter_host_archs(PROFILE_SUPPORTED_ARCH PROFILE_TEST_ARCH)
+endif()
+
+foreach(arch ${PROFILE_TEST_ARCH})
+  set(PROFILE_TEST_TARGET_ARCH ${arch})
+  if(${arch} MATCHES "arm|aarch64")
+    # This is only true if we're cross-compiling.
+    set(PROFILE_TEST_TARGET_CFLAGS ${COMPILER_RT_TEST_COMPILER_CFLAGS})
+  else()
+    get_target_flags_for_arch(${arch} PROFILE_TEST_TARGET_CFLAGS)
+    string(REPLACE ";" " " PROFILE_TEST_TARGET_CFLAGS "${PROFILE_TEST_TARGET_CFLAGS}")
+  endif()
+  set(CONFIG_NAME Profile-${arch})
+  configure_lit_site_cfg(
+    ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
+    ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg
+   )
+  list(APPEND PROFILE_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
+endforeach()
+
 add_lit_testsuite(check-profile "Running the profile tests"
-  ${CMAKE_CURRENT_BINARY_DIR}
+  ${PROFILE_TESTSUITES}
   DEPENDS ${PROFILE_TEST_DEPS})
 set_target_properties(check-profile PROPERTIES FOLDER "Profile tests")
diff --git a/compiler-rt/test/profile/lit.cfg b/compiler-rt/test/profile/lit.cfg
index b1b44a1a0665..4e654fa5adaf 100644
--- a/compiler-rt/test/profile/lit.cfg
+++ b/compiler-rt/test/profile/lit.cfg
@@ -2,8 +2,17 @@
 
 import os
 
+def get_required_attr(config, attr_name):
+  attr_value = getattr(config, attr_name, None)
+  if attr_value == None:
+    lit_config.fatal(
+      "No attribute %r in test configuration! You may need to run "
+      "tests from your build directory or add this attribute "
+      "to lit.site.cfg " % attr_name)
+  return attr_value
+
 # Setup config name.
-config.name = 'Profile'
+config.name = 'Profile-' + config.target_arch
 
 # Setup source root.
 config.test_source_root = os.path.dirname(__file__)
@@ -11,7 +20,7 @@ config.test_source_root = os.path.dirname(__file__)
 # Setup executable root.
 if hasattr(config, 'profile_lit_binary_dir') and \
         config.profile_lit_binary_dir is not None:
-    config.test_exec_root = config.profile_lit_binary_dir
+    config.test_exec_root = os.path.join(config.profile_lit_binary_dir, config.name)
 
 # If the above check didn't work, we're probably in the source tree.  Use some
 # magic to re-execute from the build tree.
@@ -36,15 +45,20 @@ config.suffixes = ['.c', '.cc', '.cpp', '.m', '.mm', '.ll', '.test']
 config.excludes = ['Inputs']
 
 # Clang flags.
-clang_cflags = [config.target_cflags] + extra_linkflags
+target_cflags=[get_required_attr(config, "target_cflags")]
+clang_cflags = target_cflags + extra_linkflags
+clang_cxxflags = config.cxx_mode_flags + clang_cflags
 
 def build_invocation(compile_flags):
   return " " + " ".join([config.clang] + compile_flags) + " "
 
 # Add clang substitutions.
 config.substitutions.append( ("%clang ", build_invocation(clang_cflags)) )
+config.substitutions.append( ("%clangxx ", build_invocation(clang_cxxflags)) )
 config.substitutions.append( ("%clang_profgen ", build_invocation(clang_cflags) + " -fprofile-instr-generate ") )
 config.substitutions.append( ("%clang_profuse=", build_invocation(clang_cflags) + " -fprofile-instr-use=") )
+config.substitutions.append( ("%clangxx_profgen ", build_invocation(clang_cxxflags) + " -fprofile-instr-generate ") )
+config.substitutions.append( ("%clangxx_profuse=", build_invocation(clang_cxxflags) + " -fprofile-instr-use=") )
 config.substitutions.append( ("%clang_profgen_gcc=", build_invocation(clang_cflags) + " -fprofile-generate=") )
 config.substitutions.append( ("%clang_profuse_gcc=", build_invocation(clang_cflags) + " -fprofile-use=") )
 
diff --git a/compiler-rt/test/profile/lit.site.cfg.in b/compiler-rt/test/profile/lit.site.cfg.in
index 758568d6753a..168caf93dfc7 100644
--- a/compiler-rt/test/profile/lit.site.cfg.in
+++ b/compiler-rt/test/profile/lit.site.cfg.in
@@ -3,6 +3,8 @@
 
 # Tool-specific config options.
 config.profile_lit_binary_dir = "@PROFILE_LIT_BINARY_DIR@"
+config.target_cflags = "@PROFILE_TEST_TARGET_CFLAGS@"
+config.target_arch = "@PROFILE_TEST_TARGET_ARCH@"
 
 # Load common config for all compiler-rt lit tests.
 lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured")