--- a/src/build/config/BUILDCONFIG.gn +++ b/src/build/config/BUILDCONFIG.gn @@ -222,6+222,8 @@ if(target_os =="android"){assert(host_os =="linux"|| host_os =="mac","Android builds are only supported on Linux and Mac hosts.") _default_toolchain ="//build/toolchain/android:android_clang_$target_cpu"+}elseif(target_os =="ohos"){+ _default_toolchain ="//build/toolchain/ohos:ohos_clang_$target_cpu"}elseif(target_os =="chromeos"|| target_os =="linux"){#See comments in build/toolchain/cros/BUILD.gn about board compiles.if(is_clang){ @@ -283,6+285,7 @@ if(custom_toolchain !=""){#aixor one of the BSDs. If you need to check these, just check the#current_os value directly.+is_ohos = current_os =="ohos" is_android = current_os =="android" is_chromeos = current_os =="chromeos" is_fuchsia = current_os =="fuchsia" @@ -366,6+369,10 @@ if(is_android){["//build/config/android:default_orderfile_instrumentation"]}+if(is_ohos){+ default_compiler_configs +=["//build/config/ohos:compiler"]+}
2. 设置 ohos 平台 clang 工具链相关路径
修改 src/build/config/clang/clang.gni。
--- a/src/build/config/clang/clang.gni +++ b/src/build/config/clang/clang.gni @@ -11,13+11,27 @@ if(is_mac){if(is_win){ default_clang_base_path+="win"}++if(is_ohos){+declare_args(){+ ohos_sdk_native_root ="//buildtools/ohos-sdk/linux/native"+}+ default_clang_base_path ="${ohos_sdk_native_root}/llvm"+ clang_lib_path ="${default_clang_base_path}/lib"+}++declare_args(){#Indicates if the build should use the Chrome-specific plugins for enforcing#codingguidelines, etc. Only used when compiling with Chrome's Clang, not#Chrome OS's.++if(is_ohos){+ clang_use_chrome_plugins = false +}else{ clang_use_chrome_plugins = is_clang &&!is_nacl &&!use_xcode_clang && default_toolchain !="//build/toolchain/cros:target"-+} clang_base_path = default_clang_base_path }
import("//build/config/sysroot.gni")
import("//build/toolchain/gcc_toolchain.gni")
declare_args(){#Whether unstripped binaries, i.e. compiled with debug symbols, should be#consideredruntime_deps rather than stripped ones. ohos_unstripped_runtime_outputs =true ohos_extra_cflags ="" ohos_extra_cppflags ="" ohos_extra_cxxflags ="" ohos_extra_asmflags ="" ohos_extra_ldflags =""}
#The ohos clang toolchains share most of the same parameters, so we have this#wrapperaround gcc_toolchain to avoid duplication of logic. # #Parameters: # - toolchain_root #Path to cpu-specific toolchain within the ndk. # - sysroot #Sysroot forthis architecture. # - lib_dir #Subdirectory inside of sysroot where libs go. # - binary_prefix #Prefix of compiler executables.template("ohos_clang_toolchain"){gcc_toolchain(target_name){assert(defined(invoker.toolchain_args),"toolchain_args must be defined for ohos_clang_toolchain()") toolchain_args = invoker.toolchain_args toolchain_args.current_os ="ohos"#Output linker map files for binary size analysis. enable_linker_map =true ohos_libc_dir =rebase_path(invoker.sysroot +"/"+ invoker.lib_dir, root_build_dir)#libs_section_prefix ="${ohos_libc_dir}/Scrt1.o"#libs_section_prefix +=" ${ohos_libc_dir}/crti.o"#libs_section_postfix ="${ohos_libc_dir}/crtn.o"if(invoker.target_name =="ohos_clang_arm"){ abi_target ="arm-linux-ohos"}elseif(invoker.target_name =="ohos_clang_arm64"){ abi_target ="aarch64-linux-ohos"}elseif(invoker.target_name =="ohos_clang_x86_64"){ abi_target ="x86_64-linux-ohos"} clang_rt_dir =rebase_path("${clang_lib_path}/${abi_target}/nanlegacy", root_build_dir)print("ohos_libc_dir :", ohos_libc_dir)print("clang_rt_dir :", clang_rt_dir)#solink_libs_section_prefix ="${ohos_libc_dir}/crti.o"#solink_libs_section_prefix +=" ${clang_rt_dir}/clang_rt.crtbegin.o"#solink_libs_section_postfix ="${ohos_libc_dir}/crtn.o"#solink_libs_section_postfix +=" ${clang_rt_dir}/clang_rt.crtend.o" _prefix =rebase_path("${clang_base_path}/bin", root_build_dir) cc ="${_prefix}/clang" cxx ="${_prefix}/clang++" ar ="${_prefix}/llvm-ar" ld = cxx readelf ="${_prefix}/llvm-readobj" nm ="${_prefix}/llvm-nm"if(!is_debug){ strip =rebase_path("${clang_base_path}/bin/llvm-strip", root_build_dir) use_unstripped_as_runtime_outputs = ohos_unstripped_runtime_outputs } extra_cflags = ohos_extra_cflags extra_cppflags = ohos_extra_cppflags extra_cxxflags = ohos_extra_cxxflags extra_asmflags = ohos_extra_asmflags extra_ldflags = ohos_extra_ldflags }}ohos_clang_toolchain("ohos_clang_arm"){ sysroot ="${sysroot}" lib_dir ="usr/lib/arm-linux-ohos" toolchain_args ={ current_cpu ="arm"}}ohos_clang_toolchain("ohos_clang_arm64"){ sysroot ="${sysroot}" lib_dir ="usr/lib/aarch64-linux-ohos" toolchain_args ={ current_cpu ="arm64"}}ohos_clang_toolchain("ohos_clang_x86_64"){ sysroot ="${sysroot}" lib_dir ="usr/lib/x86_64-linux-ohos" toolchain_args ={ current_cpu ="x86_64"}}
7. 设置 ohos 的一些编译参数
新建 build/config/ohos/BUILD.gn。
import("//build/config/sysroot.gni")
import("//build/config/clang/clang.gni")
assert(is_ohos)
ohos_clang_base_path ="${ohos_sdk_native_root}/llvm"
ohos_clang_version ="15.0.4"
if(is_ohos){if(current_cpu =="arm"){ abi_target ="arm-linux-ohos"}elseif(current_cpu =="x86"){ abi_target =""}elseif(current_cpu =="arm64"){ abi_target ="aarch64-linux-ohos"}elseif(current_cpu =="x86_64"){ abi_target ="x86_64-linux-ohos"}else{assert(false,"Architecture not supported")}}
config("compiler"){ cflags =["-ffunction-sections","-fno-short-enums","-fno-addrsig",] cflags +=["-Wno-unknown-warning-option","-Wno-int-conversion","-Wno-unused-variable","-Wno-misleading-indentation","-Wno-missing-field-initializers","-Wno-unused-parameter","-Wno-c++11-narrowing","-Wno-unneeded-internal-declaration","-Wno-undefined-var-template","-Wno-implicit-int-float-conversion",] defines =[#The NDK has these things, but doesn't define the constants to say that it#does. Define them here instead."HAVE_SYS_UIO_H",] defines +=["OHOS","__MUSL__","_LIBCPP_HAS_MUSL_LIBC","__BUILD_LINUX_WITH_CLANG","__GNU_SOURCE","_GNU_SOURCE",] ldflags =["-Wl,--no-undefined","-Wl,--exclude-libs=libunwind_llvm.a","-Wl,--exclude-libs=libc++_static.a",#Don't allow visible symbols from libraries that contain#assemblycode with symbols that aren't hidden properly.#http://crbug.com/448386"-Wl,--exclude-libs=libvpx_assembly_arm.a",] cflags +=["--target=$abi_target"] include_dirs =["${sysroot}/usr/include/${abi_target}","${ohos_clang_base_path}/lib/clang/${ohos_clang_version}/include",] ldflags +=["--target=$abi_target"]#Assign any flags set for the C compiler to asmflags so that they are sent#tothe assembler. asmflags = cflags }