porting-thirdparty-cmake.md 12.6 KB
Newer Older
E
ester.zhou 已提交
1 2
# Porting a Library Built Using CMake

Y
yangni 已提交
3 4 5

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

E
ester.zhou 已提交
6
## Source Code Acquisition
Y
yangni 已提交
7

E
ester.zhou 已提交
8
Acquire the source code of double-conversion from [double-conversion](https://github.com/google/double-conversion). The following table lists the directory structure.
Y
yangni 已提交
9

E
ester.zhou 已提交
10
**Table 1** Directory structure of the source code
Y
yangni 已提交
11

E
ester.zhou 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| 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
Y
yangni 已提交
31 32 33

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.

E
ester.zhou 已提交
34
## Cross-Compilation
Y
yangni 已提交
35

E
ester.zhou 已提交
36
### Compilation Reference
Y
yangni 已提交
37

E
ester.zhou 已提交
38
The [README.md](https://github.com/google/double-conversion/blob/master/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.
Y
yangni 已提交
39

E
ester.zhou 已提交
40
### Cross-Compilation Settings
Y
yangni 已提交
41 42 43 44 45

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.

E
ester.zhou 已提交
46
    Add configuration of the clang toolchains to the top-level file **CMakeLists.txt** listed in Table 1.
Y
yangni 已提交
47 48

    ```
N
NEEN 已提交
49 50 51 52
    set(CMAKE_CROSSCOMPILING TRUE)
    set(CMAKE_SYSTEM_NAME Generic)
    set(CMAKE_CXX_COMPILER_ID Clang)
    set(CMAKE_TOOLCHAIN_PREFIX llvm-)
Y
yangni 已提交
53
    # 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.
N
NEEN 已提交
54 55
    set(CMAKE_C_COMPILER clang)
    set(CMAKE_C_FLAGS "--target=arm-liteos -D__clang__ -march=armv7-a -w")
Y
yangni 已提交
56
    # 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.
N
NEEN 已提交
57 58
    set(CMAKE_CXX_COMPILER clang++) 
    set(CMAKE_CXX_FLAGS "--target=arm-liteos -D__clang__ -march=armv7-a -w")
S
sfohos 已提交
59
    # Specify the linker and its flags. --target and --sysroot must be specified. You can specify OHOS_SYSROOT_PATH via the suffix parameter of the cmake command.
N
NEEN 已提交
60 61 62 63
    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)
D
duangavin123 已提交
64 65 66 67
    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>")
Y
yangni 已提交
68
    # Specify the path for searching chained libraries.
N
NEEN 已提交
69
    set(CMAKE_SYSROOT ${OHOS_SYSROOT_PATH})
Y
yangni 已提交
70 71 72 73
    ```

2.  Perform the compilation.

E
ester.zhou 已提交
74 75 76 77 78 79 80
    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
   ```
Y
yangni 已提交
81

E
ester.zhou 已提交
82
   **OHOS\_SYSROOT\_PATH** specifies the absolute path where **sysroot** is located. For OpenHarmony, set **OHOS\_SYSROOT\_PATH** to the absolute path of the **out/hispark\__xxx_/ipcamera\_hispark\__xxx_/sysroot** directory. This directory is generated after full compilation is complete. Therefore, complete full compilation before porting.
Y
yangni 已提交
83

E
ester.zhou 已提交
84
3.  View the result.
E
ester.zhou 已提交
85
    
E
ester.zhou 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    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/ | - |
E
ester.zhou 已提交
102 103 104 105



## Library Test
Y
yangni 已提交
106

E
ester.zhou 已提交
107
1. Set up the OpenHarmony environment.
Y
yangni 已提交
108

E
ester.zhou 已提交
109
   Using Hi3516D V300 as an example, compile the OpenHarmony image and burn it to the development board. For details, see the content related to the small system in [Quick Start Overview](../quick-start/quickstart-overview.md).
Y
yangni 已提交
110

E
ester.zhou 已提交
111
   The following screen is displayed after a successful login to the OS.
Y
yangni 已提交
112

E
ester.zhou 已提交
113
   **Figure 1** Successful startup of OpenHarmony
Y
yangni 已提交
114

E
ester.zhou 已提交
115
   ![](figures/successful-startup-of-openharmony.png "successful-startup-of-openharmony")
Y
yangni 已提交
116

E
ester.zhou 已提交
117
2. Mount the **nfs** directory and put the executable file **cctest** into the **test** directory \(listed in Table 2) to the **nfs** directory.
Y
yangni 已提交
118

E
ester.zhou 已提交
119
3.  Perform the test cases.
Y
yangni 已提交
120

E
ester.zhou 已提交
121
    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.
Y
yangni 已提交
122

E
ester.zhou 已提交
123
    - After the **nfs** directory is successfully mounted, run the following command to list all items in the test cases:
E
ester.zhou 已提交
124
    
E
ester.zhou 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
       ```
       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**:
Y
yangni 已提交
159

E
ester.zhou 已提交
160
    
E
ester.zhou 已提交
161 162 163
          ```
          ./cctest test-bignum
          ```
Y
yangni 已提交
164

E
ester.zhou 已提交
165
       The test is passed if the following information is displayed:
Y
yangni 已提交
166

E
ester.zhou 已提交
167
    
E
ester.zhou 已提交
168 169 170
          ```
          Ran 13 tests.
          ```
Y
yangni 已提交
171 172


E
ester.zhou 已提交
173
## Adding the Compiled double-conversion Library to the OpenHarmony Project
Y
yangni 已提交
174 175 176

1.  Copy the double-conversion library to the OpenHarmony project.

E
ester.zhou 已提交
177
    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.
Y
yangni 已提交
178

E
ester.zhou 已提交
179
    **Table 3** Directory structure of the ported library
Y
yangni 已提交
180

E
ester.zhou 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
   | 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_thirdparty.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("./build",   ohos_third_party_dir))
             command = "rm * .* -rf && $CMAKE_TOOLS_PATH/cmake ..   $CMAKE_FLAG $CMAKE_TOOLCHAIN_FLAG && make -j"
             args = [
                 "--path=$exec_path",
                 "--command=${command}"
             ]
         }
     }
     ```

E
ester.zhou 已提交
213
   -   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.
E
ester.zhou 已提交
214 215 216 217

     ```
     #CMAKE_FLAG: config compile feature
     CMAKE_FLAG = "-DBUILD_TESTING=ON -DCMAKE_CXX_STANDARD=11"
