简单 cc_library 规则作为 Skylark 规则
Simple cc_library rule as a Skylark rule
我有一个非常简单的 cc_library()
规则,我想将其转换为 Skylark 规则。
cc_library()
规则是:
cc_library(
name = 'cc_library_a',
srcs = [ 'a.c' ],
)
我面临的挑战是关于通过不同方法传递的编译标志——其中之一是命令行。
所以假设我 运行 以下命令行:
# bazel build :a --copt -H
这将在编译命令中添加-H
标志。这里的问题是如何将 -H
标志传播到 Skylark 规则?
我试过以下但没有用:
def _my_rule(ctx):
_arguments = [ '-c' ]
_arguments.append(ctx.file.src.path)
_arguments += ctx.fragments.cpp.c_options
_arguments += [ '-o', ctx.outputs.out.path ]
ctx.actions.run(
outputs = [ ctx.outputs.out ],
inputs = [ ctx.file.src ],
arguments = _arguments,
executable = ctx.fragments.cpp.compiler_executable,
)
my_rule = rule(
implementation = _my_rule,
attrs = {
'src': attr.label(allow_single_file = True),
},
outputs = {
'out': '%{name}.o',
},
fragments = [ 'cpp' ],
)
BUILD文件内容如下:
load(':rules.bzl', 'my_rule')
my_rule(
name = 'skylark_a',
src = 'a.c',
)
cc_library(
name = 'cc_library_a',
srcs = [ 'a.c' ],
)
构建 Skylark 规则的输出清楚地表明 --copt
被忽略了:
# bazel build skylark_a --copt -H --subcommands
INFO: Found 1 target...
>>>>> # //:skylark_a [action 'SkylarkAction skylark_a.o']
(cd /bazel/jbasila/_bazel_jbasila/4d692aace2e7e1a45eec9fac3922ea8d/execroot/__main__ && \
exec env - \
/usr/bin/gcc -c a.c -o bazel-out/local-fastbuild/bin/skylark_a.o)
INFO: From SkylarkAction skylark_a.o:
a.c: In function 'a':
a.c:3:5: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration]
puts("a");
^~~~
Target //:skylark_a up-to-date:
bazel-bin/skylark_a.o
INFO: Elapsed time: 0.578s, Critical Path: 0.05s
我错过了什么?
因为 --copt -H
参数对 C 和 C++ 是通用的,所以您需要使用 ctx.fragments.cpp.compiler_options([])
来获取标志。您使用的 ctx.fragments.cpp.c_options
代码仅适用于特定于 C 的选项。
同样,还有一个 ctx.fragments.cpp.cxx_options([])
函数可以为您提供特定于 C++ 的选项。
我有一个非常简单的 cc_library()
规则,我想将其转换为 Skylark 规则。
cc_library()
规则是:
cc_library(
name = 'cc_library_a',
srcs = [ 'a.c' ],
)
我面临的挑战是关于通过不同方法传递的编译标志——其中之一是命令行。 所以假设我 运行 以下命令行:
# bazel build :a --copt -H
这将在编译命令中添加-H
标志。这里的问题是如何将 -H
标志传播到 Skylark 规则?
我试过以下但没有用:
def _my_rule(ctx):
_arguments = [ '-c' ]
_arguments.append(ctx.file.src.path)
_arguments += ctx.fragments.cpp.c_options
_arguments += [ '-o', ctx.outputs.out.path ]
ctx.actions.run(
outputs = [ ctx.outputs.out ],
inputs = [ ctx.file.src ],
arguments = _arguments,
executable = ctx.fragments.cpp.compiler_executable,
)
my_rule = rule(
implementation = _my_rule,
attrs = {
'src': attr.label(allow_single_file = True),
},
outputs = {
'out': '%{name}.o',
},
fragments = [ 'cpp' ],
)
BUILD文件内容如下:
load(':rules.bzl', 'my_rule')
my_rule(
name = 'skylark_a',
src = 'a.c',
)
cc_library(
name = 'cc_library_a',
srcs = [ 'a.c' ],
)
构建 Skylark 规则的输出清楚地表明 --copt
被忽略了:
# bazel build skylark_a --copt -H --subcommands
INFO: Found 1 target...
>>>>> # //:skylark_a [action 'SkylarkAction skylark_a.o']
(cd /bazel/jbasila/_bazel_jbasila/4d692aace2e7e1a45eec9fac3922ea8d/execroot/__main__ && \
exec env - \
/usr/bin/gcc -c a.c -o bazel-out/local-fastbuild/bin/skylark_a.o)
INFO: From SkylarkAction skylark_a.o:
a.c: In function 'a':
a.c:3:5: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration]
puts("a");
^~~~
Target //:skylark_a up-to-date:
bazel-bin/skylark_a.o
INFO: Elapsed time: 0.578s, Critical Path: 0.05s
我错过了什么?
因为 --copt -H
参数对 C 和 C++ 是通用的,所以您需要使用 ctx.fragments.cpp.compiler_options([])
来获取标志。您使用的 ctx.fragments.cpp.c_options
代码仅适用于特定于 C 的选项。
同样,还有一个 ctx.fragments.cpp.cxx_options([])
函数可以为您提供特定于 C++ 的选项。