Simpleperf: insert testdata into simpleperf_unit_test for target build.

Before this change, we need to download testdata/ to device to run
unit tests. By inserting testdata into unit test elf file, we no
longer need to dowload testdata/ or add -t option to run unit tests
on device.

Change-Id: I888def6d55f893768dd8917083eead869b78c24b
diff --git a/simpleperf/Android.mk b/simpleperf/Android.mk
index 1ec496e..6179cfe 100644
--- a/simpleperf/Android.mk
+++ b/simpleperf/Android.mk
@@ -248,6 +248,12 @@
 
 LOCAL_STATIC_LIBRARIES += libsimpleperf $(simpleperf_static_libraries_target)
 LOCAL_SHARED_LIBRARIES := $(simpleperf_shared_libraries_target)
+LOCAL_POST_LINK_CMD = \
+  TMP_FILE=`mktemp $(OUT_DIR)/simpleperf-post-link-XXXXXXXXXX` && \
+  (cd $(LOCAL_PATH)/testdata && zip - -0 -r .) > $$TMP_FILE && \
+  $($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_OBJCOPY) --add-section .testzipdata=$$TMP_FILE $(linked_module) && \
+  rm -f $$TMP_FILE
+
 LOCAL_MULTILIB := first
 include $(BUILD_NATIVE_TEST)
 
@@ -313,7 +319,7 @@
 include $(CLEAR_VARS)
 LOCAL_CLANG := true
 LOCAL_MODULE := libsimpleperf_cts_test
-LOCAL_CPPFLAGS := $(simpleperf_cppflags_target) -DIN_CTS_TEST
+LOCAL_CPPFLAGS := $(simpleperf_cppflags_target)
 LOCAL_SRC_FILES := $(libsimpleperf_cts_test_src_files)
 LOCAL_STATIC_LIBRARIES := $(simpleperf_static_libraries_target)
 LOCAL_SHARED_LIBRARIES := $(simpleperf_shared_libraries_target)
@@ -326,7 +332,7 @@
 LOCAL_CLANG := true
 LOCAL_MODULE := libsimpleperf_cts_test
 LOCAL_MODULE_HOST_OS := linux
-LOCAL_CPPFLAGS := $(simpleperf_cppflags_host) -DIN_CTS_TEST
+LOCAL_CPPFLAGS := $(simpleperf_cppflags_host)
 LOCAL_CPPFLAGS_linux := $(simpleperf_cppflags_host_linux)
 LOCAL_SRC_FILES := $(libsimpleperf_cts_test_src_files)
 LOCAL_STATIC_LIBRARIES := $(simpleperf_static_libraries_host)
diff --git a/simpleperf/gtest_main.cpp b/simpleperf/gtest_main.cpp
index 8ff45c8..099bd81 100644
--- a/simpleperf/gtest_main.cpp
+++ b/simpleperf/gtest_main.cpp
@@ -29,7 +29,7 @@
 
 static std::string testdata_dir;
 
-#if defined(IN_CTS_TEST)
+#if defined(__ANDROID__)
 static const std::string testdata_section = ".testzipdata";
 
 static bool ExtractTestDataFromElfSection() {
@@ -90,7 +90,7 @@
   EndIteration(cookie);
   return true;
 }
-#endif  // defined(IN_CTS_TEST)
+#endif  // defined(__ANDROID__)
 
 int main(int argc, char** argv) {
   InitLogging(argv, android::base::StderrLogger);
@@ -103,7 +103,7 @@
     }
   }
 
-#if defined(IN_CTS_TEST)
+#if defined(__ANDROID__)
   std::unique_ptr<TemporaryDir> tmp_dir;
   if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
     tmp_dir.reset(new TemporaryDir);
@@ -114,6 +114,7 @@
     }
   }
 #endif
+
   if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
     printf("Usage: %s -t <testdata_dir>\n", argv[0]);
     return 1;