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_root/ark_var.gni") 15 16template("ohos_executable") { 17 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 18 subsystem_name = invoker.subsystem_name 19 part_name = invoker.part_name 20 } else if (defined(invoker.subsystem_name)) { 21 subsystem_name = invoker.subsystem_name 22 part_name = subsystem_name 23 } else { 24 subsystem_name = "arkcompiler" 25 part_name = "common" 26 } 27 assert(part_name != "") 28 assert(subsystem_name != "") 29 30 if (defined(invoker.unit_test) && invoker.unit_test) { 31 output_dir = invoker.test_output_dir 32 } else { 33 output_dir = "${root_out_dir}/${subsystem_name}/${part_name}" 34 } 35 36 executable(target_name) { 37 forward_variables_from(invoker, 38 "*", 39 [ 40 "configs", 41 "remove_configs", 42 "subsystem_name", 43 "install_enable", 44 "part_name", 45 "use_exceptions", 46 "static_link", 47 "output_dir", 48 "unit_test", 49 "external_deps", 50 "stack_protector_ret", 51 ]) 52 output_dir = output_dir 53 if (defined(invoker.configs)) { 54 configs += invoker.configs 55 } 56 57 if (defined(invoker.remove_configs)) { 58 configs -= invoker.remove_configs 59 } 60 61 if (defined(invoker.use_exceptions) && invoker.use_exceptions) { 62 configs += [ "//arkcompiler/toolchain/build/config/compiler:exceptions" ] 63 ldflags = [] 64 } 65 66 if (!defined(libs)) { 67 libs = [] 68 } 69 if (!defined(ldflags)) { 70 ldflags = [] 71 } 72 if (!defined(inputs)) { 73 inputs = [] 74 } 75 76 if (defined(version_script)) { 77 _version_script = rebase_path(version_script, root_build_dir) 78 inputs += [ version_script ] 79 ldflags += [ "-Wl,--version-script=${_version_script}" ] 80 } 81 82 if (defined(invoker.static_link) && invoker.static_link) { 83 no_default_deps = true 84 ldflags += [ "-static" ] 85 configs -= [ "$build_root/config:executable_config" ] 86 if (is_ohos) { 87 import("$build_root/config/ohos/musl.gni") 88 deps += [ "$build_root/third_party_gn/musl:soft_static_libs" ] 89 } 90 } else if (is_ohos) { 91 if (current_cpu == "arm" || current_cpu == "arm64") { 92 libs += [ "unwind" ] 93 } 94 libs += [ "c++" ] 95 } 96 97 if (defined(invoker.external_deps) && invoker.external_deps != []) { 98 if (!defined(deps)) { 99 deps = [] 100 } 101 102 external_deps_temp_file = 103 "$target_gen_dir/${part_name}__${target_name}_external_deps_temp.json" 104 arguments = [ 105 "--root-src-dir", 106 rebase_path("//", root_build_dir), 107 "--external-deps-temp-file", 108 rebase_path(external_deps_temp_file, root_build_dir), 109 "--external-deps", 110 ] 111 arguments += invoker.external_deps 112 exec_script("$build_root/templates/cxx/external_deps_handler.py", 113 arguments, 114 "string") 115 external_deps_info = read_file(external_deps_temp_file, "json") 116 if (defined(external_deps_info.deps)) { 117 deps += external_deps_info.deps 118 } 119 } 120 } 121} 122 123template("ohos_static_library") { 124 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 125 part_name = invoker.part_name 126 } else if (defined(invoker.part_name)) { 127 part_name = invoker.part_name 128 } else if (defined(invoker.subsystem_name)) { 129 part_name = invoker.subsystem_name 130 } else { 131 part_name = "common" 132 } 133 assert(part_name != "") 134 135 static_library(target_name) { 136 forward_variables_from(invoker, 137 "*", 138 [ 139 "configs", 140 "remove_configs", 141 "subsystem_name", 142 "part_name", 143 "use_exceptions", 144 "external_deps", 145 "stack_protector_ret", 146 ]) 147 if (defined(invoker.configs)) { 148 configs += invoker.configs 149 } 150 151 if (defined(invoker.remove_configs)) { 152 configs -= invoker.remove_configs 153 } 154 155 if (defined(invoker.use_exceptions) && invoker.use_exceptions) { 156 configs += [ "//arkcompiler/toolchain/build/config/compiler:exceptions" ] 157 ldflags = [] 158 } 159 160 if (!defined(libs)) { 161 libs = [] 162 } 163 if (!defined(include_dirs)) { 164 include_dirs = [] 165 } 166 167 if (defined(invoker.external_deps) && invoker.external_deps != []) { 168 if (!defined(deps)) { 169 deps = [] 170 } 171 172 external_deps_temp_file = 173 "$target_gen_dir/${part_name}__${target_name}_external_deps_temp.json" 174 arguments = [ 175 "--root-src-dir", 176 rebase_path("//", root_build_dir), 177 "--external-deps-temp-file", 178 rebase_path(external_deps_temp_file, root_build_dir), 179 "--external-deps", 180 ] 181 arguments += invoker.external_deps 182 exec_script("$build_root/templates/cxx/external_deps_handler.py", 183 arguments, 184 "string") 185 external_deps_info = read_file(external_deps_temp_file, "json") 186 if (defined(external_deps_info.deps)) { 187 deps += external_deps_info.deps 188 } 189 } 190 } 191} 192 193template("ohos_shared_library") { 194 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 195 subsystem_name = invoker.subsystem_name 196 part_name = invoker.part_name 197 } else if (defined(invoker.subsystem_name)) { 198 subsystem_name = invoker.subsystem_name 199 part_name = subsystem_name 200 } else { 201 subsystem_name = "arkcompiler" 202 part_name = "common" 203 } 204 output_dir = "${root_out_dir}/${subsystem_name}/${part_name}" 205 shared_library(target_name) { 206 forward_variables_from(invoker, 207 "*", 208 [ 209 "relative_install_dir", 210 "configs", 211 "subsystem_name", 212 "install_enable", 213 "part_name", 214 "output_dir", 215 "install_images", 216 "use_exceptions", 217 "external_deps", 218 "stack_protector_ret", 219 "innerapi_tags", 220 ]) 221 output_dir = output_dir 222 if (defined(invoker.configs)) { 223 configs += invoker.configs 224 } 225 if (defined(invoker.remove_configs)) { 226 configs -= invoker.remove_configs 227 } 228 229 if (!defined(libs)) { 230 libs = [] 231 } 232 if (!defined(ldflags)) { 233 ldflags = [] 234 } 235 if (!defined(inputs)) { 236 inputs = [] 237 } 238 239 if (defined(version_script)) { 240 _version_script = rebase_path(version_script, root_build_dir) 241 inputs += [ version_script ] 242 ldflags += [ "-Wl,--version-script=${_version_script}" ] 243 } 244 245 if (is_ohos) { 246 if (defined(invoker.stl)) { 247 cflags_cc += [ 248 "-nostdinc++", 249 "-I" + rebase_path( 250 "//prebuilts/clang/ohos/${host_platform_dir}/llvm_ndk/include/c++/v1", 251 root_build_dir), 252 ] 253 ldflags += [ 254 "-nostdlib++", 255 "-L" + rebase_path("${clang_stl_path}/${abi_target}/c++", 256 root_build_dir), 257 ] 258 libs += [ invoker.stl ] 259 } else { 260 if (current_cpu == "arm" || current_cpu == "arm64") { 261 libs += [ "unwind" ] 262 } 263 libs += [ "c++" ] 264 } 265 } 266 267 if (defined(invoker.external_deps) && invoker.external_deps != []) { 268 if (!defined(deps)) { 269 deps = [] 270 } 271 272 external_deps_temp_file = 273 "$target_gen_dir/${part_name}__${target_name}_external_deps_temp.json" 274 arguments = [ 275 "--root-src-dir", 276 rebase_path("//", root_build_dir), 277 "--external-deps-temp-file", 278 rebase_path(external_deps_temp_file, root_build_dir), 279 "--external-deps", 280 ] 281 arguments += invoker.external_deps 282 exec_script("$build_root/templates/cxx/external_deps_handler.py", 283 arguments, 284 "string") 285 external_deps_info = read_file(external_deps_temp_file, "json") 286 if (defined(external_deps_info.deps)) { 287 deps += external_deps_info.deps 288 } 289 } 290 } 291} 292 293template("ohos_source_set") { 294 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 295 subsystem_name = invoker.subsystem_name 296 part_name = invoker.part_name 297 } else if (defined(invoker.subsystem_name)) { 298 subsystem_name = invoker.subsystem_name 299 part_name = subsystem_name 300 } else { 301 subsystem_name = "arkcompiler" 302 part_name = "common" 303 } 304 assert(subsystem_name != "") 305 assert(part_name != "") 306 307 source_set(target_name) { 308 forward_variables_from(invoker, 309 "*", 310 [ 311 "configs", 312 "remove_configs", 313 "no_default_deps", 314 "external_deps", 315 "license_file", 316 "license_as_sources", 317 "use_exceptions", 318 "subsystem_name", 319 "part_name", 320 "stack_protector_ret", 321 ]) 322 if (defined(invoker.configs)) { 323 configs += invoker.configs 324 } 325 if (defined(invoker.remove_configs)) { 326 configs -= invoker.remove_configs 327 } 328 if (defined(invoker.external_deps) && invoker.external_deps != []) { 329 external_deps_temp_file = 330 "$target_gen_dir/${part_name}__${target_name}_external_deps_temp.json" 331 arguments = [ 332 "--root-src-dir", 333 rebase_path("//", root_build_dir), 334 "--external-deps-temp-file", 335 rebase_path(external_deps_temp_file, root_build_dir), 336 "--external-deps", 337 ] 338 arguments += invoker.external_deps 339 exec_script("$build_root/templates/cxx/external_deps_handler.py", 340 arguments, 341 "string") 342 external_deps_info = read_file(external_deps_temp_file, "json") 343 if (defined(external_deps_info.deps)) { 344 deps += external_deps_info.deps 345 } 346 } 347 } 348} 349 350template("ohos_copy") { 351 assert(defined(invoker.sources), 352 "sources must be defined for ${target_name}.") 353 assert(defined(invoker.outputs), 354 "outputs must be defined for ${target_name}.") 355 if (defined(invoker.subsystem_name) && defined(invoker.part_name)) { 356 copy_subsystem_name = invoker.subsystem_name 357 copy_part_name = invoker.part_name 358 } else if (defined(invoker.subsystem_name)) { 359 copy_subsystem_name = invoker.subsystem_name 360 copy_part_name = copy_subsystem_name 361 } else { 362 copy_subsystem_name = "common" 363 copy_part_name = copy_subsystem_name 364 } 365 assert(copy_subsystem_name != "") 366 assert(copy_part_name != "") 367 368 copy(target_name) { 369 forward_variables_from(invoker, 370 "*", 371 [ 372 "relative_install_dir", 373 "module_source_dir", 374 "module_install_name", 375 "license_file", 376 "install_enable", 377 "module_type", 378 ]) 379 } 380} 381