如何通过Bazel模块扩展引入非Bazel的GitHub仓库依赖
Bazel模块扩展引入非Bazel GitHub仓库的问题解决
问题背景
尝试通过Bazel模块扩展将非Bazel项目的GitHub仓库(whisper.cpp v1.4.0)加入构建,使用git_repository拉取仓库后,构建时提示找不到外部仓库的BUILD文件。
当前配置文件:
MODULE.bazel
## MODULE.bazel module( name = "test", ) non_module_dependencies = use_extension("//:deps.bzl", "data_deps_ext") use_repo(non_module_dependencies, "whisper")
deps.bzl
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") def whisper(): git_repository( name = "whisper", remote = "https://github.com/ggerganov/whisper.cpp.git", tag = "v1.4.0" ) def _data_deps_extension_impl(_): whisper() data_deps_ext = module_extension( implementation = _data_deps_extension_impl, )
BUILD
cc_binary( name = "main", srcs = ["main.cc"], deps = [ "@whisper" ] )
错误信息
DEBUG: Repository _main~data_deps_ext~whisper instantiated at: callstack not available Repository rule git_repository defined at: /private/var/tmp/_bazel_victor/1cabe6594cc48c856544c639052f58cd/external/bazel_tools/tools/build_defs/repo/git.bzl:181:33: in <toplevel> ERROR: /Users/victor/Desktop/Check/cpp/main/BUILD:1:10: no such package '@_main~data_deps_ext~whisper//': BUILD file not found in directory '' of external repository @_main~data_deps_ext~whisper. Add a BUILD file to a directory to mark it as a package. and referenced by '//main:main' ERROR: Analysis of target '//main:main' failed; build aborted: Analysis failed INFO: Elapsed time: 5.239s INFO: 0 processes. FAILED: Build did NOT complete successfully (52 packages loaded, 211 targets configured) Fetching repository @bazel_tools~cc_configure_extension~local_config_cc; Running xcode-locatorlysis of target '//main:main' failed; build aborted: Analysis failed INFO: Elapsed time: 5.239s INFO: 0 processes. FAILED: Build did NOT complete successfully (52 packages loaded, 211 targets configured) Fetching repository @bazel_tools~cc_configure_extension~local_config_cc; Running xcode-locator
错误原因
git_repository仅负责拉取仓库,但whisper.cpp本身不是Bazel项目,根目录没有BUILD文件,Bazel无法识别该仓库的包结构和目标。需要使用支持注入自定义BUILD文件的仓库规则,为非Bazel项目补充Bazel构建规则。
解决方案
修改deps.bzl,使用new_git_repository替代git_repository,并注入自定义BUILD规则,让Bazel能识别whisper.cpp的源文件和头文件。
步骤1:更新deps.bzl
load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") def whisper(): new_git_repository( name = "whisper", remote = "https://github.com/ggerganov/whisper.cpp.git", tag = "v1.4.0", build_file_content = """ cc_library( name = "whisper", srcs = glob(["*.c", "*.cpp"]), hdrs = glob(["*.h", "*.hpp"]), includes = ["."], visibility = ["//visibility:public"], ) """, ) def _data_deps_extension_impl(_): whisper() data_deps_ext = module_extension( implementation = _data_deps_extension_impl, )
new_git_repository的build_file_content字段直接注入自定义BUILD文件,定义了一个cc_library目标,包含仓库中所有C/C++源文件和头文件。includes = ["."]确保编译时可以直接引用根目录的whisper.h等头文件。visibility = ["//visibility:public"]允许主项目引用这个库。
步骤2:调整主BUILD文件的依赖
依赖需要指向自定义的cc_library目标,而非仓库本身:
cc_binary( name = "main", srcs = ["main.cc"], deps = [ "@whisper//:whisper" ] )
步骤3:验证构建
执行bazel build //main:main,Bazel会拉取whisper.cpp仓库,使用注入的BUILD文件构建库,再编译你的main.cc。
内容的提问来源于stack exchange,提问作者Victor M
相关产品推荐
相关产品推荐

