How to use select inside a skylark rule?

1,034 views
Skip to first unread message

Ming Zhao

unread,
Jul 1, 2015, 1:24:17 PM7/1/15
to bazel-...@googlegroups.com
I defined a selector inside my custom build rule:

build_config = select({
'//tools/cpp:asan_enabled': 'asan',
'//tools/cpp:msan_enabled': 'msan',
'//tools/cpp:tsan_enabled': 'tsan',
'//tools/cpp:ubsan_enabled': 'ubsan',
'//conditions:default': None,
})

But I have no idea how to access the value of build_config. I tried
print(dir(build_config)) but it has no method. Maybe I can only access
it in the macro?

Han-Wen Nienhuys

unread,
Jul 2, 2015, 3:15:35 AM7/2/15
to Ming Zhao, bazel-...@googlegroups.com
a snippet would be useful to understand what you are trying to do.

without looking, I imagine you are creating a rule (as opposed to a
macro), and would expect the rule to have a attribute, eg.

bla = rule(myfunc, attr = {"flavor": select({ .. }))

in myfunc, you should have access to ctx.attr.flavor, which will vary
based on the configuration you are using.
> --
> You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAN0GiO1toOju7BH5dYW5Do66obaFja9q2j6%3Dkgq49y1CxMr%2BRA%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Han-Wen Nienhuys
Google Munich
han...@google.com

Ming Zhao

unread,
Jul 7, 2015, 6:47:27 PM7/7/15
to Han-Wen Nienhuys, bazel-...@googlegroups.com
I think I tried that, but I got the following error:

$ bazel build pkg:lw-ats
ERROR: /usr/local/home/mzhao/s/m3/main/tools/build_rules/pkg_deb.bzl:89:11:
Illegal argument: expected <String, Builder> type for 'attrs' but got
<string, SelectorList> instead.
ERROR: /usr/local/home/mzhao/s/m3/main/pkg/BUILD:1: Extension file
'tools/build_rules/pkg_deb.bzl' has errors.
ERROR: error loading package 'pkg': Extension file
'tools/build_rules/pkg_deb.bzl' has errors.

I ended up creating another level of indirection by wrapping it with
another macro, as you can see in my skylark rule here:
https://github.com/mzhaom/trunk/blob/master/tools/build_rules/pkg_deb.bzl

Han-Wen Nienhuys

unread,
Jul 8, 2015, 8:09:55 AM7/8/15
to Ming Zhao, bazel-...@googlegroups.com, Laurent Le Brun
Hmm, let's ask our BUILD/skylark guru.

Laurent Le Brun

unread,
Jul 8, 2015, 9:56:58 AM7/8/15
to Han-Wen Nienhuys, Greg Estren, Ming Zhao, bazel-...@googlegroups.com
Hi,

Use select only in the loading phase. It will be evaluated before the
implementation function is called.
Providing a way to evaluate a select within analysis phase is a
feature request (cc Greg who may comment on whether it's a good idea
or not).
--
Laurent

Greg Estren

unread,
Jul 8, 2015, 1:43:09 PM7/8/15
to Laurent Le Brun, Han-Wen Nienhuys, Ming Zhao, bazel-...@googlegroups.com
I assume your failed attempt was to specify the select directly within the pkg_deb_ rule class definition, right? Like:

pkg_deb_ = rule(
  _pkg_deb_impl,
  attrs = {
    ...
     'build_config': select({...}),
  })

Shouldn't that be fully resolvable before the analysis phase begins? That syntax looks like that should work to me, although we may have to tidy up Skyframe to make it work. 

Greg Estren

unread,
Jul 8, 2015, 1:43:40 PM7/8/15
to Laurent Le Brun, Han-Wen Nienhuys, Ming Zhao, bazel-...@googlegroups.com
Err: Skyframe --> Skylark.

Greg Estren

unread,
Jul 8, 2015, 2:17:50 PM7/8/15
to Laurent Le Brun, Han-Wen Nienhuys, Ming Zhao, bazel-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages