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/config/clang/clang.gni")
15import("//build/config/python.gni")
16import("//build/ohos.gni")
17import("//build/ohos_var.gni")
18import("//build/templates/cxx/cxx.gni")
19
20if (!is_arkui_x) {
21  import("//build/rust/rustc_toolchain.gni")
22}
23
24template("_testcase_resources") {
25  assert(defined(invoker.testcase_target_name))
26  assert(defined(invoker.test_output_dir))
27  assert(defined(invoker.module_out_path))
28
29  _deps = []
30  if (defined(invoker.deps)) {
31    _deps += invoker.deps
32  }
33  action_with_pydeps(target_name) {
34    if (defined(invoker.testonly)) {
35      testonly = invoker.testonly
36    }
37    deps = _deps
38    inputs = []
39    script = "//build/ohos/testfwk/testcase_resource_copy.py"
40    output_file = "$target_out_dir/$target_name.json"
41    outputs = [ output_file ]
42    depfile = "$target_gen_dir/$target_name.d"
43    args = []
44    if (defined(invoker.resource_config_file)) {
45      args += [
46        "--resource-config-file",
47        rebase_path(invoker.resource_config_file, root_build_dir),
48      ]
49      inputs += [ invoker.resource_config_file ]
50    }
51    args += [
52      "--depfile",
53      rebase_path(depfile, root_build_dir),
54      "--testcase-target-name",
55      invoker.testcase_target_name,
56      "--part-build-out-path",
57      rebase_path(root_out_dir, root_build_dir),
58      "--resource-output-path",
59      rebase_path(invoker.test_output_dir + "/resource", root_build_dir),
60      "--module-out-path",
61      invoker.module_out_path,
62      "--output-file",
63      rebase_path(output_file, root_build_dir),
64    ]
65  }
66}
67
68# ohos test template
69template("_ohos_test") {
70  assert(defined(invoker.test_type), "test_type is required.")
71  assert(defined(invoker.module_out_path))
72
73  _deps = []
74  if (defined(invoker.deps)) {
75    _deps += invoker.deps
76  }
77
78  if (defined(invoker.crate_type)) {
79    _deps += [ "//build/rust:libstd.dylib.so" ]
80  }
81
82  test_output_dir =
83      "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
84
85  # generate module list file in gn stage
86  if (!skip_generate_module_list_file) {
87    _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py"
88    _arguments = [
89      "--target",
90      target_name,
91      "--target_label",
92      get_label_info(":$target_name", "label_with_toolchain"),
93      "--source_dir",
94      rebase_path(get_label_info(":$target_name", "dir"), root_build_dir),
95      "--test_type",
96      invoker.test_type,
97    ]
98
99    _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf"
100
101    _arguments += [
102      "--output_dir",
103      rebase_path(test_output_dir, root_build_dir),
104      "--module_list_file",
105      rebase_path(_module_list_file, root_build_dir),
106    ]
107    exec_script(_gen_module_list_script, _arguments)
108  }
109
110  # copy testcase resource
111  testcase_target_name = target_name
112  _testcase_resources("${testcase_target_name}_resource_copy") {
113    if (defined(invoker.resource_config_file)) {
114      resource_config_file = invoker.resource_config_file
115    }
116    module_out_path = invoker.module_out_path
117    deps = _deps
118    testonly = true
119  }
120
121  # copy fuzz config file
122  if (defined(invoker.fuzz_config_file)) {
123    fuzz_config_file = invoker.fuzz_config_file
124    action("${target_name}_copy_fuzz_config_file") {
125      script = "//build/ohos/testfwk/fuzz_config_file_copy.py"
126      inputs = [ fuzz_config_file ]
127      args = []
128      args += [
129        "--fuzz-config-file-path",
130        rebase_path(fuzz_config_file, root_build_dir),
131        "--fuzz-config-file-output-path",
132        rebase_path(root_out_dir + "/tests/res", root_build_dir),
133      ]
134      outputs = [ "$target_out_dir/${target_name}_result.txt" ]
135    }
136  }
137
138  _has_sources = defined(invoker.sources) && invoker.sources != []
139  if (_has_sources) {
140    _c_sources_file = "$target_gen_dir/$target_name.sources"
141    write_file(_c_sources_file, rebase_path(invoker.sources, root_build_dir))
142  }
143  write_file("$test_output_dir/${target_name}_path.txt",
144             get_label_info(":$target_name", "dir"),
145             "string")
146
147  ohos_executable(target_name) {
148    forward_variables_from(invoker,
149                           "*",
150                           [
151                             "test_type",
152                             "module_out_path",
153                             "visibility",
154                           ])
155    forward_variables_from(invoker, [ "visibility" ])
156    if (!defined(deps)) {
157      deps = []
158    }
159    deps += [ ":${testcase_target_name}_resource_copy" ]
160    if (defined(invoker.fuzz_config_file)) {
161      deps += [ ":${target_name}_copy_fuzz_config_file" ]
162    }
163
164    subsystem_name = "tests"
165    part_name = invoker.test_type
166    ohos_test = true
167    testonly = true
168    output_name = "$target_name"
169  }
170}
171
172template("ohos_unittest") {
173  _ohos_test(target_name) {
174    testonly = true
175    forward_variables_from(invoker, "*")
176    test_type = "unittest"
177    deps = []
178    external_deps = []
179    if (defined(invoker.deps)) {
180      deps += invoker.deps
181    }
182    if (defined(invoker.external_deps)) {
183      external_deps += invoker.external_deps
184    }
185    external_deps += [ "googletest:gtest_main" ]
186  }
187}
188
189template("ohos_moduletest") {
190  _ohos_test(target_name) {
191    forward_variables_from(invoker, "*")
192    test_type = "moduletest"
193    deps = []
194    if (defined(invoker.deps)) {
195      deps += invoker.deps
196    }
197    deps += [ "//third_party/googletest:gtest_main" ]
198  }
199}
200
201template("ohos_systemtest") {
202  _ohos_test(target_name) {
203    forward_variables_from(invoker, "*")
204    test_type = "systemtest"
205  }
206}
207
208template("ohos_performancetest") {
209  _ohos_test(target_name) {
210    forward_variables_from(invoker, "*")
211    test_type = "performance"
212    deps = []
213    if (defined(invoker.deps)) {
214      deps += invoker.deps
215    }
216    deps += [
217      "//test/testfwk/developer_test/aw/cxx/hwext:performance_test_static",
218      "//third_party/googletest:gtest_main",
219    ]
220  }
221}
222
223template("ohos_securitytest") {
224  _ohos_test(target_name) {
225    forward_variables_from(invoker, "*")
226    test_type = "security"
227  }
228}
229
230template("ohos_reliabilitytest") {
231  _ohos_test(target_name) {
232    forward_variables_from(invoker, "*")
233    test_type = "reliability"
234  }
235}
236
237template("ohos_distributedtest") {
238  _ohos_test(target_name) {
239    forward_variables_from(invoker, "*")
240    test_type = "distributedtest"
241    deps = []
242    if (defined(invoker.deps)) {
243      deps += invoker.deps
244    }
245    deps += [
246      "//test/testfwk/developer_test/aw/cxx/distributed:distributedtest_lib",
247    ]
248  }
249}
250
251template("ohos_fuzztest") {
252  # Judge the compliation library.
253  # Do the FUZZ compliation if the compliation library is the musl.
254  if (use_musl) {
255    _ohos_test(target_name) {
256      forward_variables_from(invoker,
257                             "*",
258                             [
259                               "deps",
260                               "cflags",
261                             ])
262      test_type = "fuzztest"
263      deps = []
264      if (defined(invoker.deps)) {
265        deps += invoker.deps
266      }
267      cflags = []
268      if (defined(invoker.cflags)) {
269        cflags += invoker.cflags
270      }
271      cflags += [
272        "-fno-sanitize-coverage=trace-pc-guard,edge,trace-cmp,indirect-calls,8bit-counters",
273        "-fsanitize=fuzzer",
274      ]
275      if (target_cpu == "arm64") {
276        libs =
277            [ "$clang_lib_base_path/aarch64-linux-ohos/libclang_rt.fuzzer.a" ]
278      } else if (target_cpu == "x86_64") {
279        libs = [ "$clang_lib_base_path/x86_64-linux-ohos/libclang_rt.fuzzer.a" ]
280      } else {
281        libs = [ "$clang_lib_base_path/arm-linux-ohos/libclang_rt.fuzzer.a" ]
282      }
283    }
284  } else {
285    group(target_name) {
286      not_needed(invoker, "*")
287    }
288  }
289}
290
291template("ohos_benchmarktest") {
292  _ohos_test(target_name) {
293    testonly = true
294    forward_variables_from(invoker, "*", [ "deps" ])
295    test_type = "benchmark"
296
297    deps = []
298    external_deps = []
299    if (defined(invoker.deps)) {
300      deps += invoker.deps
301    }
302    if (defined(invoker.external_deps)) {
303      external_deps += invoker.external_deps
304    }
305    external_deps += [ "benchmark:benchmark" ]
306  }
307}
308
309template("_test_py_file_copy") {
310  assert(defined(invoker.sources), "sources is required.")
311  assert(defined(invoker.target_base_dir))
312  assert(defined(invoker.copy_output_dir))
313
314  action_with_pydeps(target_name) {
315    forward_variables_from(invoker,
316                           [
317                             "sources",
318                             "testonly",
319                             "visibility",
320                           ])
321    script = "//build/ohos/testfwk/test_py_file_copy.py"
322    deps = []
323    if (defined(invoker.deps)) {
324      deps += invoker.deps
325    }
326
327    depfile = "$target_gen_dir/$target_name.d"
328    outfile = "$target_out_dir/$target_name.out"
329    outputs = [ outfile ]
330    args = [
331      "--target-base-dir",
332      rebase_path(invoker.target_base_dir, root_build_dir),
333      "--copy-output-dir",
334      rebase_path(invoker.copy_output_dir, root_build_dir),
335      "--outfile",
336      rebase_path(outfile, root_build_dir),
337      "--depfile",
338      rebase_path(depfile, root_build_dir),
339      "--source-files",
340    ]
341    args += rebase_path(sources, root_build_dir)
342  }
343}
344
345# python test template
346template("_ohos_test_py") {
347  assert(defined(invoker.test_type), "test_type is required.")
348  assert(defined(invoker.sources), "sources is required.")
349
350  _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py"
351  _arguments = [
352    "--target",
353    target_name,
354    "--target_label",
355    get_label_info(":$target_name", "label_with_toolchain"),
356    "--source_dir",
357    rebase_path(get_label_info(":$target_name", "dir"), root_build_dir),
358    "--test_type",
359    invoker.test_type,
360  ]
361
362  if (defined(invoker.module_out_path)) {
363    test_output_dir =
364        "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
365    _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf"
366  } else {
367    test_output_dir = "$root_out_dir/tests/${invoker.test_type}"
368    _module_list_file = "$root_out_dir/module_list_files/$target_name.mlf"
369  }
370
371  _arguments += [
372    "--output_dir",
373    rebase_path(test_output_dir, root_build_dir),
374    "--module_list_file",
375    rebase_path(_module_list_file, root_build_dir),
376  ]
377  exec_script(_gen_module_list_script, _arguments)
378
379  _test_py_file_copy(target_name) {
380    testonly = true
381    target_base_dir = get_label_info(":$target_name", "dir")
382    copy_output_dir = test_output_dir
383    sources = get_path_info(invoker.sources, "abspath")
384  }
385}
386
387template("ohos_unittest_py") {
388  _ohos_test_py(target_name) {
389    forward_variables_from(invoker, "*")
390    test_type = "unittest"
391  }
392}
393
394template("ohos_moduletest_py") {
395  _ohos_test_py(target_name) {
396    forward_variables_from(invoker, "*")
397    test_type = "moduletest"
398  }
399}
400
401template("ohos_systemtest_py") {
402  _ohos_test_py(target_name) {
403    forward_variables_from(invoker, "*")
404    test_type = "systemtest"
405  }
406}
407
408template("ohos_performancetest_py") {
409  _ohos_test_py(target_name) {
410    forward_variables_from(invoker, "*")
411    test_type = "performance"
412  }
413}
414
415template("ohos_securitytest_py") {
416  _ohos_test_py(target_name) {
417    forward_variables_from(invoker, "*")
418    test_type = "security"
419  }
420}
421
422template("ohos_reliabilitytest_py") {
423  _ohos_test_py(target_name) {
424    forward_variables_from(invoker, "*")
425    test_type = "reliability"
426  }
427}
428
429template("ohos_distributedtest_py") {
430  _ohos_test_py(target_name) {
431    forward_variables_from(invoker, "*")
432    test_type = "distributedtest"
433  }
434}
435
436template("ohos_fuzztest_py") {
437  _ohos_test_py(target_name) {
438    forward_variables_from(invoker, "*")
439    test_type = "fuzztest"
440  }
441}
442
443#js api test template
444template("ohos_js_test") {
445  assert(defined(invoker.test_type), "test_type must be defined.")
446  assert(defined(invoker.hap_profile), "hap_profile must be defined.")
447  assert(defined(invoker.module_out_path), "module_out_path must be defined.")
448
449  # generate module list file in gn stage
450  _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py"
451  _arguments = [
452    "--target",
453    target_name,
454    "--target_label",
455    get_label_info(":$target_name", "label_with_toolchain"),
456    "--source_dir",
457    rebase_path(get_label_info(":$target_name", "dir"), root_build_dir),
458    "--test_type",
459    invoker.test_type,
460  ]
461
462  _test_output_dir =
463      "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
464  _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf"
465
466  write_file("$_test_output_dir/${target_name}_path.txt",
467             get_label_info(":$target_name", "dir"),
468             "string")
469
470  _arguments += [
471    "--output_dir",
472    rebase_path(_test_output_dir, root_build_dir),
473    "--module_list_file",
474    rebase_path(_module_list_file, root_build_dir),
475  ]
476  exec_script(_gen_module_list_script, _arguments)
477
478  # copy testcase resource
479  testcase_target_name = target_name
480  _testcase_resources("${testcase_target_name}_resource_copy") {
481    if (defined(invoker.resource_config_file)) {
482      resource_config_file = invoker.resource_config_file
483    }
484    module_out_path = invoker.module_out_path
485    test_output_dir = _test_output_dir
486  }
487
488  _suite_path = get_label_info(":$target_name", "dir")
489  _template_path = "//test/testfwk/developer_test/libs/js_template"
490  _target_path = "${_template_path}/$target_name"
491  _target_js_copy = "${target_name}__js_copy"
492  action_with_pydeps(_target_js_copy) {
493    script = "//build/ohos/testfwk/test_js_file_copy.py"
494    outputs = [ "$target_out_dir/$target_name.out" ]
495    args = [
496      "--suite_path",
497      rebase_path(_suite_path, root_build_dir),
498      "--template_path",
499      rebase_path(_template_path, root_build_dir),
500      "--target_path",
501      rebase_path(_target_path, root_build_dir),
502      "--test_output_dir",
503      rebase_path(_test_output_dir, root_build_dir),
504      "--target_name",
505      testcase_target_name,
506    ]
507    deps = [ ":${testcase_target_name}_resource_copy" ]
508  }
509
510  _target_js_assets = "${target_name}__intl_js_assets"
511  ohos_js_assets(_target_js_assets) {
512    source_dir = "${_target_path}/src/main/js/default"
513    deps = [ ":$_target_js_copy" ]
514  }
515  _target_resources = "${target_name}__resources"
516  ohos_resources(_target_resources) {
517    sources = [ "${_target_path}/src/main/resources" ]
518    deps = [ ":$_target_js_assets" ]
519    hap_profile = invoker.hap_profile
520  }
521
522  ohos_hap(target_name) {
523    forward_variables_from(invoker,
524                           "*",
525                           [
526                             "test_type",
527                             "module_out_path",
528                             "visibility",
529                           ])
530    forward_variables_from(invoker, [ "visibility" ])
531
532    deps = [
533      ":$_target_js_assets",
534      ":$_target_resources",
535    ]
536    final_hap_path = "$_test_output_dir/$target_name.hap"
537    js_build_mode = "debug"
538    testonly = true
539  }
540}
541
542template("ohos_js_unittest") {
543  ohos_js_test(target_name) {
544    forward_variables_from(invoker, "*")
545    test_type = "unittest"
546  }
547}
548
549template("ohos_js_stage_test") {
550  if (defined(invoker.part_name)) {
551    _part_name = invoker.part_name
552  }
553  if (defined(invoker.subsystem_name)) {
554    _subsystem_name = invoker.subsystem_name
555  }
556  if (defined(invoker.test_type)) {
557    _test_type = invoker.test_type
558  }
559  _build_part_boolean = true
560
561  if (defined(invoker.module_out_path)) {
562    _test_output_dir =
563        "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
564  } else {
565    _test_output_dir = "$root_out_dir/tests/${invoker.test_type}"
566  }
567  write_file("$_test_output_dir/${target_name}_path.txt",
568             get_label_info(":$target_name", "dir"),
569             "string")
570
571  # generate module list file in gn stage
572  if (!skip_generate_module_list_file) {
573    _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py"
574    _arguments = [
575      "--target",
576      target_name,
577      "--target_label",
578      get_label_info(":$target_name", "label_with_toolchain"),
579      "--source_dir",
580      rebase_path(get_label_info(":$target_name", "dir"), root_build_dir),
581      "--test_type",
582      _test_type,
583    ]
584
585    _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf"
586
587    _arguments += [
588      "--output_dir",
589      rebase_path(_test_output_dir, root_build_dir),
590      "--module_list_file",
591      rebase_path(_module_list_file, root_build_dir),
592    ]
593    exec_script(_gen_module_list_script, _arguments)
594  }
595
596  assert(defined(invoker.hap_name),
597         "hap_name is required in target ${target_name}")
598  assert(!defined(invoker.final_hap_path),
599         "please use hap_name instead of final_hap_path")
600  _hap_name = invoker.hap_name
601  _final_hap_path = ""
602  _target_name = ""
603  _final_hap_path =
604      "$root_out_dir/tests/haps/${invoker.module_out_path}/${_hap_name}.hap"
605  _target_name = "module_${target_name}"
606
607  _archive_filename = "${_hap_name}.hap"
608
609  if (_build_part_boolean == true) {
610    target("ohos_hap", _target_name) {
611      forward_variables_from(invoker, "*")
612      final_hap_path = _final_hap_path
613      testonly = true
614    }
615  } else {
616    print(tmp_subsystem_part + " is not build")
617    print(_target_name)
618    if (defined(invoker.certificate_profile)) {
619      print(invoker.certificate_profile)
620    }
621    if (defined(invoker.ets2abc)) {
622      print(invoker.ets2abc)
623    }
624    if (defined(invoker.deps)) {
625      print(invoker.deps)
626    }
627    if (defined(invoker.hap_profile)) {
628      print(invoker.hap_profile)
629    }
630    if (defined(invoker.testonly)) {
631      print(invoker.testonly)
632    }
633  }
634
635  _deps = [ ":module_${target_name}" ]
636
637  _archive_testfile = "$root_out_dir/tests/$_test_type/${invoker.module_out_path}/${_archive_filename}"
638  _arguments = []
639  _arguments = [
640    "--build_target_name",
641    target_name,
642    "--project_path",
643    rebase_path("."),
644    "--archive_testfile",
645    rebase_path("${_archive_testfile}"),
646    "--final_hap_path",
647    rebase_path("${_final_hap_path}"),
648    "--test_type",
649    _test_type,
650    "--module_out_path",
651    invoker.module_out_path,
652  ]
653  if (_subsystem_name != "") {
654    _arguments += [
655      "--subsystem_name",
656      _subsystem_name,
657    ]
658  }
659  if (_part_name != "") {
660    _arguments += [
661      "--part_name",
662      _part_name,
663    ]
664  }
665
666  action(target_name) {
667    deps = _deps
668    script = rebase_path("//build/ohos/testfwk/test_js_stage_file_copy.py")
669    args = _arguments
670    outputs = [ _archive_testfile ]
671    testonly = true
672  }
673}
674
675template("ohos_js_stage_unittest") {
676  ohos_js_stage_test(target_name) {
677    forward_variables_from(invoker, "*")
678    test_type = "unittest"
679  }
680}
681
682template("ohos_rust_unittest") {
683  _target_name = target_name
684  _rustflags = []
685  _public_deps = [ "//build/rust/tests:original_libstd.so" ]
686  ohos_unittest("$_target_name") {
687    forward_variables_from(invoker, "*")
688    public_deps = _public_deps
689    if (!defined(invoker.crate_name)) {
690      crate_name = _target_name
691    }
692    crate_type = "bin"
693    if (defined(invoker.crate_type)) {
694      assert(invoker.crate_type == crate_type,
695             "crate_type should be $crate_type or use default value.")
696    }
697
698    if (defined(invoker.rustc_lints)) {
699      rustc_lints = invoker.rustc_lints
700    }
701    if (defined(invoker.clippy_lints)) {
702      clippy_lints = invoker.clippy_lints
703    }
704
705    if (!defined(rustc_lints) && !defined(clippy_lints)) {
706      file_path =
707          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
708      file_path_split = string_split(file_path[0], "/")
709      source_dir_begin = file_path_split[2]
710
711      if (source_dir_begin == "openharmony") {
712        _rustflags += allowAllLints
713      } else if (source_dir_begin == "prebuilts") {
714        _rustflags += allowAllLints
715      } else if (source_dir_begin == "vendor") {
716        _rustflags += rustcVendorLints
717        _rustflags += clippyVendorLints
718      } else if (source_dir_begin == "device") {
719        _rustflags += rustcVendorLints
720        _rustflags += clippyVendorLints
721      } else {
722        _rustflags += rustcOhosLints
723        _rustflags += clippyOhosLints
724      }
725    }
726
727    if (defined(rustc_lints)) {
728      if (invoker.rustc_lints == "openharmony") {
729        _rustflags += rustcOhosLints
730      } else if (rustc_lints == "vendor") {
731        _rustflags += rustcVendorLints
732      } else if (rustc_lints == "none") {
733        _rustflags += allowAllLints
734      }
735    }
736    if (defined(clippy_lints)) {
737      if (invoker.clippy_lints == "openharmony") {
738        _rustflags += clippyOhosLints
739      } else if (clippy_lints == "vendor") {
740        _rustflags += clippyVendorLints
741      } else if (clippy_lints == "none") {
742        _rustflags += allowAllLints
743      }
744    }
745    if (!defined(rustflags)) {
746      rustflags = _rustflags
747    } else {
748      rustflags += _rustflags
749    }
750    edition = rust_default_edition
751    if (defined(invoker.edition)) {
752      edition = invoker.edition
753    }
754    rustflags += [
755      "--cfg",
756      "feature=\"test\"",
757      "--test",
758      "-Csymbol-mangling-version=v0",
759      "--edition=${edition}",
760    ]
761  }
762}
763
764template("ohos_rust_systemtest") {
765  _target_name = target_name
766  _rustflags = []
767  _public_deps = [ "//build/rust/tests:original_libstd.so" ]
768  ohos_systemtest("$_target_name") {
769    forward_variables_from(invoker, "*")
770    public_deps = _public_deps
771    if (!defined(invoker.crate_name)) {
772      crate_name = _target_name
773    }
774    crate_type = "bin"
775    if (defined(invoker.crate_type)) {
776      assert(invoker.crate_type == crate_type,
777             "crate_type should be $crate_type or use default value.")
778    }
779
780    if (defined(invoker.rustc_lints)) {
781      rustc_lints = invoker.rustc_lints
782    }
783    if (defined(invoker.clippy_lints)) {
784      clippy_lints = invoker.clippy_lints
785    }
786
787    if (!defined(rustc_lints) && !defined(clippy_lints)) {
788      file_path =
789          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
790      file_path_split = string_split(file_path[0], "/")
791      source_dir_begin = file_path_split[2]
792
793      if (source_dir_begin == "openharmony") {
794        _rustflags += allowAllLints
795      } else if (source_dir_begin == "prebuilts") {
796        _rustflags += allowAllLints
797      } else if (source_dir_begin == "vendor") {
798        _rustflags += rustcVendorLints
799        _rustflags += clippyVendorLints
800      } else if (source_dir_begin == "device") {
801        _rustflags += rustcVendorLints
802        _rustflags += clippyVendorLints
803      } else {
804        _rustflags += rustcOhosLints
805        _rustflags += clippyOhosLints
806      }
807    }
808
809    if (defined(rustc_lints)) {
810      if (invoker.rustc_lints == "openharmony") {
811        _rustflags += rustcOhosLints
812      } else if (rustc_lints == "vendor") {
813        _rustflags += rustcVendorLints
814      } else if (rustc_lints == "none") {
815        _rustflags += allowAllLints
816      }
817    }
818    if (defined(clippy_lints)) {
819      if (invoker.clippy_lints == "openharmony") {
820        _rustflags += clippyOhosLints
821      } else if (clippy_lints == "vendor") {
822        _rustflags += clippyVendorLints
823      } else if (clippy_lints == "none") {
824        _rustflags += allowAllLints
825      }
826    }
827    if (!defined(rustflags)) {
828      rustflags = _rustflags
829    } else {
830      rustflags += _rustflags
831    }
832    edition = rust_default_edition
833    if (defined(invoker.edition)) {
834      edition = invoker.edition
835    }
836    rustflags += [
837      "--cfg",
838      "feature=\"test\"",
839      "--test",
840      "-Csymbol-mangling-version=v0",
841      "--edition=${edition}",
842    ]
843  }
844}
845