IT评测·应用市场-qidao123.com技术社区

标题: 鸿蒙NEXT开发【GN构建工程配置HarmonyOS编译工具链】编译构建 [打印本页]

作者: 大号在练葵花宝典    时间: 2024-11-12 07:01
标题: 鸿蒙NEXT开发【GN构建工程配置HarmonyOS编译工具链】编译构建
本文将先容如安在GN工程中配置HarmonyOS工具链,然后通过HarmonyOS工具链编译出可以在HarmonyOS环境下使用的三方库。
概述

HarmonyOS编译子系统是以GN和Ninja构建为基座,对构建和配置粒度进行部件化抽象、对内建模块进行功能加强、对业务模块进行功能扩展的系统,该系统提供以下基本功能:

Ninja: 是一个专注于快速编译的小型构建系统。
GN: Generate Ninja的缩写,用于产生Ninja文件。
编译环境配置

  1. mkdir depot_tools
  2. cd depot_tools
  3. git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
复制代码
将depot_tools的路径加到环境变量中:
编译.bashrc文件将depot_tools路径信息加到最后一行。
  1. ```
  2. vi ~/.bashrc                                    
  3. ```
复制代码
在.bashrc文件的最后添加下面一行代码。

  1. ```
  2. export PATH="$PATH:/xxx/depot_tools"
  3. ```
复制代码
此处需配置绝对路径信息,例如这里创建的本地路径是/mnt/d/my_code/depot_tools,故此处配置如上图。
革新环境变量使其生效:
  1. ```
  2. source ~/.bashrc
  3. ```
复制代码
  1. sudo apt update
  2. sudo apt install python
复制代码
直接输入指令sudo apt install python可能会安装失败,需要先输入sudo apt update更新一下可用包的最新列表。

判断python是否安装乐成:
输入python显示python版本即可。

GN构建工程适配流程


起首,需要添加HarmonyOS平台的宏界说;然后配置好HarmonyOS平台的工具链信息(包括clang工具链,sysroot以及clang版本);接着需要在toolchain路径下配置各个架构的ohos_clang_toolchain;然后扩充gcc_toolchain模版功能,配置HarmonyOS用于启动引导步伐的.o文件信息。剩下的就是需要设置一些HarmonyOS的编译参数(主要是底子的编译选项、宏界说等);然后在BUILD.gn中不同架构平台的分支处,添加对应的HarmonyOS平台的分支,其中未适配HarmonyOS的三方库可以先走Linux分支的编译配置。更加具体的信息可参考下节的适配案例。
webRTC适配案例

本文将通过webRTC的GN构建工程案例来对上一章节的流程进行实操讲解。WebRTC (Web Real-Time Communications) 是一项实时通讯技能,它允许网络应用大概站点,在不借助中心前言的环境下,建立浏览器之间点对点(Peer-to-Peer)的连接,实现视频流和(或)音频流大概其他任意数据的传输。下面我们来相识下怎样通过GN构建工程将webRTC适配到HarmonyOS系统上。
适配流程

