huangcm
2025-07-01 676035278781360996553c427a12bf358249ebf7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Defines a target named $(my_target) for running robolectric tests.
 
# Running the tests is done in two stages: we first generate the test output to
# $(my_target_output), which is also added to the dist list, and store the
# return value of running the tests in $(my_target_retval). After that we
# process the output and return value as part of $(my_target). This is needed
# to make sure that we can install the test output even if the tests actually
# fail.
 
# Files in which to store the output and return value of the tests.
my_target_xml := $(intermediates)/$(my_filename_stem)-output.xml
my_target_output := $(intermediates)/$(my_filename_stem)-output.txt
my_target_retval := $(intermediates)/$(my_filename_stem)-retval.txt
 
# We should always re-run the tests, even if nothing has changed.
# So until the build system has a dedicated "no cache" option, claim
# to write a file that is never produced.
my_target_nocache := $(intermediates)/$(my_filename_stem)-nocache
 
# Private variables.
$(my_target_output): PRIVATE_MODULE := $(LOCAL_MODULE)
$(my_target_output): PRIVATE_TESTS := $(my_tests)
$(my_target_output): PRIVATE_JARS := $(my_jars)
$(my_target_output): PRIVATE_JAVA_ARGS := $(my_java_args)
$(my_target_output): PRIVATE_ROBOLECTRIC_PATH := $(my_robolectric_path)
$(my_target_output): PRIVATE_ROBOLECTRIC_SCRIPT_PATH := $(my_robolectric_script_path)
$(my_target_output): PRIVATE_TARGET_MESSAGE := $(my_target_message)
$(my_target_output): PRIVATE_TARGET_OUTPUT := $(my_target_output)
$(my_target_output): PRIVATE_TARGET_RETVAL := $(my_target_retval)
$(my_target_output): PRIVATE_TARGET_NOCACHE := $(my_target_nocache)
$(my_target_output): PRIVATE_TIMEOUT := $(my_timeout)
$(my_target_output): PRIVATE_XML_OUTPUT_FILE := $(my_target_xml)
$(my_target_output): .KATI_IMPLICIT_OUTPUTS := $(my_target_xml) $(my_target_retval) $(my_target_nocache)
# Runs the Robolectric tests and saves the output and return value.
$(my_target_output): $(my_jars)
   @echo "host Robolectric: $(PRIVATE_MODULE)"
   # Run `touch` to always create the output XML file, so the build doesn't break even if the
   # runner failed to create the XML output
   $(hide) touch "$(PRIVATE_XML_OUTPUT_FILE)"
   $(hide) rm -f "$(PRIVATE_TARGET_NOCACHE)"
   $(hide) \
     PRIVATE_INTERMEDIATES="$(dir $@)" \
     PRIVATE_JARS="$(PRIVATE_JARS)" \
     PRIVATE_JAVA_ARGS="$(PRIVATE_JAVA_ARGS)" \
     PRIVATE_ROBOLECTRIC_PATH="$(PRIVATE_ROBOLECTRIC_PATH)" \
     PRIVATE_ROBOLECTRIC_SCRIPT_PATH="$(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)" \
     PRIVATE_RUN_INDIVIDUALLY="$(ROBOTEST_RUN_INDIVIDUALLY)" \
     PRIVATE_TARGET_MESSAGE="$(PRIVATE_TARGET_MESSAGE)" \
     PRIVATE_TIMEOUT="$(PRIVATE_TIMEOUT)" \
     PRIVATE_TESTS="$(PRIVATE_TESTS)" \
     XML_OUTPUT_FILE="$(PRIVATE_XML_OUTPUT_FILE)" \
     TEST_WORKSPACE="$(PRIVATE_MODULE)" \
     $(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)/wrapper.sh \
       "$(PRIVATE_MODULE)" \
       "$(PRIVATE_TARGET_OUTPUT)" \
       "$(PRIVATE_TARGET_RETVAL)" \
       wrap \
       $(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)/robotest.sh
 
# Private variables.
$(my_target): PRIVATE_MODULE := $(LOCAL_MODULE)
$(my_target): PRIVATE_TARGET_OUTPUT := $(my_target_output)
$(my_target): PRIVATE_TARGET_RETVAL := $(my_target_retval)
$(my_target): PRIVATE_FAILURE_FATAL := $(my_failure_fatal)
$(my_target): PRIVATE_ROBOLECTRIC_SCRIPT_PATH := $(my_robolectric_script_path)
# Process the output and the return value of the tests. This will fail if the
# return value is non-zero.
$(my_target): $(my_target_output) $(my_target_xml)
   $(hide) \
     result=0; \
     $(PRIVATE_ROBOLECTRIC_SCRIPT_PATH)/wrapper.sh \
       "$(PRIVATE_MODULE)" \
       "$(PRIVATE_TARGET_OUTPUT)" \
       "$(PRIVATE_TARGET_RETVAL)" \
       eval \
         || result=$$?; \
     if [ "$(strip $(PRIVATE_FAILURE_FATAL))" = true ]; then \
       exit "$$result"; \
     fi
   $(hide) touch $@
 
# Add the output of the tests to the dist list, so that we will include it even
# if the tests fail.
$(call dist-for-goals, $(my_phony_target), \
    $(my_target_output):robotests/$(LOCAL_MODULE)-$(notdir $(my_target_output)) \
    $(my_target_xml):robotests/$(LOCAL_MODULE)-$(notdir $(my_target_xml)))
 
# Clean up local variables.
my_target_output :=
my_target_retval :=
my_target_xml :=
my_target_nocache :=
my_filename_stem :=