summaryrefslogtreecommitdiff
path: root/testing/test.gni
blob: 3f6f175613234cc473a9092e35e2663b8ea340a6 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# ==============================================================================
# TEST SETUP
# ==============================================================================

template("_gen_isolate") {
  testonly = true
  _runtime_deps_file = "$target_gen_dir/$target_name.runtime_deps"
  group("${target_name}__write_deps") {
    forward_variables_from(invoker,
                           [
                             "data",
                             "data_deps",
                             "deps",
                             "public_deps",
                           ])
    write_runtime_deps = _runtime_deps_file
  }

  action(target_name) {
    script = "//testing/generate_isolate.py"
    inputs = [
      _runtime_deps_file,
    ]
    outputs = [
      invoker.output,
    ]
    args = [
      "--output-directory=.",
      "--out-file",
      rebase_path(invoker.output, root_build_dir),
      "--runtime-deps-file",
      rebase_path(_runtime_deps_file, root_build_dir),
    ]
    if (is_android) {
      args += [ "--apply-android-filters" ]
    }
    if (defined(invoker.apply_device_filters) && invoker.apply_device_filters) {
      args += [ "--apply-device-filters" ]
    }
    _assert_no_odd_data =
        defined(invoker.assert_no_odd_data) && invoker.assert_no_odd_data
    if (_assert_no_odd_data) {
      args += [ "--assert-no-odd-data" ]
    }
    if (defined(invoker.command)) {
      _isolate_dir = get_path_info(invoker.output, "dir")
      args += [
        "--command",
        rebase_path(invoker.command, _isolate_dir),
      ]
    }
    deps = [
      ":${invoker.target_name}__write_deps",
    ]
  }
}

