Integrating the yxml Library into OpenHarmony via Makefile Build System

Source Acquisition

Obtain the source code for the yxml library from the project repository. The primary directory consists of the core source files, benchmarking utilities, and the original build scripts. The key files for integration are the main implementation file (yxml.c), the header file (yxml.h), and the build definition file (Makefile).

Toolchain Configuration

To compile the library for the OpenHarmony environment, the Makefile must be adapted to use the LLVM/Clang toolchain. Locate the compiler and linker variable definitions in the root Makefile and replace them with the following configuration targeting the ARM-LiteOS architecture.

# Configure the cross-compilation toolchain
# Ensure the toolchain binaries are accessible via the system PATH
CC := clang
AR := llvm-ar

# Compilation flags must include specific target and sysroot options
TARGET_FLAGS := --target=arm-liteos -march=armv7-a -mcpu=cortex-a7 \
                -mfloat-abi=softfp -mfpu=neon-vfpv4

SYSROOT_PATH := $(OHOS_SYSROOT)

CFLAGS := -Wall -Wextra -Wno-unused-parameter -O2 -g \
           $(TARGET_FLAGS) --sysroot=$(SYSROOT_PATH)

Compilation Execution

Navigate to the root directory of the yxml source code in the terminal. Execute the build command while specifying the absolute path to the OpenHarmony sysroot directory. This path typically corresponds to out/hispark_xxx/ipcamera_hispark_xxx/sysroot within the OpenHarmony source tree.

make test OHOS_SYSROOT=/path/to/openharmony/sysroot

Upon successful compilation, an out directory will be generated. It contains the static library artifact within the lib subdirectory and the compiled test binaries in the test subdirectory.

Functional Verification

Automated shell scripts are typically not supported in the OpenHarmony shell environment, particularly those relying on IO redirection operators. Therefore, manual verification of the test cases is required.

1. Initiate the Test Binary: Run the executable generated in the test directory.

./test

2. Provide Input Data: Copy the content of an XML test file directly into the terminal prompt. For example, using a processing instruction test case:

<?SomePI sample_data?><root/>

3. Verify Output: Compare the terminal output against the expected result file (e.g., *.out).

Expected output for the input above:

pistart SomePI
picontent sample_data
piend
elemstart root
elemend
ok

OpenHarmony Build Integration

To incorporate the library into the OpenHarmony build system, specific configuration files (BUILD.gn and config.gni) must be created alongside a helper script to bridge GN and Makefile build processes.

1. BUILD.gn Configuration

Create a BUILD.gn file to define the build targets. This configuration invokes a wrapper script to execute the Makefile commands.

import("//build/config/ohos/ohos.gni")
import("config.gni")

group("yxml_lib") {
  deps = [ ":yxml_make" ]
}

action("yxml_make") {
  script = "//third_party/yxml/make_wrapper.py"
  
  # Define outputs to track build status
  outputs = [ "$target_out_dir/yxml_build_log.txt" ]
  
  # Calculate relative path for execution
  src_dir = rebase_path("./", root_build_dir)
  
  # Construct the command string
  build_cmd = "make clean && ${MAKE_EXEC_CMD}"
  
  args = [
    "--src-path=${src_dir}",
    "--build-cmd=${build_cmd}",
  ]
}

2. config.gni Configuration

Define the build parameters in config.gni. This allows conditional compilation of test cases based on configuration flags.

# Enable or disable unit test compilation
ENABLE_UNIT_TESTS = "true"

if (ENABLE_UNIT_TESTS == "true") {
  # Compile with test targets enabled
  MAKE_EXEC_CMD = "make test OHOS_SYSROOT=${root_out_dir}sysroot/"
} else {
  # Standard library compilation only
  MAKE_EXEC_CMD = "make OHOS_SYSROOT=${root_out_dir}sysroot/"
}

3. Directory Structure

After integration, the directory structure should align with the OpenHarmony third-party layout:

  • OpenHarmony/third_party/yxml/BUILD.gn: The GN build definition file.
  • OpenHarmony/third_party/yxml/config.gni: Configuration options for the build process.
  • OpenHarmony/third_party/yxml/make_wrapper.py: Python script responsible for invoking shell commands via GN.
  • OpenHarmony/third_party/yxml/yxml/: The directory containing the original third-party source code.

Tags: OpenHarmony Makefile Porting cross-compilation GN

Posted on Sun, 13 Sep 2026 16:39:47 +0000 by keyurshah