compiler-rt does not build `crtbegin.o`/`crtend.o` for OpenHarmony targets
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## Summary
When compiling compiler-rt for an OpenHarmony target (`CMAKE_SYSTEM_NAME=OHOS`), the CRT objects `clang_rt.crtbegin.o` and `clang_rt.crtend.o` are never built, so a freshly built clang cannot link even a trivial program.
## Description
Building the LLVM runtimes for `aarch64-linux-ohos`:
```
cmake -S runtimes -B build_crt -G Ninja \
-DLLVM_ENABLE_RUNTIMES=compiler-rt \
-DCMAKE_SYSTEM_NAME=OHOS ... # via a toolchain file
-DCOMPILER_RT_BUILD_CRT=ON
```
We observe that after `cmake --build ... --target install`, the clang resource directory contained only `libclang_rt.builtins.a`, with no `clang_rt.crtbegin.o`/`clang_rt.crtend.o`. Consequently, linking failed with:
```
ld.lld: error: cannot open crtbeginS.o: No such file or directory
ld.lld: error: cannot open crtendS.o: No such file or directory
```
This is because `compiler-rt/cmake/crt-config-ix.cmake` gates the CRT build on the OS name:
```cmake
if (CRT_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux|SerenityOS" AND NOT LLVM_USE_SANITIZER)
set(COMPILER_RT_HAS_CRT TRUE)
else()
set(COMPILER_RT_HAS_CRT FALSE)
endif()
```
`OS_NAME` is `CMAKE_SYSTEM_NAME` lower-cased (with only Android special-cased above it). For `CMAKE_SYSTEM_NAME=OHOS` the regex does not match, so `COMPILER_RT_HAS_CRT` becomes `FALSE` and the CRT targets are never created — even though OpenHarmony is Linux-kernel based and uses musl.
Evidence that OHOS should be treated as a Linux-family OS:
- `llvm/include/llvm/TargetParser/Triple.h` already treats OpenHarmony as musl:
```cpp
bool isMusl() const {
return ... || getEnvironment() == Triple::OpenHOS || isOSLiteOS();
}
```
- `clang -dM -E -x c /dev/null` for `--target=aarch64-linux-ohos` defines `__linux__`, `__linux`, `__unix__`, `__ELF__`, `__OHOS__`.
As a workaround, we set `CMAKE_SYSTEM_NAME=Linux` in the toolchain (keeping the compiler target triple `aarch64-linux-ohos`, so `__OHOS__`/`__linux__` are still defined). With that, the `clang_rt.crtbegin-*`/`clang_rt.crtend-*` targets appear and the CRT objects are built.
**Suggested fix:** include OHOS in the CRT-supported OS check, e.g. `OS_NAME MATCHES "Linux|SerenityOS|OHOS"`, consistent with Android already being special-cased in the same file.
---
We used branch `release/22.x` commit `ca7933e47d3a3451d81e72ac174dcb5aa28b59d1`.
Environment: HarmonyOS 7.0.0.102, `uname -a`: `HarmonyOS localhost HongMeng Kernel 1.13.0 #1 SMP Sat Aug 15 11:19:26 UTC 2026 aarch64 GNU/Linux`
Contributor guide
Research direction
Start with compiler-rt/cmake/crt-config-ix.cmake and inspect how CMAKE_SYSTEM_NAME becomes OS_NAME and controls COMPILER_RT_HAS_CRT. Reconfigure the runtimes for an aarch64-linux-ohos target with COMPILER_RT_BUILD_CRT=ON, then build and install; done means clang_rt.crtbegin.o and clang_rt.crtend.o are created for the OHOS target and a trivial program can link.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake
- Domain
- build-system, compilers
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100