bazel C++構文入門
9350 ワード
bazelのすべてのコードは現在のエンジニアリングで、各エンジニアリングはWORKSPACEです.各WORKSPACEの下に複数のBUILDファイルがあります.BUILD内には複数のtargetsがあり、これらのtargetsはstarlark言語で宣言されています.
Starlark言語pythonに似ています. スレッドセキュリティ データ型None,bool,dict,function,int,list,string,depset,struct 可変データ構造はlistsとdicts コマンドライン
ルール#ルール#
または
コマンドラインパラメータドキュメント
さぎょうげんりtargetに関するBUILDファイルのロード inputsとdependenciesを解析し、action graphを生成 graph実行、outputs産出 Actiongraph:bazelはこの図に依存してファイルの変化を追跡し、再コンパイルが必要かどうかを追跡し、ユーザーにコード間の依存関係図を提供することもできます.
依存宣言同一ファイルBUILD内 異なるBUILDファイル間
:hello-time定義libディレクトリの異なるディレクトリBUILD bazelでは異なるパッケージと呼ばれます可視性同一パッケージ内のtargetsデフォルトで互いに見える異なるパッケージ間のtargetsの可視性は手動で定義する必要がある
各パッケージのBUILDファイルの上部にtargetsの他のパッケージのデフォルトの表示を宣言できます.
すべてのパッケージについて次のように表示されます.
target
2種類ありますrule target、例えばcc_library file target
C++ベストプラクティス
BUILDファイルBUILDファイルごとにcc_を1個含むlibraryルールターゲット なるべく細粒度C++ライブラリで並列速度を向上させ、インクリメンタルコンパイルを低減 srcにファイルが1つしかない場合はcc_libraryの名前はこのファイル名と同じです.例えば、 libraryごとに個別のtest targetがあり、testの最後にこのtargetとテストファイル名を付けます.例えば
义齿すべてのincludeパスがWORKSPACEディレクトリに対して 非システムディレクトリ用 使用しない サードパーティライブラリに使用可能 サードパーティ製ライブラリに依存している場合、これらのライブラリのファイルに既存のinclude pathが現在のディレクトリに格納されている場合、workspace rootからファイルを参照するルールに合致しないため、次のディレクトリなどのディレクトリを追加する必要があります.
以上、bazel要求
複数のファイルを含む
globの使用
依存処理bazelでの依存非伝達解析 他のヘッダファイルが含まれている場合は、このヘッダファイルのtargetを含めます.このヘッダファイルの内部のincludeは管理しません.たとえば
sandwichはbreadに依存し、breadはflourに依存するが、sandwichはflourに依存しない.単一ヘッダファイル、実装されていない場合はtargetも定義する必要がある、例えば
外部ライブラリを含める
Google Testを使用する場合は、WORKSPACEで次のように指定できます.
【注】ライブラリにBUILDファイルが含まれている場合は、そのヘッダファイルはすべてこのディレクトリに対する pthreadへのリンクが必要 したがって、最終的なコンパイルルールは次のとおりです.
これは少し乱れているように見えますが、そのバージョンのディレクトリ名が含まれているので、この名前は
削除後のgtest.BUILDファイルは以下の通りです.
現在、他の
より詳細なcc rule説明はcc rulesを参照
テストケースの作成
ファイル作成
作成
注意
テストの実行例:
出力:
このセクションはbazel C++use caseから
プリコンパイルされたライブラリを含む動的ライブラリ
外部依存の処理
Working with external dependencies
bazelエンジニアリングに依存
非bazelエンジニアリングに依存
BUILDファイルの作成が必要
依存非表示(Shadowing)同じパッケージの異なるバージョンに依存 その他
getting started bazel C++
Starlark言語
ルール#ルール#
bazel [] []
または
bazel [] [] -- []
コマンドラインパラメータドキュメント
さぎょうげんり
依存宣言
cc_library(
name = "hello-greet",
srcs = ["hello-greet.cc"],
hdrs = ["hello-greet.h"],
)
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
],
)
cc_library(
name = "hello-greet",
srcs = ["hello-greet.cc"],
hdrs = ["hello-greet.h"],
)
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
"//lib:hello-time",
],
)
:hello-time定義libディレクトリの異なるディレクトリBUILD bazelでは異なるパッケージと呼ばれます
cc_library(
name = "hello-time",
srcs = ["hello-time.cc"],
hdrs = ["hello-time.h"],
visibility = ["//main:__pkg__"],
)
各パッケージのBUILDファイルの上部にtargetsの他のパッケージのデフォルトの表示を宣言できます.
package(
default_visibility = [
"//tensorflow_serving:internal",
],
features = ["-layering_check"],
)
すべてのパッケージについて次のように表示されます.
cc_proto_library(
name = "cc_echo_c++_proto",
deps = [
":echo_c++_proto",
],
visibility = [
"//visibility:public",
],
)
target
2種類あります
C++ベストプラクティス
BUILDファイル
cc_library(
name = "mylib",
srcs = ["mylib.cc"],
hdrs = ["mylib.h"],
deps = [":lower-level-lib"]
)
cc_test(
name = "mylib_test",
srcs = ["mylib_test.cc"],
deps = [":mylib"]
)
义齿
#include "foo/bar/baz.h"
、システムディレクトリ用#include
.
和..
include_prefix
およびstrip_include_prefix
└── my-project
├── legacy
│ └── some_lib
│ ├── BUILD
│ ├── include
│ │ └── some_lib.h
│ └── some_lib.cc
└── WORKSPACE
以上、bazel要求
some_lib.h
・必ずlegacy/some_lib/include/some_lib.h
という形で含まなければならないがsome_lib.cc
現在は"include/some_lib.h"
このように含まれるので、このinclude pathを有効にするには、以下のように経路を指定する必要がある(検証対象):cc_library(
name = "some_lib",
srcs = ["some_lib.cc"],
hdrs = ["include/some_lib.h"],
copts = ["-Ilegacy/some_lib/include"],
)
複数のファイルを含む
globの使用
cc_library(
name = "build-all-the-files",
srcs = glob(["*.cc"]),
hdrs = glob(["*.h"]),
)
依存処理
sandwichはbreadに依存し、breadはflourに依存するが、sandwichはflourに依存しない.
cc_library(
name = "sandwich",
srcs = ["sandwich.cc"],
hdrs = ["sandwich.h"],
deps = [":bread"],
)
cc_library(
name = "bread",
srcs = ["bread.cc"],
hdrs = ["bread.h"],
deps = [":flour"],
)
cc_library(
name = "flour",
srcs = ["flour.cc"],
hdrs = ["flour.h"],
)
cc_library(
name = "source_adapter",
hdrs = ["source_adapter.h"],
visibility = [
"//visibility:public",
],
deps = [
":loader",
":servable_data",
":source",
":storage_path",
":target",
"//tensorflow_serving/util:class_registration",
"@org_tensorflow//tensorflow/core:lib",
],
)
外部ライブラリを含める
Google Testを使用する場合は、WORKSPACEで次のように指定できます.
new_http_archive(
name = "gtest",
url = "https://github.com/google/googletest/archive/release-1.7.0.zip",
sha256 = "b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0",
build_file = "gtest.BUILD",
)
【注】ライブラリにBUILDファイルが含まれている場合は、
non-new_
関数を使用できます.ファイルを作成するgtest.BUILD
Google Testをコンパイルするためのファイルです.Google testは特殊なニーズがあるため、コンパイルルールが複雑になります.特殊性は、次のとおりです.googletest-release-1.7.0/src/gtest-all.cc
・#include
了googletest-release-1.7.0/src/
下のすべてのファイルがあるので、このファイルを排除する必要があるgoogletest-release-1.7.0/include/
例えば"gtest/gtest.h"
なので、このディレクトリをcoptsの-Iオプションに追加する必要があるcc_library(
name = "main",
srcs = glob(
["googletest-release-1.7.0/src/*.cc"],
exclude = ["googletest-release-1.7.0/src/gtest-all.cc"]
),
hdrs = glob([
"googletest-release-1.7.0/include/**/*.h",
"googletest-release-1.7.0/src/*.h"
]),
copts = [
"-Iexternal/gtest/googletest-release-1.7.0/include"
],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)
これは少し乱れているように見えますが、そのバージョンのディレクトリ名が含まれているので、この名前は
new_http_archive
で使用できますstrip_prefix
削除:new_http_archive(
name = "gtest",
url = "https://github.com/google/googletest/archive/release-1.7.0.zip",
sha256 = "b58cb7547a28b2c718d1e38aee18a3659c9e3ff52440297e965f5edffe34b6d0",
build_file = "gtest.BUILD",
strip_prefix = "googletest-release-1.7.0",
)
削除後のgtest.BUILDファイルは以下の通りです.
cc_library(
name = "main",
srcs = glob(
["src/*.cc"],
exclude = ["src/gtest-all.cc"]
),
hdrs = glob([
"include/**/*.h",
"src/*.h"
]),
copts = ["-Iexternal/gtest/include"],
linkopts = ["-pthread"],
visibility = ["//visibility:public"],
)
現在、他の
cc_ rules
依存できる@gtest//:main
.より詳細なcc rule説明はcc rulesを参照
テストケースの作成
ファイル作成
./test/hello-test.cc
#include "gtest/gtest.h"
#include "lib/hello-greet.h"
TEST(HelloTest, GetGreet) {
EXPECT_EQ(get_greet("Bazel"), "Hello Bazel");
}
作成
./test/BUILD
cc_test(
name = "hello-test",
srcs = ["hello-test.cc"],
copts = ["-Iexternal/gtest/include"],
deps = [
"@gtest//:main",
"//lib:hello-greet",
],
)
注意
hello-greet
対hello-test
を表示するには./lib/BUILD
ファイルに属性を追加する必要がありますvisibility
値は//test:__pkg__
です.テストの実行例:
bazel test test:hello-test
出力:
INFO: Found 1 test target...
Target //test:hello-test up-to-date:
bazel-bin/test/hello-test
INFO: Elapsed time: 4.497s, Critical Path: 2.53s
//test:hello-test PASSED in 0.3s
Executed 1 out of 1 tests: 1 test passes.
このセクションはbazel C++use caseから
プリコンパイルされたライブラリを含む
cc_library(
name = "mylib",
srcs = ["mylib.so"],
hdrs = ["mylib.h"],
)
外部依存の処理
Working with external dependencies
bazelエンジニアリングに依存
local_repository
git_repository
http_archive
非bazelエンジニアリングに依存
new_local_repository
new_git_repository
new_http_archive
BUILDファイルの作成が必要
依存非表示(Shadowing)
getting started bazel C++