示例代码中D:\my_code\compare_src_ohos\src_ohos\中为源代码,D:\my_code\src_ohos\路径下为修改之后的代码。
  1. import("//build/config/sysroot.gni")
  2. import("//build/toolchain/gcc_toolchain.gni")
  3. declare_args() {
  4.   # Whether unstripped binaries, i.e. compiled with debug symbols, should be
  5.   # considered runtime_deps rather than stripped ones.
  6.   ohos_unstripped_runtime_outputs = true
  7.   ohos_extra_cflags = ""
  8.   ohos_extra_cppflags = ""
  9.   ohos_extra_cxxflags = ""
  10.   ohos_extra_asmflags = ""
  11.   ohos_extra_ldflags = ""
  12. }
  13. # The ohos clang toolchains share most of the same parameters, so we have this
  14. # wrapper around gcc_toolchain to avoid duplication of logic.
  15. #
  16. # Parameters:
  17. #  - toolchain_root
  18. #      Path to cpu-specific toolchain within the ndk.
  19. #  - sysroot
  20. #      Sysroot for this architecture.
  21. #  - lib_dir
  22. #      Subdirectory inside of sysroot where libs go.
  23. #  - binary_prefix
  24. #      Prefix of compiler executables.
  25. template("ohos_clang_toolchain") {
  26.   gcc_toolchain(target_name) {
  27.     assert(defined(invoker.toolchain_args),
  28.            "toolchain_args must be defined for ohos_clang_toolchain()")
  29.     toolchain_args = invoker.toolchain_args
  30.     toolchain_args.current_os = "ohos"
  31.     # Output linker map files for binary size analysis.
  32.     enable_linker_map = true
  33.     ohos_libc_dir =
  34.         rebase_path(invoker.sysroot + "/" + invoker.lib_dir, root_build_dir)
  35.     libs_section_prefix = "${ohos_libc_dir}/Scrt1.o"
  36.     libs_section_prefix += " ${ohos_libc_dir}/crti.o"
  37.     libs_section_postfix = "${ohos_libc_dir}/crtn.o"
  38.     if (invoker.target_name == "ohos_clang_arm") {
  39.       abi_target = "arm-linux-ohos"
  40.     } else if (invoker.target_name == "ohos_clang_arm64") {
  41.       abi_target = "aarch64-linux-ohos"
  42.     } else if (invoker.target_name == "ohos_clang_x86_64") {
  43.       abi_target = "x86_64-linux-ohos"
  44.     }
  45.     clang_rt_dir =
  46.         rebase_path("${clang_lib_path}/${abi_target}/nanlegacy",
  47.                     root_build_dir)
  48.     print("ohos_libc_dir :", ohos_libc_dir)
  49.     print("clang_rt_dir :", clang_rt_dir)
  50.     solink_libs_section_prefix = "${ohos_libc_dir}/crti.o"
  51.     solink_libs_section_prefix += " ${clang_rt_dir}/clang_rt.crtbegin.o"
  52.     solink_libs_section_postfix = "${ohos_libc_dir}/crtn.o"
  53.     solink_libs_section_postfix += " ${clang_rt_dir}/clang_rt.crtend.o"
  54.     _prefix = rebase_path("${clang_base_path}/bin", root_build_dir)
  55.     cc = "${_prefix}/clang"
  56.     cxx = "${_prefix}/clang++"
  57.     ar = "${_prefix}/llvm-ar"
  58.     ld = cxx
  59.     readelf = "${_prefix}/llvm-readobj"
  60.     nm = "${_prefix}/llvm-nm"
  61.     if (!is_debug) {
  62.       strip = rebase_path("${clang_base_path}/bin/llvm-strip", root_build_dir)
  63.       use_unstripped_as_runtime_outputs = ohos_unstripped_runtime_outputs
  64.     }
  65.     extra_cflags = ohos_extra_cflags
  66.     extra_cppflags = ohos_extra_cppflags
  67.     extra_cxxflags = ohos_extra_cxxflags
  68.     extra_asmflags = ohos_extra_asmflags
  69.     extra_ldflags = ohos_extra_ldflags
  70.   }
  71. }
  72. ohos_clang_toolchain("ohos_clang_arm") {
  73.   sysroot = "${sysroot}"
  74.   lib_dir = "usr/lib/arm-linux-ohos"
  75.   toolchain_args = {
  76.     current_cpu = "arm"
  77.   }
  78. }
  79. ohos_clang_toolchain("ohos_clang_arm64") {
  80.   sysroot = "${sysroot}"
  81.   lib_dir = "usr/lib/aarch64-linux-ohos"
  82.   toolchain_args = {
  83.     current_cpu = "arm64"
  84.   }
  85. }
  86. ohos_clang_toolchain("ohos_clang_x86_64") {
  87.   sysroot = "${sysroot}"
  88.   lib_dir = "usr/lib/x86_64-linux-ohos"
  89.   toolchain_args = {
  90.     current_cpu = "x86_64"
  91.   }
  92. }
