Skip to content

OpenHarmony (musl) compatibility: sched_setaffinity, _GNU_SOURCE for OHOS, no addchdir - #29156

Draft
momo0410 wants to merge 1 commit into
ggml-org:masterfrom
momo0410:openharmony-port
Draft

momo0410 wants to merge 1 commit into
ggml-org:masterfrom
momo0410:openharmony-port

Conversation

@momo0410

Copy link
Copy Markdown

Motivation

llama.cpp currently fails to build for OpenHarmony (the open-source HarmonyOS), whose standard-system userland is musl + LLVM with CMAKE_SYSTEM_NAME=OHOS (targets x86_64-linux-ohos / aarch64-linux-ohos). Three small libc/toolchain assumptions break the build:

  1. _GNU_SOURCE is only defined when the system name matches Linux/Android (ggml/src/CMakeLists.txt), so musl hides cpu_set_t / CPU_SET used by the thread-affinity code. CMAKE_SYSTEM_NAME=OHOS falls into the else() branch which only sets _XOPEN_SOURCE=600.
  2. The CPU-affinity code calls pthread_setaffinity_np / pthread_getaffinity_np (glibc extensions). OpenHarmony's musl does not provide the _np variants; POSIX sched_setaffinity(0, ...) (already used for __ANDROID__) works on musl.
  3. The vendored vendor/sheredom/subprocess.h calls posix_spawn_file_actions_addchdir_np; OpenHarmony's musl has neither spelling of addchdir. The header documents SUBPROCESS_HAVE_CWD=0 as the supported override for exactly this case.

Changes

  • top-level CMakeLists.txt: define _GNU_SOURCE and SUBPROCESS_HAVE_CWD=0 when CMAKE_SYSTEM_NAME STREQUAL "OHOS"
  • ggml/src/ggml-cpu/ggml-cpu.c: use sched_setaffinity when __MUSL__ is defined (set by the OHOS toolchain; also correct for other musl targets)
  • common/common.cpp: shim pthread_{get,set}affinity_np to sched_{get,set}affinity on musl

No behavior change for Linux/glibc, Android, Apple or Windows builds.

Testing

Verified on OpenHarmony 7.0.0.39 standard system (x86_64, VMware, kernel 6.6.101):

  • llama-cli -m stories260K.gguf: completion works, ~2426 t/s generation
  • llama-cli -m Qwen2.5-0.5B-Instruct-Q4_0.gguf --single-turn -p '用一句话介绍开源鸿蒙操作系统': correct Chinese chat output, ~29 t/s generation (4 threads, CPU backend)
  • llama-server --host 0.0.0.0 --port 8080: /health returns OK, OpenAI-compatible /v1/chat/completions verified from a remote client

Also cross-built clean for aarch64-linux-ohos (not yet run on ARM hardware). Build/deploy scripts and full verification log: https://github.com/momo0410/llama.cpp-openharmony-port

DCO: signed off.

- Define _GNU_SOURCE for CMAKE_SYSTEM_NAME=OHOS so musl exposes
  cpu_set_t / CPU_SET / M_PI and other GNU extensions used by ggml
- Use sched_setaffinity instead of pthread_setaffinity_np on musl
  (OpenHarmony musl does not provide the _np variants)
- Vendor subprocess.h: set SUBPROCESS_HAVE_CWD=0 on OHOS, which has no
  posix_spawn_file_actions_addchdir

Signed-off-by: momo0410 <[email protected]>
@momo0410
momo0410 requested review from a team and ggerganov as code owners September 19, 2026 20:18
@github-actions github-actions Bot added build Compilation issues ggml changes relating to the ggml tensor library for machine learning labels Sep 19, 2026
@ggml-gh-bot

ggml-gh-bot Bot commented Sep 19, 2026

Copy link
Copy Markdown

Hi @momo0410, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • PR Template not respected: Please respect the template when creating a new pull request. Make sure to fill out all required sections.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

@ggml-gh-bot ggml-gh-bot Bot added the draft PR will be changed to draft by github-actions bot label Sep 19, 2026
@github-actions
github-actions Bot marked this pull request as draft September 19, 2026 20:23
@github-actions github-actions Bot removed the draft PR will be changed to draft by github-actions bot label Sep 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build Compilation issues ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant