[llvm] [CMake] Special-case HarmonyOS hosts in get_host_triple() (PR #219882)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 30 22:26:08 PDT 2026


https://github.com/abhinav-phi created https://github.com/llvm/llvm-project/pull/219882

Fixes #219653

`config.guess` cannot identify HarmonyOS: `uname -s` reports `HarmonyOS`, which `config.guess` does not recognize, and its probing is rejected by the system sandbox, so configuring LLVM on HarmonyOS fails hard even with `TMPDIR` pointed at a safe location (reproducer in the issue). The current workaround is to hard-code the host triple.

Handle HarmonyOS/OpenHarmony/OHOS hosts in `get_host_triple()` the same way Windows, MinGW, z/OS and AIX are already handled: derive the triple from CMake's host detection in the form clang targets on OHOS, e.g. `aarch64-unknown-linux-ohos` (matching `clang -dumpmachine`), covering the architectures the OHOS toolchain supports (AArch64, ARM, x86_64, RISCV64, LoongArch64).

One subtlety worth noting: on HarmonyOS, `uname -p` succeeds while printing `unknown`, and CMake only retries with `uname -m` when `uname -p` exits non-zero (Modules/CMakeDetermineSystem.cmake), so `CMAKE_HOST_SYSTEM_PROCESSOR` ends up as the literal string `unknown` on real hardware. In that case the module falls back to invoking `uname -m` directly.

Testing: added `llvm/test/CMake/get-host-triple.test`, which drives the module in script mode with the CMake host detection variables simulated (via `llvm/test/CMake/Inputs/get-host-triple.cmake`), covering every architecture and the `OpenHarmony`/`OHOS` spellings of the host system name. The fallback and error paths were verified manually with local cmake runs.


>From 1f3ea13134a84be3d247afcb7f0cd1007676507d Mon Sep 17 00:00:00 2001
From: abhinav-phi <alpha9coder at gmail.com>
Date: Mon, 31 Aug 2026 04:30:39 +0530
Subject: [PATCH] [CMake] Special-case HarmonyOS hosts in get_host_triple()

config.guess cannot identify HarmonyOS: uname -s reports "HarmonyOS",
which config.guess does not recognize, and its probing is rejected by
the system sandbox, so configuring LLVM on HarmonyOS fails hard.

Handle HarmonyOS/OpenHarmony/OHOS hosts in get_host_triple() the same
way Windows, MinGW, z/OS and AIX are already handled: derive the triple
from CMake's host detection in the form clang targets on OHOS, e.g.
aarch64-unknown-linux-ohos. CMake may report the host processor as
"unknown" because uname -p does, in which case fall back to asking
uname -m directly.

Add a lit test driving the module in script mode with the CMake host
detection variables simulated.

Fixes #219653
---
 llvm/cmake/modules/GetHostTriple.cmake       | 40 +++++++++++++++++++-
 llvm/test/CMake/Inputs/get-host-triple.cmake | 25 ++++++++++++
 llvm/test/CMake/get-host-triple.test         | 28 ++++++++++++++
 llvm/test/CMake/lit.local.cfg                |  1 +
 llvm/test/lit.site.cfg.py.in                 |  1 +
 5 files changed, 94 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/CMake/Inputs/get-host-triple.cmake
 create mode 100644 llvm/test/CMake/get-host-triple.test
 create mode 100644 llvm/test/CMake/lit.local.cfg

diff --git a/llvm/cmake/modules/GetHostTriple.cmake b/llvm/cmake/modules/GetHostTriple.cmake
index cbecd85df5e43..120f2de36bb75 100644
--- a/llvm/cmake/modules/GetHostTriple.cmake
+++ b/llvm/cmake/modules/GetHostTriple.cmake
@@ -1,5 +1,5 @@
 # Returns the host triple.
-# Invokes config.guess
+# Invokes config.guess, except for platforms it cannot identify.
 
 function( get_host_triple var )
   if( MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
@@ -41,6 +41,44 @@ function( get_host_triple var )
     else()
       set( value "powerpc-ibm-aix" )
     endif()
+  elseif( CMAKE_HOST_SYSTEM_NAME MATCHES "HarmonyOS|OpenHarmony|OHOS" )
+    # config.guess cannot identify HarmonyOS: uname -s reports "HarmonyOS",
+    # which config.guess does not recognize, and its probing is rejected by
+    # the system sandbox. Build the triple from CMake's host detection using
+    # the form clang targets on OHOS (e.g. aarch64-unknown-linux-ohos). CMake
+    # itself may report the host processor as "unknown" because uname -p does,
+    # so fall back to asking uname -m directly.
+    set( TT_MACHINE "${CMAKE_HOST_SYSTEM_PROCESSOR}" )
+    if( NOT TT_MACHINE OR TT_MACHINE STREQUAL "unknown" )
+      find_program( TT_UNAME uname PATHS /bin /usr/bin /usr/local/bin
+        NO_CMAKE_FIND_ROOT_PATH )
+      set( TT_RV 1 )
+      if( TT_UNAME )
+        execute_process( COMMAND ${TT_UNAME} -m
+          RESULT_VARIABLE TT_RV
+          OUTPUT_VARIABLE TT_MACHINE
+          OUTPUT_STRIP_TRAILING_WHITESPACE
+          ERROR_QUIET )
+      endif()
+      if( NOT TT_RV EQUAL 0 )
+        set( TT_MACHINE "" )
+      endif()
+    endif()
+    if( TT_MACHINE MATCHES "aarch64|arm64" )
+      set( value "aarch64-unknown-linux-ohos" )
+    elseif( TT_MACHINE MATCHES "^arm" )
+      set( value "arm-unknown-linux-ohos" )
+    elseif( TT_MACHINE MATCHES "^x86_64" )
+      set( value "x86_64-unknown-linux-ohos" )
+    elseif( TT_MACHINE MATCHES "^riscv64" )
+      set( value "riscv64-unknown-linux-ohos" )
+    elseif( TT_MACHINE MATCHES "^loongarch64" )
+      set( value "loongarch64-unknown-linux-ohos" )
+    else()
+      message( FATAL_ERROR
+        "Failed to determine host triple for ${CMAKE_HOST_SYSTEM_NAME}: "
+        "host processor \"${TT_MACHINE}\" is not supported" )
+    endif()
   else()
     if(CMAKE_HOST_SYSTEM_NAME STREQUAL Windows AND NOT MSYS)
       message(WARNING "unable to determine host target triple")
diff --git a/llvm/test/CMake/Inputs/get-host-triple.cmake b/llvm/test/CMake/Inputs/get-host-triple.cmake
new file mode 100644
index 0000000000000..a6156feaaf1e4
--- /dev/null
+++ b/llvm/test/CMake/Inputs/get-host-triple.cmake
@@ -0,0 +1,25 @@
+# Driver for testing llvm/cmake/modules/GetHostTriple.cmake. It simulates a
+# host platform by setting the variables that CMake's platform detection
+# normally provides, then reports the triple computed by get_host_triple().
+#
+# Usage:
+#   cmake -DSRC_ROOT=<llvm source root>
+#         -DHOST_SYSTEM_NAME=<simulated CMAKE_HOST_SYSTEM_NAME>
+#         -DHOST_SYSTEM_PROCESSOR=<simulated CMAKE_HOST_SYSTEM_PROCESSOR>
+#         -P get-host-triple.cmake
+
+if( NOT SRC_ROOT )
+  message( FATAL_ERROR "SRC_ROOT is not set" )
+endif()
+
+set( CMAKE_HOST_SYSTEM_NAME "${HOST_SYSTEM_NAME}" )
+set( CMAKE_HOST_SYSTEM_PROCESSOR "${HOST_SYSTEM_PROCESSOR}" )
+
+include( "${SRC_ROOT}/cmake/modules/GetHostTriple.cmake" )
+
+function( report_host_triple )
+  get_host_triple( host_triple )
+  message( "host_triple=${host_triple}" )
+endfunction()
+
+report_host_triple()
diff --git a/llvm/test/CMake/get-host-triple.test b/llvm/test/CMake/get-host-triple.test
new file mode 100644
index 0000000000000..3613f96d6faa1
--- /dev/null
+++ b/llvm/test/CMake/get-host-triple.test
@@ -0,0 +1,28 @@
+# Test get_host_triple() from llvm/cmake/modules/GetHostTriple.cmake for host
+# systems where config.guess cannot be used, such as HarmonyOS: uname -s
+# reports "HarmonyOS", which config.guess does not recognize, and its probing
+# is rejected by the system sandbox. The CMake variables that CMake's platform
+# detection normally provides are simulated via the driver in Inputs.
+
+RUN: %cmake -DSRC_ROOT=%S/../.. -DHOST_SYSTEM_NAME=HarmonyOS -DHOST_SYSTEM_PROCESSOR=aarch64 -P %S/Inputs/get-host-triple.cmake 2>&1 | FileCheck %s --check-prefix=HARMONYOS-AARCH64
+HARMONYOS-AARCH64: host_triple=aarch64-unknown-linux-ohos
+
+RUN: %cmake -DSRC_ROOT=%S/../.. -DHOST_SYSTEM_NAME=HarmonyOS -DHOST_SYSTEM_PROCESSOR=armv7l -P %S/Inputs/get-host-triple.cmake 2>&1 | FileCheck %s --check-prefix=HARMONYOS-ARM
+HARMONYOS-ARM: host_triple=arm-unknown-linux-ohos
+
+RUN: %cmake -DSRC_ROOT=%S/../.. -DHOST_SYSTEM_NAME=HarmonyOS -DHOST_SYSTEM_PROCESSOR=x86_64 -P %S/Inputs/get-host-triple.cmake 2>&1 | FileCheck %s --check-prefix=HARMONYOS-X86_64
+HARMONYOS-X86_64: host_triple=x86_64-unknown-linux-ohos
+
+RUN: %cmake -DSRC_ROOT=%S/../.. -DHOST_SYSTEM_NAME=HarmonyOS -DHOST_SYSTEM_PROCESSOR=riscv64 -P %S/Inputs/get-host-triple.cmake 2>&1 | FileCheck %s --check-prefix=HARMONYOS-RISCV64
+HARMONYOS-RISCV64: host_triple=riscv64-unknown-linux-ohos
+
+RUN: %cmake -DSRC_ROOT=%S/../.. -DHOST_SYSTEM_NAME=HarmonyOS -DHOST_SYSTEM_PROCESSOR=loongarch64 -P %S/Inputs/get-host-triple.cmake 2>&1 | FileCheck %s --check-prefix=HARMONYOS-LOONGARCH64
+HARMONYOS-LOONGARCH64: host_triple=loongarch64-unknown-linux-ohos
+
+# The OpenHarmony and OHOS spellings of the host system name are handled the
+# same way.
+RUN: %cmake -DSRC_ROOT=%S/../.. -DHOST_SYSTEM_NAME=OpenHarmony -DHOST_SYSTEM_PROCESSOR=aarch64 -P %S/Inputs/get-host-triple.cmake 2>&1 | FileCheck %s --check-prefix=OPENHARMONY-AARCH64
+OPENHARMONY-AARCH64: host_triple=aarch64-unknown-linux-ohos
+
+RUN: %cmake -DSRC_ROOT=%S/../.. -DHOST_SYSTEM_NAME=OHOS -DHOST_SYSTEM_PROCESSOR=aarch64 -P %S/Inputs/get-host-triple.cmake 2>&1 | FileCheck %s --check-prefix=OHOS-AARCH64
+OHOS-AARCH64: host_triple=aarch64-unknown-linux-ohos
diff --git a/llvm/test/CMake/lit.local.cfg b/llvm/test/CMake/lit.local.cfg
new file mode 100644
index 0000000000000..09c1b45005a1a
--- /dev/null
+++ b/llvm/test/CMake/lit.local.cfg
@@ -0,0 +1 @@
+config.substitutions.append(("%cmake", "'%s'" % config.host_cmake))
diff --git a/llvm/test/lit.site.cfg.py.in b/llvm/test/lit.site.cfg.py.in
index b13ccd74d0928..7f12fbb8d0ad3 100644
--- a/llvm/test/lit.site.cfg.py.in
+++ b/llvm/test/lit.site.cfg.py.in
@@ -26,6 +26,7 @@ config.ocamlfind_executable = "@OCAMLFIND@"
 config.have_ocamlopt = @HAVE_OCAMLOPT@
 config.ocaml_flags = "@OCAMLFLAGS@"
 config.ptxas_executable = "@PTXAS_EXECUTABLE@"
+config.host_cmake = "@CMAKE_COMMAND@"
 config.enable_assertions = @ENABLE_ASSERTIONS@
 config.targets_to_build = "@TARGETS_TO_BUILD@"
 config.native_target = "@LLVM_NATIVE_ARCH@"



More information about the llvm-commits mailing list