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/python.gni") 15import("//build/ohos/build_var.gni") 16import("//build/ohos/notice/notice.gni") 17import("//build/version.gni") 18 19sdk_base_build_gn = "//build/ohos/sdk/BUILD.gn" 20generated_files_dir = get_path_info(sdk_base_build_gn, "gen_dir") 21generated_sdk_module_install_paths = 22 "${generated_files_dir}/ohos_sdk_install_paths.json" 23 24sdk_system_windows = "windows" 25sdk_system_linux = "linux" 26sdk_system_darwin = "darwin" 27sdk_system_ohos = "ohos" 28 29ohos_sdk_out_dir = "$product_output_dir/${product_name}" 30ohos_sdk_copy_dir = "$root_build_dir/${product_name}" 31 32declare_args() { 33 sdk_check_flag = true 34 sdk_for_hap_build = false 35} 36 37declare_args() { 38 enable_sign = false 39} 40 41sdk_toolchains = { 42 windows = "//build/toolchain/mingw:mingw_x86_64" 43 ohos = "//build/toolchain/ohos:ohos_clang_arm64" 44 45 if (host_cpu == "arm64") { 46 linux = "//build/toolchain/linux:clang_arm64" 47 darwin = "//build/toolchain/mac:clang_arm64" 48 } else { 49 linux = "//build/toolchain/linux:clang_x64" 50 darwin = "//build/toolchain/mac:clang_x64" 51 } 52} 53 54if (host_cpu == "arm64") { 55 arch = "arm64" 56} else if (host_cpu == "riscv64") { 57 arch = "riscv64" 58} else { 59 arch = "x64" 60} 61 62if (build_default_sdk_target) { 63 if (host_os == "mac") { 64 sdk_systems = [ sdk_system_darwin ] 65 } else if (host_os == "linux" && host_cpu == "arm64") { 66 sdk_systems = [ sdk_system_linux ] 67 } else { 68 sdk_systems = [ 69 sdk_system_windows, 70 sdk_system_linux, 71 sdk_system_ohos, 72 ] 73 } 74} else { 75 sdk_systems = [] 76 if (build_mac_sdk_target) { 77 sdk_systems += [ sdk_system_darwin ] 78 } 79 if (build_windows_sdk_target) { 80 sdk_systems += [ sdk_system_windows ] 81 } 82 if (build_linux_sdk_target) { 83 sdk_systems += [ sdk_system_linux ] 84 } 85 if (build_ohos_sdk_target) { 86 sdk_systems += [ sdk_system_ohos ] 87 } 88} 89 90if (use_current_sdk) { 91 sdk_systems = [] 92 sdk_systems += [ sdk_system_linux ] 93} 94 95template("copy_and_archive") { 96 assert(defined(invoker.dest_dir)) 97 assert(defined(invoker.sdk_system)) 98 assert(defined(invoker.sdk_type)) 99 assert(defined(invoker.sdk_modules_desc_file)) 100 forward_variables_from(invoker, 101 [ 102 "testonly", 103 "sdk_class", 104 ]) 105 action_with_pydeps(target_name) { 106 deps = [] 107 if (defined(invoker.deps)) { 108 deps += invoker.deps 109 } 110 111 script = "//build/ohos/sdk/copy_sdk_modules.py" 112 depfile = "$target_gen_dir/$target_name.d" 113 114 _sdk_output_archive = 115 "$ohos_sdk_out_dir/${invoker.sdk_system}/${invoker.zipfile_name}" 116 _notice_output_archive = "${sdk_notice_archive_dir}/${invoker.sdk_system}-${invoker.sdk_type}.zip" 117 118 if (use_current_sdk) { 119 _sdk_output_archive = "$ohos_sdk_out_dir/${sdk_class}/${invoker.sdk_system}/${invoker.zipfile_name}" 120 _notice_output_archive = "${sdk_notice_archive_dir}/${sdk_class}/${invoker.sdk_system}-${invoker.sdk_type}.zip" 121 } 122 123 outputs = [ 124 _sdk_output_archive, 125 _notice_output_archive, 126 ] 127 128 args = [ 129 "--sdk-modules-desc-file", 130 rebase_path(invoker.sdk_modules_desc_file, root_build_dir), 131 "--dest-dir", 132 rebase_path(invoker.dest_dir, root_build_dir), 133 "--sdk-output-archive", 134 rebase_path(_sdk_output_archive, root_build_dir), 135 "--notice-output-archive", 136 rebase_path(_notice_output_archive, root_build_dir), 137 "--depfile", 138 rebase_path(depfile, root_build_dir), 139 "--archive-dir", 140 rebase_path("${invoker.dest_dir}/${invoker.sdk_type}", root_build_dir), 141 ] 142 143 archive_path_args = [ 144 "--sdk-archive-paths-file", 145 rebase_path(generated_sdk_module_install_paths, root_build_dir), 146 ] 147 148 if (use_current_sdk) { 149 archive_path_args = [] 150 archive_path_args += [ 151 "--sdk-archive-paths-file", 152 rebase_path( 153 "${generated_files_dir}/${sdk_class}/${sdk_class}_sdk_install_paths.json", 154 root_build_dir), 155 ] 156 } 157 158 args += archive_path_args 159 160 if (defined(invoker.zip_prefix_path)) { 161 args += [ 162 "--zip-prefix-path", 163 invoker.zip_prefix_path, 164 ] 165 } 166 167 if (use_current_sdk) { 168 enable_archive_sdk = false 169 } 170 171 if (enable_archive_sdk) { 172 args += [ "--enable-archive-sdk=${enable_archive_sdk}" ] 173 } 174 } 175} 176 177template("make_sdk_modules") { 178 assert(defined(invoker.zipfile_name)) 179 assert(defined(invoker.sdk_modules)) 180 assert(defined(invoker.sdk_toolchain)) 181 assert(defined(invoker.sdk_type)) 182 assert(defined(invoker.sdk_system)) 183 184 if (invoker.sdk_modules == []) { 185 not_needed(invoker, [ "sdk_toolchain" ]) 186 } 187 copy_and_archive(target_name) { 188 forward_variables_from(invoker, 189 [ 190 "testonly", 191 "sdk_system", 192 "sdk_type", 193 "zipfile_name", 194 "sdk_class", 195 ]) 196 _sdk_modules = [] 197 _sdk_module_infos = [] 198 199 foreach(_label, invoker.sdk_modules) { 200 _target_label = get_label_info(_label, "label_no_toolchain") 201 sources = [ _target_label ] 202 if (sources == []) { 203 _sdk_modules += [ _target_label ] 204 } else { 205 _sdk_modules += [ "${_target_label}(${invoker.sdk_toolchain})" ] 206 } 207 sources = [] 208 } 209 not_needed(invoker, [ "sdk_toolchain" ]) 210 211 foreach(_label, _sdk_modules) { 212 _module_info_file = get_label_info(_label, "target_out_dir") + "/" + 213 get_label_info(_label, "name") + "_module_info.json" 214 _sdk_module_infos += [ 215 { 216 label = get_label_info(_label, "label_no_toolchain") 217 module_info_file = rebase_path(_module_info_file, root_build_dir) 218 }, 219 ] 220 } 221 sdk_modules_desc_file = "${target_gen_dir}/${target_name}_sdk_modules.json" 222 write_file(sdk_modules_desc_file, _sdk_module_infos, "json") 223 224 deps = _sdk_modules 225 if (defined(invoker.deps)) { 226 deps += invoker.deps 227 } 228 229 dest_dir = "${ohos_sdk_copy_dir}/${sdk_system}" 230 if (use_current_sdk) { 231 dest_dir = "${root_build_dir}/${sdk_class}-current-sdk/${sdk_system}/${api_version}" 232 } 233 234 zip_prefix_path = "${invoker.sdk_type}" 235 } 236} 237 238template("make_linux_sdk_modules") { 239 make_sdk_modules(target_name) { 240 forward_variables_from(invoker, 241 [ 242 "testonly", 243 "zipfile_name", 244 "sdk_modules", 245 "sdk_type", 246 "sdk_class", 247 "deps", 248 ]) 249 sdk_toolchain = sdk_toolchains.linux 250 sdk_system = sdk_system_linux 251 } 252} 253 254template("make_windows_sdk_modules") { 255 make_sdk_modules(target_name) { 256 forward_variables_from(invoker, 257 [ 258 "testonly", 259 "zipfile_name", 260 "sdk_modules", 261 "sdk_type", 262 "sdk_class", 263 "deps", 264 ]) 265 sdk_toolchain = sdk_toolchains.windows 266 sdk_system = sdk_system_windows 267 } 268} 269 270template("make_darwin_sdk_modules") { 271 make_sdk_modules(target_name) { 272 forward_variables_from(invoker, 273 [ 274 "testonly", 275 "zipfile_name", 276 "sdk_modules", 277 "sdk_type", 278 "sdk_class", 279 "deps", 280 ]) 281 sdk_toolchain = sdk_toolchains.darwin 282 sdk_system = sdk_system_darwin 283 } 284} 285 286template("make_ohos_sdk_modules") { 287 make_sdk_modules(target_name) { 288 forward_variables_from(invoker, 289 [ 290 "testonly", 291 "zipfile_name", 292 "sdk_modules", 293 "sdk_type", 294 "deps", 295 ]) 296 sdk_toolchain = sdk_toolchains.ohos 297 sdk_system = sdk_system_ohos 298 } 299} 300 301template("current_sdk") { 302 # parse the sdk description file 303 forward_variables_from(invoker, 304 [ 305 "sdk_desc_file", 306 "sdk_class", 307 ]) 308 309 hap_build_sdk_description_file = "${generated_files_dir}/${sdk_class}/${sdk_class}_sdk_description_hap.json" 310 hap_build_sdk_config_parser = 311 "//build/ohos/sdk/generate_hap_build_sdk_config.py" 312 hap_build_sdk_args = [ 313 "--sdk-description-file", 314 rebase_path(sdk_desc_file, root_build_dir), 315 "--output-hap-build-sdk-desc-file", 316 rebase_path(hap_build_sdk_description_file, root_build_dir), 317 ] 318 exec_script(hap_build_sdk_config_parser, hap_build_sdk_args) 319 320 sdk_desc_file = hap_build_sdk_description_file 321 sdk_parse_script = "//build/ohos/sdk/parse_sdk_description.py" 322 323 gen_sdk_modules_gni = 324 "${generated_files_dir}/${sdk_class}/${sdk_class}_sdk_modules.gni" 325 gen_sdk_module_install_paths = 326 "${generated_files_dir}/${sdk_class}/${sdk_class}_sdk_install_paths.json" 327 gen_sdk_types_file = 328 "${generated_files_dir}/${sdk_class}/generated_sdk_types.txt" 329 330 parse_args = [ 331 "--sdk-description-file", 332 rebase_path(sdk_desc_file, root_build_dir), 333 "--sdk-modules-gni", 334 rebase_path(gen_sdk_modules_gni, root_build_dir), 335 "--sdk-install-info-file", 336 rebase_path(gen_sdk_module_install_paths, root_build_dir), 337 "--sdk-types-file", 338 rebase_path(gen_sdk_types_file, root_build_dir), 339 "--base-platform", 340 "phone", 341 "--variant-to-product", 342 rebase_path("//build/ohos/sdk/variant_to_product.json", root_build_dir), 343 "--source-root-dir", 344 rebase_path("//", root_build_dir), 345 "--node-js", 346 rebase_path(nodejs, root_build_dir), 347 ] 348 foreach(p, target_platform_list) { 349 parse_args += [ 350 "--platforms", 351 p, 352 ] 353 } 354 355 exec_script(sdk_parse_script, parse_args) 356 357 sdk_types = read_file(gen_sdk_types_file, "list lines") 358 359 generate_all_types_sdk_script = "//build/ohos/sdk/generate_all_types_sdk.py" 360 generated_build_gn = "${generated_files_dir}/${sdk_class}/BUILD.gn" 361 generate_args = [ 362 "--sdk-modules", 363 "ohos_sdk_modules", 364 "--output", 365 rebase_path(generated_build_gn, root_build_dir), 366 "--current-dir", 367 get_path_info(sdk_base_build_gn, "dir"), 368 "--generated-sdk-modules-gni", 369 gen_sdk_modules_gni, 370 "--type2displayname", 371 rebase_path("//build/ohos/sdk/type_to_display_name.json", root_build_dir), 372 "--api-version", 373 api_version, 374 "--release-type", 375 release_type, 376 "--meta-version", 377 meta_version, 378 "--sdk-class", 379 sdk_class, 380 ] 381 382 if (use_current_sdk) { 383 generate_args += [ "--use-current-sdk" ] 384 } 385 386 generate_args += [ 387 "--sdk-systems", 388 "linux", 389 ] 390 391 foreach(sdk_type, sdk_types) { 392 generate_args += [ 393 "--sdk-types", 394 sdk_type, 395 ] 396 } 397 398 exec_script(generate_all_types_sdk_script, generate_args) 399 400 group(target_name) { 401 deps = [] 402 deps += [ "${generated_files_dir}/${sdk_class}:generated_ohos_sdk" ] 403 } 404} 405