交叉编译
本帖最后由 死龙 于 2020-11-16 09:00 编辑
很多朋友对移植鸿蒙存在不少疑惑,其中对交叉编译工具链的配置也完全不清楚。现在我们来一步步完成树莓派3上鸿蒙系统的交叉编译工具链配置操作。
首先,我们在命令行中输入
结果肯定是失败的,我们来看看提示。如下图所示,提示的内容是找不到 build/lite/product/rpi3.json 这个文件。 这么说来,编译脚本第一步就是先定位这个 product 文件夹里的设置内容,然后根据该文件的设置布置编译任务。
那我们配置的第一步,就是新建这个文件了
--------------------------------------------------------------------------------------
1、新建build/lite/product/rpi3.json ,并复制3518的内容
我们在这个文件夹新建 build/lite/product/rpi3.json 文件,并输入以下内容。
- {
- "ohos_version": "OpenHarmony 1.0",
- "board": "rpi3",
- "kernel": "liteos_a",
- "compiler": "arm-none-eabi-gcc",
- "subsystem": [
- {
- "name": "hiviewdfx",
- "component": [
- { "name": "hilog_lite", "dir": "//base/hiviewdfx/frameworks/hilog_lite/featured:hilog_static", "features":[] },
- { "name": "hilog_lite", "dir": "//base/hiviewdfx/frameworks/hilog_lite/featured:hilog_shared", "features":[] }
- ]
- },
- {
- "name": "distributedschedule",
- "component": [
- { "name": "samgr_lite", "dir": "//foundation/distributedschedule/services/samgr_lite:samgr", "features":[] },
- { "name": "safwk_lite", "dir": "//foundation/distributedschedule/services/safwk_lite:safwk_lite", "features":[] },
- { "name": "dtbschedmgr_lite", "dir": "//foundation/distributedschedule/services/dtbschedmgr_lite:dtbschedmgr", "features":[] }
- ]
- },
- {
- "name": "security",
- "component": [
- { "name": "hichainsdk_lite", "dir": "//base/security/frameworks/hichainsdk_lite:hichainsdk", "features":[] },
- { "name": "iam_lite", "dir": "//base/security/services/iam_lite:pms_lite", "features":[] },
- { "name": "app_verify", "dir": "//base/security/frameworks/app_verify:verify", "features":[] }
- ]
- },
- {
- "name": "multimedia",
- "component": [
- { "name": "media_lite", "dir": "//foundation/multimedia/services/media_lite:media_lite", "features":[] }
- ]
- },
- {
- "name": "startup",
- "component": [
- { "name": "bootstrap_lite", "dir": "//base/startup/services/bootstrap_lite/source:bootstrap", "features":[] },
- { "name": "parameter", "dir": "//base/startup/frameworks/syspara_lite/parameter:parameter", "features":[] },
- { "name": "token", "dir": "//base/startup/frameworks/syspara_lite/token:token", "features":[] },
- { "name": "init_lite", "dir": "//base/startup/services/init_lite:init_lite", "features":[] },
- { "name": "appspawn_lite", "dir": "//base/startup/services/appspawn_lite:appspawn_lite", "features":[] }
- ]
- },
- {
- "name": "kernel",
- "component": [
- { "name": "liteos_a", "dir": "//kernel/liteos_a:kernel", "features":[] }
- ]
- },
- {
- "name": "communication",
- "component": [
- { "name": "ipc_lite", "dir": "//foundation/communication/frameworks/ipc_lite:liteipc", "features":[] },
- { "name": "softbus_lite", "dir": "//foundation/communication/services/softbus_lite:softbus", "features":[] },
- { "name": "wpa_supplicant", "dir": "//third_party/wpa_supplicant/wpa_supplicant-2.9:wpa_supplicant", "features":[] }
- ]
- },
- {
- "name": "hdf",
- "component": [
- { "name": "posix", "dir": "//drivers/hdf/lite/posix:hdf_posix", "features":[] },
- { "name": "manager", "dir": "//drivers/hdf/lite/manager:hdf_manager", "features":[] },
- { "name": "wifi", "dir": "//vendor/huawei/hdf/wifi:wifi_firmware", "features":[] },
- { "name": "display", "dir": "//vendor/huawei/hdf/display/hdi:hdi_display", "features":[] },
- { "name": "input", "dir": "//vendor/huawei/hdf/input/hdi:hdi_input", "features":[] }
- ]
- },
- {
- "name": "appexecfwk",
- "component": [
- { "name": "bundle_lite", "dir": "//foundation/appexecfwk/frameworks/bundle_lite:appexecfwk_kits_lite", "features":[] },
- { "name": "bundlemgr_lite", "dir": "//foundation/appexecfwk/services/bundlemgr_lite:appexecfwk_services_lite", "features":[] }
- ]
- },
- {
- "name": "utils",
- "component": [
- { "name": "kv_store", "dir": "//utils/native/lite/kv_store:kv_store", "features":[] }
- ]
- },
- {
- "name": "vendor",
- "component": [
- { "name": "middleware", "dir": "//vendor/hisi/hi35xx/middleware:middleware_source_sdk", "features":[] },
- { "name": "hardware", "dir": "//vendor/hisi/hi35xx/hardware:hardware_media_sdk", "features":[] }
- ]
- },
- {
- "name": "test",
- "component": [
- { "name": "xts_act", "dir": "//test/xts/acts", "features":[] },
- { "name": "xts_tools", "dir": "//test/xts/tools", "features":[] },
- { "name": "test", "dir": "//test/developertest:test", "features":[] }
- ]
- }
- ],
- "vendor_adapter_dir": "//vendor/hisi/hi35xx/hi3518ev300/hi3518ev300_adapter",
- "third_party_dir": "//third_party",
- "ohos_product_type":"",
- "ohos_manufacture":"",
- "ohos_brand":"",
- "ohos_market_name":"",
- "ohos_product_series":"",
- "ohos_product_model":"",
- "ohos_software_model":"",
- "ohos_hardware_model":"",
- "ohos_hardware_profile":"",
- "ohos_serial":"",
- "ohos_bootloader_version":"",
- "ohos_secure_patch_level":"",
- "ohos_abi_list":""
- }
-
说明:板子设定为rpi3,编译器设定为 arm-none-eabi-gcc,并删掉一些组件
=========
接下来,我们再运行一下编译命令(python build.py rpi3 )。我们填写了一个不在原系统里支持的编译器,运行命令也是会出错的。果然,提示不支持这个编译器。
好的,那第二步的工作就确定了
--------------------------------------------------------------------------------------
2、编写 arm-none-eabi-gcc 配置文件 我们先用VScode搜索一下 Error: Unsupport compiler 这个错误。然后定位到 build/lite/config.py 这个文件的124行附件。将内容修改为以下内容。
- if compiler == "clang":
- compiler_bin = "clang"
- elif compiler == "arm-none-eabi-gcc":
- compiler_bin = "arm-none-eabi-gcc"
- elif compiler == "gcc":
- compiler_bin = "riscv32-unknown-elf-gcc"
- else:
新增了编译器设置为 arm-none-eabi-gcc 的判断和执行部分
=======
然后我们再次运行编译命令,又出现了一个新错误提示,如下图。
好的,这次我们在VScode中搜索 not OHOS compiler ,同样是在 build/lite/config.py 这个文件的 160行附近找到了对应判断语句。
我们来将对应内容修改为
- if compiler == 'gcc' or compiler == 'arm-none-eabi-gcc':
- return True
======
然后我们再次运行编译命令,又出现了一个新错误提示,如下图。
好的,根据提示缺少的文件,我们新建 build/lite/config/boards/rpi3.gni 文件,并添加以下内容。
- # Copyright (c) 2020 Huawei Device Co., Ltd.
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- board_arch = "armv7-a"
- board_cpu = "cortex-a7"
=======
然后我们再次运行编译命令,又出现了一个新错误提示,如下图。
然后继续新建缺少的文件 build/lite/toolchain/arm-none-eabi-gcc.gni ,添加以下内容
- # Copyright (c) 2020 Huawei Device Co., Ltd.
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- ohos_build_compiler_so_strip = "arm-none-eabi-objcopy --strip-all"
- ohos_build_compiler_bin_strip = "arm-none-eabi-objcopy --strip-all"
- template("arm_none_eabi_gcc_toolchain") {
- toolchain(target_name) {
- assert(defined(invoker.cc), "clang toolchain must specify a "cc" value")
- assert(defined(invoker.cxx), "clang toolchain must specify a "cxx" value")
- assert(defined(invoker.ar), "clang toolchain must specify a "ar" value")
- assert(defined(invoker.as), "clang toolchain must specify a "as" value")
- assert(defined(invoker.ld), "clang toolchain must specify a "ld" value")
- cc = invoker.cc
- cxx = invoker.cxx
- ar = invoker.ar
- as = invoker.as
- ld = invoker.ld
- need_strip = false
- if(defined(invoker.strip)) {
- strip = invoker.strip
- need_strip = true
- }
- tool("cc") {
- command = "$cc {{defines}} {{include_dirs}} {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
- description = "arm-none-eabi-gcc {{output}}"
- outputs = [
- "{{source_out_dir}}/{{source_name_part}}.o",
- ]
- }
- tool("cxx") {
- depfile = "{{output}}.d"
- command = "$cxx {{defines}} {{include_dirs}} {{cflags_cc}} -c {{source}} -o {{output}}"
- description = "arm-none-eabi-g++ {{output}}"
- outputs = [
- "{{source_out_dir}}/{{source_name_part}}.o",
- ]
- }
- tool("asm") {
- depfile = "{{output}}.d"
- command = "$as {{include_dirs}} {{asmflags}} -c {{source}} -o {{output}}"
- description = "arm-none-eabi-as {{output}}"
- outputs = [
- "{{source_out_dir}}/{{source_name_part}}.o",
- ]
- }
- tool("alink") {
- outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
- rspfile = "{{output}}.rsp"
- rspfile_content = "{{inputs}}"
- command = "$ar -cr {{output}} @"$rspfile""
- description = "arm-none-eabi-ar {{output}}"
- outputs = [
- outfile,
- ]
- default_output_dir = "{{root_out_dir}}/libs"
- default_output_extension = ".a"
- output_prefix = "lib"
- }
- tool("solink") {
- outfile = "{{output_dir}}/{{target_output_name}}{{output_extension}}"
- rspfile = "$outfile.rsp"
- rspfile_content = "{{inputs}}"
- command = "$ld -shared {{ldflags}} {{inputs}} -o $outfile"
- if (need_strip) {
- unstripped_outfile = outfile
- command += " && $strip" + " "$unstripped_outfile" "$outfile""
- }
- default_output_extension = ".so"
- description = "SOLINK $outfile"
- default_output_dir = "{{root_out_dir}}"
- output_prefix = "lib"
- outputs = [
- outfile
- ]
- }
- tool("link") {
- outfile = "{{output_dir}}/bin/{{target_output_name}}{{output_extension}}"
- rspfile = "$outfile.rsp"
- custom_ld_flags = " "
- command = "$cc {{ldflags}} {{inputs}} $custom_ld_flags -o $outfile"
- if (need_strip) {
- command += " && $strip $outfile"
- }
- description = "GCC LINK $outfile"
- default_output_dir = "{{root_out_dir}}"
- rspfile_content = "{{inputs}}"
- outputs = [
- outfile
- ]
- }
- tool("stamp") {
- if (host_os == "win") {
- command = "cmd /c type nul > "{{output}}""
- } else {
- command = "/usr/bin/touch {{output}}"
- }
- description = "STAMP {{output}}"
- }
- tool("copy") {
- command = "cp -afd {{source}} {{output}}"
- description = "COPY {{source}} {{output}}"
- }
- }
- }
================
再运行编译命令,继续查看错误输出,如下图
是的,到这一步的时候,提示就有点奇怪了。明明设置了 arm-none-eabi-gcc ,却依然提示找 linux_x86_64_clang 。还是老方法,VScode搜索,找到了 build/lite/config/BUILDCONFIG.gn 的 28 行 ,将内容修改为:
- if(ohos_kernel_type == "liteos_a") {
- if(target_cpu == "cortex-a8") {
- set_default_toolchain("//build/lite/toolchain:linux_arm_none_eabi_gcc")
- } else {
- set_default_toolchain("//build/lite/toolchain:linux_x86_64_clang")
- }
- default_target_configs = [ "//build/lite/config:liteos_a" ]
同时,build/lite/toolchain/BUILD.gn
文件中似乎也缺少定义,在文件最后添加以下内容
- if (ohos_build_compiler == "arm-none-eabi-gcc") {
- arm_none_eabi_gcc_toolchain("linux_arm_none_eabi_gcc") {
- ohos_build_compiler_prefix = "arm-none-eabi"
- cc = "${ohos_build_compiler_prefix}-gcc"
- cxx = "${ohos_build_compiler_prefix}-g++"
- ar = "${ohos_build_compiler_prefix}-ar"
- as = "${ohos_build_compiler_prefix}-as"
- ld = cc
- if (ohos_build_type == "release") {
- strip = "${ohos_build_compiler_prefix}-objcopy --strip-all"
- }
- }
- }
================
再运行编译命令,继续查看错误输出,如下图
好的,到这里,基本上就配置得差不多了,我们看一下
out/rpi3/toolchain.ninja
这个文件,如下图,gn 工具已经正确识别了工具链了
这时候大家可以再仔细检查一下,编译命令是否正确,不正确的话就回去修改 build/lite/toolchain/arm-none-eabi-gcc.gni
文件。
===========
树莓派编译工具链移植暂时就在这里停止先,还未完成
更多回帖
长按上方图片保存到相册