bazel - Simple cc_library rule as a Skylark rule -
i have simple cc_library() rule convert skylark rule.
the cc_library() rule is:
cc_library( name = 'cc_library_a', srcs = [ 'a.c' ], ) the challange i'm facing concerining compilation flags passed through different methods - one, command line. assume run following command line:
# bazel build :a --copt -h
this add -h flag compilation command. question here how can -h flag propagated skylark rule?
i have tried following did not work:
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' ], ) the build file content following:
load(':rules.bzl', 'my_rule') my_rule( name = 'skylark_a', src = 'a.c', ) cc_library( name = 'cc_library_a', srcs = [ 'a.c' ], ) the output of building skylark rule shows --copt ignored:
# 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: 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 what missing?
because --copt -h parameter generic c , c++ need flags using ctx.fragments.cpp.compiler_options([]) instead. ctx.fragments.cpp.c_options code using c specific options.
likewise, there ctx.fragments.cpp.cxx_options([]) function provides c++ specific options.
Comments
Post a Comment