# Define a test as an executable (or apk on Android) with the "testonly" flag
# set.
# Variable:
#   use_raw_android_executable: Use executable() rather than android_apk().
#   use_native_activity: Test implements ANativeActivity_onCreate().
template("test") {
  if (is_android) {
    import("//build/config/android/config.gni")
    import("//build/config/android/rules.gni")
    import("//build/config/sanitizers/sanitizers.gni")

    _use_raw_android_executable = defined(invoker.use_raw_android_executable) &&
                                  invoker.use_raw_android_executable

    # output_name is used to allow targets with the same name but in different
    # packages to still produce unique runner scripts.
    _output_name = invoker.target_name
    if (defined(invoker.output_name)) {
      _output_name = invoker.output_name
    }

    _test_runner_target = "${_output_name}__test_runner_script"
    _wrapper_script_vars = [ "shard_timeout" ]
    _gen_isolate_vars = [
      "allow_odd_runtime_deps",
      "ignore_all_data_deps",
    ]
    _generate_device_isolate =
        !defined(invoker.ignore_all_data_deps) || !invoker.ignore_all_data_deps

    if (_generate_device_isolate) {
      _allow_odd_runtime_deps = defined(invoker.allow_odd_runtime_deps) &&
                                invoker.allow_odd_runtime_deps

      # The device isolate is needed at runtime, so it cannot go in
      # target_gen_dir, as builder/tester configurations do not include it.
      _target_dir_name = get_label_info(":$target_name", "dir")
      _device_isolate_path = "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.device.isolate"
      _gen_isolate_target_name = "${target_name}__isolate"
      _gen_isolate(_gen_isolate_target_name) {
        forward_variables_from(invoker,
                               [
                                 "data",
                                 "data_deps",
                                 "deps",
                                 "public_deps",
                               ])
        assert_no_odd_data = !_allow_odd_runtime_deps
        output = _device_isolate_path
        apply_device_filters = true
      }
    }

    assert(_use_raw_android_executable || enable_java_templates)

    if (_use_raw_android_executable) {
      _exec_target = "${target_name}__exec"
      _dist_target = "${target_name}__dist"
      _exec_output =
          "$target_out_dir/${invoker.target_name}/${invoker.target_name}"

      executable(_exec_target) {
        # Configs will always be defined since we set_defaults in BUILDCONFIG.gn.
        configs = []
        data_deps = []
        forward_variables_from(
            invoker,
            "*",
            _wrapper_script_vars + _gen_isolate_vars + [ "extra_dist_files" ])
        testonly = true

        # Thanks to the set_defaults() for test(), configs are initialized with
        # the default shared_library configs rather than executable configs.
        configs -= [
          "//build/config:shared_library_config",
          "//build/config/android:hide_native_jni_exports",
        ]
        configs += [ "//build/config:executable_config" ]

        # Don't output to the root or else conflict with the group() below.
        output_name = rebase_path(_exec_output, root_out_dir)
        if (is_component_build || is_asan) {
          data_deps += [ "//build/android:cpplib_stripped" ]
        }
      }

      create_native_executable_dist(_dist_target) {
        testonly = true
        dist_dir = "$root_out_dir/$target_name"
        binary = _exec_output
        deps = [
          ":$_exec_target",
        ]
        if (defined(invoker.extra_dist_files)) {
          extra_files = invoker.extra_dist_files
        }
      }
    } else {
      _library_target = "_${target_name}__library"
      _apk_target = "${target_name}_apk"
      _apk_specific_vars = [
        "android_manifest",
        "enable_multidex",
        "proguard_configs",
        "proguard_enabled",
        "use_default_launcher",
        "write_asset_list",
        "use_native_activity",
      ]
      shared_library(_library_target) {
        # Configs will always be defined since we set_defaults in BUILDCONFIG.gn.
        configs = []  # Prevent list overwriting warning.
        configs = invoker.configs
        testonly = true

        deps = []
        forward_variables_from(invoker,
                               "*",
                               _apk_specific_vars + _wrapper_script_vars +
                                   _gen_isolate_vars + [ "visibility" ])

        if (!defined(invoker.use_default_launcher) ||
            invoker.use_default_launcher) {
          deps += [ "//testing/android/native_test:native_test_native_code" ]
        }
      }
      unittest_apk(_apk_target) {
        forward_variables_from(invoker, _apk_specific_vars + [ "deps" ])
        unittests_dep = ":$_library_target"
        apk_name = invoker.target_name
        if (defined(invoker.output_name)) {
          apk_name = invoker.output_name
          unittests_binary = "lib${apk_name}.so"
          install_script_name = "install_${invoker.output_name}"
        }
        deps += [ ":$_library_target" ]

        # TODO(agrieve): Remove this data_dep once bots don't build the _apk
        #     target (post-GYP).
        # It's a bit backwards for the apk to depend on the runner script, since
        # the apk is conceptually a runtime_dep of the script. However, it is
        # currently necessary because the bots build this _apk target directly
        # rather than the group() below.
        data_deps = [
          ":$_test_runner_target",
        ]
      }

      # Incremental test targets work only for .apks.
      _incremental_test_runner_target =
          "${_output_name}_incremental__test_runner_script"
      test_runner_script(_incremental_test_runner_target) {
        forward_variables_from(invoker, _wrapper_script_vars)
        if (_generate_device_isolate) {
          isolate_file = _device_isolate_path
          deps = [
            ":$_gen_isolate_target_name",
          ]
        }
        apk_target = ":$_apk_target"
        test_name = "${_output_name}_incremental"
        test_type = "gtest"
        test_suite = _output_name
        incremental_install = true
      }
      group("${target_name}_incremental") {
        testonly = true
        datadeps = [
          ":$_incremental_test_runner_target",
        ]
        deps = [
          ":${_apk_target}_incremental",
        ]
      }
    }

    _test_runner_target = "${_output_name}__test_runner_script"
    test_runner_script(_test_runner_target) {
      forward_variables_from(invoker, _wrapper_script_vars)
      if (_generate_device_isolate) {
        isolate_file = _device_isolate_path
        deps = [
          ":$_gen_isolate_target_name",
        ]
      }

      if (_use_raw_android_executable) {
        executable_dist_dir = "$root_out_dir/$_dist_target"
      } else {
        apk_target = ":$_apk_target"
      }
      test_name = _output_name
      test_type = "gtest"
      test_suite = _output_name
    }

    group(target_name) {
      testonly = true
      deps = [
        ":$_test_runner_target",
      ]
      if (_use_raw_android_executable) {
        deps += [ ":$_dist_target" ]
      } else {
        deps += [ ":$_apk_target" ]
      }
    }

    # TODO(GYP_GONE): Delete this after we've converted everything to GN.
    # The _run targets exist only for compatibility w/ GYP.
    group("${target_name}_apk_run") {
      testonly = true
      deps = [
        ":${invoker.target_name}",
      ]
    }
  } else if (is_ios) {
    import("//build/config/ios/rules.gni")

    _test_target = target_name
    _resources_bundle_data = target_name + "_resources_bundle_data"

    bundle_data(_resources_bundle_data) {
      visibility = [
        ":${_test_target}",
        ":${_test_target}_generate_executable",
      ]
      sources = [
        "//testing/gtest_ios/Default.png",
      ]
      outputs = [
        "{{bundle_resources_dir}}/{{source_file_part}}",
      ]
    }

    ios_app_bundle(_test_target) {
      testonly = true

      # See above call.
      set_sources_assignment_filter([])
      forward_variables_from(invoker, "*", [ "testonly" ])

      # Provide sensible defaults in case invoker did not define any of those
      # required variables.
      if (!defined(info_plist) && !defined(info_plist_target)) {
        info_plist = "//testing/gtest_ios/unittest-Info.plist"
      }
      if (!defined(entitlements_path)) {
        entitlements_path = "//testing/gtest_ios"
      }
      if (!defined(code_signing_identity)) {
        code_signing_identity = ""
      }

      # TODO(crbug.com/603102): remove this once gyp support is dropped and all
      # application uses the target name as value for BUNDLE_ID_TEST_NAME.
      if (defined(invoker.app_name)) {
        app_name = invoker.app_name
      } else {
        app_name = target_name
      }

      if (!defined(extra_substitutions)) {
        extra_substitutions = []
      }
      extra_substitutions += [ "BUNDLE_ID_TEST_NAME=$app_name" ]

      if (!defined(deps)) {
        deps = []
      }
      deps += [
        ":$_resources_bundle_data",

        # All shared libraries must have the sanitizer deps to properly link in
        # asan mode (this target will be empty in other cases).
        "//build/config/sanitizers:deps",
      ]
    }
  } else {
    executable(target_name) {
      deps = []
      forward_variables_from(invoker, "*")

      testonly = true
      deps += [
        # All shared libraries must have the sanitizer deps to properly link in
        # asan mode (this target will be empty in other cases).
        "//build/config/sanitizers:deps",

        # Give tests the default manifest on Windows (a no-op elsewhere).
        "//build/win:default_exe_manifest",
      ]
    }

    # TODO(GYP_GONE): Delete this after we've converted everything to GN.
    # The _run targets exist only for compatibility with GYP.
    group("${target_name}_run") {
      testonly = true
      deps = [
        ":${invoker.target_name}",
      ]
    }

    if (defined(invoker.output_name) && target_name != invoker.output_name) {
      group("${invoker.output_name}_run") {
        testonly = true
        deps = [
          ":${invoker.target_name}",
        ]
      }
    }
  }
}