1 下载onnx
git clone https://github.com/microsoft/onnxruntime
cd onnxruntime
git submodule update --init --recursive
2 编译
由于是交叉编译,所以需要设置一下编译工具,在网上搜索看到了这个
chineseocr_lite/build-onnxruntime-android.sh at onnx · DayBreak-u/chineseocr_lite · GitHub
于是 我用下面的命令进行编译
./build.sh --skip_tests --config Release --build_shared_lib --cmake_extra_defines CMAKE_C_COMPILER=arm-ca9-linux-gnueabihf-gcc CMAKE_CXX_COMPILER=arm-ca9-linux-gnueabihf-g++ --parallel 10
这里就不用写-DCMAKE_C_COMPILER=arm-ca9-linux-gnueabihf-gcc -DCMAKE_CXX_COMPILER=arm-ca9-linux-gnueabihf-g++了,直接写CMAKE_C_COMPILER=arm-ca9-linux-gnueabihf-gcc CMAKE_CXX_COMPILER=arm-ca9-linux-gnueabihf-g++即可。
然后编译提示下面的错误,
我某搜索引擎搜了一下这个报错
/bin/sh: 1: ../protobuf/cmake/protoc-3.11.3.0: Exec format error
得到如下答案,
然后点击链接去了https://github.com/ucb-bar/onnxruntime-riscv/issues/7
可以看到
然后继续去链接 onnxruntime-riscv/build.sh at 34dbafc75241cc86c813fc679a55355d7863fd99 · ucb-bar/onnxruntime-riscv · GitHub
然后我用了他的build.sh,把其中的交叉编译工具链修改了一下,修改后的如下
#!/bin/bash
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Get directory this script is in
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Setup RISCV environment variables. Ensure that riscv/esp-tools GCC is in your path.
export CXX=arm-ca9-linux-gnueabihf-g++
export CC=arm-ca9-linux-gnueabihf-gcc
#export CXXFLAGS="-march=rv64imafdc "
# Download protoc if we don't have it
if [ ! -d "build/protoc" ]; then
mkdir -p "build/protoc"
curl --location "https://github.com/protocolbuffers/protobuf/releases/download/v3.11.3/protoc-3.11.3-linux-x86_64.zip" --output "build/protoc/protoc.zip"
cd build/protoc
unzip protoc.zip
fi
cd $DIR
# NOTE: If you're NOT building for the first time adding "--parallel" when invoking this script will parallelize build
# requires python3.6 or higher
python3 $DIR/tools/ci_build/build.py --arm --skip_tests --config Release --build_shared_lib --disable_contrib_ops --build_dir=build "$@"
# On first build, it might fail on linking binary, complaining about missing atomics (this is despite having -latomic).
# Rebuilding fixes this for some reason. This started happening after the version bump subsequent to commit 4db932, as there were some CMake file changes in those commits.
if [ -d "build/Debug" ]; then
cd build/Debug
# /scratch/pranavprakash/chipyard/esp-tools-install/bin/riscv64-unknown-linux-gnu-g++ -march=rv64imafdc -mabi=lp64d -Wno-error=attributes -Dgsl_CONFIG_CONTRACT_VIOLATION_THROWS -Wall -Wextra -ffunction-sections -fdata-sections -Werror -Wno-parentheses -g -Wno-nonnull-compare -Wno-deprecated-copy -latomic -static CMakeFiles/onnx_test_runner.dir/scratch/pranavprakash/onnxruntime/onnxruntime/onnxruntime/test/onnx/main.cc.o -o onnx_test_runner libonnx_test_runner_common.a libonnxruntime_test_utils.a libonnxruntime_session.a libonnxruntime_providers_systolic.a libonnxruntime_optimizer.a libonnxruntime_providers.a libonnxruntime_util.a libonnxruntime_framework.a libonnxruntime_util.a libonnxruntime_graph.a libonnxruntime_common.a libonnxruntime_mlas.a libonnx_test_data_proto.a external/re2/libre2.a onnx/libonnx.a onnx/libonnx_proto.a external/protobuf/cmake/libprotobuf-lited.a external/re2/libre2.a external/nsync/libnsync_cpp.a -ldl -Wl,--whole-archive -lpthread -latomic -lrt -Wl,--no-whole-archive
else
# If we're release build don't bother linking the test runner binaries
cd build/Release
fi
然后我再次编译发现还是报同样的错误,然后我又去搜了几个其他的方法也都不行,然后我又回头看了看这个解决方法,为什么别人用这种方法能解决呢,我又停下来思考了下,我猜测报错的原因看着像是电脑需要的是x86的proto,而我的./build/Release/external/protobuf/cmake/protoc-3.11.3.0用file命令可以看到是arm32格式的,而上面的解决方法其实就像在build.sh里面增加了一个下载x86格式的proto的操作,只不过这个下载proto是放在了build/protoc/bin/protoc里面,于是我
cp ./build/protoc/bin/protoc ./build/Release/external/protobuf/cmake/
我把这个下载x86的proto放到了./build/Release/external/protobuf/cmake/protoc-3.11.3.0同一个目录里面,再次编译,编译成功。