Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Bazel genrules offer a temp directory?

Tags:

bazel

Does Bazel offer a variable substitution for a temp directory in genrules?

Sometimes I need a staging area before creating the final output artefact.

I am imagining something like this:

genrule(
    name = "example",
    srcs = [ "a.txt" ],
    cmd = "cp $< $(TMP)/b.txt && cp $(TMP)/b.txt $@",
)

$(TMP) would be a folder generated for me by Bazel on each rule execution.

like image 484
sdgfsdh Avatar asked Mar 05 '19 11:03

sdgfsdh


People also ask

Which directory is used for holding temporary files?

In Unix and Linux, the global temporary directories are /tmp and /var/tmp. Web browsers periodically write data to the tmp directory during page views and downloads. Typically, /var/tmp is for persistent files (as it may be preserved over reboots), and /tmp is for more temporary files.

What is the Temp directory used for?

The Temp folder is a directory on your Windows PC used to store temporary files. Clearing the Temp folder is a standard procedure for system administration to reduce the amount of storage space used.

What is Genrule?

A genrule generates one or more files using a user-defined Bash command. Genrules are generic build rules that you can use if there's no specific rule for the task. For example, you could run a Bash one-liner.

How do you use a Bazel bin?

To run Bazel, go to your base workspace directory or any of its subdirectories and type bazel . % bazel help [Bazel release bazel-<version>] Usage: bazel <command> <options> ... Available commands: analyze-profile Analyzes build profile data. aquery Executes a query on the post-analysis action graph.


2 Answers

No it doesn't. (As of Bazel 0.23.1)

It does set $TMPDIR though (even with --incompatible_strict_action_env), so mktemp should work. But $TMPDIR is by no means a dedicated temp directory (it's often just /tmp), so be careful what you clobber.

like image 172
László Avatar answered Oct 11 '22 23:10

László


I migrated my genrule to a full Starlark rule. There I can do

tmp = ctx.actions.declare_directory("TMP_" + ctx.label.name)

and just use that directory as my temp in further actions.

It is similar to what the Starlark tutorial shows, in https://docs.bazel.build/versions/2.0.0/skylark/rules-tutorial.html#creating-a-file. The difference is that I do not register that directory as an output. That is, I don't do something like

return [DefaultInfo(files = depset([tmp]))]
like image 42
user7610 Avatar answered Oct 12 '22 00:10

user7610