Skip to content

Fix stacktrace compilation on musl libc (Alpine Linux)#28249

Open
tekintian wants to merge 2 commits intomicrosoft:mainfrom
tekintian:main
Open

Fix stacktrace compilation on musl libc (Alpine Linux)#28249
tekintian wants to merge 2 commits intomicrosoft:mainfrom
tekintian:main

Conversation

@tekintian
Copy link
Copy Markdown

📋 Summary

This PR fixes a compilation error when building ONNX Runtime on Alpine Linux and other musl libc-based distributions. The issue occurs because execinfo.h is a glibc-specific header that is not available in musl libc.

🔧 Changes

File Modified: onnxruntime/core/platform/posix/stacktrace.cc

Change: Added && defined(__GLIBC__) condition to the preprocessor check before including <execinfo.h>

// Before:
#if !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) && !defined(_AIX)
#include <execinfo.h>
#endif

// After:
#if !defined(__ANDROID__) && !defined(__wasm__) && !defined(_OPSCHEMA_LIB_) && !defined(_AIX) && defined(__GLIBC__)
#include <execinfo.h>
#endif

❓ Problem

When building ONNX Runtime on Alpine Linux (which uses musl libc instead of glibc), the build fails with:

/opt/onnxruntime/onnxruntime/core/platform/posix/stacktrace.cc:7:10: 
fatal error: execinfo.h: No such file or directory
    7 | #include <execinfo.h>
      |          ^~~~~~~~~~~~
compilation terminated.

This happens because:

  1. execinfo.h is a glibc-specific header for backtrace functionality
  2. musl libc (used by Alpine Linux) does not provide this header
  3. The existing preprocessor checks don't account for musl libc systems

✅ Solution

By adding && defined(__GLIBC__) to the condition, we ensure that:

  • execinfo.h is only included on glibc-based systems (Linux with glibc)
  • On musl libc systems (Alpine Linux), the stacktrace functionality is gracefully disabled
  • The rest of the code continues to work without stacktrace support

This is consistent with how other platform-specific features are handled in the codebase (e.g., __ANDROID__, __wasm__, _AIX).

🧪 Testing

Build Environment

  • OS: Alpine Linux 3.23
  • Compiler: GCC 15.2.0
  • CMake: 4.1.3
  • ONNX Runtime: 1.26.0

Build Command

docker build -f Dockerfile.onnx-alpine \
             -t onnx-alpine .

Results

✅ Successfully compiled ONNX Runtime 1.26.0
✅ Generated libonnxruntime.so.1.26.0 (24 MB)
✅ All core functionality working
✅ No runtime errors

Build Statistics

  • Total .o files: 1076
  • Build time: ~30 minutes (with local mirror)
  • No compilation errors
  • No linking errors

📊 Impact

Positive Impact

  1. Enables Alpine Linux support: Users can now build ONNX Runtime on Alpine Linux
  2. Smaller Docker images: Alpine-based images are significantly smaller than glibc-based ones
  3. Better security: Alpine's minimal attack surface is beneficial for production deployments
  4. No performance impact: Stacktrace is primarily used for debugging; disabling it in release builds has minimal impact

Potential Concerns

  • Stacktrace functionality will be unavailable on musl libc systems
  • This is acceptable because:
    • Stacktrace is mainly used for debugging and error reporting
    • Production builds typically don't need detailed stack traces
    • Other error handling mechanisms remain functional

🔄 Compatibility

This change is fully backward compatible:

  • No API changes
  • No behavior changes on glibc systems
  • Only affects compilation on non-glibc systems
  • Existing binaries continue to work unchanged

📝 Related Issues

This fix addresses the common issue reported by users trying to build ONNX Runtime on:

  • Alpine Linux
  • Other musl libc-based distributions
  • Minimal container images

🚀 Usage Example

After this fix, users can build ONNX Runtime on Alpine Linux:

FROM alpine:3.23

RUN apk add --no-cache build-base cmake git python3 wget

# Clone and build ONNX Runtime
RUN git clone https://github.com/microsoft/onnxruntime.git && \
    cd onnxruntime && \
    ./build.sh --config Release --build_shared_lib

COPY --from=builder /opt/onnxruntime/build/Linux/Release/libonnxruntime.so* /usr/local/lib/

✍️ Author

TekinTian tekintian@gmail.com

📅 Date

April 28, 2026


Checklist

  • Code follows the project's coding standards
  • Changes are minimal and focused
  • No breaking changes introduced
  • Tested on target platform (Alpine Linux 3.23)
  • Documentation updated (if applicable)
  • Commit message follows conventional format
  • Signed-off-by line included

Note: This is a critical fix for Alpine Linux users and should be considered for inclusion in the next release.

- Add __GLIBC__ check to conditionally include execinfo.h
- execinfo.h is glibc-specific and not available in musl libc
- This fix enables ONNX Runtime to compile on Alpine Linux and other musl-based distributions
- The stacktrace functionality will be gracefully disabled on non-glibc systems

Fixes compilation error:
  fatal error: execinfo.h: No such file or directory

Tested on:
- Alpine Linux 3.23 with GCC 15.2.0
- Successfully builds ONNX Runtime 1.26.0

Signed-off-by: Tekin <tekintian@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant