diff options
author | Laurent Le Brun <laurentlb@google.com> | 2015-05-06 11:51:25 +0000 |
---|---|---|
committer | Lukacs Berki <lberki@google.com> | 2015-05-07 14:02:59 +0000 |
commit | 9357b27db77880c3b9beac3d6015c1e011a0c248 (patch) | |
tree | 0911b9371473ee8f891f10c595883fd4c60cf288 /site/docs/skylark | |
parent | ccd03539fd23375c8a20835c7ac048ce3e4717f0 (diff) |
Skylark cookbook example: Execute a binary
--
MOS_MIGRATED_REVID=92915630
Diffstat (limited to 'site/docs/skylark')
-rw-r--r-- | site/docs/skylark/cookbook.md | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/site/docs/skylark/cookbook.md b/site/docs/skylark/cookbook.md index 2a6468eaf4..d6ccb7d4ab 100644 --- a/site/docs/skylark/cookbook.md +++ b/site/docs/skylark/cookbook.md @@ -198,6 +198,92 @@ file( ``` +## <a name="execute-bin"></a>Execute a binary + +This rule executes an existing binary. In this particular example, the +binary is a tool that merges files. During the analysis phase, we cannot +access any arbitrary label: the dependency must have been previously +declared. To do so, the rule needs a label attribute. In this example, we +will give the label a default value and make it private (so that it is not +visible to end users). Keeping the label private can simplify maintenance, +since you can easily change the arguments and flags you pass to the tool. + +`execute.bzl`: + +```python +def _impl(ctx): + # The list of arguments we pass to the script. + args = [ctx.outputs.out.path] + [f.path for f in ctx.files.srcs] + # Action to call the script. + ctx.action( + inputs=ctx.files.srcs, + outputs=[ctx.outputs.out], + arguments=args, + executable=ctx.executable._merge_tool) + +concat = rule( + implementation=_impl, + attrs={ + "srcs": attr.label_list(allow_files=True), + "out": attr.output(mandatory=True), + "_merge_tool": attr.label(executable=True, default=Label("//pkg:merge")) + } +) +``` + +Any executable target can be used. In this example, we will use a +`sh_binary` rule that concatenates all the inputs. + +`BUILD`: + +``` +load("execute", "concat") + +concat( + name = "sh", + srcs = [ + "header.html", + "body.html", + "footer.html", + ], + out = "page.html", +) + +# This target is used by the shell rule. +sh_binary( + name = "merge", + srcs = ["merge.sh"], +) +``` + +`merge.sh`: + +```python +#! /bin/bash + +out=$1 +shift +cat $* > $out +``` + +`header.html`: + +``` +<html><body> +``` + +`body.html`: + +``` +content +``` + +`footer.html`: + +``` +</body></html> +``` + ## <a name="execute"></a>Execute an input binary This rule has a mandatory `binary` attribute. It is a label that can refer |