From 9a8064d1784efc75e3e83e01ffc3a51b93cf56ee Mon Sep 17 00:00:00 2001 From: Laurent Le Brun Date: Tue, 28 Jun 2016 19:34:02 +0000 Subject: Some cleanup in skylark cookbook/macros doc -- MOS_MIGRATED_REVID=126104630 --- site/docs/skylark/cookbook.md | 67 +++++++++++++++++++++---------------------- site/docs/skylark/macros.md | 6 ++-- 2 files changed, 36 insertions(+), 37 deletions(-) (limited to 'site/docs/skylark') diff --git a/site/docs/skylark/cookbook.md b/site/docs/skylark/cookbook.md index a66c0d9b4c..8dfe03ebe2 100644 --- a/site/docs/skylark/cookbook.md +++ b/site/docs/skylark/cookbook.md @@ -1,25 +1,31 @@ --- layout: documentation -title: Skylark Cookbook +title: Extensions examples --- -# Skylark cookbook +# Extensions examples -## Macro creating a native rule +## Macro creating a rule + +An example of a macro creating a rule. + +`empty.bzl`: + +```python +def _impl(ctx): + print("This rule does nothing") -An example of a macro creating a native rule. Native rules are accessed using -the native module. +empty = rule(implementation=_impl) +``` `extension.bzl`: ```python +# Loading the rule. The rule doesn't have to be in a separate file. +load("//pkg:empty.bzl", "empty") + def macro(name, visibility=None): - # Creating a native genrule. - native.genrule( - name = name, - outs = [name + '.txt'], - cmd = 'echo hello > $@', - visibility = visibility, - ) + # Creating the rule. + empty(name = name, visibility = visibility) ``` `BUILD`: @@ -30,28 +36,23 @@ load("//pkg:extension.bzl", "macro") macro(name = "myrule") ``` -## Macro creating a Skylark rule - -An example of a macro creating a Skylark rule. - -`empty.bzl`: - -```python -def _impl(ctx): - print("This rule does nothing") +## Macro creating a native rule -empty = rule(implementation=_impl) -``` +An example of a macro creating a native rule. Native rules are special rules +that are automatically available (without load). They are +accessed using the native module. `extension.bzl`: ```python -# Loading the Skylark rule. The rule doesn't have to be in a separate file. -load("//pkg:empty.bzl", "empty") - def macro(name, visibility=None): - # Creating the Skylark rule. - empty(name = name, visibility = visibility) + # Creating a native genrule. + native.genrule( + name = name, + outs = [name + '.txt'], + cmd = 'echo hello > $@', + visibility = visibility, + ) ``` `BUILD`: @@ -62,10 +63,10 @@ load("//pkg:extension.bzl", "macro") macro(name = "myrule") ``` -## Macro combining Skylark and native rules +## Macro multiple rules -There's currently no easy way to create a Skylark rule that directly uses the -action of a native rule. You can work around this using Skylark macros: +There's currently no easy way to create a rule that directly uses the +action of a native rule. You can work around this using macros: ```python def cc_and_something_else_binary(name, srcs, deps, csrcs, cdeps) @@ -100,8 +101,6 @@ def _impl(ctx): _cc_and_something_else_binary = rule(implementation=_impl) ``` -In the future, Skylark will have access to the build actions of native rules -through an API, and this sort of work-around will no longer be necessary. ## Conditional instantiation @@ -885,7 +884,7 @@ macro( ## Debugging tips -Here are some examples on how to debug Skylark macros and rules using +Here are some examples on how to debug macros and rules using print. `debug.bzl`: diff --git a/site/docs/skylark/macros.md b/site/docs/skylark/macros.md index 4e0b93b0e8..6242af8d1a 100644 --- a/site/docs/skylark/macros.md +++ b/site/docs/skylark/macros.md @@ -27,11 +27,11 @@ macro), use the constant [PACKAGE_NAME](lib/globals.html#PACKAGE_NAME). ## Examples -* [Macro creating native rules](cookbook.md#macro_native). +* [Macro creating rules](cookbook.md#macro). -* [Macro creating Skylark rules](cookbook.md#macro_skylark). +* [Macro creating native rules](cookbook.md#macro_native). -* [Macro combining Skylark and native rules](cookbook.md#macro_compound). +* [Macro combining multiple rules](cookbook.md#macro_compound). ## Debugging -- cgit v1.2.3