1# Copyright (c) 2020 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/lite/config/subsystem/aafwk/path.gni")
15
16template("lite_library") {
17  assert(defined(invoker.target_type), "Library target_type is required.")
18  assert(defined(invoker.sources), "Library sources is required.")
19  target_type = invoker.target_type
20  shared_lib = target_type == "shared_library"
21
22  if (shared_lib &&
23      (ohos_kernel_type == "liteos_m" || ohos_kernel_type == "uniproton")) {
24    group(target_name) {
25      if (defined(invoker.sources)) {
26        assert(invoker.sources != "")
27      }
28      if (defined(invoker.public_configs)) {
29        assert(invoker.public_configs != "")
30      }
31      if (defined(invoker.public_deps)) {
32        assert(invoker.public_deps != "")
33      }
34      if (defined(invoker.output_name)) {
35        assert(invoker.output_name != "")
36      }
37    }
38  } else {
39    target(target_type, target_name) {
40      forward_variables_from(invoker, "*", [ "remove_configs" ])
41      cflags = []
42      cflags_cc = []
43      ldflags = []
44      if (defined(invoker.cflags)) {
45        cflags += invoker.cflags
46      }
47      if (defined(invoker.cflags_cc)) {
48        cflags_cc += invoker.cflags_cc
49        ldflags += [ "-lstdc++" ]
50      }
51      if (defined(invoker.ldflags)) {
52        ldflags += invoker.ldflags
53      }
54      if (defined(invoker.remove_configs)) {
55        configs -= invoker.remove_configs
56      }
57      shared_lib = target_type == "shared_library"
58      if (shared_lib) {
59        cflags += [ "-fPIC" ]
60        cflags_cc += [ "-fPIC" ]
61      } else if (!shared_lib && (ohos_kernel_type != "liteos_m" &&
62                                 ohos_kernel_type != "uniproton")) {
63        cflags += [ "-fPIE" ]
64        cflags_cc += [ "-fPIE" ]
65      }
66    }
67  }
68}
69
70# Defines a component
71#
72# The lite_component template defines all the modules contained in a subsystem
73#
74# Parameters
75#
76#   features (required)
77#     [list of scopes] Defines all features in the component.
78template("lite_component") {
79  assert(defined(invoker.features), "Component features is required.")
80
81  if (!defined(invoker.target_type)) {
82    target_type = "group"
83  } else if (invoker.target_type == "static_library") {
84    target_type = "group"
85  } else {
86    target_type = invoker.target_type
87  }
88  assert(target_type != "")
89
90  target(target_type, target_name) {
91    deps = []
92
93    forward_variables_from(invoker, "*")
94
95    # add component deps
96    if (defined(invoker.deps)) {
97      deps += invoker.deps
98    }
99
100    # add component features
101    foreach(feature_label, features) {
102      deps += [ feature_label ]
103    }
104  }
105}
106
107template("build_ext_component") {
108  forward_variables_from(invoker, [ "testonly" ])
109  if (defined(invoker.version)) {
110    print(invoker.version)
111  }
112  action(target_name) {
113    forward_variables_from(invoker,
114                           [
115                             "no_default_deps",
116                             "deps",
117                           ])
118    args = []
119    if (defined(invoker.exec_path)) {
120      args += [ "--path=${invoker.exec_path}" ]
121    }
122    if (defined(invoker.enable)) {
123      args += [ "--enable=${invoker.enable}" ]
124    }
125    if (defined(invoker.prebuilts)) {
126      args += [ "--prebuilts=${invoker.prebuilts}" ]
127    }
128    if (defined(invoker.command)) {
129      args += [ "--command=${invoker.command}" ]
130    }
131
132    # external component build log
133    target_dir = rebase_path("${target_out_dir}/build.log")
134    args += [ "--target_dir=${target_dir}" ]
135
136    # external component error log if compile failed
137    out_dir = rebase_path("${root_out_dir}/error.log")
138    args += [ "--out_dir=${out_dir}" ]
139    script = "//build/lite/build_ext_components.py"
140    outputs = [ "$target_out_dir/${target_name}_build_ext_components.txt" ]
141    if (defined(invoker.outputs)) {
142      outputs += invoker.outputs
143    }
144  }
145}
146
147template("ohos_tools") {
148  target(invoker.target_type, target_name) {
149    forward_variables_from(invoker, "*")
150    output_dir = "$root_out_dir/tools/$target_name"
151    if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") {
152      configs -= [ "//build/lite/config:ohos" ]
153    } else if (ohos_kernel_type == "liteos_m") {
154      configs -= [ "//build/lite/config:liteos" ]
155    }
156    configs -= [ "//build/lite/config:pie_executable_config" ]
157    configs -= [ "//build/lite/config:static_pie_config" ]
158    configs += [ "//build/lite/config:tools" ]
159  }
160}
161
162template("generate_notice_file") {
163  assert(defined(invoker.module_name), "module_name in required.")
164  assert(defined(invoker.module_source_dir_list),
165         "module_source_dir_list in required.")
166  assert(target_name != "")
167  forward_variables_from(invoker,
168                         [
169                           "module_name",
170                           "module_source_dir_list",
171                         ])
172  gen_script = rebase_path("//build/lite/gen_module_notice_file.py")
173
174  foreach(module_source_dir, module_source_dir_list) {
175    arguments = []
176    arguments = [
177      "--root-out-dir",
178      rebase_path(root_out_dir),
179      "--module-source-dir",
180      rebase_path(module_source_dir),
181      "--module-relative-source-dir",
182      rebase_path(module_source_dir, "//"),
183      "--target-name",
184      module_name,
185    ]
186    ret_msg = ""
187    ret_msg = exec_script(gen_script, arguments, "list lines")
188    if (ret_msg != "") {
189      foreach(msg, ret_msg) {
190        print(msg)
191      }
192    }
193  }
194}
195