We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
Best PracticesCompilation and BuildUsing the lycium Tool to Quickly Compile Third-Party Libraries

Using the lycium Tool to Quickly Compile Third-Party Libraries

Overview

With the continuous advancement of technologies, the requirements for mobile app development are escalating. In conventional mobile app development, relatively complex configuration and environment setup are required, which inflates development costs. To solve this problem, the lycium tool can be used to help you simplify the development process and reduce the development time.

lycium is a compilation framework tool that helps you quickly cross-compile C/C++ third-party libraries using the shell language and quickly verify the compilation on HarmonyOS.

You only need to set the compilation mode and parameters for the corresponding C/C++ third-party library, and then use lycium to quickly compile binary files that can run on HarmonyOS.

This section uses OpenSSL as an example to describe how to use the lycium tool to quickly compile a third-party library.

Using the lycium Tool to Quickly Compile Third-Party Libraries

This section describes how to use the lycium tool to quickly compile the OpenSSL third-party library source code using the OpenHarmony SDK in the Linux environment.

Setting Up the Compilation Environment

  1. For details about how to set up the Linux build environment and download the HarmonyOS SDK, see Setting Up the Environment.
  2. After the SDK is downloaded, configure the SDK toolchain to the environment variables.
    1. lycium supports cross compilation of C/C++ third-party libraries. The SDK toolchain involves only tools in the native directory. Therefore, the OHOS_SDK path must be set to the parent directory of the native tool. To configure the SDK environment variables in the Linux environment, perform the following operation:
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. owner@ubuntu:/mnt/e$ export OHOS_SDK=/xxx/ohos-sdk/linux # Set the SDK path to your own SDK decompression directory.
    2. Copy the build tools.

      To simplify command configuration during development, several compilation commands are integrated for the Arm and AArch64 architectures and stored in the lycium/Buildtools directory. Before using the lycium tool, you need to copy the compilation commands to the corresponding directory of the SDK. The procedure is as follows:

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. owner@ubuntu:/mnt/e/tpc_c_cplusplus-master$ cd lycium/Buildtools #: Go to the tool package directory.
      2. owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium/Buildtools$ sha512sum -c SHA512SUM # Check whether the tool package is normal. If "toolchain.tar.gz: OK" is displayed, the tool package is normal. Otherwise, the tool package is abnormal and needs to be downloaded again.
      3. owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium/Buildtools$ tar -zxvf toolchain.tar.gz # Decompress and copy the build tools.
      4. owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium/Buildtools$ cp toolchain/* ${OHOS_SDK}/native/llvm/bin # Copy the command to the native/llvm/bin directory of the toolchain.
    3. If the CMake version in the SDK is too early (CMake 3.26 or later is recommended), upgrade the SDK.

Compiling the Third-Party Library

  1. Modify the compilation mode and parameters of the third-party library.

    The lycium framework provides the HPKBUILD file for you to build and configure the corresponding C/C++ third-party library.

    1. Create a folder in the thirdparty directory to store the third-party library file and HPKBUILD file.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/thirdparty$ mkdir openssl
    2. Copy the HPKBUILD template file to the new third-party library directory.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/thirdparty$ cp /xxx/tpc_c_cplusplus-master/lycium/template/HPKBUILD openssl
    3. Modify the configuration option in the HPKBUILD file based on the third-party library.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. pkgname=NAME # Database name (Required)
      2. pkgver=VERSION # Library version (Required)
      3. source="https://downloads.sourceforge.net/$pkgname/$pkgname-$pkgver.tar.gz" # Source code download link of the library (required)
      4. buildtools= # (Required) Build tools. Currently, the cmake, configure, and make tools are supported. Use a tool based on the compilation and building method of the third-party library.
      5. builddir= # (Required) Directory name after the source package is decompressed.
      6. # Omit some configuration options.
      7. # Prepare for the build, such as setting environment variables and creating build directories.
      8. prepare() {
      9. }
      10. # Run the build command.
      11. build() {
      12. }
      13. # Installation and packaging
      14. package() {
      15. }
      16. # Check
      17. check() {
      18. }
      19. # Clear the environment.
      20. cleanbuild() {
      21. }

      Each build script must define the corresponding variables and five functions based on the preceding rules. The variables are required and must be correctly set based on the library information. Otherwise, the build fails.

      The following is an example:

      The OpenSSL compilation and build mode is configure. For configure cross compilation, the host type and corresponding environment variables need to be configured. The framework integrates the environment variable setting API, which is encapsulated in envset.sh. Therefore, in addition to basic information, you also need to define a host variable and import the envset.sh file. The basic variable configuration is as follows:

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. pkgname=openssl # Library name
      2. pkgver=OpenSSL_1_1_1u # Version number of the library
      3. source="https://github.com/openssl/$pkgname/archive/refs/tags/$pkgver.zip" # Path of the source package of the library
      4. archs=("armeabi-v7a" "arm64-v8a") # Architecture information
      5. buildtools="configure" # The build mode is configure.
      6. builddir=$pkgname-${pkgver} # Name of the folder generated after the OpenSSL source package is decompressed.
      7. packagename=$builddir.zip # Package name
      8. source envset.sh # Execute envset.sh, the environment setup script, which (located at tpc_c_cplusplus/lycium/script) typically defines variables and functions required for the build process.
      9. host= # Define the host variable.

      Create a build directory in the prepare() function and configure the environment variables of the corresponding architecture.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. prepare() {
      2. mkdir -p $builddir/$ARCH-build
      3. if [ $ARCH == ${archs[0]} ] # Check if the environment variable $ARCH (representing the target CPU architecture) matches the first element of the archs array.
      4. then
      5. setarm32ENV
      6. host=linux-generic32
      7. elif [ $ARCH == ${archs[1]} ]
      8. then
      9. setarm64ENV
      10. host=linux-aarch64
      11. else
      12. echo "${ARCH} not support"
      13. return -1
      14. fi
      15. }

      The build() function uses the configure command to generate Makefile and execute the make command.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. build() {
      2. cd $builddir/$ARCH-build
      3. ../Configure $* $host > `pwd`/build.log 2>&1
      4. make -j4 >> `pwd`/build.log 2>&1
      5. ret=$?
      6. cd $OLDPWD
      7. return $ret
      8. }

      During the OpenSSL test, test cases need to be generated by compiling the target depend. Therefore, the corresponding check() function needs to be modified. Run the make depend command in the check() function, clear the corresponding environment variables after the execution, and use comments to describe the test method of the library on the device.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. check() {
      2. cd $builddir/$ARCH-build
      3. make depend >> `pwd`/build.log 2>&1
      4. cd $OLDPWD
      5. if [ $ARCH == ${archs[0]} ]
      6. then
      7. unsetarm32ENV
      8. fi
      9. if [ $ARCH == ${archs[1]} ]
      10. then
      11. unsetarm64ENV
      12. fi
      13. unset host
      14. echo "Test must be on HarmonyOS device!"
      15. # real test CMD
      16. # Add the compilation directory to the LD_LIBRARY_PATH environment variable.
      17. # make test
      18. }

      For the package() and cleanbuild() functions, use the default settings in the template.

  2. Compile the third-party library.

    After the compilation mode parameters of the third-party library are configured, run the ./build.sh openssl command (openssl is the name of the created directory) in the lycium directory to automatically compile the third-party library, package the library, and install it in the usr/pkgname/ARCH directory of the current directory (pkgname indicates the third-party library name, and ARCH indicates the architecture name).

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium$ ./build.sh openssl # Compile the libraries in the thirdparty directory by default.
    2. Build OS linux
    3. OHOS_SDK=/mnt/e/ohos-sdk/linux
    4. CLANG_VERSION=15.0.4
    5. Build openssl OpenSSL_1_1_1u start!
    6. % Total % Received % Xferd Average Speed Time Time Time Current
    7. Dload Upload Total Spent Left Speed
    8. 100 222 0 222 0 0 457 0 --:--:-- --:--:-- --:--:-- 456
    9. 100 11.3M 0 11.3M 0 0 958k 0 --:--:-- 0:00:12 --:--:-- 1802k
    10. Compile HarmonyOS armeabi-v7a openssl OpenSSL_1_1_1u libs...
    11. # Omit some compilation information.
    12. ALL JOBS DONE!!!

    If no error is reported and the log prints "ALL JOBS DONE!!!", the third-party library is compiled successfully.

  3. View the compiled third-party library file.

    After the compilation is successful, go to the lycium/usr directory to view the generated file.

Third-Party Libraries Integrated in Applications

  1. Copy the binary file generated by the third-party library to the app project directory.

    To better manage the third-party libraries integrated into your app, create a thirdparty directory in the cpp directory of your app project and copy the generated binary files and header files to this directory.

    As shown in the following figure, xxx indicates the name of the third-party library. The xxx folder contains binary files generated in the AArch64 and Arm architectures. Each architecture directory contains the header file directory include and binary file directory lib of the library.

    If the binary file of the third-party library is a .so file, copy the .so file to the entry/libs/${OHOS_ARCH}/ directory in the project directory, as shown in the following figure.

    Precautions for referencing dynamic libraries:

    1. When referencing a dynamic library, an app searches for the dynamic library based on the soname, therefore, you need to copy the library file named soname to the entry/libs/${OHOS_ARCH}/ directory. To view the soname, run the ${OHOS_SDK}/native/llvm/bin/llvm-readelf -d libxxx.so command.

    2. Copy the .so file correctly.

      Copy method: Copy the .so file to the Windows OS without compression, or compress the .so file into a .zip file and copy it to the Windows OS. After the .so file is copied correctly, the size of the .so file should be the same as that of the entity file in the original library.

      NOTE

      If the .so file is copied to the Windows OS in .tar, .gz, .7z, or .bzip2 format and then decompressed, the file is a soft link of the entity library. The size of the file is different from that of the entity library. As a result, the file cannot be used.

  2. Configure links.

    To configure links, you only need to add the corresponding target_link_libraries to the CMakeLists.txt file in the cpp directory. (You only need to enter one of the dynamic library link and static library link.)

    • Configure the static library link.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. target_link_libraries(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/xxx/${OHOS_ARCH}/lib/libxxx.a)
    • Configure the dynamic library link.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. target_link_libraries(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/xxx/${OHOS_ARCH}/lib/libxxx.so)

  3. Configure the header file paths.

    Add target_include_directories to the CMakeLists.txt file in the cpp directory.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. target_include_directories(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/xxx/${OHOS_ARCH}/include)

  4. After the link and header file path of the third-party library are configured, you can call the third-party library APIs in your app based on the service logic. For details, see Integrating a Third-Party Dynamic Link Library (.so).
Search in Best Practices
Enter a keyword.