xref: /test/xts/tools/build/suite.gni (revision 31c75014)
1# Copyright (c) 2021 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6#     http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14import("//build/ohos.gni")
15import("//build/test.gni")
16if (precise_xts) {
17  import("//out/preloader/precise_run.gni")
18}
19
20declare_args() {
21  SUITES_OUTPUT_ROOT = "$root_out_dir/suites"
22  SUITE_ARCHIVE_DIR = "$root_out_dir/suites/archives"
23  TESTCONFIG_FILENAME = "Test.xml"
24  XTS_ROOT = "//test/xts"
25  ACTS_ROOT = "//test/xts/acts"
26  HATS_ROOT = "//test/xts/hats"
27  HITS_ROOT = "//test/xts/hits"
28  DCTS_ROOT = "//test/xts/dcts"
29
30  # create testsuite archive is time-consuming, do it only if necessary
31  make_archive = false
32  XTS_SUITENAME = getenv("XTS_SUITENAME")
33}
34
35template("ohos_testsuite_base") {
36  assert(defined(invoker.project_type),
37         "project_type is required in target ${target_name}")
38
39  _part_name = "common"
40
41  if (defined(invoker.part_name)) {
42    _part_name = invoker.part_name
43  }
44
45  precise_build_boolean = true
46  if (precise_xts && filter_include(precise_run,
47                                    [
48                                      "all",
49                                      _part_name,
50                                    ]) == []) {
51    print("precise_xts is on ,${_part_name} is not on build")
52    precise_build_boolean = false
53  }
54  if (precise_build_boolean) {
55    _subsystem_name = "common"
56    if (defined(invoker.subsystem_name)) {
57      _subsystem_name = invoker.subsystem_name
58    }
59    _project_type = invoker.project_type
60
61    _build_part_boolean = false
62    tmp_subsystem_part = "${_subsystem_name}_${_part_name}"
63    _part_script_judge = "//test/xts/tools/build/judgePart.py"
64    _script_judge_args1 = [
65      rebase_path("$preloader_output_dir") + "/parts_config.json",
66      tmp_subsystem_part,
67      "judgePart",
68    ]
69    _build_part_boolean_str = exec_script(rebase_path(_part_script_judge),
70                                          _script_judge_args1,
71                                          "trim string")
72    if (_build_part_boolean_str == "True") {
73      _build_part_boolean = true
74    }
75
76    if (_subsystem_name == "kernel" || _subsystem_name == "common") {
77      _build_part_boolean = true
78    }
79
80    _test_files = ""
81    if (defined(invoker.generated_testfiles)) {
82      foreach(file, invoker.generated_testfiles) {
83        _test_files =
84            _test_files + "," + rebase_path("$root_gen_dir") + "/" + file
85      }
86    }
87
88    _is_testbundle = defined(invoker.is_testbundle) && invoker.is_testbundle
89
90    if (defined(invoker.sub_output_dir)) {
91      _output_file_dir = rebase_path(
92              "$root_out_dir/tests/moduletest/${invoker.sub_output_dir}")
93    } else if (defined(invoker.module_out_path)) {
94      _output_file_dir = rebase_path(
95              "$root_out_dir/tests/moduletest/${invoker.module_out_path}")
96    } else {
97      _output_file_dir = rebase_path("$root_out_dir/tests/moduletest")
98    }
99    _output_file = "${_output_file_dir}/module_${target_name}"
100
101    if (_project_type == "gtest" || _project_type == "ctestbundle") {
102      _output_file = "${_output_file_dir}/${target_name}"
103      _archive_filename = "${target_name}"
104      if (_build_part_boolean == true) {
105        target("ohos_moduletest", "module_${target_name}") {
106          forward_variables_from(invoker, "*")
107          testonly = true
108        }
109      } else {
110        print(tmp_subsystem_part + " is not build")
111        if (defined(invoker.public_configs)) {
112          print(invoker.public_configs)
113        }
114        if (defined(invoker.external_deps)) {
115          print(invoker.external_deps)
116        }
117        if (defined(invoker.deps)) {
118          print(invoker.deps)
119        }
120        if (defined(invoker.configs)) {
121          print(invoker.configs)
122        }
123        if (defined(invoker.sources)) {
124          print(invoker.sources)
125        }
126      }
127    } else if (_project_type == "zunit" || _project_type == "javatestbundle") {
128      _output_file = "${_output_file_dir}/module_${target_name}.dex"
129      _archive_filename = "${target_name}.dex"
130      if (_build_part_boolean == true) {
131        target("ohos_java_moduletest", "module_${target_name}") {
132          forward_variables_from(invoker, "*")
133          testonly = true
134        }
135      }
136    } else if (_project_type == "hostjunit") {
137      _output_file = "${_output_file_dir}/module_${target_name}.jar"
138      if (defined(invoker.final_jar_path)) {
139        _output_file = invoker.final_jar_path
140      }
141      _archive_filename = "${target_name}.jar"
142      if (_build_part_boolean == true) {
143        target("java_library", "module_${target_name}") {
144          forward_variables_from(invoker, "*")
145          is_host_library = true
146        }
147      }
148    } else if (_project_type == "testhap" || _project_type == "haptestbundle" ||
149               _project_type == "testhapassist" || _project_type == "testapp" ||
150               _project_type == "testappassist") {
151      assert(defined(invoker.hap_name),
152             "hap_name is required in target ${target_name}")
153      assert(!defined(invoker.final_hap_path),
154             "please use hap_name instead of final_hap_path")
155
156      _hap_name = invoker.hap_name
157      _final_hap_path = ""
158      _target_name = ""
159      _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}"
160      subsystem_name = XTS_SUITENAME
161      if (subsystem_name == "acts") {
162        _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/acts"
163      }
164      if (_project_type == "testhapassist") {
165        _final_hap_path = "${_suite_out_dir}/testcases/${_hap_name}.hap"
166        _target_name = target_name
167      } else {
168        _final_hap_path = "${SUITES_OUTPUT_ROOT}/haps/${_hap_name}.hap"
169        _target_name = "module_${target_name}"
170      }
171      _output_file = _final_hap_path
172      _archive_filename = "${_hap_name}.hap"
173      if (_build_part_boolean == true) {
174        if (_project_type == "testapp") {
175          target("ohos_app", _target_name) {
176            forward_variables_from(invoker, "*")
177            subsystem_name = XTS_SUITENAME
178            hap_out_dir = "${SUITES_OUTPUT_ROOT}/haps"
179            testonly = true
180          }
181        } else if (_project_type == "testappassist") {
182          target("ohos_app", target_name) {
183            forward_variables_from(invoker, "*")
184            subsystem_name = XTS_SUITENAME
185            hap_out_dir = "${_suite_out_dir}/testcases"
186            testonly = true
187          }
188        } else {
189          target("ohos_hap", _target_name) {
190            forward_variables_from(invoker, "*")
191            subsystem_name = XTS_SUITENAME
192            final_hap_path = _final_hap_path
193            testonly = true
194          }
195        }
196      } else {
197        print(tmp_subsystem_part + " is not build")
198        not_needed(invoker, "*")
199      }
200    } else if (_project_type == "pythontest") {
201      if (defined(invoker.outputs_dir)) {
202        _out_put_dir = invoker.outputs_dir
203        _archive_filename = "${_subsystem_name}/${_out_put_dir}"
204      } else {
205        _archive_filename = "${_subsystem_name}"
206      }
207      _test_files = invoker.output_file
208      _deps = []
209      if (defined(invoker.deps)) {
210        _deps = invoker.deps
211      }
212    } else if (_project_type == "js_test_hap") {
213      _hap_name = invoker.test_hap_name
214      _output_file = invoker.hap_source_path
215      _archive_filename = "${_hap_name}.hap"
216    }
217
218    _apilibrary_deps = ""
219    if (_is_testbundle && defined(invoker.deps)) {
220      foreach(dep, invoker.deps) {
221        _apilibrary_deps = _apilibrary_deps + "," + dep
222      }
223    }
224
225    if (_project_type != "pythontest" && _project_type != "js_test_hap" &&
226        _project_type != "testhapassist" && _project_type != "testappassist") {
227      _deps = [ ":module_${target_name}" ]
228    } else if (_project_type == "pythontest") {
229      print("this is pythontest")
230    } else if (_project_type != "testhapassist" &&
231               _project_type != "testappassist") {
232      _deps = []
233    }
234    _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}"
235
236    _archive_testfile = ""
237    subsystem_name = XTS_SUITENAME
238    if (subsystem_name == "acts") {
239      _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/acts"
240    }
241    if (_project_type == "testhapassist" || _project_type == "testappassist") {
242      _archive_testfile =
243          "${_suite_out_dir}/testcases/module_${_archive_filename}"
244    } else if (_project_type == "testapp") {
245      _hap_name = invoker.hap_name
246      if (_hap_name == "validator" && subsystem_name == "acts") {
247        _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/acts-validator"
248        _archive_testfile = "${_suite_out_dir}/testcases/${_archive_filename}"
249      } else {
250        _archive_testfile = "${_suite_out_dir}/testcases/${_archive_filename}"
251      }
252    } else {
253      _archive_testfile = "${_suite_out_dir}/testcases/${_archive_filename}"
254    }
255    _arguments = [
256      "build_module_with_testbundle",
257      "--build_gen_dir",
258      rebase_path("$root_gen_dir"),
259      "--build_target_name",
260      target_name,
261      "--buildgen_testfile",
262      rebase_path(_output_file),
263      "--project_path",
264      rebase_path("."),
265      "--test_xml",
266      rebase_path(TESTCONFIG_FILENAME),
267      "--project_type",
268      _project_type,
269      "--suite_out_dir",
270      rebase_path("${_suite_out_dir}"),
271      "--archive_testfile",
272      rebase_path("${_archive_testfile}"),
273    ]
274
275    if (_subsystem_name != "") {
276      _arguments += [
277        "--subsystem_name",
278        _subsystem_name,
279      ]
280    }
281    if (_part_name != "") {
282      _arguments += [
283        "--part_name",
284        _part_name,
285      ]
286    }
287    if (_apilibrary_deps != "") {
288      _arguments += [
289        "--apilibrary_deps",
290        _apilibrary_deps,
291      ]
292    }
293
294    if (_test_files != "") {
295      _arguments += [
296        "--test_files",
297        _test_files,
298      ]
299    }
300    if (_build_part_boolean == true && _project_type != "testhapassist" &&
301        _project_type != "testappassist") {
302      action(target_name) {
303        deps = _deps
304        script = rebase_path("//test/xts/tools/build/suite.py")
305        args = _arguments
306        outputs = [ _archive_testfile ]
307        testonly = true
308      }
309    } else if (_build_part_boolean == false) {
310      action(target_name) {
311        script = rebase_path("//test/xts/tools/build/judgePart.py")
312        args = _arguments
313        outputs = [ _archive_testfile ]
314        testonly = true
315      }
316      if (defined(_deps)) {
317        print(_deps)
318      }
319    }
320  } else {
321    group(target_name) {
322      print(target_name + " is not build")
323      not_needed(invoker, "*")
324    }
325  }
326}
327
328template("ohos_moduletest_suite") {
329  target("ohos_testsuite_base", "${target_name}") {
330    forward_variables_from(invoker, "*")
331    if (!defined(module_out_path)) {
332      module_out_path = "xts/modules"
333    }
334    project_type = "gtest"
335  }
336}
337
338template("ohos_hap_suite") {
339  target("ohos_testsuite_base", "${target_name}") {
340    forward_variables_from(invoker, "*")
341
342    # auto add HJUnitRunner entry ability and test-framework
343    if (defined(hap_profile)) {
344      # NOTE:: the GN tool disallow source files located in the ${out_dir}
345      # so we put the generated files in the xts dir. remember to REMOVE these.
346      _profile_relative_path = rebase_path(hap_profile, rebase_path(XTS_ROOT))
347      _fixed_profile_path =
348          "${XTS_ROOT}/autogen_apiobjs/${_profile_relative_path}"
349      _fixer_script = rebase_path(
350              "//test/xts/tools/build/adapter/haptest_manifest_fixer.py")
351      exec_script(_fixer_script,
352                  [
353                    "add_entryability",
354                    "--raw_file=" + rebase_path(hap_profile),
355                    "--dest_file=" + rebase_path(_fixed_profile_path),
356                  ])
357      hap_profile = _fixed_profile_path
358    }
359
360    if (defined(deps)) {
361      deps += [ "//test/xts/tools/hjunit:testkit_harmonyjunitrunner_java" ]
362    } else {
363      deps = [ "//test/xts/tools/hjunit:testkit_harmonyjunitrunner_java" ]
364    }
365
366    project_type = "testhap"
367  }
368}
369
370template("ohos_js_hap_suite") {
371  target("ohos_testsuite_base", "${target_name}") {
372    forward_variables_from(invoker, "*")
373    project_type = "testhap"
374
375    #js_build_mode = "debug"
376  }
377}
378
379template("ohos_js_app_suite") {
380  target("ohos_testsuite_base", "${target_name}") {
381    forward_variables_from(invoker, "*")
382    project_type = "testapp"
383  }
384}
385
386template("ohos_shell_app_suite") {
387  target("ohos_adapter_shell_app_suite", "${target_name}") {
388    forward_variables_from(invoker, "*")
389  }
390}
391
392template("ohos_prebuilt_suite") {
393  assert(!defined(invoker.source_files) || !defined(invoker.jar_path) ||
394             !defined(invoker.source_dir),
395         "source_files, jar_path or source_dir must be specified one.")
396  assert(!defined(invoker.final_jar_path),
397         "final_jar_path is not allowed in target ${target_name}")
398
399  if (defined(invoker.jar_path)) {
400    _output_name = "${target_name}.jar"
401    if (defined(invoker.output_name)) {
402      _output_name = "${invoker.output_name}.jar"
403    }
404
405    _output_type = "tools"
406    if (defined(invoker.output_type)) {
407      _output_type = invoker.output_type
408    }
409
410    _final_jar_path =
411        "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/${_output_type}/${_output_name}"
412
413    target("java_prebuilt", "${target_name}") {
414      forward_variables_from(invoker, "*")
415      final_jar_path = _final_jar_path
416      is_host_library = true
417    }
418  } else {
419    assert(defined(invoker.output_dir),
420           "output_dir is require in target ${target_name}")
421    _outputs = []
422    _copy_args = [
423      "--method_name",
424      "copy_file",
425    ]
426    _deps = []
427    _output_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/${invoker.output_dir}"
428    if (defined(invoker.source_dir)) {
429      _copy_args += [
430        "--arguments",
431        "output=" + rebase_path(_output_dir) + "#source_dirs=" +
432            rebase_path(invoker.source_dir) + "#to_dir=True",
433      ]
434      _outputs = [ _output_dir ]
435    } else if (defined(invoker.source_files)) {
436      _sources = ""
437      foreach(src, invoker.source_files) {
438        _sources = _sources + rebase_path(src) + ","
439      }
440      _copy_args += [
441        "--arguments",
442        "output=" + rebase_path(_output_dir) + "#sources=" + _sources +
443            "#to_dir=True",
444      ]
445      _outputs = [ _output_dir ]
446    }
447    if (defined(invoker.deps)) {
448      _deps = invoker.deps
449    }
450    action(target_name) {
451      script = rebase_path("//test/xts/tools/build/utils.py")
452      deps = _deps
453      args = _copy_args
454      outputs = _outputs
455    }
456  }
457}
458
459template("ohos_deploy_xdevice") {
460  _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}"
461  subsystem_name = XTS_SUITENAME
462  if (subsystem_name == "acts") {
463    _hap_name = target_name
464    if (_hap_name == "validator") {
465      _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/acts-validator"
466    } else {
467      _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/acts"
468    }
469  }
470
471  _args = [
472    "build_xdevice",
473    "--source_dir",
474    rebase_path("//test/testfwk/xdevice"),
475    "--suite_out_dir",
476    rebase_path(_suite_out_dir),
477  ]
478
479  if (defined(invoker.configs_dir)) {
480    _args += [
481      "--configs_dir",
482      rebase_path(rebase_path(invoker.configs_dir)),
483    ]
484  }
485
486  if (defined(invoker.resources_dir)) {
487    _args += [
488      "--resources_dir",
489      rebase_path(rebase_path(invoker.resources_dir)),
490    ]
491  }
492
493  action(target_name) {
494    testonly = true
495    script = rebase_path("//test/xts/tools/build/suite.py")
496    args = _args
497    outputs = [
498      "${_suite_out_dir}/tools/xdevice-ohos-0.0.0.tar.gz",
499      "${_suite_out_dir}/tools/xdevice-0.0.0.tar.gz",
500      "${_suite_out_dir}/tools/xdevice-devicetest-0.0.0.tar.gz",
501      "${_suite_out_dir}/tools/run.sh",
502      "${_suite_out_dir}/tools/run.bat",
503    ]
504  }
505}
506
507template("ohos_test_suite") {
508  _output = "${SUITES_OUTPUT_ROOT}/${target_name}.zip"
509  _suite_path = rebase_path("${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}")
510  _suite_archive_dir = rebase_path("${SUITE_ARCHIVE_DIR}")
511  _prebuilts_files = rebase_path("//xts/resource")
512  _gen_args = [
513    "archive_suite",
514    "--suite_path",
515    _suite_path,
516    "--prebuilts_resource",
517    _prebuilts_files,
518    "--suite_archive_dir",
519    _suite_archive_dir,
520    "--make_archive",
521    "${make_archive}",
522  ]
523
524  _deps = []
525  if (defined(invoker.deps)) {
526    _deps += invoker.deps
527  }
528
529  action(target_name) {
530    testonly = true
531    script = rebase_path("//test/xts/tools/build/suite.py")
532    deps = _deps
533    args = _gen_args
534    outputs = [ _output ]
535  }
536}
537
538template("pythontest_suite") {
539  assert(defined(invoker.script), "script is required in target ${target_name}")
540
541  _subsystem_name = ""
542  if (defined(invoker.subsystem_name)) {
543    _subsystem_name = invoker.subsystem_name
544  } else {
545    _local_path = rebase_path(".")
546    _args1 = [
547      "--method_name",
548      "get_subsystem_name",
549      "--arguments",
550      "path=${_local_path}",
551    ]
552    _subsystem_name =
553        exec_script(rebase_path("//test/xts/tools/build/utils.py"),
554                    _args1,
555                    "trim string")
556  }
557  _deps = []
558  if (defined(invoker.deps)) {
559    _deps = invoker.deps + _deps
560  }
561  _outputs_dir = ""
562  if (defined(invoker.outputs_dir)) {
563    _outputs_dir = invoker.outputs_dir
564  }
565  _output_file = rebase_path("${invoker.script}")
566
567  target("ohos_testsuite_base", "${target_name}") {
568    project_type = "pythontest"
569    subsystem_name = _subsystem_name
570    output_file = _output_file
571    deps = _deps
572    outputs_dir = _outputs_dir
573  }
574}
575
576template("executable_suite") {
577  assert(defined(invoker.suite_name),
578         "suite_name is required in target ${target_name}")
579  _suite_name = invoker.suite_name
580  _local_path = rebase_path(".")
581  _args1 = [
582    "--method_name",
583    "get_subsystem_name",
584    "--arguments",
585    "path=${_local_path}",
586  ]
587  _subsystem_name = exec_script(rebase_path("//test/xts/tools/build/utils.py"),
588                                _args1,
589                                "trim string")
590
591  _outputs_dir = ""
592  if (defined(invoker.outputs_dir)) {
593    _outputs_dir = "${invoker.outputs_dir}"
594  }
595  ohos_executable(target_name) {
596    forward_variables_from(invoker,
597                           "*",
598                           [
599                             "test_type",
600                             "module_out_path",
601                             "visibility",
602                           ])
603    forward_variables_from(invoker, [ "visibility" ])
604    if (!defined(deps)) {
605      deps = []
606    }
607
608    subsystem_name = "tests"
609    part_name = "ssts"
610    ohos_test = true
611    testonly = true
612    output_name = "$target_name"
613    test_output_dir = "$SUITES_OUTPUT_ROOT/${_suite_name}/testcases/${_subsystem_name}/${_outputs_dir}"
614    if (defined(invoker.output_extension)) {
615      output_extension = invoker.output_extension
616    }
617  }
618}
619
620template("js_hap_suite") {
621  assert(defined(invoker.hap_source_path),
622         "hap_source_path is required in target ${target_name}")
623  assert(defined(invoker.test_hap_name),
624         "test_hap_name is required in target ${target_name}")
625  if (defined(invoker.deps)) {
626    _deps = invoker.deps
627  }
628  target("ohos_testsuite_base", "${target_name}") {
629    forward_variables_from(invoker, "*")
630    project_type = "js_test_hap"
631  }
632}
633
634template("ohos_hap_assist_suite") {
635  target("ohos_testsuite_base", target_name) {
636    forward_variables_from(invoker, "*")
637    project_type = "testhapassist"
638  }
639}
640
641template("ohos_app_assist_suite") {
642  target("ohos_testsuite_base", target_name) {
643    forward_variables_from(invoker, "*")
644    project_type = "testappassist"
645  }
646}
647
648template("ohos_sh_assist_suite") {
649  _part_name = "common"
650
651  if (defined(invoker.part_name)) {
652    _part_name = invoker.part_name
653  }
654
655  precise_build_boolean = true
656  if (precise_xts && filter_include(precise_run,
657                                    [
658                                      "all",
659                                      _part_name,
660                                    ]) == []) {
661    print("precise_xts is on ,${_part_name} is not on build")
662    precise_build_boolean = false
663  }
664
665  if (precise_build_boolean) {
666    action(target_name) {
667      forward_variables_from(invoker, "*")
668    }
669  } else {
670    group(target_name) {
671      print(target_name + " is not build")
672      not_needed(invoker, "*")
673    }
674  }
675}
676