复制代码
  1. import("//build/config/sysroot.gni")
  2. assert(is_ohos)
  3. ohos_clang_base_path = "/mnt/d/ohos/ohos-sdk/linux/native/llvm"
  4. ohos_clang_version = "15.0.4"
  5. if (is_ohos) {
  6.   if (current_cpu == "arm") {
  7.     abi_target = "arm-linux-ohos"
  8.   } else if (current_cpu == "x86") {
  9.     abi_target = ""
  10.   } else if (current_cpu == "arm64") {
  11.     abi_target = "aarch64-linux-ohos"
  12.   } else if (current_cpu == "x86_64") {
  13.     abi_target = "x86_64-linux-ohos"
  14.   } else {
  15.     assert(false, "Architecture not supported")
  16.   }
  17. }
  18. config("compiler") {
  19.   cflags = [
  20.     "-ffunction-sections",
  21.     "-fno-short-enums",
  22.     "-fno-addrsig",
  23.   ]
  24.   cflags += [
  25.     "-Wno-unknown-warning-option",
  26.     "-Wno-int-conversion",
  27.     "-Wno-unused-variable",
  28.     "-Wno-misleading-indentation",
  29.     "-Wno-missing-field-initializers",
  30.     "-Wno-unused-parameter",
  31.     "-Wno-c++11-narrowing",
  32.     "-Wno-unneeded-internal-declaration",
  33.     "-Wno-undefined-var-template",
  34.     "-Wno-implicit-int-float-conversion",
  35.   ]
  36.   defines = [
  37.     # The NDK has these things, but doesn't define the constants to say that it
  38.     # does. Define them here instead.
  39.     "HAVE_SYS_UIO_H",
  40.   ]
  41.   defines += [
  42.     "OHOS",
  43.     "__MUSL__",
  44.     "_LIBCPP_HAS_MUSL_LIBC",
  45.     "__BUILD_LINUX_WITH_CLANG",
  46.     "__GNU_SOURCE",
  47.     "_GNU_SOURCE",
  48.   ]
  49.   ldflags = [
  50.     "-Wl,--no-undefined",
  51.     "-Wl,--exclude-libs=libunwind_llvm.a",
  52.     "-Wl,--exclude-libs=libc++_static.a",
  53.     # Don't allow visible symbols from libraries that contain
  54.     # assembly code with symbols that aren't hidden properly.
  55.     # http://crbug.com/448386
  56.     "-Wl,--exclude-libs=libvpx_assembly_arm.a",
  57.   ]
  58.   cflags += [ "--target=$abi_target" ]
  59.   include_dirs = [
  60.     "${sysroot}/usr/include/${abi_target}",
  61.     "${ohos_clang_base_path}/lib/clang/${ohos_clang_version}/include",
  62.   ]
  63.   ldflags += [ "--target=$abi_target" ]
  64.   # Assign any flags set for the C compiler to asmflags so that they are sent
  65.   # to the assembler.
  66.   asmflags = cflags
  67. }
复制代码
  1. gn gen ../out/xxx --args='is_clang=true target_os="ohos" target_cpu="arm64" xxx'  
  2. ninja -v -C ../out/xxx ${target_name} -j16
复制代码
可以根据需要在编译指令中添加对应参数信息。
检察具体编译命令:
可以在gn gen命令中添加–ninja-args="-v -dkeeprsp"用于检察具体编译命令,这个命令将会在编译过程中打印具体的编译命令,并且保存编译过程中生成的rsp文件。
检察一个目标被谁依赖:
例如gn refs out/intermediate/arm64_72 //pc:rtc_pc_base。这个命令将显示与目标//pc:rtc_pc_base相关的全部依赖项并列出全部引用了该目标的其他目标或文件。
常见问题总结

在对webRTC的GN工程进行HarmonyOS工具链适配过程中,碰到了一些常见问题场景。下面针对这些问题做一个具体分析。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4