構成可能な属性(一般に select()
と呼ばれます)は、ユーザーがコマンドラインでビルドルールの属性の値を切り替えることができます。
たとえば、アーキテクチャに適した実装を自動的に選択するマルチプラットフォーム ライブラリや、ビルド時にカスタマイズできる機能構成可能なバイナリに使用できます。
例
# myapp/BUILD
cc_binary(
name = "mybinary",
srcs = ["main.cc"],
deps = select({
":arm_build": [":arm_lib"],
":x86_debug_build": [":x86_dev_lib"],
"//conditions:default": [":generic_lib"],
}),
)
config_setting(
name = "arm_build",
values = {"cpu": "arm"},
)
config_setting(
name = "x86_debug_build",
values = {
"cpu": "x86",
"compilation_mode": "dbg",
},
)
これは、コマンドラインのフラグに基づいて依存関係を「選択」する cc_binary
を宣言しています。具体的には、deps
は次のようになります。
コマンド | deps = |
bazel build //myapp:mybinary --cpu=arm |
[":arm_lib"] |
bazel build //myapp:mybinary -c dbg --cpu=x86 |
[":x86_dev_lib"] |
bazel build //myapp:mybinary --cpu=ppc |
[":generic_lib"] |
bazel build //myapp:mybinary -c dbg --cpu=ppc |
[":generic_lib"] |
select()
は、構成条件(config_setting
ターゲットを参照するラベル)に基づいて選択される値のプレースホルダとして機能します。構成可能な属性で select()
を使用すると、さまざまな条件が満たされるときに、属性に異なる値を効果的に適用できます。
一致は明確である必要があります。複数の条件が一致する場合は、次のいずれかになります。
* すべて同じ値に解決されます。たとえば、Linux x86 で実行している場合、どちらのブランチも「hello」に解決されるため、{"@platforms//os:linux": "Hello", "@platforms//cpu:x86_64": "Hello"}
は一義的です。* One の values
は、他のすべてのコードの厳密なスーパーセットです。たとえば、values = {"cpu": "x86", "compilation_mode": "dbg"}
は values = {"cpu": "x86"}
を明確に特殊化したものです。
組み込み条件 //conditions:default
は、他に何も実行されていない場合に自動的に一致します。
この例では deps
を使用していますが、select()
は srcs
、resources
、cmd
など、他のほとんどの属性でも同様に機能します。構成不可能な属性はごく一部であり、明確に注釈が付けられています。たとえば、config_setting
独自の values
属性は構成できません。
select()
と依存関係
一部の属性は、ターゲット内のすべての推移的依存関係のビルド パラメータを変更します。たとえば、genrule
の tools
は、--cpu
を Bazel を実行しているマシンの CPU に変更します(クロスコンパイルにより、ターゲットがビルドされている CPU とは異なる場合があります)。これを構成の移行といいます。
与えられた
#myapp/BUILD
config_setting(
name = "arm_cpu",
values = {"cpu": "arm"},
)
config_setting(
name = "x86_cpu",
values = {"cpu": "x86"},
)
genrule(
name = "my_genrule",
srcs = select({
":arm_cpu": ["g_arm.src"],
":x86_cpu": ["g_x86.src"],
}),
tools = select({
":arm_cpu": [":tool1"],
":x86_cpu": [":tool2"],
}),
)
cc_binary(
name = "tool1",
srcs = select({
":arm_cpu": ["armtool.cc"],
":x86_cpu": ["x86tool.cc"],
}),
)
ランニング
$ bazel build //myapp:my_genrule --cpu=arm
x86
デベロッパー マシンで、ビルドを g_arm.src
、tool1
、x86tool.cc
にバインドします。my_genrule
に接続されている select
はどちらも、--cpu=arm
を含む my_genrule
のビルド パラメータを使用します。tool1
とその推移的依存関係の tools
属性は、--cpu
を x86
に変更します。tool1
の select
は、--cpu=x86
を含む tool1
のビルド パラメータを使用します。
構成条件
構成可能な属性の各キーは、config_setting
または constraint_value
へのラベル参照です。
config_setting
は、想定されるコマンドライン フラグ設定の単なる集合です。これらをターゲットにカプセル化することで、ユーザーが複数の場所から参照できる「標準」の条件を簡単に維持できます。
constraint_value
は、マルチプラットフォーム動作をサポートします。
組み込みフラグ
--cpu
のようなフラグは Bazel に組み込まれています。ビルドツールは、すべてのプロジェクトのすべてのビルドでフラグをネイティブに認識します。これらは、config_setting
の values
属性で指定されます。
config_setting(
name = "meaningful_condition_name",
values = {
"flag1": "value1",
"flag2": "value2",
...
},
)
flagN
はフラグ名です(--
がないため、"--cpu"
ではなく "cpu"
)。そのフラグの期待値は valueN
です。:meaningful_condition_name
は、values
のすべてのエントリが一致する場合に一致します。順序は無関係です。
valueN
は、コマンドラインで設定した場合と同様に解析されます。これは次のことを意味します。
bazel build -c opt
に一致するvalues = { "compilation_mode": "opt" }
bazel build --force_pic=1
に一致するvalues = { "force_pic": "true" }
bazel build --noforce_pic
に一致するvalues = { "force_pic": "0" }
config_setting
は、ターゲットの動作に影響するフラグのみをサポートしています。たとえば、--show_progress
は、Bazel がユーザーに進行状況を報告する方法にのみ影響するため、許可されません。ターゲットはそのフラグを使用して結果を構築できません。サポートされているフラグの正確なセットについては説明されていません。実際には、「意味がある」フラグのほとんどが機能します。
カスタムフラグ
Starlark ビルド設定を使用すると、独自のプロジェクト固有のフラグをモデル化できます。組み込みフラグとは異なり、これらはビルド ターゲットとして定義されるため、Bazel はターゲット ラベルで参照します。
これは、config_setting
の flag_values
属性でトリガーされます。
config_setting(
name = "meaningful_condition_name",
flag_values = {
"//myflags:flag1": "value1",
"//myflags:flag2": "value2",
...
},
)
動作は組み込みフラグと同じです。実際の例については、こちらをご覧ください。
--define
は、カスタムフラグ(--define foo=bar
など)の代替の以前の構文です。これは、values 属性(values = {"define": "foo=bar"}
)または define_values 属性(define_values = {"foo": "bar"}
)で表すことができます。--define
は下位互換性のためにのみサポートされます。可能な限り Starlark ビルド設定を優先します。
values
、flag_values
、define_values
は個別に評価されます。config_setting
は、すべての値が一致する場合に一致します。
デフォルトの条件
組み込み条件 //conditions:default
は、他の条件と一致しない場合に一致します。
「1 つの一致」ルールのため、一致もデフォルト条件もない構成可能な属性の場合は、"no matching conditions"
エラーが出力されます。これにより、予期しない設定によるサイレント障害から保護できます。
# myapp/BUILD
config_setting(
name = "x86_cpu",
values = {"cpu": "x86"},
)
cc_library(
name = "x86_only_lib",
srcs = select({
":x86_cpu": ["lib.cc"],
}),
)
$ bazel build //myapp:x86_only_lib --cpu=arm
ERROR: Configurable attribute "srcs" doesn't match this configuration (would
a default condition help?).
Conditions checked:
//myapp:x86_cpu
エラーをさらに明確にするために、select()
の no_match_error
属性を使用してカスタム メッセージを設定できます。
プラットフォーム
コマンドラインで複数のフラグを指定できるため柔軟性がありますが、ターゲットをビルドするたびに個別にフラグを設定するのは手間がかかります。プラットフォームを使用すると、これらをシンプルなバンドルに統合できます。
# myapp/BUILD
sh_binary(
name = "my_rocks",
srcs = select({
":basalt": ["pyroxene.sh"],
":marble": ["calcite.sh"],
"//conditions:default": ["feldspar.sh"],
}),
)
config_setting(
name = "basalt",
constraint_values = [
":black",
":igneous",
],
)
config_setting(
name = "marble",
constraint_values = [
":white",
":metamorphic",
],
)
# constraint_setting acts as an enum type, and constraint_value as an enum value.
constraint_setting(name = "color")
constraint_value(name = "black", constraint_setting = "color")
constraint_value(name = "white", constraint_setting = "color")
constraint_setting(name = "texture")
constraint_value(name = "smooth", constraint_setting = "texture")
constraint_setting(name = "type")
constraint_value(name = "igneous", constraint_setting = "type")
constraint_value(name = "metamorphic", constraint_setting = "type")
platform(
name = "basalt_platform",
constraint_values = [
":black",
":igneous",
],
)
platform(
name = "marble_platform",
constraint_values = [
":white",
":smooth",
":metamorphic",
],
)
プラットフォームはコマンドラインで指定できます。プラットフォームの constraint_values
のサブセットを含む config_setting
を有効にして、これらの config_setting
を select()
式で一致できるようにします。
たとえば、my_rocks
の srcs
属性を calcite.sh
に設定するには、次のコマンドを実行します。
bazel build //my_app:my_rocks --platforms=//myapp:marble_platform
プラットフォームがなければ、
bazel build //my_app:my_rocks --define color=white --define texture=smooth --define type=metamorphic
select()
は、constraint_value
を直接読み取ることもできます。
constraint_setting(name = "type")
constraint_value(name = "igneous", constraint_setting = "type")
constraint_value(name = "metamorphic", constraint_setting = "type")
sh_binary(
name = "my_rocks",
srcs = select({
":igneous": ["igneous.sh"],
":metamorphic" ["metamorphic.sh"],
}),
)
これにより、単一の値に対してチェックを行うだけでよい場合に、ボイラープレート config_setting
を使用する必要がなくなります。
プラットフォームはまだ開発中です。詳しくは、ドキュメントをご覧ください。
select()
を組み合わせる
select
は同じ属性で複数回使用できます。
sh_binary(
name = "my_target",
srcs = ["always_include.sh"] +
select({
":armeabi_mode": ["armeabi_src.sh"],
":x86_mode": ["x86_src.sh"],
}) +
select({
":opt_mode": ["opt_extras.sh"],
":dbg_mode": ["dbg_extras.sh"],
}),
)
select
を他の select
の中に指定することはできません。selects
をネストする必要があり、属性が他のターゲットを値として受け取る場合は、中間ターゲットを使用します。
sh_binary(
name = "my_target",
srcs = ["always_include.sh"],
deps = select({
":armeabi_mode": [":armeabi_lib"],
...
}),
)
sh_library(
name = "armeabi_lib",
srcs = select({
":opt_mode": ["armeabi_with_opt.sh"],
...
}),
)
複数の条件が一致する場合に select
を一致させる必要がある場合は、AND チェーンを検討してください。
OR チェーン
次の点を考慮してください。
sh_binary(
name = "my_target",
srcs = ["always_include.sh"],
deps = select({
":config1": [":standard_lib"],
":config2": [":standard_lib"],
":config3": [":standard_lib"],
":config4": [":special_lib"],
}),
)
ほとんどの条件は同じ依存関係に評価されます。ただし、この構文は読み取りも維持も困難です。[":standard_lib"]
を複数回繰り返す必要がないようにするとよいでしょう。
選択肢の一つとして、値を BUILD 変数として事前定義する方法があります。
STANDARD_DEP = [":standard_lib"]
sh_binary(
name = "my_target",
srcs = ["always_include.sh"],
deps = select({
":config1": STANDARD_DEP,
":config2": STANDARD_DEP,
":config3": STANDARD_DEP,
":config4": [":special_lib"],
}),
)
これにより、依存関係の管理が容易になります。ただし、不要な重複が発生します。
より直接的なサポートを受けるには、次のいずれかを使用してください。
selects.with_or
Skylib の selects
モジュールの with_or マクロは、select
内で直接 OR
ing 条件をサポートします。
load("@bazel_skylib//lib:selects.bzl", "selects")
sh_binary(
name = "my_target",
srcs = ["always_include.sh"],
deps = selects.with_or({
(":config1", ":config2", ":config3"): [":standard_lib"],
":config4": [":special_lib"],
}),
)
selects.config_setting_group
Skylib の selects
モジュール内の config_setting_group マクロは、複数の config_setting
の OR
をサポートしています。
load("@bazel_skylib//lib:selects.bzl", "selects")
config_setting(
name = "config1",
values = {"cpu": "arm"},
)
config_setting(
name = "config2",
values = {"compilation_mode": "dbg"},
)
selects.config_setting_group(
name = "config1_or_2",
match_any = [":config1", ":config2"],
)
sh_binary(
name = "my_target",
srcs = ["always_include.sh"],
deps = select({
":config1_or_2": [":standard_lib"],
"//conditions:default": [":other_lib"],
}),
)
selects.with_or
とは異なり、異なるターゲットは異なる属性間で :config1_or_2
を共有できます。
1 つの条件が他の条件を明確に「特殊化」していないか、すべての条件が同じ値に解決されている場合を除き、複数の条件が一致するとエラーになります。詳しくはこちらをご覧ください。
AND チェーン
複数の条件が一致する場合に select
分岐を一致させる必要がある場合は、Skylib マクロ config_setting_group を使用します。
config_setting(
name = "config1",
values = {"cpu": "arm"},
)
config_setting(
name = "config2",
values = {"compilation_mode": "dbg"},
)
selects.config_setting_group(
name = "config1_and_2",
match_all = [":config1", ":config2"],
)
sh_binary(
name = "my_target",
srcs = ["always_include.sh"],
deps = select({
":config1_and_2": [":standard_lib"],
"//conditions:default": [":other_lib"],
}),
)
OR チェーンとは異なり、既存の config_setting
を select
内で直接 AND
にすることはできません。これらを明示的に config_setting_group
でラップする必要があります。
カスタム エラー メッセージ
デフォルトでは、一致する条件がない場合、select()
が接続されているターゲットはエラーで失敗します。
ERROR: Configurable attribute "deps" doesn't match this configuration (would
a default condition help?).
Conditions checked:
//tools/cc_target_os:darwin
//tools/cc_target_os:android
これは、no_match_error
属性を使用してカスタマイズできます。
cc_library(
name = "my_lib",
deps = select(
{
"//tools/cc_target_os:android": [":android_deps"],
"//tools/cc_target_os:windows": [":windows_deps"],
},
no_match_error = "Please build with an Android or Windows toolchain",
),
)
$ bazel build //myapp:my_lib
ERROR: Configurable attribute "deps" doesn't match this configuration: Please
build with an Android or Windows toolchain
ルールの互換性
ルールの実装は、構成可能な属性の解決された値を受け取ります。たとえば、
# myapp/BUILD
some_rule(
name = "my_target",
some_attr = select({
":foo_mode": [":foo"],
":bar_mode": [":bar"],
}),
)
$ bazel build //myapp/my_target --define mode=foo
ルール実装コードで ctx.attr.some_attr
が [":foo"]
と表示される。
マクロは select()
句を受け入れ、ネイティブ ルールに渡すことができます。ただし、データを直接操作することはできません。たとえば マクロを変換して
select({"foo": "val"}, ...)
たとえば
select({"foo": "val_with_suffix"}, ...)
これには 2 つの理由があります。
まず、select
が選択するパスを知る必要があるマクロは機能しません。マクロは、フラグ値が判明する前に行われる Bazel の読み込みフェーズで評価されるためです。これは Bazel の設計上の制限であり、すぐに変更される可能性があります。
2 つ目に、すべての select
パスを反復処理するだけのマクロは、技術的には可能ですが、UI が一貫していません。これを変えるにはさらなる設計が必要です。
Bazel クエリと cquery
Bazel query
は、Bazel の読み込みフェーズに沿って動作します。つまり、ターゲットが使用するコマンドライン フラグは認識されません。これらのフラグは、ビルドの後半(分析フェーズ)まで評価されないためです。そのため、どの select()
分岐が選択されているかを判断できません。
Bazel cquery
は、Bazel の分析フェーズ後に動作するため、この情報がすべて含まれ、select()
を正確に解決できます。
次の点を検討します。
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
# myapp/BUILD
string_flag(
name = "dog_type",
build_setting_default = "cat"
)
cc_library(
name = "my_lib",
deps = select({
":long": [":foo_dep"],
":short": [":bar_dep"],
}),
)
config_setting(
name = "long",
flag_values = {":dog_type": "dachshund"},
)
config_setting(
name = "short",
flag_values = {":dog_type": "pug"},
)
query
は、:my_lib
の依存関係を過剰近似しています。
$ bazel query 'deps(//myapp:my_lib)'
//myapp:my_lib
//myapp:foo_dep
//myapp:bar_dep
cquery
は正確な依存関係を表示します。
$ bazel cquery 'deps(//myapp:my_lib)' --//myapp:dog_type=pug
//myapp:my_lib
//myapp:bar_dep
よくある質問
マクロで select() が動作しない理由
select() はルールでは機能します。詳しくは、ルールの互換性をご覧ください。
この質問でよく見られる主な問題は、select() がマクロで機能しないことです。これらはルールとは異なります。違いについては、ルールとマクロのドキュメントをご覧ください。エンドツーエンドの例を次に示します。
ルールとマクロを定義します。
# myapp/defs.bzl
# Rule implementation: when an attribute is read, all select()s have already
# been resolved. So it looks like a plain old attribute just like any other.
def _impl(ctx):
name = ctx.attr.name
allcaps = ctx.attr.my_config_string.upper() # This works fine on all values.
print("My name is " + name + " with custom message: " + allcaps)
# Rule declaration:
my_custom_bazel_rule = rule(
implementation = _impl,
attrs = {"my_config_string": attr.string()},
)
# Macro declaration:
def my_custom_bazel_macro(name, my_config_string):
allcaps = my_config_string.upper() # This line won't work with select(s).
print("My name is " + name + " with custom message: " + allcaps)
ルールとマクロをインスタンス化します。
# myapp/BUILD
load("//myapp:defs.bzl", "my_custom_bazel_rule")
load("//myapp:defs.bzl", "my_custom_bazel_macro")
my_custom_bazel_rule(
name = "happy_rule",
my_config_string = select({
"//third_party/bazel_platforms/cpu:x86_32": "first string",
"//third_party/bazel_platforms/cpu:ppc": "second string",
}),
)
my_custom_bazel_macro(
name = "happy_macro",
my_config_string = "fixed string",
)
my_custom_bazel_macro(
name = "sad_macro",
my_config_string = select({
"//third_party/bazel_platforms/cpu:x86_32": "first string",
"//third_party/bazel_platforms/cpu:ppc": "other string",
}),
)
sad_macro
が select()
を処理できないため、ビルドが失敗します。
$ bazel build //myapp:all
ERROR: /myworkspace/myapp/BUILD:17:1: Traceback
(most recent call last):
File "/myworkspace/myapp/BUILD", line 17
my_custom_bazel_macro(name = "sad_macro", my_config_stri..."}))
File "/myworkspace/myapp/defs.bzl", line 4, in
my_custom_bazel_macro
my_config_string.upper()
type 'select' has no method upper().
ERROR: error loading package 'myapp': Package 'myapp' contains errors.
sad_macro
をコメントアウトすると、ビルドは成功します。
# Comment out sad_macro so it doesn't mess up the build.
$ bazel build //myapp:all
DEBUG: /myworkspace/myapp/defs.bzl:5:3: My name is happy_macro with custom message: FIXED STRING.
DEBUG: /myworkspace/myapp/hi.bzl:15:3: My name is happy_rule with custom message: FIRST STRING.
定義上マクロは Bazel がビルドのコマンドライン フラグを読み取る前に評価されるため、これを変更することはできません。これは、select() を評価するための十分な情報がないことを意味します。
ただし、マクロは select()
を不透明な blob としてルールに渡すことができます。
# myapp/defs.bzl
def my_custom_bazel_macro(name, my_config_string):
print("Invoking macro " + name)
my_custom_bazel_rule(
name = name + "_as_target",
my_config_string = my_config_string,
)
$ bazel build //myapp:sad_macro_less_sad
DEBUG: /myworkspace/myapp/defs.bzl:23:3: Invoking macro sad_macro_less_sad.
DEBUG: /myworkspace/myapp/defs.bzl:15:3: My name is sad_macro_less_sad with custom message: FIRST STRING.
select() が常に true を返すのはなぜですか。
定義上、マクロは(ルールではなく)select()
を評価できないため、評価しようとすると通常、エラーが発生します。
ERROR: /myworkspace/myapp/BUILD:17:1: Traceback
(most recent call last):
File "/myworkspace/myapp/BUILD", line 17
my_custom_bazel_macro(name = "sad_macro", my_config_stri..."}))
File "/myworkspace/myapp/defs.bzl", line 4, in
my_custom_bazel_macro
my_config_string.upper()
type 'select' has no method upper().
ブール値は通知なく失敗する特殊なケースであるため、特に注意してください。
$ cat myapp/defs.bzl
def my_boolean_macro(boolval):
print("TRUE" if boolval else "FALSE")
$ cat myapp/BUILD
load("//myapp:defs.bzl", "my_boolean_macro")
my_boolean_macro(
boolval = select({
"//third_party/bazel_platforms/cpu:x86_32": True,
"//third_party/bazel_platforms/cpu:ppc": False,
}),
)
$ bazel build //myapp:all --cpu=x86
DEBUG: /myworkspace/myapp/defs.bzl:4:3: TRUE.
$ bazel build //mypro:all --cpu=ppc
DEBUG: /myworkspace/myapp/defs.bzl:4:3: TRUE.
これは、マクロが select()
の内容を認識しないためです。実際に評価されるのは、select()
オブジェクト自体です。Pythonic 設計標準によると、ごく一部の例外を除き、すべてのオブジェクトが自動的に true を返します。
辞書のように select() を読めるか
マクロは select を評価できません。これは、Bazel がビルドのコマンドライン パラメータを認識する前にマクロを評価するためです。たとえば、各値に接尾辞を追加するなど、少なくとも select()
の辞書を読み取ることはできるか?
概念的には可能ですが、まだ Bazel の機能ではありません。
ここでは、単純な辞書を作成して、それを select()
にフィードします。
$ cat myapp/defs.bzl
def selecty_genrule(name, select_cmd):
for key in select_cmd.keys():
select_cmd[key] += " WITH SUFFIX"
native.genrule(
name = name,
outs = [name + ".out"],
srcs = [],
cmd = "echo " + select(select_cmd + {"//conditions:default": "default"})
+ " > $@"
)
$ cat myapp/BUILD
selecty_genrule(
name = "selecty",
select_cmd = {
"//third_party/bazel_platforms/cpu:x86_32": "x86 mode",
},
)
$ bazel build //testapp:selecty --cpu=x86 && cat bazel-genfiles/testapp/selecty.out
x86 mode WITH SUFFIX
select()
型とネイティブ型の両方をサポートする場合は、次のようにします。
$ cat myapp/defs.bzl
def selecty_genrule(name, select_cmd):
cmd_suffix = ""
if type(select_cmd) == "string":
cmd_suffix = select_cmd + " WITH SUFFIX"
elif type(select_cmd) == "dict":
for key in select_cmd.keys():
select_cmd[key] += " WITH SUFFIX"
cmd_suffix = select(select_cmd + {"//conditions:default": "default"})
native.genrule(
name = name,
outs = [name + ".out"],
srcs = [],
cmd = "echo " + cmd_suffix + "> $@",
)
select() が bind() と連動しないのはなぜですか?
まず、bind()
は使用しないでください。alias()
が採用されたため、サポートが終了しています。
技術的には、bind()
は BUILD ルールではなくリポジトリ ルールです。
Repo ルールには特定の構成がなく、BUILD ルールと同じように評価されません。したがって、bind()
内の select()
を実際に特定のブランチに評価することはできません。
このタイプのランタイム判定を実行するには、代わりに alias()
を使用し、actual
属性に select()
を指定する必要があります。alias()
は BUILD ルールであり、特定の構成で評価されるため、これは正しく機能します。
$ cat WORKSPACE
workspace(name = "myapp")
bind(name = "openssl", actual = "//:ssl")
http_archive(name = "alternative", ...)
http_archive(name = "boringssl", ...)
$ cat BUILD
config_setting(
name = "alt_ssl",
define_values = {
"ssl_library": "alternative",
},
)
alias(
name = "ssl",
actual = select({
"//:alt_ssl": "@alternative//:ssl",
"//conditions:default": "@boringssl//:ssl",
}),
)
この設定では、--define ssl_library=alternative
を渡すことができます。//:ssl
または //external:ssl
に依存するターゲットには、@alternative//:ssl
にある代替手段が表示されます。
ただし、bind()
の使用はやめてください。
select() が期待したものを選択しないのはなぜですか?
//myapp:foo
に、想定する条件を選択しない select()
がある場合は、cquery と bazel config
を使用してデバッグします。
ビルドするトップレベル ターゲットが //myapp:foo
の場合は、次のコマンドを実行します。
$ bazel cquery //myapp:foo <desired build flags>
//myapp:foo (12e23b9a2b534a)
サブグラフのどこかで //myapp:foo に依存する他のターゲット //bar
をビルドする場合は、次のコマンドを実行します。
$ bazel cquery 'somepath(//bar, //myapp:foo)' <desired build flags>
//bar:bar (3ag3193fee94a2)
//bar:intermediate_dep (12e23b9a2b534a)
//myapp:foo (12e23b9a2b534a)
//myapp:foo
の隣にある (12e23b9a2b534a)
は、//myapp:foo
の select()
を解決する構成のハッシュです。その値は bazel config
で検査できます。
$ bazel config 12e23b9a2b534a
BuildConfigurationValue 12e23b9a2b534a
Fragment com.google.devtools.build.lib.analysis.config.CoreOptions {
cpu: darwin
compilation_mode: fastbuild
...
}
Fragment com.google.devtools.build.lib.rules.cpp.CppOptions {
linkopt: [-Dfoo=bar]
...
}
...
次に、この出力を各 config_setting
で想定される設定と比較します。
//myapp:foo
は、同じビルド内の異なる構成に存在する場合があります。somepath
を使用して適切なものを取得する方法については、cquery のドキュメントをご覧ください。
select()
がプラットフォームで動作しないのはなぜですか?
Bazel では、セマンティクスが不明瞭なため、特定のプラットフォームがターゲット プラットフォームかどうかを確認する構成可能な属性はサポートされていません。
次に例を示します。
platform(
name = "x86_linux_platform",
constraint_values = [
"@platforms//cpu:x86",
"@platforms//os:linux",
],
)
cc_library(
name = "lib",
srcs = [...],
linkopts = select({
":x86_linux_platform": ["--enable_x86_optimizations"],
"//conditions:default": [],
}),
)
ターゲット プラットフォームに @platforms//cpu:x86
と @platforms//os:linux
の両方の制約があり、ここで定義される :x86_linux_platform
ではない場合、この BUILD
ファイルでどの select()
を使用する必要がありますか?BUILD
ファイルの作成者と別のプラットフォームを定義したユーザーでは、考え方が異なる場合があります。
別の方法があれば教えてください。
代わりに、次の制約が設定された任意のプラットフォームに一致する config_setting
を定義します。
config_setting(
name = "is_x86_linux",
constraint_values = [
"@platforms//cpu:x86",
"@platforms//os:linux",
],
)
cc_library(
name = "lib",
srcs = [...],
linkopts = select({
":is_x86_linux": ["--enable_x86_optimizations"],
"//conditions:default": [],
}),
)
このプロセスでは特定のセマンティクスが定義され、どのプラットフォームが目的の条件を満たすかが明確になります。
YouTube で本当にselect
したいのであれば、どうすればよいですか?
ビルド要件でプラットフォームを確認する必要がある場合は、config_setting
の --platforms
フラグの値を反転できます。
config_setting(
name = "is_specific_x86_linux_platform",
values = {
"platforms": ["//package:x86_linux_platform"],
},
)
cc_library(
name = "lib",
srcs = [...],
linkopts = select({
":is_specific_x86_linux_platform": ["--enable_x86_optimizations"],
"//conditions:default": [],
}),
)
Bazel チームでは、この方法を推奨しているわけではありません。想定した条件が一致しない場合に、ビルドが過度に制約され、ユーザーが混乱します。