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/config/python.gni") 15import("//build/lite/config/subsystem/aafwk/path.gni") 16 17template("generate_lite_notice") { 18 action_with_pydeps(target_name) { 19 assert(defined(invoker.module_name), "module_name in required.") 20 assert(target_name != "") 21 forward_variables_from(invoker, 22 [ 23 "module_name", 24 "module_source_dir", 25 "deps", 26 "license_file", 27 "module_type", 28 "outputs", 29 30 # Some license file are generated in gn gen. 31 # Such notices should not be used as sources. 32 "license_as_sources", 33 ]) 34 script = rebase_path("//build/ohos/notice/collect_module_notice_file.py") 35 depfile = "${target_gen_dir}/$target_name.d" 36 module_name = invoker.module_name 37 38 if (!defined(outputs)) { 39 outputs = [] 40 41 # _notice_subdir = "${invoker.subsystem_name}/${invoker.part_name}" 42 if (defined(module_type) && module_type == "static_library") { 43 outputs += 44 [ "$root_build_dir/NOTICE_FILES/rootfs/libs/$module_name.a.txt" ] 45 } else if (defined(module_type) && module_type == "shared_library") { 46 outputs += [ 47 "$root_build_dir/NOTICE_FILES/rootfs/usr/lib/$module_name.so.txt", 48 ] 49 } else if (defined(module_type) && module_type == "executable") { 50 outputs += 51 [ "$root_build_dir/NOTICE_FILES/rootfs/bin/$module_name.bin.txt" ] 52 } 53 } 54 55 args = [ 56 "--module-source-dir", 57 rebase_path(module_source_dir, root_build_dir), 58 "--depfile", 59 rebase_path(depfile, root_build_dir), 60 ] 61 foreach(o, outputs) { 62 args += [ 63 "--output", 64 rebase_path(o, root_build_dir), 65 ] 66 } 67 68 if (defined(license_file)) { 69 _license_as_sources = true 70 if (defined(license_as_sources)) { 71 _license_as_sources = license_as_sources 72 } 73 if (_license_as_sources) { 74 inputs = [ license_file ] 75 } 76 args += [ 77 "--license-file", 78 rebase_path(license_file, root_build_dir), 79 ] 80 } 81 } 82} 83 84template("lite_library") { 85 assert(defined(invoker.target_type), "Library target_type is required.") 86 assert(defined(invoker.sources), "Library sources is required.") 87 target_type = invoker.target_type 88 shared_lib = target_type == "shared_library" 89 90 if (shared_lib && 91 (ohos_kernel_type == "liteos_m" || ohos_kernel_type == "uniproton")) { 92 group(target_name) { 93 if (defined(invoker.sources)) { 94 assert(invoker.sources != "") 95 } 96 if (defined(invoker.public_configs)) { 97 assert(invoker.public_configs != "") 98 } 99 if (defined(invoker.public_deps)) { 100 assert(invoker.public_deps != "") 101 } 102 if (defined(invoker.output_name)) { 103 assert(invoker.output_name != "") 104 } 105 } 106 } else { 107 notice_target_type = invoker.target_type 108 if (notice_target_type == "shared_library" || 109 notice_target_type == "static_library" || 110 notice_target_type == "executable") { 111 _notice_target = "${target_name}__notice" 112 _main_target_name = target_name 113 generate_lite_notice(_notice_target) { 114 module_type = notice_target_type 115 module_name = _main_target_name 116 module_source_dir = get_label_info(":${_main_target_name}", "dir") 117 } 118 } 119 120 target(target_type, target_name) { 121 forward_variables_from(invoker, "*", [ "remove_configs" ]) 122 cflags = [] 123 cflags_cc = [] 124 ldflags = [] 125 if (defined(invoker.cflags)) { 126 cflags += invoker.cflags 127 } 128 if (defined(invoker.cflags_cc)) { 129 cflags_cc += invoker.cflags_cc 130 ldflags += [ "-lstdc++" ] 131 } 132 if (defined(invoker.ldflags)) { 133 ldflags += invoker.ldflags 134 } 135 if (defined(invoker.remove_configs)) { 136 configs -= invoker.remove_configs 137 } 138 shared_lib = target_type == "shared_library" 139 if (shared_lib) { 140 cflags += [ "-fPIC" ] 141 cflags_cc += [ "-fPIC" ] 142 } else if (!shared_lib && (ohos_kernel_type != "liteos_m" && 143 ohos_kernel_type != "uniproton")) { 144 cflags += [ "-fPIE" ] 145 cflags_cc += [ "-fPIE" ] 146 } 147 if (!defined(deps)) { 148 deps = [] 149 } 150 if (defined(invoker.target_type)) { 151 notice_target_type = invoker.target_type 152 if (notice_target_type == "shared_library" || 153 notice_target_type == "static_library" || 154 notice_target_type == "executable") { 155 _notice_target = "${target_name}__notice" 156 deps += [ ":$_notice_target" ] 157 } 158 } 159 } 160 } 161} 162 163# Defines a component 164# 165# The lite_component template defines all the modules contained in a subsystem 166# 167# Parameters 168# 169# features (required) 170# [list of scopes] Defines all features in the component. 171template("lite_component") { 172 assert(defined(invoker.features), "Component features is required.") 173 174 if (!defined(invoker.target_type)) { 175 target_type = "group" 176 } else if (invoker.target_type == "static_library") { 177 target_type = "group" 178 } else { 179 target_type = invoker.target_type 180 } 181 assert(target_type != "") 182 183 target(target_type, target_name) { 184 deps = [] 185 186 forward_variables_from(invoker, "*") 187 188 # add component deps 189 if (defined(invoker.deps)) { 190 deps += invoker.deps 191 } 192 193 # add component features 194 foreach(feature_label, features) { 195 deps += [ feature_label ] 196 } 197 } 198} 199 200template("build_ext_component") { 201 forward_variables_from(invoker, [ "testonly" ]) 202 if (defined(invoker.version)) { 203 print(invoker.version) 204 } 205 action(target_name) { 206 forward_variables_from(invoker, 207 [ 208 "no_default_deps", 209 "deps", 210 ]) 211 args = [] 212 if (defined(invoker.exec_path)) { 213 args += [ "--path=${invoker.exec_path}" ] 214 } 215 if (defined(invoker.enable)) { 216 args += [ "--enable=${invoker.enable}" ] 217 } 218 if (defined(invoker.prebuilts)) { 219 args += [ "--prebuilts=${invoker.prebuilts}" ] 220 } 221 if (defined(invoker.command)) { 222 args += [ "--command=${invoker.command}" ] 223 } 224 225 # external component build log 226 target_dir = rebase_path("${target_out_dir}/build.log") 227 args += [ "--target_dir=${target_dir}" ] 228 229 # external component error log if compile failed 230 out_dir = rebase_path("${root_out_dir}/error.log") 231 args += [ "--out_dir=${out_dir}" ] 232 script = "//build/lite/build_ext_components.py" 233 outputs = [ "$target_out_dir/${target_name}_build_ext_components.txt" ] 234 if (defined(invoker.outputs)) { 235 outputs += invoker.outputs 236 } 237 } 238} 239 240template("ohos_tools") { 241 target(invoker.target_type, target_name) { 242 forward_variables_from(invoker, "*") 243 output_dir = "$root_out_dir/tools/$target_name" 244 if (ohos_kernel_type == "liteos_a" || ohos_kernel_type == "linux") { 245 configs -= [ "//build/lite/config:ohos" ] 246 } else if (ohos_kernel_type == "liteos_m") { 247 configs -= [ "//build/lite/config:liteos" ] 248 } 249 configs -= [ "//build/lite/config:pie_executable_config" ] 250 configs -= [ "//build/lite/config:static_pie_config" ] 251 configs += [ "//build/lite/config:tools" ] 252 } 253} 254 255template("generate_notice_file") { 256 assert(defined(invoker.module_name), "module_name in required.") 257 assert(defined(invoker.module_source_dir_list), 258 "module_source_dir_list in required.") 259 assert(target_name != "") 260 forward_variables_from(invoker, 261 [ 262 "module_name", 263 "module_source_dir_list", 264 ]) 265 gen_script = rebase_path("//build/lite/gen_module_notice_file.py") 266 267 foreach(module_source_dir, module_source_dir_list) { 268 arguments = [] 269 arguments = [ 270 "--root-out-dir", 271 rebase_path(root_out_dir), 272 "--module-source-dir", 273 rebase_path(module_source_dir), 274 "--module-relative-source-dir", 275 rebase_path(module_source_dir, "//"), 276 "--target-name", 277 module_name, 278 ] 279 ret_msg = "" 280 ret_msg = exec_script(gen_script, arguments, "list lines") 281 if (ret_msg != "") { 282 foreach(msg, ret_msg) { 283 print(msg) 284 } 285 } 286 } 287} 288