1 Star 0 Fork 4.9K

bill / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
porting-a-library-built-using-cmake.md 26.02 KB
一键复制 编辑 原始数据 按行查看 历史
NEEN 提交于 2021-03-12 17:59 . !197 Docs Update version 1.0.1

Porting a Library Built Using CMake

The following shows how to port the double-conversion library.

Source Code Acquisition

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

Directory

Description

double-conversion/cmake/

Template used for building with CMake

double-conversion/double-conversion/

Directory of source files

double-conversion/msvc/

-

double-conversion/test/

Source files of the test cases

double-conversion/.gitignore

-

double-conversion/AUTHORS

-

double-conversion/BUILD

-

double-conversion/CMakeLists.txt

Top-level file used for building with CMake

double-conversion/COPYING

-

double-conversion/Changelog

-

double-conversion/LICENSE

-

double-conversion/Makefile

-

double-conversion/README.md

-

double-conversion/SConstruct

-

double-conversion/WORKSPACE

-

Porting Guidelines

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.

Cross-Compilation

Compilation Reference

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.

Cross-Compilation Settings

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.

  1. 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})
  2. 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.

  3. View the result.

    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

    Directory

    Description

    double-conversion/build/libdouble-conversion.a

    Static library file

    double-conversion/build/test/

    Test cases and CMake cache files

    double-conversion/build/CMakeCache.txt

    Cache files during CMake building

    double-conversion/build/CMakeFiles/

    -

    double-conversion/build/cmake_install.cmake

    -

    double-conversion/build/CTestTestfile.cmake

    -

    double-conversion/build/DartConfiguration.tcl

    -

    double-conversion/build/generated/

    -

    double-conversion/build/Makefile

    -

    double-conversion/build/Testing/

    -

Library Test

  1. 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.

    Figure 1 Successful startup of OpenHarmony

  2. Mount the nfs directory and put the executable file cctest into the test directory (listed in Table 2) to the nfs directory.

  3. 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.

    • After the nfs directory is successfully mounted, run the following command to list all items in the test cases:
    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<
    ...
    • Run the following command to test test-bignum:
    ./cctest test-bignum

    The test is passed if the following information is displayed:

    Ran 13 tests.

Adding the Compiled double-conversion Library to the OpenHarmony Project

  1. 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

    Directory

    Description

    openHarmony/third_party/double-conversion/BUILD.gn

    GN file for adding the third-party library to the OpenHarmony project

    openHarmony/third_party/double-conversion/build_thirdpaty.py

    Script file for GN to call the shell command to convert compilation from GN to CMake.

    openHarmony/third_party/double-conversion/config.gni

    Third-party library compilation configuration file, which can be modified to determine whether the test cases will be used during the building

    openHarmony/third_party/double-conversion/double-conversion/

    Directory of the third-party library to be ported

  2. Add the GN file to the CMake adaptation file.

    • The following shows the implementation for building using the newly added BUILD.gn file. For other third-party libraries that can be independently compiled using CMake, you only need to change the target paths when porting them to OpenHarmony.
    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}"
            ]
        }
    }
    • The newly added config.gni file is used to configure the library in the following example implementation. For other third-party libraries that can be independently compiled using CMake, you only need to change the configuration of CMAKE_FLAG when porting them to OpenHarmony.
    #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..."
    • The following shows the implementation of the newly added build_thirdparty.py file. For other third-party libraries that can be independently compiled using CMake, you can port them to OpenHarmony without modifications.
    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())
    • Add a configuration item in the configuration file to control compiling of the library. By default, library compilation is disabled.

    For example, add the following configuration to the //build/lite/ohos_var.gni file:

    declare_args() {
        ohos_build_thirdparty_migrated_from_fuchisa = false
    }
  3. 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":[] }
      ]
    },

    Figure 2 Position for adding the configuration

  4. Build the library.

    • Manual building

    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.

    • Automatic building with the project

    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.

1
https://gitee.com/ximeibaba/docs.git
git@gitee.com:ximeibaba/docs.git
ximeibaba
docs
docs
master

搜索帮助