E
ester.zhou 已提交
218 219
   
     #toolchain: follow up-layer,depend on $ohos_build_compiler
E
ester.zhou 已提交
220
     if (ohos_build_compiler == "clang") {
E
ester.zhou 已提交
221
         CMAKE_TOOLCHAIN_FLAG = "-DOHOS_SYSROOT_PATH=${root_out_dir}sysroot"
E
ester.zhou 已提交
222 223 224 225 226 227 228 229
     } else {
         CMAKE_TOOLCHAIN_FLAG = ""
     }

     #CMake tools path,no need setting if this path already joined to $PATH.
     CMAKE_TOOLS_PATH = "setting CMake tools path..."
     ```

E
ester.zhou 已提交
230
   - 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.
E
ester.zhou 已提交
231 232

     
E
ester.zhou 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
     ```
     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())
     ```
E
ester.zhou 已提交
274 275 276
   
   - Add a configuration item in the configuration file to control compiling of the library. By default, library compilation is disabled.
   
E
ester.zhou 已提交
277
     For example, add the following configuration to the **//build/lite/ohos\_var.gni** file:
E
ester.zhou 已提交
278 279 280 281 282 283 284 285 286 287 288 289
   
     ```
     declare_args() {
         ohos_build_thirdparty_migrated_from_fuchisa = true
      }
     ```

3. Build the library.

   Execute the following command:

   ```
E
ester.zhou 已提交
290
   hb build -T //third_party/double-conversion:double-conversion
E
ester.zhou 已提交
291 292
   ```
   
E
ester.zhou 已提交
293
   If the compilation is successful, a static library file and test cases will be generated in the **build** directory.
Y
yangni 已提交
294

E
ester.zhou 已提交
295
 <!--no_check-->