question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Interpolate bash variable into a genrule's string parameter macro

See original GitHub issue

We’d like to do something like

# foo/BUCK
for i in ['Linux', 'Darwin']:
  genrule(
    name = 'bar-' + i,
    # blah
  )

genrule(
  name = 'bar',
  cmd = 'cp $(location :bar-$(uname -s)) $OUT',
  out = 'my-plat-bar'
)

where we want to let people refer to //foo:bar that’s specific to their own system’s platform, but also allow them to refer to //foo:bar-Linux and //foo:bar-Darwin if they want to explicitly build the genrule for the other architecture.

Unfortunately, using bash interpolation within a string parameter macro like $(location {}) doesn’t work; it’d be nice if it did.

Maybe we just shouldn’t have the convenience of having a single build rule refer have different outputs depending on platform? Seems unfortunate, though 😦

(Context: we currently use import sys, an unsafe import, to do this from python instead of inside the genrule itself.)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jiangty-addeparcommented, Oct 23, 2017

Oh, that helps! Thanks 😃

0reactions
ttsugriycommented, Oct 21, 2017

I’m not sure what are you using sh_binary for, since having a dependency on a genrule is sufficient to create its output for the rule that depends on it. I think adding an executable = Trueto your genrule is all you need to use genrules directly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Variable interpolation in the shell - bash - Stack Overflow
This is why export PATH=$PATH:$addpath works to add :$addpath (which would be subject to parameter expansion) to the PATH environment variable.
Read more >
variable interpolation in shell script - Unix Stack Exchange
A very common mistake. This is missing curly braces: ERROR_FILE="$HOME/pg_bak/error_bak/$FILE_error.txt". and is fixed by:
Read more >
General Rules | Bazel
The genrule command is executed by a Bash shell that is configured to fail when a command or a pipeline fails, using set...
Read more >
Pass interpolating string to macro - Julia Discourse
I'm trying to define basic macro but fail: macro mydebug(s) :(println($s)) end function main() i = 42 @mydebug("$(i)") end. This one yield:
Read more >
How to allow arbitrary expressions in format strings
If the format! macro interpolates only identifiers, then in my ... enough that named variables and string interpolation are required. At the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found