1# Copyright (c) 2022 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/templates/cxx/cxx.gni")
15
16allowAllLints = [
17  "--cap-lints",
18  "allow",
19]
20rustcOhosLints = [
21  "-A",
22  "deprecated",
23  "-D",
24  "missing-docs",
25  "-D",
26  "warnings",
27]
28rustcVendorLints = [
29  "-A",
30  "deprecated",
31  "-D",
32  "warnings",
33]
34clippyOhosLints = [
35  "-A",
36  "clippy::type-complexity",
37  "-A",
38  "clippy::unnecessary-wraps",
39  "-A",
40  "clippy::unusual-byte-groupings",
41  "-A",
42  "clippy::upper-case-acronyms",
43]
44clippyVendorLints = [
45  "-A",
46  "clippy::complexity",
47  "-A",
48  "clippy::perf",
49  "-A",
50  "clippy::style",
51]
52
53template("rust_target_lints") {
54  _target_name = target_name
55  _crate_name = target_name
56  _crate_type = invoker.crate_type
57  _target_type = invoker.target_type
58  _rustflags = []
59
60  target(_target_type, "$_target_name") {
61    forward_variables_from(invoker, "*")
62    crate_name = _crate_name
63    crate_type = _crate_type
64
65    if (defined(invoker.rustc_lints)) {
66      rustc_lints = invoker.rustc_lints
67    }
68    if (defined(invoker.clippy_lints)) {
69      clippy_lints = invoker.clippy_lints
70    }
71    if (!defined(rustc_lints) && !defined(clippy_lints)) {
72      file_path =
73          get_path_info(get_path_info(invoker.sources, "dir"), "abspath")
74      file_path_split = string_split(file_path[0], "/")
75      source_dir_begin = file_path_split[2]
76      print(source_dir_begin)
77      if (source_dir_begin == "openharmony") {
78        _rustflags += allowAllLints
79      } else if (source_dir_begin == "prebuilts") {
80        _rustflags += allowAllLints
81      } else if (source_dir_begin == "vendor") {
82        _rustflags += rustcVendorLints
83        _rustflags += clippyVendorLints
84      } else if (source_dir_begin == "device") {
85        _rustflags += rustcVendorLints
86        _rustflags += clippyVendorLints
87      } else {
88        _rustflags += rustcOhosLints
89        _rustflags += clippyOhosLints
90      }
91    }
92
93    if (defined(rustc_lints)) {
94      if (invoker.rustc_lints == "openharmony") {
95        _rustflags += rustcOhosLints
96      } else if (rustc_lints == "vendor") {
97        _rustflags += rustcVendorLints
98      } else if (rustc_lints == "none") {
99        _rustflags += allowAllLints
100      }
101    }
102    if (defined(clippy_lints)) {
103      if (invoker.clippy_lints == "openharmony") {
104        _rustflags += clippyOhosLints
105      } else if (clippy_lints == "vendor") {
106        _rustflags += clippyVendorLints
107      } else if (clippy_lints == "none") {
108        _rustflags += allowAllLints
109      }
110    }
111    if (!defined(rustflags)) {
112      rustflags = _rustflags
113    } else {
114      rustflags += _rustflags
115    }
116  }
117}
118