#!/bin/bash
set -e
VERSION=1.16.3
PKG_FILE=v${VERSION}.tar.gz
LOCAL_DIR=$(pwd)
ROOT_DIR=${LOCAL_DIR}/../..
BUILD_DIR=${LOCAL_DIR}/install_comm
LLT_DIR=${LOCAL_DIR}/install_llt
OUTPUT_DIR=${ROOT_DIR}/output/kernel/dependency/onnxruntime
export ONNX_PYTHONHOME=/usr1/build/workspace/dependency/Python-3.8.3
export PATH=$ONNX_PYTHONHOME/bin:$PATH
export LD_LIBRARY_PATH=$ONNX_PYTHONHOME/lib:$LD_LIBRARY_PATH
echo "[onnxruntime] Python version: $(python3 --version)"
export ONNX_CMAKEHOME=/usr1/build/workspace/dependency/cmake-3.26.0
export LD_LIBRARY_PATH=$ONNX_CMAKEHOME/lib:$LD_LIBRARY_PATH
export PATH=$ONNX_CMAKEHOME/bin:$PATH
echo "[onnxruntime] CMake version: $(cmake --version)"
if [ -f /etc/centos-release ]; then
export ONNX_BINUTILS=/usr1/build/workspace/dependency/binutils-2.40
export LD_LIBRARY_PATH=$ONNX_BINUTILS/lib:$LD_LIBRARY_PATH
export PATH=$ONNX_BINUTILS/bin:$PATH
echo "[onnxruntime] Binutils version: $(ld --version)"
fi
ARCH=$(uname -m)
echo "[onnxruntime] Version: ${VERSION}"
echo "[onnxruntime] Architecture: ${ARCH}"
echo "[onnxruntime] Package: ${PKG_FILE}"
if [ ! -f "${PKG_FILE}" ]; then
echo "Error: source package not found: ${PKG_FILE}"
echo ""
echo "Please download from:"
echo "https://github.com/microsoft/onnxruntime/archive/refs/tags/${PKG_FILE}"
exit 1
fi
rm -rf "${LOCAL_DIR}/onnxruntime-${VERSION}"
rm -rf "${BUILD_DIR}" "${LLT_DIR}"
rm -rf "${OUTPUT_DIR}/comm" "${OUTPUT_DIR}/llt"
echo "[onnxruntime] Extracting source package..."
mkdir -p "${LOCAL_DIR}/onnxruntime-${VERSION}"
tar -zxf "${PKG_FILE}" -C "${LOCAL_DIR}/onnxruntime-${VERSION}" --strip-components 1
cd "${LOCAL_DIR}/onnxruntime-${VERSION}"
sed -i 's|^eigen;.*|eigen;https://github.com/eigenteam/eigen-git-mirror/archive/e7248b26a1ed53fa030c5c459f7ea095dfd276ac/eigen-e7248b26a1ed53fa030c5c459f7ea095dfd276ac.zip;ca78943c7e45fa6fe31de288306e9e51a5eee60f|' cmake/deps.txt
sed -i 's|-Xlinker -rpath=\\\$ORIGIN||g' cmake/onnxruntime.cmake
sed -i "s|-Wl,-rpath='\\\$ORIGIN'||g" cmake/onnxruntime.cmake
sed -i "s|-Wl,-rpath=\"\\\$ORIGIN\"||g" cmake/onnxruntime.cmake
sed -i 's|INSTALL_RPATH "@loader_path"||g' cmake/onnxruntime.cmake
sed -i 's|BUILD_WITH_INSTALL_RPATH TRUE|BUILD_WITH_INSTALL_RPATH FALSE|g' cmake/onnxruntime.cmake
./build.sh --config Release --build_shared_lib --parallel --compile_no_warning_as_error --skip_submodule_sync --allow_running_as_root --skip-keras-test --skip_onnx_tests --skip_tests --build_dir build \
--cmake_extra_defines CMAKE_C_FLAGS="-fstack-protector-strong -D_FORTIFY_SOURCE=2 -O2" \
--cmake_extra_defines CMAKE_CXX_FLAGS="-fstack-protector-strong -D_FORTIFY_SOURCE=2 -O2" \
--cmake_extra_defines CMAKE_SHARED_LINKER_FLAGS="-Wl,-z,relro,-z,now,-z,noexecstack" \
--cmake_extra_defines CMAKE_EXE_LINKER_FLAGS="-Wl,-z,relro,-z,now,-z,noexecstack"
COMPILE_OUTPUT_DIR=$(pwd)/build/Release
echo "[onnxruntime] Installing from: $(pwd)"
mkdir -p "${BUILD_DIR}/lib"
mkdir -p "${BUILD_DIR}/include"
echo "[onnxruntime] Copying library files..."
if [ -f "${COMPILE_OUTPUT_DIR}/libonnxruntime.so.${VERSION}" ]; then
cp "${COMPILE_OUTPUT_DIR}/libonnxruntime.so.${VERSION}" "${BUILD_DIR}/lib/"
cd "${BUILD_DIR}/lib"
ln -sf "libonnxruntime.so.${VERSION}" libonnxruntime.so.1
ln -sf libonnxruntime.so.1 libonnxruntime.so
cd - > /dev/null
echo " - Installed libonnxruntime.so.${VERSION}"
else
echo "Error: libonnxruntime.so.${VERSION} not found in ${COMPILE_OUTPUT_DIR}"
exit 1
fi
echo "[onnxruntime] Copying header files..."
SESSION_INCLUDE_DIR=$(pwd)/include/onnxruntime/core/session
PROVIDERS_INCLUDE_DIR=$(pwd)/include/onnxruntime/core/providers/cpu
FRAMEWORK_INCLUDE_DIR=$(pwd)/include/onnxruntime/core/framework
TRAINING_INCLUDE_DIR=$(pwd)/orttraining/orttraining/training_api/include
if [ -d "${SESSION_INCLUDE_DIR}" ]; then
cp "${SESSION_INCLUDE_DIR}/onnxruntime_c_api.h" "${BUILD_DIR}/include/"
cp "${SESSION_INCLUDE_DIR}/onnxruntime_cxx_api.h" "${BUILD_DIR}/include/"
cp "${SESSION_INCLUDE_DIR}/onnxruntime_cxx_inline.h" "${BUILD_DIR}/include/"
cp "${SESSION_INCLUDE_DIR}/onnxruntime_float16.h" "${BUILD_DIR}/include/"
cp "${SESSION_INCLUDE_DIR}/onnxruntime_session_options_config_keys.h" "${BUILD_DIR}/include/"
cp "${SESSION_INCLUDE_DIR}/onnxruntime_run_options_config_keys.h" "${BUILD_DIR}/include/"
echo " - Installed core session headers"
else
echo "Error: session include directory not found at ${SESSION_INCLUDE_DIR}"
exit 1
fi
if [ -f "${PROVIDERS_INCLUDE_DIR}/cpu_provider_factory.h" ]; then
cp "${PROVIDERS_INCLUDE_DIR}/cpu_provider_factory.h" "${BUILD_DIR}/include/"
echo " - Installed cpu_provider_factory.h"
fi
if [ -f "${FRAMEWORK_INCLUDE_DIR}/provider_options.h" ]; then
cp "${FRAMEWORK_INCLUDE_DIR}/provider_options.h" "${BUILD_DIR}/include/"
echo " - Installed provider_options.h"
fi
if [ -d "${TRAINING_INCLUDE_DIR}" ]; then
if [ -f "${TRAINING_INCLUDE_DIR}/onnxruntime_training_c_api.h" ]; then
cp "${TRAINING_INCLUDE_DIR}/onnxruntime_training_c_api.h" "${BUILD_DIR}/include/"
cp "${TRAINING_INCLUDE_DIR}/onnxruntime_training_cxx_api.h" "${BUILD_DIR}/include/"
cp "${TRAINING_INCLUDE_DIR}/onnxruntime_training_cxx_inline.h" "${BUILD_DIR}/include/"
echo " - Installed training API headers"
fi
fi
if [ -f "${COMPILE_OUTPUT_DIR}/onnxruntime_config.h" ]; then
cp "${COMPILE_OUTPUT_DIR}/onnxruntime_config.h" "${BUILD_DIR}/include/"
echo " - Installed onnxruntime_config.h"
fi
echo "[onnxruntime] Stripping library files..."
find "${BUILD_DIR}/lib" -name "*.so*" -type f -exec strip {} \; 2>/dev/null || true
echo "[onnxruntime] Creating llt output..."
cp -a "${BUILD_DIR}" "${LLT_DIR}"
echo "[onnxruntime] Copying files to output..."
mkdir -p "${OUTPUT_DIR}/comm"
mkdir -p "${OUTPUT_DIR}/llt"
cp -a "${BUILD_DIR}/." "${OUTPUT_DIR}/comm/"
cp -a "${LLT_DIR}/." "${OUTPUT_DIR}/llt/"
echo "[onnxruntime] Build completed successfully!"
echo "Output: ${OUTPUT_DIR}"
echo ""
echo "Libraries:"
find "${BUILD_DIR}/lib" -maxdepth 1 -name "libonnxruntime.so*" -printf " - %f\n" | sort
echo ""
echo "Headers:"
echo " - onnxruntime_c_api.h"
echo " - onnxruntime_cxx_api.h"
echo ""
echo "To use ONNX Runtime:"
echo " export LD_LIBRARY_PATH=${OUTPUT_DIR}/comm/lib:\$LD_LIBRARY_PATH"