規則
- cc_binary
- cc_import
- cc_library
- cc_proto_library
- fdo_prefetch_hints
- fdo_profile
- propeller_optimize
- cc_test
- cc_toolchain
- cc_toolchain_suite
cc_binary
cc_binary(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, copts, defines, deprecation, distribs, env, exec_compatible_with, exec_properties, features, includes, licenses, linkopts, linkshared, linkstatic, local_defines, malloc, nocopts, output_licenses, restricted_to, stamp, tags, target_compatible_with, testonly, toolchains, visibility, win_def_file)
隱含輸出目標
name.stripped(只會在明確要求時建構):移除 最新版本的二進位檔案strip -g已在二進位檔上執行,以移除偵錯 符號。您可以透過指令列--stripopt=-foo。只有在明確要求時,系統才會建構這個輸出內容。name.dwp(僅在明確要求時建構):如果 Fission 已啟用:偵錯 適用於遠端部署二進位檔偵錯的資訊套件。其他: 空白檔案。
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
deps
|
可以是 |
srcs
|
所有
總共 如果規則名稱位於
允許的
...以及產生這些檔案的任何規則。 不同的擴充功能代表 改成 gcc 慣例 |
additional_linker_inputs
|
舉例來說,可以在這裡提供已編譯的 Windows .res 檔案,以便嵌入 做為二進位目標 |
copts
|
此屬性中的每個字串都會按照指定順序新增至
如果套件已宣告功能
|
defines
|
-D 的前面,並新增至這個目標的編譯指令列。
並套用仰賴規則的每項規則請務必小心,因為這可能有
影響深遠的影響如有疑問,請將定義值加進
local_defines。
|
includes
|
套用至「Make 變數」替代。
每個字串前面都會加上 標頭必須新增至 srcs 或 Hdr,否則就無法使用 預設規則。 |
linkopts
|
LINKOPTS 之前,
來連結二進位資料
這份清單中每個開頭不是 |
linkshared
|
linkshared=True。預設
這個選項就會停用
出現這個旗標,表示連結是透過
如果同時指定 |
linkstatic
|
cc_binary 和
cc_test:連結靜態的二進位檔
模式。cc_library.linkstatic:請參閱下方說明。
這個選項預設為
如果啟用此選項,且是二進位檔或測試項目,這個選項會指示建構工具
連結執行檔的方法有三種:
如果值為 |
local_defines
|
-D 的前面,並新增至這個目標的編譯指令列。
而非依附關係
|
malloc
|
根據預設,C++ 二進位檔會與 |
nocopts
|
COPTS
(包括規則的 copts 屬性中明確指定的值) 會從
COPTS:用於編譯這項規則。
這個屬性應該很少需要用。
|
stamp
|
除非其依附元件變更,否則系統「不會」重新建構加上時間戳記的二進位檔。 |
win_def_file
|
只有在 Windows 是目標平台時,才能使用這項屬性。 可用於 匯出符號。 |
cc_import
cc_import(name, data, hdrs, alwayslink, compatible_with, deprecation, distribs, features, interface_library, licenses, restricted_to, shared_library, static_library, system_provided, tags, target_compatible_with, testonly, visibility)
cc_import 規則可讓使用者匯入預先編譯的 C/C++ 程式庫。
以下為一般用途:
1.連結靜態資料庫
cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.a", # If alwayslink is turned on, # libmylib.a will be forcely linked into any binary that depends on it. # alwayslink = 1, )
cc_import( name = "mylib", hdrs = ["mylib.h"], shared_library = "libmylib.so", )
cc_import( name = "mylib", hdrs = ["mylib.h"], # mylib.lib is a import library for mylib.dll which will be passed to linker interface_library = "mylib.lib", # mylib.dll will be available for runtime shared_library = "mylib.dll", )
system_provided=True」(Windows)
cc_import( name = "mylib", hdrs = ["mylib.h"], # mylib.lib is an import library for mylib.dll which will be passed to linker interface_library = "mylib.lib", # mylib.dll is provided by system environment, for example it can be found in PATH. # This indicates that Bazel is not responsible for making mylib.dll available. system_provided = 1, )
在 Unix 上:
cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.a", shared_library = "libmylib.so", ) # first will link to libmylib.a cc_binary( name = "first", srcs = ["first.cc"], deps = [":mylib"], linkstatic = 1, # default value ) # second will link to libmylib.so cc_binary( name = "second", srcs = ["second.cc"], deps = [":mylib"], linkstatic = 0, )
cc_import( name = "mylib", hdrs = ["mylib.h"], static_library = "libmylib.lib", # A normal static library interface_library = "mylib.lib", # An import library for mylib.dll shared_library = "mylib.dll", ) # first will link to libmylib.lib cc_binary( name = "first", srcs = ["first.cc"], deps = [":mylib"], linkstatic = 1, # default value ) # second will link to mylib.dll through mylib.lib cc_binary( name = "second", srcs = ["second.cc"], deps = [":mylib"], linkstatic = 0, )
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
hdrs
|
|
alwayslink
|
如果始終連結無法在 Windows 上與 VS 2017 搭配運作,這是因為 已知問題 請將 VS 2017 升級至最新版本。 |
interface_library
|
允許的檔案類型:
|
shared_library
|
允許的檔案類型:
|
static_library
|
允許的檔案類型:
|
system_provided
|
interface_library
shared_library 必須留空。
|
cc_library
cc_library(name, deps, srcs, data, hdrs, additional_compiler_inputs, alwayslink, compatible_with, copts, defines, deprecation, distribs, exec_compatible_with, exec_properties, features, implementation_deps, include_prefix, includes, licenses, linkopts, linkstamp, linkstatic, local_defines, nocopts, restricted_to, strip_include_prefix, tags, target_compatible_with, testonly, textual_hdrs, toolchains, visibility, win_def_file)
標頭納入檢查
凡是用於建構作業的標頭檔案,都必須在 hdrs 中宣告,或是
第 srcs 項規則 (共 cc_* 項)。這是強制執行的,
針對 cc_library 規則,hdrs 中的標頭是由以下欄位構成的公開介面:
您可以直接從 hdrs 和
程式庫本身的 srcs,以及 hdrs 和
cc_* 規則的 srcs 列出了 deps 中的程式庫。
srcs 中的標頭只能直接從 hdrs 中的檔案加入
和程式庫本身的 srcs判斷是否要
建議您詢問 hdrs 或 srcs,是否希望這個資料庫的使用者
可直接加入。這與介於
程式設計語言中的 public 和 private 瀏覽權限。
cc_binary 和 cc_test 規則沒有匯出介面,因此它們
也沒有 hdrs 屬性。屬於二進位檔或測試集的所有標頭
直接列於 srcs 中。
請看以下範例來說明這些規則。
cc_binary(
name = "foo",
srcs = [
"foo.cc",
"foo.h",
],
deps = [":bar"],
)
cc_library(
name = "bar",
srcs = [
"bar.cc",
"bar-impl.h",
],
hdrs = ["bar.h"],
deps = [":baz"],
)
cc_library(
name = "baz",
srcs = [
"baz.cc",
"baz-impl.h",
],
hdrs = ["baz.h"],
)
下表列出了本範例中允許的直接納入項目。例如:
允許「foo.cc」直接加入 foo.h 和 bar.h,但
並非 baz.h。
| 包含檔案 | 允許納入的項目 |
|---|---|
| foo.h | bar.h |
| foo.cc | foo.h bar.h |
| bar.h | bar-impl.h baz.h |
| Bar-impl.h | bar.h baz.h |
| bar.cc | bar.h bar-impl.h baz.h |
| baz.h | baz-impl.h |
| baz-impl.h | baz.h |
| baz.cc | baz.h baz-impl.h |
納入檢查規則僅適用於 direct
包容。在上述範例中,foo.cc 可允許
包含 bar.h,其中可能包括 baz.h
允許轉彎,允許包括baz-impl.h。技術上
.cc 檔案的編譯方式可能間接包含任何標頭
該檔案位於 hdrs 或 srcs 中
遞移 deps 封閉的任何cc_library。於
在此情況下,編譯器可能會讀取 baz.h 和 baz-impl.h
編譯 foo.cc 時,但 foo.cc 不得
包含 #include "baz.h"。為了達成這個目標
已允許,必須將 baz 新增至 deps
(共 foo 個)。
遺憾的是,Bazel 目前無法區分直接和遞移性
無法偵測檔案是否包含非法包含
只允許間接納入的標頭。例如:
如果直接用在上述 foo.cc 的範例中,Bazel 不會抱怨
包含 baz.h。這會違法,因為foo
不會直接依附於 baz。目前未產生任何錯誤
啟用後,我們日後可能會加入這類錯誤檢查。
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
deps
|
可以是 |
srcs
|
所有
總共 如果規則名稱位於
允許的
...以及產生這些檔案的任何規則。 不同的擴充功能代表 改成 gcc 慣例 |
hdrs
|
此為強烈建議使用此處宣告的標頭檔案位置
描述程式庫的介面這些標頭就會
依據來源納入這項規則或依附規則。
不應由這個程式庫用戶端包含的標頭
列在 |
additional_compiler_inputs
|
|
alwayslink
|
srcs (即使有些不包含二進位檔參照的符號)。
如果程式碼中的程式碼未明確呼叫,這個方法就很實用
二進位檔案 (舉例來說,如果您的程式碼註冊以取得某些回呼)
這項工具可讓您存取
該基礎架構的各項服務
如果始終連結無法在 Windows 上與 VS 2017 搭配運作,這是因為 已知問題 請將 VS 2017 升級至最新版本。 |
copts
|
此屬性中的每個字串都會按照指定順序新增至
如果套件已宣告功能
|
defines
|
-D 的前面,並新增至這個目標的編譯指令列。
並套用仰賴規則的每項規則請務必小心,因為這可能有
影響深遠的影響如有疑問,請將定義值加進
local_defines。
|
implementation_deps
|
deps,標頭和包含這些程式庫的路徑 (及其全部路徑)
遞移依附元件) 只會用於這個程式庫的編譯作業,不適用於
依靠它使用 implementation_deps 指定的程式庫仍在連結中
取決於這個程式庫的二進位目標
目前僅限使用 cc_libraries,並受到標記的保護
|
include_prefix
|
設定後,即可存取這項規則 系統會先移除 |
includes
|
套用至「Make 變數」替代。
每個字串前面都會加上 標頭必須新增至 srcs 或 Hdr,否則就無法使用 預設規則。 |
linkopts
|
LINKOPTS 之前,
來連結二進位資料
這份清單中每個開頭不是 |
linkstamp
|
base 套件。
|
linkstatic
|
cc_binary 和
cc_test:連結靜態的二進位檔
模式。cc_library.linkstatic:請參閱下方說明。
這個選項預設為
如果啟用此選項,且是二進位檔或測試項目,這個選項會指示建構工具
連結執行檔的方法有三種:
如果值為 |
local_defines
|
-D 的前面,並新增至這個目標的編譯指令列。
而非依附關係
|
nocopts
|
COPTS
(包括規則的 copts 屬性中明確指定的值) 會從
COPTS:用於編譯這項規則。
這個屬性應該很少需要用。
|
strip_include_prefix
|
設定後,即可存取這項規則 如果是相對路徑,則會採用套件相關路徑。如果是絕對值 可理解為存放區相關路徑 緊接在 |
textual_hdrs
|
這是宣告無法自行編譯的標頭檔案的位置; 也就是說,這些物件必須一律 在其他來源檔案以文字形式納入 再也不是件繁重乏味的工作 |
win_def_file
|
只有在 Windows 是目標平台時,才能使用這項屬性。 可用於 匯出符號。 |
cc_proto_library
cc_proto_library(name, deps, data, compatible_with, deprecation, distribs, exec_compatible_with, exec_properties, features, licenses, restricted_to, tags, target_compatible_with, testonly, visibility)
cc_proto_library 會從 .proto 檔案產生 C++ 程式碼。
deps 必須指向 proto_library
規則。
範例:
cc_library(
name = "lib",
deps = [":foo_cc_proto"],
)
cc_proto_library(
name = "foo_cc_proto",
deps = [":foo_proto"],
)
proto_library(
name = "foo_proto",
)
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
deps
|
proto_library 清單
並產生 C++ 程式碼
|
fdo_prefetch_hints
fdo_prefetch_hints(name, compatible_with, deprecation, distribs, features, licenses, profile, restricted_to, tags, target_compatible_with, testonly, visibility)
代表位於工作區或指定位置的 FDO 預先擷取提示設定檔 範例:
fdo_prefetch_hints(
name = "hints",
profile = "//path/to/hints:profile.afdo",
)
fdo_profile(
name = "hints_abs",
absolute_path_profile = "/absolute/path/profile.afdo",
)
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
profile
|
|
fdo_profile
fdo_profile(name, absolute_path_profile, compatible_with, deprecation, distribs, features, licenses, profile, proto_profile, restricted_to, tags, target_compatible_with, testonly, visibility)
代表位於工作區或指定絕對路徑的 FDO 設定檔。 範例:
fdo_profile(
name = "fdo",
profile = "//path/to/fdo:profile.zip",
)
fdo_profile(
name = "fdo_abs",
absolute_path_profile = "/absolute/path/profile.zip",
)
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
absolute_path_profile
|
|
profile
|
|
proto_profile
|
|
propeller_optimize
propeller_optimize(name, compatible_with, deprecation, distribs, features, ld_profile, licenses, restricted_to, tags, target_compatible_with, testonly, visibility)
代表工作區中的 Propeller 最佳化設定檔。 範例:
propeller_optimize(
name = "layout",
cc_profile = "//path:cc_profile.txt",
ld_profile = "//path:ld_profile.txt"
)
propeller_optimize(
name = "layout_absolute",
absolute_cc_profile = "/absolute/cc_profile.txt",
absolute_ld_profile = "/absolute/ld_profile.txt"
)
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
ld_profile
|
|
cc_test
cc_test(name, deps, srcs, data, additional_linker_inputs, args, compatible_with, copts, defines, deprecation, distribs, env, env_inherit, exec_compatible_with, exec_properties, features, flaky, includes, licenses, linkopts, linkstatic, local, local_defines, malloc, nocopts, restricted_to, shard_count, size, stamp, tags, target_compatible_with, testonly, timeout, toolchains, visibility, win_def_file)
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
deps
|
可以是 |
srcs
|
所有
總共 如果規則名稱位於
允許的
...以及產生這些檔案的任何規則。 不同的擴充功能代表 改成 gcc 慣例 |
additional_linker_inputs
|
舉例來說,可以在這裡提供已編譯的 Windows .res 檔案,以便嵌入 做為二進位目標 |
copts
|
此屬性中的每個字串都會按照指定順序新增至
如果套件已宣告功能
|
defines
|
-D 的前面,並新增至這個目標的編譯指令列。
並套用仰賴規則的每項規則請務必小心,因為這可能有
影響深遠的影響如有疑問,請將定義值加進
local_defines。
|
includes
|
套用至「Make 變數」替代。
每個字串前面都會加上 標頭必須新增至 srcs 或 Hdr,否則就無法使用 預設規則。 |
linkopts
|
LINKOPTS 之前,
來連結二進位資料
這份清單中每個開頭不是 |
linkstatic
|
cc_binary 和
cc_test:連結靜態的二進位檔
模式。cc_library.linkstatic:請參閱下方說明。
這個選項預設為
如果啟用此選項,且是二進位檔或測試項目,這個選項會指示建構工具
連結執行檔的方法有三種:
如果值為 |
local_defines
|
-D 的前面,並新增至這個目標的編譯指令列。
而非依附關係
|
malloc
|
根據預設,C++ 二進位檔會與 |
nocopts
|
COPTS
(包括規則的 copts 屬性中明確指定的值) 會從
COPTS:用於編譯這項規則。
這個屬性應該很少需要用。
|
stamp
|
除非其依附元件變更,否則系統「不會」重新建構加上時間戳記的二進位檔。 |
win_def_file
|
只有在 Windows 是目標平台時,才能使用這項屬性。 可用於 匯出符號。 |
cc_toolchain
cc_toolchain(name, all_files, ar_files, as_files, compatible_with, compiler, compiler_files, compiler_files_without_includes, coverage_files, cpu, deprecation, distribs, dwp_files, dynamic_runtime_lib, exec_transition_for_inputs, features, libc_top, licenses, linker_files, module_map, objcopy_files, restricted_to, static_runtime_lib, strip_files, supports_header_parsing, supports_param_files, tags, target_compatible_with, testonly, toolchain_config, toolchain_identifier, visibility)
代表 C++ 工具鍊。
此規則負責:
-
收集執行 C++ 動作所需的所有構件。做法是
例如
all_files、compiler_fileslinker_files或其他結尾為_files的屬性)。這些 最常見的檔案群組發現了所有必要檔案。 -
為 C++ 動作產生正確的指令列。方法是使用
CcToolchainConfigInfo提供者 (請見下方說明)。
使用 toolchain_config 屬性設定 C++ 工具鍊。
另請參閱
第 頁
查看詳細的 C++ 工具鍊設定和工具鍊選擇說明文件。
使用 tags = ["manual"],以免建立及設定工具鍊
叫用 bazel build //... 時不需要使用
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
all_files
|
all_files 是超集
所有其他構件屬性 (例如 linktamp 編譯需要一併編譯)
並連結檔案,因此需要 all_files)。
這是 |
ar_files
|
包含封存動作所需的所有 cc_toolchain 構件。 |
as_files
|
組合動作所需的所有 cc_toolchain 構件。 |
compiler
|
toolchain_identifier 屬性。這是一件名詞
晚於
從 CROSSTOOL 遷移至 Starlark
,將由以下開發人員移除:
#7075。
設定後,就會用於執行 Crosstool_config.toolchain 選取。需要 的優先順序高於 --cpu Bazel 選項 |
compiler_files
|
|
compiler_files_without_includes
|
|
coverage_files
|
|
cpu
|
設定後,就會用於執行 Crosstool_config.toolchain 選取。需要 的優先順序高於 --cpu Bazel 選項 |
dwp_files
|
|
dynamic_runtime_lib
|
這將用於「static_link_cpp_runtimes」功能已啟用,而我們要連結 動態依附元件 |
exec_transition_for_inputs
|
|
libc_top
|
|
linker_files
|
|
module_map
|
|
objcopy_files
|
|
static_runtime_lib
|
這將用於「static_link_cpp_runtimes」功能已啟用,而我們要連結 靜態依附元件 |
strip_files
|
|
supports_header_parsing
|
|
supports_param_files
|
|
toolchain_config
|
cc_toolchain_config_info 的規則標籤。
|
toolchain_identifier
|
直到問題 #5380 已修正
建議您使用這個方法將 |
cc_toolchain_suite
cc_toolchain_suite(name, compatible_with, deprecation, distribs, features, licenses, restricted_to, tags, target_compatible_with, testonly, toolchains, visibility)
代表 C++ 工具鍊的集合。
此規則負責:
- 收集所有相關 C++ 工具鍊。
-
根據
--cpu和--compiler選項選擇一個工具鍊 並傳遞至 Bazel
另請參閱 第 頁 查看詳細的 C++ 工具鍊設定和工具鍊選擇說明文件。
引數
| 屬性 | |
|---|---|
name |
此目標的專屬名稱。 |
toolchains
|
cc_toolchain 標籤。「<cpu>」只有在以下情況下才會使用:--cpu
會傳遞至 Bazel,以及「<cpu>|<compiler>」
--cpu 和 --compiler 會傳遞到 Bazel。範例:
cc_toolchain_suite(
name = "toolchain",
toolchains = {
"piii|gcc": ":my_cc_toolchain_for_piii_using_gcc",
"piii": ":my_cc_toolchain_for_piii_using_default_compiler",
},
)
|