bazel编译litert
bazel 编译
一、安装工具
# 1. 更新系统并安装依赖
sudo apt update
sudo apt install -y curl git openjdk-17-jdk
# 2. 下载 Bazelisk 并命名为 `bazel`
curl -LO "https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64"
sudo mv bazelisk-linux-amd64 /usr/local/bin/bazel
sudo chmod +x /usr/local/bin/bazel
二、常用命令
1、编译项目中的所有目标,在项目根目录运行: bazel build //...
这会告诉 Bazel 递归地查找并构建从根目录开始的所有 BUILD 文件中定义的所有目标。
2、编译特定目录下的所有目标:如果你只想编译 litert/cc 及其子目录下的所有内容,可以运行:
bazel build //litert/cc/...
3、编译指定目标,其中有一个名为 litert_api_with_dynamic_runtime 的库
bazel build //litert/cc:litert_api_with_dynamic_runtime
4、要运行 litert/cc 目录及其子目录中定义的所有测试,可以使用 ... 通配符:
bazel test //litert/cc/...
三、使用bazel编译LiteRT
1、下载LiteRT代码
https://github.com/google-ai-edge/LiteRT
2、下载sdk和ndk(都要是linux的)
ndk下载很简单,到对应网址下载25版本即可
sdk下载:先下载 command-tool https://developer.android.com/studio#command-tools
然后通过command-tool sdkmanager 下载platforms和build-tools
./cmdline-tools/bin/sdkmanager \
"platform-tools"\
"build-tools;34.0.0"\
"build-tools;33.0.2"\
"platforms;android-34"\
"platforms;android-33"\
"platforms;android-31"\
--sdk_root=~/work/cfc/sdk
3、到项目主目录执行 python3.13 configure.py ,按照指引一步步设置,这里我们需要编译的库跑在安卓手机,所以前面都选N
交互如下:
You have bazel 7.6.1 installed.
Please specify the location of python. [Default is /usr/local/bin/python3.13]:
Found possible Python library paths:
/usr/local/lib/python3.13/site-packages
Please input the desired Python library path to use. Default is [/usr/local/lib/python3.13/site-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]: N
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.
Do you want to use Clang to build TensorFlow? [Y/n]: n
GCC will be used to compile TensorFlow.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.
Please specify the home path of the Android NDK to use. [Default is /home/njsmoketest/Android/Sdk/ndk-bundle]: /home/njsmoketest/work/cfc/ndk/android-ndk-r25c
Please specify the (min) Android NDK API level to use. [Available levels: [16, 17, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32, 33]] [Default is 21]:
Please specify the home path of the Android SDK to use. [Default is /home/njsmoketest/Android/Sdk]: /home/njsmoketest/work/cfc/sdk
Please specify the Android SDK API level to use. [Available levels: ['31', '33', '34']] [Default is 34]:
Please specify an Android build tools version to use. [Available versions: ['33.0.2', '34.0.0']] [Default is 34.0.0]:
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
--config=mkl # Build with MKL support.
--config=mkl_aarch64 # Build with oneDNN and Compute Library for the Arm Architecture (ACL).
--config=monolithic # Config for mostly static monolithic build.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v1 # Build with TensorFlow 1 API instead of TF 2 API.
Preconfigured Bazel build configs to DISABLE default on features:
--config=nogcp # Disable GCP support.
--config=nonccl # Disable NVIDIA NCCL support.
4、编译,我们只编译需要的,整个编译的话会报其他错误,暂时就这样了。
bazel build //litert/cc:litert_api_with_dynamic_runtime