bazel 專案新增 automake/autoconf 專案解決辦法
問題描述:
專案 A 是用 bazel 構建,專案 B 是用 automake / autoconf 或是 CMake 構建的,那麼如何在 專案 A 中使用專案 B 呢?
方法一:clone B,然後在專案 B 中新增 WORKSPACE 和 BUILD,然後在 A 中引用 clone 並修改之後的專案 B
- 缺點:你需要維護 clone B,每次想要更新 B 都需要手動更新 clone B
一個更好的方法 :
方法二:在 專案 A 中使用 new_git_repository 或是 http_archive 新增專案 B 並指定 BUILD 檔案,假設我們需要依賴專案 B:LAME MP3 Encoder
new_git_repository( name = "org_lame", build_file = "lame.BUILD", commit = "3f89eaa001d9cc5a52e88f6b493c6eec750e9eab", remote = "https://github.com/lifeiteng/lame.git", ) # BUILD 檔案 licenses(["notice"]) cc_library( name = "lame", srcs = glob([ "libmp3lame/*.c", "libmp3lame/vector/*.c", "mpglib/*.c", ]), hdrs = glob([ "include/*.h", "libmp3lame/*.h", "libmp3lame/vector/*.h", "mpglib/*.h", ]), copts = ["-Wno-sign-compare -DHAVE_CONFIG_H"], includes = ["include", "libmp3lame", "mpglib", "."], visibility = ["//visibility:public"], )
萬事大吉? 不是的 ,很多使用 automake/autoconf 的專案構建步驟
是 ofollow,noindex">lame-make
% ./configure# 生成 config.h % make
或是 opus-make
% ./autogen.sh % ./configure# 生成 config.h % make
如果沒有 生成 config.h ,編譯通常會失敗,因此我們需要在 build_file ="lame.BUILD" 裡面顯式地生成 config.h, bazel 文件 Working with external dependencies 中沒有相關例子,那麼如何操作呢?
genrule( name = "config_h", srcs = [ "configure", "libmp3lame/lame.c", "install-sh", "config.guess", "config.rpath", "config.sub", ] + glob(["**/*.in"]), outs = ["config.h"], cmd = "./$(location configure) " + "&& cp config.h $(location config.h)", ) cc_library( name = "liblame", srcs = glob([ "libmp3lame/*.c", "libmp3lame/vector/*.c", "mpglib/*.c", ]), hdrs = glob([ "include/*.h", "libmp3lame/*.h", "libmp3lame/vector/*.h", "mpglib/*.h", ]) + [ "config.h", ], copts = ["-Wno-sign-compare -DHAVE_CONFIG_H"], includes = ["include", "libmp3lame", "mpglib", "."], visibility = ["//visibility:public"], )
或是
genrule( name = "config_h", srcs = [ "autogen.sh", "configure.ac", ] + glob(["**/*.in", "m4/*.m4"]), outs = ["config.h"], cmd = "./$(location autogen.sh) " + "&& ./`dirname $(location autogen.sh)`/configure " + "&& cp config.h $(location config.h)", ) cc_library( name = "libopus", srcs = glob( [ "src/*.c", "celt/*.c", "silk/*.c", "silk/float/*.c", ], exclude = [ "celt/opus_custom_demo.c", "src/opus_demo.c", "src/repacketizer_demo.c", ], ), hdrs = glob( [ "include/*.h", "celt/*.h", "silk/*.h", "silk/float/*.h", "src/*.h", ], ) + [ "config.h", ], copts = ["-Wno-sign-compare -DHAVE_CONFIG_H"], includes = ["include", "src", "silk/float", "silk", "celt", "."], visibility = ["//visibility:public"], )
這裡要注意的是動態確定 configure、autogen.sh 的位置: ./$(location configure) 或是 ./$(location autogen.sh) ,以及正確拷貝 config.h。
bazel-external-deps