1# Copyright (C) 2021 Huawei Device Co., Ltd. 2# Copyright (C) 2019 The Android Open Source Project 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import("//build/ohos/ndk/ndk.gni") 16import("//third_party/musl/musl_config.gni") 17import("//third_party/musl/musl_src.gni") 18import("//third_party/musl/musl_template.gni") 19 20toolchains_dir = "//prebuilts/clang/ohos" 21musl_target_out_dir = "${root_out_dir}/obj/third_party/musl" 22ndk_musl_include = "ndk_musl_include" 23 24if (host_os == "mac") { 25 if (host_cpu == "arm64") { 26 toolchains_file_name = "darwin-arm64" 27 deps_name = "darwin_arm64" 28 } else { 29 toolchains_file_name = "darwin-x86_64" 30 deps_name = "darwin_x86_64" 31 } 32 33 darwin_system_toolchains_dir = "${toolchains_dir}/${toolchains_file_name}" 34 ohos_ndk_toolchains("${deps_name}") { 35 dest_dir = "$ndk_darwin_toolchains_out_dir" 36 sources = [ 37 "${darwin_system_toolchains_dir}/llvm/NOTICE", 38 "${darwin_system_toolchains_dir}/llvm/bin", 39 "${darwin_system_toolchains_dir}/llvm/include", 40 "${darwin_system_toolchains_dir}/llvm/lib", 41 "${darwin_system_toolchains_dir}/llvm/libexec", 42 "${darwin_system_toolchains_dir}/llvm/python3", 43 "${darwin_system_toolchains_dir}/llvm/script", 44 "${darwin_system_toolchains_dir}/llvm/share", 45 ] 46 license_file = "${darwin_system_toolchains_dir}/llvm/NOTICE" 47 } 48 49 action("darwin_toolchain") { 50 outputs = [ "${ndk_darwin_toolchains_out_dir}" ] 51 script = "toolchain.sh" 52 args = 53 [ "-i" ] + [ rebase_path("${darwin_system_toolchains_dir}/libcxx-ndk") ] 54 args += [ "-o" ] + [ rebase_path("${ndk_darwin_toolchains_out_dir}") ] 55 if (host_cpu == "arm64") { 56 deps = [ ":darwin_arm64" ] 57 } else { 58 deps = [ ":darwin_x86_64" ] 59 } 60 } 61} else { 62 linux_x86_64_toolchains_dir = "${toolchains_dir}/linux-x86_64" 63 windows_x86_64_toolchain_dir = "${toolchains_dir}/windows-x86_64" 64 65 ohos_ndk_toolchains("linux_x86_64") { 66 dest_dir = "$ndk_linux_toolchains_out_dir" 67 sources = [ 68 "${linux_x86_64_toolchains_dir}/llvm/NOTICE", 69 "${linux_x86_64_toolchains_dir}/llvm/bin", 70 "${linux_x86_64_toolchains_dir}/llvm/include", 71 "${linux_x86_64_toolchains_dir}/llvm/lib", 72 "${linux_x86_64_toolchains_dir}/llvm/libexec", 73 "${linux_x86_64_toolchains_dir}/llvm/python3", 74 "${linux_x86_64_toolchains_dir}/llvm/script", 75 "${linux_x86_64_toolchains_dir}/llvm/share", 76 ] 77 license_file = "${linux_x86_64_toolchains_dir}/llvm/NOTICE" 78 } 79 80 ohos_ndk_toolchains("windows_x86_64") { 81 dest_dir = "$ndk_windows_toolchains_out_dir" 82 sources = [ 83 "${windows_x86_64_toolchain_dir}/llvm/NOTICE", 84 "${windows_x86_64_toolchain_dir}/llvm/bin", 85 "${windows_x86_64_toolchain_dir}/llvm/include", 86 "${windows_x86_64_toolchain_dir}/llvm/lib", 87 "${windows_x86_64_toolchain_dir}/llvm/libexec", 88 "${windows_x86_64_toolchain_dir}/llvm/script", 89 "${windows_x86_64_toolchain_dir}/llvm/share", 90 ] 91 args = [ "--follow-all-symlinks" ] 92 license_file = "${windows_x86_64_toolchain_dir}/llvm/NOTICE" 93 } 94 action("linux_toolchain") { 95 outputs = [ "${ndk_linux_toolchains_out_dir}" ] 96 script = "toolchain.sh" 97 args = 98 [ "-i" ] + [ rebase_path("${linux_x86_64_toolchains_dir}/libcxx-ndk") ] 99 args += [ "-o" ] + [ rebase_path("${ndk_linux_toolchains_out_dir}") ] 100 deps = [ ":linux_x86_64" ] 101 } 102 103 action("windows_toolchain") { 104 outputs = [ "${ndk_windows_toolchains_out_dir}" ] 105 script = "toolchain.sh" 106 args = 107 [ "-i" ] + [ rebase_path("${windows_x86_64_toolchain_dir}/libcxx-ndk") ] 108 args += [ "-o" ] + [ rebase_path("${ndk_windows_toolchains_out_dir}") ] 109 deps = [ ":windows_x86_64" ] 110 } 111} 112 113group("ndk_toolchain") { 114 if (host_os == "mac") { 115 deps = [ ":darwin_toolchain" ] 116 } else { 117 deps = [ 118 ":linux_toolchain", 119 ":windows_toolchain", 120 ] 121 } 122} 123 124group("musl_sysroot") { 125 deps = [ 126 ":copy_musl_sysroot", 127 ":musl_arm_bits_arm32", 128 ":musl_arm_uapi_arm32", 129 ":musl_bits_aarch64", 130 ":musl_bits_x86_64", 131 ] 132} 133 134action("copy_ndk_uapi") { 135 outputs = [ "${musl_target_out_dir}/${ndk_musl_include}/linux" ] 136 script = "${musl_dir}/scripts/copy_uapi.sh" 137 args = [ "-i" ] + [ rebase_path("${uapi_dir}") ] 138 args += 139 [ "-o" ] + [ rebase_path("${musl_target_out_dir}/${ndk_musl_include}") ] 140 args += [ "-t" ] + [ "${musl_arch}" ] 141} 142 143action("copy_ndk_musl_headers") { 144 outputs = [ "${musl_target_out_dir}/${ndk_musl_include}" ] 145 script = "copy_ndk_musl_headers.sh" 146 args = [ "-i" ] + [ rebase_path("${musl_dir}/${ndk_musl_include}") ] 147 args += 148 [ "-o" ] + [ rebase_path("${musl_target_out_dir}/${ndk_musl_include}") ] 149 deps = [ ":copy_ndk_uapi" ] 150} 151 152action("copy_musl_sysroot") { 153 outputs = [ "${ndk_headers_out_dir}" ] 154 script = "copy_musl_sysroot.sh" 155 args = 156 [ "-i" ] + [ rebase_path("${musl_target_out_dir}/${ndk_musl_include}") ] 157 args += [ "-o" ] + [ rebase_path("${ndk_headers_out_dir}") ] 158 args += [ "-t" ] + [ "${musl_arch}" ] 159 deps = [ ":copy_ndk_musl_headers" ] 160} 161 162musl_libs_arm32 = [ 163 "//third_party/musl:create_alltypes_h(//build/toolchain/ohos:ohos_clang_arm)", 164 "//third_party/musl:create_syscall_h(//build/toolchain/ohos:ohos_clang_arm)", 165 "//third_party/musl:musl_copy_inc_bits(//build/toolchain/ohos:ohos_clang_arm)", 166] 167 168musl_libs_aarch64 = [ 169 "//third_party/musl:create_alltypes_h(//build/toolchain/ohos:ohos_clang_arm64)", 170 "//third_party/musl:create_syscall_h(//build/toolchain/ohos:ohos_clang_arm64)", 171 "//third_party/musl:musl_copy_inc_bits(//build/toolchain/ohos:ohos_clang_arm64)", 172] 173 174musl_libs_x86_64 = [ 175 "//third_party/musl:create_alltypes_h(//build/toolchain/ohos:ohos_clang_x86_64)", 176 "//third_party/musl:create_syscall_h(//build/toolchain/ohos:ohos_clang_x86_64)", 177 "//third_party/musl:musl_copy_inc_bits(//build/toolchain/ohos:ohos_clang_x86_64)", 178] 179 180musl_lib_arm32 = [ 181 "//third_party/musl:soft_musl_crt_install_action(//build/toolchain/ohos:ohos_clang_arm)", 182 "//third_party/musl:soft_libc_musl_static(//build/toolchain/ohos:ohos_clang_arm)", 183 "//third_party/musl:soft_libm(//build/toolchain/ohos:ohos_clang_arm)", 184 "//third_party/musl:soft_libdl(//build/toolchain/ohos:ohos_clang_arm)", 185 "//third_party/musl:soft_libpthread(//build/toolchain/ohos:ohos_clang_arm)", 186 "//third_party/musl:soft_libcrypt(//build/toolchain/ohos:ohos_clang_arm)", 187 "//third_party/musl:soft_libutil(//build/toolchain/ohos:ohos_clang_arm)", 188 "//third_party/musl:soft_libxnet(//build/toolchain/ohos:ohos_clang_arm)", 189 "//third_party/musl:soft_libresolv(//build/toolchain/ohos:ohos_clang_arm)", 190 "//third_party/musl:soft_librt(//build/toolchain/ohos:ohos_clang_arm)", 191] 192 193musl_lib_arm64 = [ 194 "//third_party/musl:soft_musl_crt_install_action(//build/toolchain/ohos:ohos_clang_arm64)", 195 "//third_party/musl:soft_libc_musl_static(//build/toolchain/ohos:ohos_clang_arm64)", 196 "//third_party/musl:soft_libm(//build/toolchain/ohos:ohos_clang_arm64)", 197 "//third_party/musl:soft_libdl(//build/toolchain/ohos:ohos_clang_arm64)", 198 "//third_party/musl:soft_libpthread(//build/toolchain/ohos:ohos_clang_arm64)", 199 "//third_party/musl:soft_libcrypt(//build/toolchain/ohos:ohos_clang_arm64)", 200 "//third_party/musl:soft_libutil(//build/toolchain/ohos:ohos_clang_arm64)", 201 "//third_party/musl:soft_libxnet(//build/toolchain/ohos:ohos_clang_arm64)", 202 "//third_party/musl:soft_libresolv(//build/toolchain/ohos:ohos_clang_arm64)", 203 "//third_party/musl:soft_librt(//build/toolchain/ohos:ohos_clang_arm64)", 204] 205 206musl_lib_x86_64 = [ 207 "//third_party/musl:soft_musl_crt_install_action(//build/toolchain/ohos:ohos_clang_x86_64)", 208 "//third_party/musl:soft_libc_musl_static(//build/toolchain/ohos:ohos_clang_x86_64)", 209 "//third_party/musl:soft_libm(//build/toolchain/ohos:ohos_clang_x86_64)", 210 "//third_party/musl:soft_libdl(//build/toolchain/ohos:ohos_clang_x86_64)", 211 "//third_party/musl:soft_libpthread(//build/toolchain/ohos:ohos_clang_x86_64)", 212 "//third_party/musl:soft_libcrypt(//build/toolchain/ohos:ohos_clang_x86_64)", 213 "//third_party/musl:soft_libutil(//build/toolchain/ohos:ohos_clang_x86_64)", 214 "//third_party/musl:soft_libxnet(//build/toolchain/ohos:ohos_clang_x86_64)", 215 "//third_party/musl:soft_libresolv(//build/toolchain/ohos:ohos_clang_x86_64)", 216 "//third_party/musl:soft_librt(//build/toolchain/ohos:ohos_clang_x86_64)", 217] 218 219## arm-linux-ohos asm directory 220ohos_ndk_copy("musl_arm_uapi_arm32") { 221 dest_dir = "$ndk_os_irrelevant_out_dir/sysroot/usr/include/arm-linux-ohos" 222 223 sources = 224 [ "//kernel/linux/patches/linux-5.10/prebuilts/usr/include/asm-arm/asm" ] 225 args = [ "--ignore-stale" ] 226} 227 228## arm-linux-ohos bits directory 229ohos_copy("musl_arm_bits_arm32") { 230 deps = musl_libs_arm32 231 sources = [] 232 sources_temp = [ 233 "alltypes.h", 234 "syscall.h", 235 ] 236 sources_temp += musl_inc_bits_files 237 sources_orig = get_path_info(sources_temp, "file") 238 239 # print("${sources_orig}") 240 241 foreach(s, sources_orig) { 242 sources += [ get_label_info(deps[0], "target_out_dir") + 243 "/usr/include/arm-linux-ohos/bits/${s}" ] 244 } 245 outputs = [ "$ndk_os_irrelevant_out_dir/sysroot/usr/include/arm-linux-ohos/bits/{{source_file_part}}" ] 246} 247 248## aarch64-linux-ohos bits directory 249ohos_copy("musl_bits_aarch64") { 250 deps = musl_libs_aarch64 251 sources = [] 252 sources_temp = [ 253 "alltypes.h", 254 "syscall.h", 255 ] 256 sources_temp += musl_inc_bits_files 257 sources_orig = get_path_info(sources_temp, "file") 258 259 # print("${sources_orig}") 260 261 foreach(s, sources_orig) { 262 sources += [ get_label_info(deps[0], "target_out_dir") + 263 "/usr/include/aarch64-linux-ohos/bits/${s}" ] 264 } 265 outputs = [ "$ndk_os_irrelevant_out_dir/sysroot/usr/include/aarch64-linux-ohos/bits/{{source_file_part}}" ] 266} 267 268## x86_64-linux-ohos bits directory 269ohos_copy("musl_bits_x86_64") { 270 deps = musl_libs_x86_64 271 sources = [] 272 sources_temp = [ 273 "alltypes.h", 274 "syscall.h", 275 ] 276 sources_temp += musl_inc_bits_files 277 sources_orig = get_path_info(sources_temp, "file") 278 279 # print("${sources_orig}") 280 281 foreach(s, sources_orig) { 282 sources += [ get_label_info(deps[0], "target_out_dir") + 283 "/usr/include/x86_64-linux-ohos/bits/${s}" ] 284 } 285 outputs = [ "$ndk_os_irrelevant_out_dir/sysroot/usr/include/x86_64-linux-ohos/bits/{{source_file_part}}" ] 286} 287 288ohos_copy("musl_ndk_libs_arm32") { 289 deps = musl_lib_arm32 290 crt_dir = "${root_build_dir}/obj/third_party/musl/usr/lib/arm-linux-ohos" 291 sources = [ 292 "${crt_dir}/Scrt1.o", 293 "${crt_dir}/crt1.o", 294 "${crt_dir}/crti.o", 295 "${crt_dir}/crtn.o", 296 "${crt_dir}/rcrt1.o", 297 ] 298 sources += [ get_label_info(deps[0], "target_out_dir") + 299 "/usr/lib/arm-linux-ohos/libm.a" ] 300 sources += [ get_label_info(deps[0], "target_out_dir") + 301 "/usr/lib/arm-linux-ohos/libdl.a" ] 302 sources += [ get_label_info(deps[0], "target_out_dir") + 303 "/usr/lib/arm-linux-ohos/libc.a" ] 304 sources += [ get_label_info(deps[0], "target_out_dir") + 305 "/usr/lib/arm-linux-ohos/libcrypt.a" ] 306 sources += [ get_label_info(deps[0], "target_out_dir") + 307 "/usr/lib/arm-linux-ohos/libpthread.a" ] 308 sources += [ get_label_info(deps[0], "target_out_dir") + 309 "/usr/lib/arm-linux-ohos/libresolv.a" ] 310 sources += [ get_label_info(deps[0], "target_out_dir") + 311 "/usr/lib/arm-linux-ohos/librt.a" ] 312 sources += [ get_label_info(deps[0], "target_out_dir") + 313 "/usr/lib/arm-linux-ohos/libutil.a" ] 314 sources += [ get_label_info(deps[0], "target_out_dir") + 315 "/usr/lib/arm-linux-ohos/libxnet.a" ] 316 317 outputs = [ "$ndk_os_irrelevant_out_dir/sysroot/usr/lib/arm-linux-ohos/{{source_file_part}}" ] 318} 319 320ohos_copy("musl_ndk_libs_aarch64") { 321 deps = musl_lib_arm64 322 sources = [ get_label_info(deps[0], "target_out_dir") + 323 "/usr/lib/aarch64-linux-ohos/Scrt1.o" ] 324 sources += [ get_label_info(deps[0], "target_out_dir") + 325 "/usr/lib/aarch64-linux-ohos/crt1.o" ] 326 sources += [ get_label_info(deps[0], "target_out_dir") + 327 "/usr/lib/aarch64-linux-ohos/crti.o" ] 328 sources += [ get_label_info(deps[0], "target_out_dir") + 329 "/usr/lib/aarch64-linux-ohos/crtn.o" ] 330 sources += [ get_label_info(deps[0], "target_out_dir") + 331 "/usr/lib/aarch64-linux-ohos/rcrt1.o" ] 332 sources += [ get_label_info(deps[0], "target_out_dir") + 333 "/usr/lib/aarch64-linux-ohos/libc.a" ] 334 sources += [ get_label_info(deps[0], "target_out_dir") + 335 "/usr/lib/aarch64-linux-ohos/libdl.a" ] 336 sources += [ get_label_info(deps[0], "target_out_dir") + 337 "/usr/lib/aarch64-linux-ohos/libm.a" ] 338 sources += [ get_label_info(deps[0], "target_out_dir") + 339 "/usr/lib/aarch64-linux-ohos/libcrypt.a" ] 340 sources += [ get_label_info(deps[0], "target_out_dir") + 341 "/usr/lib/aarch64-linux-ohos/libpthread.a" ] 342 sources += [ get_label_info(deps[0], "target_out_dir") + 343 "/usr/lib/aarch64-linux-ohos/libresolv.a" ] 344 sources += [ get_label_info(deps[0], "target_out_dir") + 345 "/usr/lib/aarch64-linux-ohos/librt.a" ] 346 sources += [ get_label_info(deps[0], "target_out_dir") + 347 "/usr/lib/aarch64-linux-ohos/libutil.a" ] 348 sources += [ get_label_info(deps[0], "target_out_dir") + 349 "/usr/lib/aarch64-linux-ohos/libxnet.a" ] 350 351 outputs = [ "$ndk_os_irrelevant_out_dir/sysroot/usr/lib/aarch64-linux-ohos/{{source_file_part}}" ] 352} 353 354ohos_copy("musl_ndk_libs_x86_64") { 355 deps = musl_lib_x86_64 356 crt_dir = "${root_build_dir}/obj/third_party/musl/usr/lib/x86_64-linux-ohos" 357 sources = [ 358 "${crt_dir}/Scrt1.o", 359 "${crt_dir}/crt1.o", 360 "${crt_dir}/crti.o", 361 "${crt_dir}/crtn.o", 362 "${crt_dir}/rcrt1.o", 363 ] 364 sources += [ get_label_info(deps[0], "target_out_dir") + 365 "/usr/lib/x86_64-linux-ohos/libc.a" ] 366 sources += [ get_label_info(deps[0], "target_out_dir") + 367 "/usr/lib/x86_64-linux-ohos/libdl.a" ] 368 sources += [ get_label_info(deps[0], "target_out_dir") + 369 "/usr/lib/x86_64-linux-ohos/libm.a" ] 370 sources += [ get_label_info(deps[0], "target_out_dir") + 371 "/usr/lib/x86_64-linux-ohos/libcrypt.a" ] 372 sources += [ get_label_info(deps[0], "target_out_dir") + 373 "/usr/lib/x86_64-linux-ohos/libpthread.a" ] 374 sources += [ get_label_info(deps[0], "target_out_dir") + 375 "/usr/lib/x86_64-linux-ohos/libresolv.a" ] 376 sources += [ get_label_info(deps[0], "target_out_dir") + 377 "/usr/lib/x86_64-linux-ohos/librt.a" ] 378 sources += [ get_label_info(deps[0], "target_out_dir") + 379 "/usr/lib/x86_64-linux-ohos/libutil.a" ] 380 sources += [ get_label_info(deps[0], "target_out_dir") + 381 "/usr/lib/x86_64-linux-ohos/libxnet.a" ] 382 383 outputs = [ "$ndk_os_irrelevant_out_dir/sysroot/usr/lib/x86_64-linux-ohos/{{source_file_part}}" ] 384} 385