規則
- cc_binary
- cc_import
- cc_library
- cc_proto_library
- cc_shared_library
- cc_static_library
- fdo_prefetch_hints
- fdo_profile
- memprof_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, link_extra_lib, 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
|
字串清單;預設值為
須視「建立變數」替代項目而定。
每個字串都會加上 標頭必須新增至 srcs 或 hdrs,否則在編譯作業受到沙箱限制時 (預設),標頭將無法供相依規則使用。 |
link_extra_lib
|
標籤;預設值為
根據預設,C++ 二進位檔會連結至 |
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, deps, 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 an 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, )
cc_import
支援 include 屬性。例如:cc_import( name = "curl_lib", hdrs = glob(["vendor/curl/include/curl/*.h"]), includes = [ "vendor/curl/include" ], shared_library = "vendor/curl/lib/.libs/libcurl.dylib", )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 deps
大多數建構規則定義的典型屬性」。
|
hdrs
|
標籤清單;預設值為 |
alwayslink
|
布林值;預設值為 如果 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, additional_linker_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
中的檔案,以及 cc_*
規則的 hdrs
和 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 |
納入檢查規則僅適用於直接納入的項目。在上述範例中,foo.cc
可包含 bar.h
,而 bar.h
可能包含 baz.h
,baz.h
則可包含 baz-impl.h
。從技術上來說,.cc
檔案的編譯作業可能會遞移地包含 hdrs
或 srcs
中任何 cc_library
的標頭檔案,而這些檔案位於遞移 deps
封閉中。在這種情況下,編譯器可能會在編譯 foo.cc
時讀取 baz.h
和 baz-impl.h
,但 foo.cc
不得包含 #include "baz.h"
。如要允許這麼做,必須將 baz
新增至 foo
的 deps
。
Bazel 依附於工具鍊支援,以強制執行納入檢查規則。
工具鍊必須支援 layering_check
功能,且必須明確要求,例如透過 --features=layering_check
指令列標記或 package
函式的 features
參數。Bazel 提供的工具鍊僅支援 Unix 和 macOS 上的 clang。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 這些可以是 |
srcs
|
標籤清單;預設值為 系統會編譯所有 系統不會編譯 所有 如果規則名稱位於
允許的
以及產生這些檔案的任何規則。 根據 gcc 慣例,不同的副檔名代表不同的程式設計語言。 |
hdrs
|
標籤清單;預設值為 這是宣告標頭檔案的強烈建議位置,這些檔案會說明程式庫的介面。來源可透過這項規則或依附規則,納入這些標頭。不應由這個程式庫的用戶端納入的標頭,應列在 |
additional_compiler_inputs
|
標籤清單;預設值為 |
additional_linker_inputs
|
標籤清單;預設值為 舉例來說,這裡可以提供已編譯的 Windows .res 檔案,以便嵌入二進位目標。 |
alwayslink
|
布林值;預設值為 srcs 中列出的所有檔案的物件檔案,即使有些檔案不含二進位檔參照的符號也一樣。如果二進位檔中的程式碼未明確呼叫您的程式碼,例如您的程式碼註冊接收某項服務提供的某些回呼,這就很有用。如果 alwayslink 無法在 Windows 上的 VS 2017 運作,這是已知問題,請將 VS 2017 升級至最新版本。 |
copts
|
字串清單;預設值為
這個屬性中的每個字串都會依指定順序新增至
如果套件宣告了功能
|
defines
|
字串清單;預設值為 -D 為前置字元,然後新增至這個目標的編譯指令列,以及每個依附於這個目標的規則。請務必謹慎操作,因為這可能會造成深遠影響。如有疑問,請改為在 local_defines 中新增定義值。
|
implementation_deps
|
標籤清單;預設值為 deps 不同,這些程式庫 (以及所有遞移依附元件) 的標頭和包含路徑僅用於編譯這個程式庫,而非依附於這個程式庫的程式庫。使用 implementation_deps 指定的程式庫仍會連結至依附於這個程式庫的二進位目標。目前使用範圍僅限於 cc_libraries,並受 |
include_prefix
|
字串;預設值為 設定後,即可在規則的 系統會先移除 |
includes
|
字串清單;預設值為
須視「建立變數」替代項目而定。
每個字串都會加上 標頭必須新增至 srcs 或 hdrs,否則在編譯作業受到沙箱限制時 (預設),標頭將無法供相依規則使用。 |
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 規則清單。 |
cc_shared_library
查看規則來源cc_shared_library(name, deps, additional_linker_inputs, dynamic_deps, exports_filter, shared_lib_name, tags, user_link_flags, win_def_file)
並產生共用程式庫。
範例
cc_shared_library( name = "foo_shared", deps = [ ":foo", ], dynamic_deps = [ ":bar_shared", ], additional_linker_inputs = [ ":foo.lds", ], user_link_flags = [ "-Wl,--version-script=$(location :foo.lds)", ], ) cc_library( name = "foo", srcs = ["foo.cc"], hdrs = ["foo.h"], deps = [ ":bar", ":baz", ], ) cc_shared_library( name = "bar_shared", shared_lib_name = "bar.so", deps = [":bar"], ) cc_library( name = "bar", srcs = ["bar.cc"], hdrs = ["bar.h"], ) cc_library( name = "baz", srcs = ["baz.cc"], hdrs = ["baz.h"], )
在範例中,foo_shared
會靜態連結 foo
和 baz
,後者是遞移依附元件。因為 dynamic_dep
bar_shared
已動態提供 bar
,因此不會連結。
foo_shared
會使用連結器指令碼 *.lds 檔案,控管要匯出的符號。cc_shared_library
規則邏輯不會控管要匯出哪些符號,只會使用假設要匯出的內容,在分析階段提供錯誤訊息 (如果兩個共用程式庫匯出相同的目標)。
cc_shared_library
的每個直接依附元件都會假設為已匯出。因此,Bazel 在分析期間會假設 foo
是由 foo_shared
匯出。baz
不會由 foo_shared
匯出。系統也會假設 exports_filter
比對到的每個目標都會匯出。
範例中的每個 cc_library
最多只能出現在一個 cc_shared_library
中。如果我們也想將 baz
連結到 bar_shared
,就需要在 baz
中加入 tags = ["LINKABLE_MORE_THAN_ONCE"]
。
由於 shared_lib_name
屬性,bar_shared
產生的檔案會命名為 bar.so
,而不是 Linux 預設的 libbar.so
。
錯誤
Two shared libraries in dependencies export the same symbols.
只要您建立的目標有兩個不同的cc_shared_library
依附元件匯出相同目標,就會發生這種情況。如要修正這個問題,請在其中一個 cc_shared_library
依附元件中停止匯出程式庫。
Two shared libraries in dependencies link the same library statically
當您使用兩個不同的 cc_shared_library
依附元件建立新的 cc_shared_library
,並靜態連結相同目標時,就會發生這種情況。與匯出錯誤類似。
如要修正這個問題,請停止將程式庫連結至其中一個cc_shared_library
依附元件。同時,仍連結該程式庫的項目必須匯出程式庫,這樣未連結的項目才能繼續查看符號。另一種方式是抽出匯出目標的第三個程式庫。第三種方式是使用 LINKABLE_MORE_THAN_ONCE
標記違規的 cc_library
,但這種修正方式不常見,而且您絕對要確保 cc_library
確實可多次連結。
'//foo:foo' is already linked statically in '//bar:bar' but not exported`
也就是說,您 deps
的遞移閉包中的程式庫可存取,不必經過其中一個 cc_shared_library
依附元件,但已連結至 dynamic_deps
中的不同 cc_shared_library
,且未匯出。
解決方法是從 cc_shared_library
依附元件匯出,或是提取匯出該元件的第三方 cc_shared_library
。
Do not place libraries which only contain a precompiled dynamic library in deps.
如果您有預先編譯的動態程式庫,則不需要也不會靜態連結至目前建立的 cc_shared_library
目標。因此不屬於 cc_shared_library
的 deps
。如果這個預先編譯的動態程式庫是其中一個 cc_libraries
的依附元件,則 cc_library
需要直接依附該程式庫。
Trying to export a library already exported by a different shared library
如果您在目前的規則中聲明要匯出目標,但該目標已由其中一個動態依附元件匯出,就會看到這則錯誤訊息。
如要修正這個問題,請從 deps
移除目標,並只依賴動態依附元件,或確保 exports_filter
不會擷取這個目標。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為
只要這些直接依附元件的任何遞移程式庫依附元件尚未由
在分析期間,規則實作會將
如果將同一個程式庫靜態連結至多個 |
additional_linker_inputs
|
標籤清單;預設值為 user_link_flags 屬性執行這項操作。
|
dynamic_deps
|
標籤清單;預設值為 cc_shared_library 。
|
exports_filter
|
字串清單;預設值為
任何目標
請注意,這項屬性實際上不會將依附元件邊緣新增至這些目標,依附元件邊緣應改由 允許的語法如下:
|
shared_lib_name
|
字串;預設值為 |
user_link_flags
|
字串清單;預設值為 cc_shared_library( name = "foo_shared", additional_linker_inputs = select({ "//src/conditions:linux": [ ":foo.lds", ":additional_script.txt", ], "//conditions:default": []}), user_link_flags = select({ "//src/conditions:linux": [ "-Wl,-rpath,kittens", "-Wl,--version-script=$(location :foo.lds)", "-Wl,--script=$(location :additional_script.txt)", ], "//conditions:default": []}), ... ) |
win_def_file
|
標籤;預設值為 只有在 Windows 是目標平台時,才應使用這項屬性。 連結共用程式庫時,可用於 匯出符號。 |
cc_static_library
查看規則來源cc_static_library(name, deps, tags)
產生的靜態程式庫包含 deps
中列出的目標物件檔案,以及這些檔案的遞移依附元件,並優先使用 PIC
物件。
輸出群組
linkdeps
文字檔,內含 deps
中列出目標的遞移依附元件標籤,這些依附元件未將任何物件檔案提供給靜態程式庫,但至少提供一個靜態、動態或介面程式庫。產生的靜態程式庫可能需要這些程式庫在連結時可用。
linkopts
文字檔,內含 deps
中列出目標的所有遞移依附元件的使用者提供 linkopts
。
重複符號
根據預設,cc_static_library
規則會檢查產生的靜態程式庫是否含有重複符號。如果發生這種情況,建構作業會失敗,並顯示錯誤訊息,列出重複的符號和包含這些符號的物件檔案。
您可以透過設定 features = ["-symbol_check"]
停用每個目標或套件的這項檢查,也可以透過 --features=-symbol_check
全域停用。
symbol_check
的工具鍊支援
Bazel 隨附的自動設定 C++ 工具鍊支援所有平台上的 symbol_check
功能。自訂工具鍊可透過下列兩種方式之一,新增對該項目的支援:
- 實作
ACTION_NAMES.validate_static_library
動作,並透過symbol_check
功能啟用該動作。系統會使用兩個引數叫用動作中設定的工具:要檢查重複符號的靜態程式庫,以及檢查通過時必須建立的檔案路徑。 symbol_check
功能會新增封存器旗標,導致建立靜態程式庫的動作因符號重複而失敗。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
deps
|
標籤清單;預設值為 不提供任何物件檔案的依附元件不會納入靜態程式庫,但其標籤會收集在 |
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
|
標籤;預設值為 |
memprof_profile
查看規則來源memprof_profile(name, absolute_path_profile, compatible_with, deprecation, distribs, features, licenses, profile, restricted_to, tags, target_compatible_with, testonly, visibility)
代表工作區中或指定絕對路徑的 MEMPROF 設定檔。範例:
memprof_profile( name = "memprof", profile = "//path/to/memprof:profile.afdo", ) memprof_profile( name = "memprof_abs", absolute_path_profile = "/absolute/path/profile.afdo", )
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
absolute_path_profile
|
字串;預設值為 |
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, link_extra_lib, 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
|
字串清單;預設值為
須視「建立變數」替代項目而定。
每個字串都會加上 標頭必須新增至 srcs 或 hdrs,否則在編譯作業受到沙箱限制時 (預設),標頭將無法供相依規則使用。 |
link_extra_lib
|
標籤;預設值為
根據預設,C++ 二進位檔會連結至 |
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_files, compiler_files_without_includes, coverage_files, 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_files
、linker_files
等屬性,或是以_files
結尾的其他屬性。這些屬性最常見於 filegroups,會將所有必要檔案 globbing。 -
為 C++ 動作產生正確的命令列。這項操作透過
CcToolchainConfigInfo
提供者完成 (詳情請見下文)。
使用 toolchain_config
屬性設定 C++ 工具鍊。
如需詳細的 C++ 工具鍊設定和工具鍊選取說明文件,請參閱
這個頁面
。
使用 tags = ["manual"]
,避免在叫用 bazel build //...
時不必要地建構及設定工具鍊
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
all_files
|
標籤 (必填) 所有 cc_toolchain 構件的集合。這些構件會新增為所有 rules_cc 相關動作的輸入內容 (但使用下列屬性中更精確構件集的動作除外)。Bazel 假設all_files 是所有其他提供構件屬性的超集 (例如,linkstamp 編譯需要編譯和連結檔案,因此會採用 all_files )。
這是 |
ar_files
|
標籤;預設值為 收集封存動作所需的所有 cc_toolchain 構件。 |
as_files
|
標籤;預設值為 組裝動作所需的所有 cc_toolchain 構件集合。 |
compiler_files
|
標籤 (必填) Collection of all cc_toolchain artifacts required for compile actions. |
compiler_files_without_includes
|
標籤;預設值為 |
coverage_files
|
標籤;預設值為 |
dwp_files
|
標籤 (必填) 收集 dwp 動作所需的所有 cc_toolchain 構件。 |
dynamic_runtime_lib
|
標籤;預設值為 啟用「static_link_cpp_runtimes」功能,並動態連結依附元件時,就會使用這個值。 |
exec_transition_for_inputs
|
布林值;預設值為 |
libc_top
|
標籤;預設值為 |
linker_files
|
標籤 (必填) 收集連結動作所需的所有 cc_toolchain 構件。 |
module_map
|
標籤;預設值為 |
objcopy_files
|
標籤 (必填) 收集 objcopy 動作所需的所有 cc_toolchain 構件。 |
static_runtime_lib
|
標籤;預設值為 啟用「static_link_cpp_runtimes」功能並靜態連結依附元件時,系統會使用這個值。 |
strip_files
|
標籤 (必填) 所有 cc_toolchain 構件的集合,這些構件是剝除動作的必要條件。 |
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++ 工具鍊。
-
根據傳遞至 Bazel 的
--cpu
和--compiler
選項選取其中一個工具鍊。
如需詳細的 C++ 工具鍊設定和工具鍊選取說明文件,請參閱 這個頁面 。
引數
屬性 | |
---|---|
name |
名稱:必填 這個目標的專屬名稱。 |
toolchains
|
從「<cpu>」或「<cpu>|<compiler>」字串到 cc_toolchain 標籤的對應。如果只將 --cpu 傳遞至 Bazel,系統會使用「<cpu>」;如果將 --cpu 和 --compiler 傳遞至 Bazel,系統會使用「<cpu>|<compiler>」。範例:
cc_toolchain_suite( name = "toolchain", toolchains = { "piii|gcc": ":my_cc_toolchain_for_piii_using_gcc", "piii": ":my_cc_toolchain_for_piii_using_default_compiler", }, ) |