Add build script to generate TF test XML file with filters

Bug: 68270274
Test: run 'atest' in frameworks/base
Change-Id: I99b741fae18997caa6aefbd3ab804e5d30e7ae36
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 1e64c4f..7863c7d 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -236,6 +236,7 @@
 LOCAL_STRIP_MODULE:=
 LOCAL_SYSTEM_SHARED_LIBRARIES:=none
 LOCAL_TARGET_REQUIRED_MODULES:=
+LOCAL_TEST_CONFIG_OPTIONS:=
 LOCAL_TEST_DATA:=
 LOCAL_TEST_MODULE_TO_PROGUARD_WITH:=
 LOCAL_TIDY:=
diff --git a/core/config.mk b/core/config.mk
index 232f1d0..cbafd50 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -135,6 +135,8 @@
 BUILD_HOST_DALVIK_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_java_library.mk
 BUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_static_java_library.mk
 
+BUILD_HOST_TEST_CONFIG:= $(BUILD_SYSTEM)/host_test_config.mk
+BUILD_TARGET_TEST_CONFIG:= $(BUILD_SYSTEM)/target_test_config.mk
 
 # ###############################################################
 # Parse out any modifier targets.
diff --git a/core/definitions.mk b/core/definitions.mk
index c7799b0..23d8648 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -3018,7 +3018,9 @@
   HOST_DALVIK_JAVA_LIBRARY \
   HOST_DALVIK_STATIC_JAVA_LIBRARY \
   base_rules \
-  HEADER_LIBRARY
+  HEADER_LIBRARY \
+  HOST_TEST_CONFIG \
+  TARGET_TEST_CONFIG
 
 $(foreach s,$(STATS.MODULE_TYPE),$(eval STATS.MODULE_TYPE.$(s) :=))
 define record-module-type
diff --git a/core/envsetup.mk b/core/envsetup.mk
index ddf903c..509e7f6 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -382,6 +382,8 @@
 
 HOST_CROSS_OUT_GEN := $(HOST_CROSS_OUT)/gen
 
+HOST_OUT_TEST_CONFIG := $(HOST_OUT)/test_config
+
 # Out for HOST_2ND_ARCH
 HOST_2ND_ARCH_VAR_PREFIX := 2ND_
 HOST_2ND_ARCH_MODULE_SUFFIX := _32
@@ -457,6 +459,7 @@
 TARGET_OUT_NOTICE_FILES := $(TARGET_OUT_INTERMEDIATES)/NOTICE_FILES
 TARGET_OUT_FAKE := $(PRODUCT_OUT)/fake_packages
 TARGET_OUT_TESTCASES := $(PRODUCT_OUT)/testcases
+TARGET_OUT_TEST_CONFIG := $(PRODUCT_OUT)/test_config
 
 ifeq ($(SANITIZE_LITE),true)
 # When using SANITIZE_LITE, APKs must not be packaged with sanitized libraries, as they will not
diff --git a/core/host_test_config.mk b/core/host_test_config.mk
new file mode 100644
index 0000000..b9975e5
--- /dev/null
+++ b/core/host_test_config.mk
@@ -0,0 +1,25 @@
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+# Common rules for building a TradeFed test XML file for host side tests.
+#
+
+$(call record-module-type,HOST_TEST_CONFIG)
+
+LOCAL_IS_HOST_MODULE := true
+
+include $(BUILD_SYSTEM)/test_config_common.mk
diff --git a/core/target_test_config.mk b/core/target_test_config.mk
new file mode 100644
index 0000000..61f5d2b
--- /dev/null
+++ b/core/target_test_config.mk
@@ -0,0 +1,23 @@
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+#
+# Common rules for building a TradeFed test XML file for target side tests.
+#
+
+$(call record-module-type,TARGET_TEST_CONFIG)
+
+include $(BUILD_SYSTEM)/test_config_common.mk
diff --git a/core/test_config_common.mk b/core/test_config_common.mk
new file mode 100644
index 0000000..487f9f2
--- /dev/null
+++ b/core/test_config_common.mk
@@ -0,0 +1,56 @@
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_MODULE_CLASS := TEST_CONFIG
+
+# Output test config files to testcases directory.
+ifeq (,$(filter general-tests, $(LOCAL_COMPATIBILITY_SUITE)))
+  LOCAL_COMPATIBILITY_SUITE += general-tests
+endif
+
+LOCAL_MODULE_SUFFIX := .config
+
+my_test_config_file := $(wildcard $(LOCAL_PATH)/$(LOCAL_MODULE).xml)
+LOCAL_SRC_FILES :=
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+# The test config is not in a standalone XML file.
+ifndef my_test_config_file
+
+ifndef LOCAL_TEST_CONFIG_OPTIONS
+  $(call pretty-error,LOCAL_TEST_CONFIG_OPTIONS must be set if the test XML file is not provided.)
+endif
+
+my_base_test_config_file := $(LOCAL_PATH)/AndroidTest.xml
+my_test_config_file := $(dir $(LOCAL_BUILT_MODULE))AndroidTest.xml
+
+$(my_test_config_file) : PRIVATE_test_config_options := $(LOCAL_TEST_CONFIG_OPTIONS)
+$(my_test_config_file) : $(my_base_test_config_file)
+	@echo "Create $(notdir $@) with options: $(PRIVATE_test_config_options)."
+	$(eval _option_xml := \
+		$(foreach option,$(PRIVATE_test_config_options), \
+			$(eval p := $(subst :,$(space),$(option))) \
+			<option name="$(word 1,$(p))" value="$(word 2,$(p))" \/>\n))
+	$(hide) sed 's&</configuration>&$(_option_xml)</configuration>&' $< > $@
+
+endif # my_test_config_file
+
+$(LOCAL_BUILT_MODULE) : $(my_test_config_file)
+	$(copy-file-to-target)
+
+my_base_test_config_file :=
+my_test_config_file :=