同步操作将从 OpenHarmony/docs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
The following shows how to port the double-conversion library.
Acquire the source code of double-conversion from https://github.com/google/double-conversion. The following table lists the directory structure.
Table 1 Directory structure of the source code
Cross-compile the double-conversion library by modifying the toolchain to generate executable files for the OpenHarmony platform and then add these files to the OpenHarmony project by invoking CMake via GN.
The README.md file in the code repository details the procedures for compiling the double-conversion library using CMake as well as the testing methods. This document focuses on the building, compilation, and testing of the library. If you have any questions during library porting, refer to the README.md file. For porting of other third-party libraries that can be independently built with CMake, you can refer to the compilation guides provided by the libraries.
The following steps show how to configure and modify the toolchains for cross-compiling the libraries built using CMake to compile executable files for the OpenHarmony platform.
Configure the toolchains.
Add configuration of the clang toolchains to the top-level file CMakeLists.txt listed in Table 1.
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CXX_COMPILER_ID Clang)
set(CMAKE_TOOLCHAIN_PREFIX llvm-)
# Specify the C compiler (ensure that the path of the toolchain has been added to the PATH environment variable) and its flags. To perform cross-compilation using clang, the --target flag must be specified.
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_FLAGS "--target=arm-liteos -D__clang__ -march=armv7-a -w")
# Specify the C++ compiler (ensure that the path of the toolchain has been added to the PATH environment variable) and its flags. To perform cross-compilation, the --target flag must be specified.
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_FLAGS "--target=arm-liteos -D__clang__ -march=armv7-a -w")
# Specify the linker and its flags. --target and --sysroot must be specified. You can specify OHOS_ROOT_PATH via the suffix parameter of the cmake command.
set(MY_LINK_FLAGS "--target=arm-liteos --sysroot=${OHOS_SYSROOT_PATH}")
set(CMAKE_LINKER clang)
set(CMAKE_CXX_LINKER clang++)
set(CMAKE_C_LINKER clang)
set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINKER}
${MY_LINK_FLAGS} <FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINKER}
${MY_LINK_FLAGS} <FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
# Specify the path for searching chained libraries.
set(CMAKE_SYSROOT ${OHOS_SYSROOT_PATH})
Perform the compilation.
Run a Linux command to enter the directory (listed in Table 1) for storing double-conversion source files and then run the following commands:
mkdir build && cd build
cmake .. -DBUILD_TESTING=ON -DOHOS_SYSROOT_PATH="..."
make -j
OHOS_SYSROOT_PATH specifies the absolute path where sysroot is located. Taking OpenHarmony for example, set OHOS_SYSROOT_PATH to the absolute path of openHarmony/prebuilts/lite/sysroot.
After step 2 is complete, a static library file and test cases are generated in the build directory.
Table 2 Directory structure of compiled files
Set up the OpenHarmony environment.
Using Hi3518EV300 as an example, compile the OpenHarmony image and burn it to the development board. For details, see Developing the First Example Program Running on Hi3518.
The following screen is displayed after a successful login to the OS.
Mount the nfs directory and put the executable file cctest into the test directory (listed in Table 2) to the nfs directory.
Perform the test cases.
If the double-conversion library is not cross-compiled, you can execute the test cases by running the make test command and obtain the result via CMake. However, this command is not applicable to the library after cross-compilation. This way, you can perform the test cases by executing the generated test case files.
cd nfs
./cctest --list
Some items are as follows:
test-bignum/Assign<
test-bignum/ShiftLeft<
test-bignum/AddUInt64<
test-bignum/AddBignum<
test-bignum/SubtractBignum<
test-bignum/MultiplyUInt32<
test-bignum/MultiplyUInt64<
test-bignum/MultiplyPowerOfTen<
test-bignum/DivideModuloIntBignum<
test-bignum/Compare<
test-bignum/PlusCompare<
test-bignum/Square<
test-bignum/AssignPowerUInt16<
test-bignum-dtoa/BignumDtoaVariousDoubles<
test-bignum-dtoa/BignumDtoaShortestVariousFloats<
test-bignum-dtoa/BignumDtoaGayShortest<
test-bignum-dtoa/BignumDtoaGayShortestSingle<
test-bignum-dtoa/BignumDtoaGayFixed<
test-bignum-dtoa/BignumDtoaGayPrecision<
test-conversions/DoubleToShortest<
test-conversions/DoubleToShortestSingle<
...
./cctest test-bignum
The test is passed if the following information is displayed:
Ran 13 tests.
Copy the double-conversion library to the OpenHarmony project.
Copy this library that can be cross-compiled to the third_party directory of OpenHarmony. To avoid modifying the BUILD.gn file in the directory of the third-party library to be ported, add a directory to store adaptation files, including BUILD.gn, build_thirdparty.py, and config.gni, for converting GN to CMake building.
Table 3 Directory structure of the ported library
Add the GN file to the CMake adaptation file.
import("config.gni")
group("double-conversion") {
if (ohos_build_thirdparty_migrated_from_fuchisa == true) {
deps = [":make"]
}
}
if (ohos_build_thirdparty_migrated_from_fuchisa == true) {
action("make") {
script = "//third_party/double-conversion/build_thirdparty.py"
outputs = ["$root_out_dir/log_dc.txt"]
exec_path = rebase_path(rebase_path("./double-conversion/build", root_build_dir))
command = "rm * .* -rf && $CMAKE_TOOLS_PATH/cmake .. $CMAKE_FLAG $CMAKE_TOOLCHAIN_FLAG && make -j"
args = [
"--path=$exec_path",
"--command=${command}"
]
}
}
#CMAKE_FLAG: config compile feature
CMAKE_FLAG = "-DBUILD_TESTING=ON -DCMAKE_CXX_STANDARD=11"
#toolchain: follow up-layer, depend on $ohos_build_compiler
if (ohos_build_compiler == "clang") {
CMAKE_TOOLCHAIN_FLAG = "-DOHOS_SYSROOT_PATH=${ohos_root_path}prebuilts/lite/sysroot/"
} else {
CMAKE_TOOLCHAIN_FLAG = ""
}
#CMake tools path,no need setting if this path already joined to $PATH.
CMAKE_TOOLS_PATH = "setting CMake tools path..."
import os
import sys
from subprocess import Popen
import argparse
import shlex
def cmd_exec(command):
cmd = shlex.split(command)
proc = Popen(cmd)
proc.wait()
ret_code = proc.returncode
if ret_code != 0:
raise Exception("{} failed, return code is {}".format(cmd, ret_code))
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--path', help='Build path.')
parser.add_argument('--command', help='Build command.')
parser.add_argument('--enable', help='enable python.', nargs='*')
args = parser.parse_args()
if args.enable:
if args.enable[0] == 'false':
return
if args.path:
curr_dir = os.getcwd()
os.chdir(args.path)
if args.command:
if '&&' in args.command:
command = args.command.split('&&')
for data in command:
cmd_exec(data)
else:
cmd_exec(args.command)
os.chdir(curr_dir)
if __name__ == '__main__':
sys.exit(main())
For example, add the following configuration to the //build/lite/ohos_var.gni file:
declare_args() {
ohos_build_thirdparty_migrated_from_fuchisa = false
}
Add the library to the compiling configuration.
Add the directory where the library is located to the project compiling configuration. Taking Hi3518EV300 for example, modify the openHarmony\build\lite\product\ ipcamera_hi3518ev300.json file and add the following configuration to subsystem, as shown in Figure 2.
{
"name": "double-conversion",
"component": [
{ "name": "double-conversion", "dir": "//third_party/double-conversion:double-conversion", "features":[] }
]
},
Build the library.
Execute the following command:
python build.py ipcamera_hi3518ev300 -T //third_party/double-conversion:double-conversion
If the compilation is successful, a static library file and test cases will be generated in the build directory.
Modify the compiling configuration in the //build/lite/ohos_var.gni file.
ohos_build_thirdparty_migrated_from_fuchisa = true
Execute the following command:
python build.py ipcamera_hi3518ev300 -b debug
If the compilation is successful, a static library file and test cases will be generated in the build directory.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。