Defining configuration in existing rules

73 views
Skip to first unread message

Preston Van Loon

unread,
Jul 24, 2019, 11:32:41 AM7/24/19
to bazel-discuss
I've been trying to find a way to set cfg on rules from the attr level.

Here's an example of what we're trying to achieve:

go_library(
   name = "go_default_library",
   srcs = ["library.go"],
   deps = [
     select({
       "//:custom_define_config1" : "//:a,
       "//:custom_define_config2" : "//:b,
     })
   ],
)

config_setting(
    name = "custom_define_config1",
    define_values = {
        "foo": "bar",
    },
)

config_setting(
    name = "custom_define_config2",
    define_values = {
        "foo": "baz",
    },
)

We can conditionally build the library with --define foo=bar or --define foo=baz. However, I want to define these on certain targets.

go_test(
   name = "test_with_config1",
   srcs = ["config_test.go"],
   embed = [":go_default_library"],
   cfg = {"foo": "bar"},
)

go_test(
   name = "test_with_config2",
   srcs = ["config_test.go"],
   embed = [":go_default_library"],
   cfg = {"foo": "baz"},
)


How can this be achieved without forking rules_go with complicated starlark transitions?


Background / resources I've read:
- "Flagless multiplatform builds" https://github.com/bazelbuild/bazel/issues/6519

Greg Estren

unread,
Jul 25, 2019, 1:18:06 PM7/25/19
to Preston Van Loon, bazel-discuss
Hi Preston,

Starlark transitions are the way to do this, and I hope you ultimately don't find them *too* complicated. There's definitely infrastructure required to define them but I hope as time goes on there will be enough examples and support libraries to make common cases easy.

In particular check out https://github.com/gregestren/snippets/tree/master/cc_binary_selectable_copts, which speaks pretty closely to what you're describing. If that works for you it should be possible to get exactly the above while basically copying two setup files from that example, and without having to fork rules_go.

Let me know how that works (or not) for you.


--
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/dd0b6475-fc0c-4cda-8169-c64ad472a399%40googlegroups.com.

pre...@prysmaticlabs.com

unread,
Nov 15, 2019, 6:38:47 PM11/15/19
to bazel-discuss
Hey Greg,

Thanks for your reply. This is kind of what I am looking for but I am struggling to understand how to implement it with rules_go. Is there no way to describe the defined flags on a build target? 

It seems like the pieces for this are all there, but usability is not. Does a transition rule really have to copy the binary as it does in your example? 
I was hoping just to tell starlark "hey, compile me and all of my deps with --define foo=baz as described in the targets 'cfg' property".  

Best,
Preston 
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages