How to Port pngquant to HarmonyOS/OpenHarmony with Lycium: HPKBUILD, Cross-Compilation, and Dependency Resolution

This article explains how to port pngquant 2.18.0 to the HarmonyOS/OpenHarmony PC platform with Lycium. The core challenge is that its handwritten configure script is not cross-dependency friendly, so you must explicitly export the pkg-config path and inject header and library paths. Keywords: Lycium, pngquant, OpenHarmony.

Technical Specifications Snapshot

Parameter Description
Project pngquant 2.18.0
Target Platform HarmonyOS / OpenHarmony PC
Primary Languages C, Shell
Build Method Handwritten configure + make
License GPL-3.0-or-later
Core Dependencies libpng, zlib, lcms2, libimagequant
Target Architectures armeabi-v7a, arm64-v8a
Key Framework Lycium / tpc_c_cplusplus
Upstream Repository github.com/kornelski/pngquant
Community Interest The original article had about 190 views, 7 likes, and 6 bookmarks on CSDN

pngquant Has Clear Value in HarmonyOS PC Scenarios

pngquant is a classic lossy PNG compression tool that fits asset size reduction, static resource preprocessing, and build pipeline optimization. For OpenHarmony, it is not a GUI component. It is a command-line tool that you can deploy directly.

Its porting value is not about whether it can compile once, but whether it can integrate reliably into Lycium’s dependency orchestration model. The real issues center on configure detection, pkg-config scope, and cross-toolchain variable propagation.

pngquant porting diagram AI Visual Insight: The image is a placeholder illustration from the original CSDN article. Its main purpose is to introduce the pngquant porting workflow, usually in the context of cross-compilation, dependency adaptation, and script organization, rather than any runtime UI.

pngquant Is Not a Standard Autoconf Project, and You Must Confirm This First

Many third-party libraries in Lycium can reuse common GNU Autoconf practices, but the configure file in the pngquant 2.x root directory is a handwritten Bash script. It supports options such as --with-libpng, --extra-cflags, --extra-ldflags, and --disable-sse, but its detection logic depends much more heavily on explicit arguments.

This means that if you only set buildtools to configure without completing the dependency paths, the script can easily pick up libraries under the host machine’s /usr, or fail directly with libpng not found.

# First check which options the bundled pngquant configure script supports
./configure --help  # Confirm support for key options such as with-libpng and extra-cflags

This command helps you quickly verify that pngquant uses a handwritten configure entry point rather than a script generated by standard autotools.

Dependency Discovery in Lycium Must Be Explicitly Exported to Child Processes

Lycium assembles the dependency pkgconfigpath according to depends, but this only exists as a shell variable in the current process. If you do not export PKG_CONFIG_LIBDIR, the pkg-config child process invoked inside configure does not know where to find the cross-compiled libpng, zlib, and lcms2 packages.

This is the single most critical fact in this port: the issue is not whether the variable has a value, but whether child processes can inherit it. Many failed builds stop here.

zroot=$LYCIUM_ROOT/usr/zlib/$ARCH
pngroot=$LYCIUM_ROOT/usr/libpng/$ARCH
lcmsroot=$LYCIUM_ROOT/usr/lcms2/$ARCH

export PKG_CONFIG_LIBDIR="${pkgconfigpath}"  # Make the cross-dependency pkg-config path visible to configure child processes

This configuration passes the dependency information resolved by Lycium to the pngquant configure stage where it is actually needed.

pkg-config Alone Is Not Enough, and You Must Also Add -I and -L

pngquant’s lookup logic also scans default system directories, so in cross-compilation scenarios you should still pass --extra-cflags and --extra-ldflags explicitly. This ensures that header and library paths remain available even if the pkg-config metadata is incomplete.

This matters especially because libpng may use a layout such as include/libpng16, while lcms2 may provide lib64. For that reason, it is best to use [ -d ... ] checks before appending paths, so you do not pollute the compiler arguments.

extra_cflags="-Wno-unused-command-line-argument"
[ -d "${pngroot}/include" ] && extra_cflags="$extra_cflags -I${pngroot}/include"  # Add the libpng header path
[ -d "${pngroot}/include/libpng16" ] && extra_cflags="$extra_cflags -I${pngroot}/include/libpng16"  # Support the libpng16 directory layout
[ -d "${zroot}/include" ] && extra_cflags="$extra_cflags -I${zroot}/include"
[ -d "${lcmsroot}/include" ] && extra_cflags="$extra_cflags -I${lcmsroot}/include"

This script provides configure with stable, cross-platform header search paths.

HPKBUILD Organizes Source Retrieval, Environment Setup, and the Installation Prefix

In HPKBUILD, depends=("libpng" "zlib" "lcms2") tells Lycium to build the dependency libraries first. By contrast, makedepends should only include executable commands available on the host system, not library names. You must fetch the source with git clone -b $pkgver --recursive, or the libimagequant submodule will be missing.

At the same time, buildtools="configure" in Lycium routes the build through the configuredependpath branch and passes `–prefix=$LYCIUM_ROOT/usr/pngquant/

/` into the script. Do not omit `”$@”`. “`bash pkgname=pngquant pkgver=2.18.0 depends=(“libpng” “zlib” “lcms2″) # Declare cross dependencies so Lycium builds them first buildtools=”configure” cloneFlag=true “` This metadata defines the smallest buildable unit for pngquant in the Lycium ecosystem. ### The Standard build() Invocation Is Already Well Established The actual build entry point should stay simple: pass Lycium’s prefix, specify `–with-libpng`, enable `–with-lcms2`, and disable SSE detection, which is meaningless in ARM scenarios. The top-level `Makefile` will then build libimagequant under `lib/` automatically. “`bash ./configure “$@” \ –with-libpng=”$pngroot” \ –with-lcms2 \ –disable-sse \ –extra-cflags=”$extra_cflags” \ –extra-ldflags=”$extra_ldflags” \ CC=”$CC” AR=”$AR” CFLAGS=”$CFLAGS” LDFLAGS=”$LDFLAGS” # Align with the OHOS toolchain variables make -j$(nproc) # Build pngquant and libimagequant in parallel make install # Install to the prefix specified by Lycium “` This command sequence captures the recommended pngquant build path for HarmonyOS/OpenHarmony. ## The Recommended Build Order Should Complete Dependencies Before the Target Library If this is your first build in a Lycium repository, use the order `zlib -> libpng -> lcms2 -> pngquant`. If the dependencies already exist under `lycium/usr/…`, you can build pngquant alone. “`shell cd lycium ./build.sh zlib libpng lcms2 pngquant # Build in dependency order during the first run # or ./build.sh pngquant # Build only the target package when dependencies are already available “` Use these commands to let Lycium build the dependency chain and install the final artifacts. ### The Output Location and Integration Model Both Favor Command-Line Toolchains After a successful build, you can usually find the executable at `lycium/usr/pngquant/ /bin/pngquant`. It is a good fit for distribution with system images, delivery to devices through hdc, or invocation as part of an upper-layer asset compression pipeline. If you want to package it into a HAP, you usually also need to handle ABI alignment, dependent `.so` packaging paths, and sandbox permissions. That is significantly more complex than deploying it as a standalone command-line tool. ## You Can Troubleshoot Common Failures Quickly with Three Rules First, check whether `PKG_CONFIG_LIBDIR` was exported. Second, verify that `pngroot`, `zroot`, and `lcmsroot` match the real installation directories. Third, confirm that the source was cloned with `–recursive`. If you see SSE- or OpenMP-related errors, keep `–disable-sse` first. Do not enable extra acceleration until you have verified toolchain support. ## FAQ ### Q1: Why does configure still report `libpng not found` even though I set `pkgconfigpath`? A: Because assigning a variable is not the same as making it visible to child processes. You must run `export PKG_CONFIG_LIBDIR=”${pkgconfigpath}”`, and also provide `–with-libpng`, `–extra-cflags`, and `–extra-ldflags`. ### Q2: Why should `buildtools` be set to `configure` for pngquant instead of `cmake`? A: Because pngquant 2.x uses a handwritten `configure` script with `make`. It is not a CMake project. In Lycium, setting it to `configure` routes the build through the correct parameter-passing branch. ### Q3: Can I put libraries such as zlib or libpng in `makedepends`? A: No. Lycium validates `makedepends` against executable commands available on the host. Library dependencies belong in `depends`, where the framework can build them first and assemble the correct paths. ## The Reference Information Is Sufficient for Reproduction The upstream project is `github.com/kornelski/pngquant`, and the porting framework can be studied through `tpc_c_cplusplus` and `lycium_plusplus`. If you want a reusable implementation, you can also review the already adapted `oh-tpc/pngquant` repository. Core summary: This article systematically explains how to cross-compile and install pngquant 2.18.0 for HarmonyOS/OpenHarmony PC with Lycium, focusing on the handwritten configure script, `PKG_CONFIG_LIBDIR` export, explicit dependency path injection, and HPKBUILD orchestration.