github.phpd.cn/thought-machine/please@v12.2.0+incompatible/docs/lexicon.html (about)

     1      <h1>The Lexicon of Please</h1>
     2  
     3      <p>This is the reference for the complete set of builtin rules & functions.</p>
     4  
     5      <ul>
     6        <li><a href="#builtins">Builtin rules and functions</a></li>
     7        <li><a href="#cc">C and C++ rules</a></li>
     8        <li><a href="#go">Go rules</a></li>
     9        <li><a href="#java">Java rules</a></li>
    10        <li><a href="#misc">Miscellaneous rules</a></li>
    11        <li><a href="#proto">Protocol buffer rules</a></li>
    12        <li><a href="#python">Python rules</a></li>
    13        <li><a href="#sh">Shell rules</a></li>
    14      </ul>
    15  
    16      <h2><a name="builtins">Builtin functions</a></h2>
    17  
    18      <h3><a name="subinclude">subinclude</a></h3>
    19  
    20      <p><pre class="rule"><code>subinclude(target)</code></pre></p>
    21  
    22      <p>Includes the output of a build target as extra rules in this one.</p>
    23  
    24      <p>This is the closest equivalent to <code>import</code> in the BUILD language. It behaves
    25        somewhat differently though in that the contents of the subincluded file are added to the
    26        globals of the current module so can be called directly.</p>
    27  
    28      <p>The target that you attempt to subinclude is simply a normal build target, with the
    29        restriction that it must have exactly one output. The simplest form of this is a single-file
    30        <a href="#filegroup">filegroup</a> or <a href="export_file">export_file</a> rule, but
    31        it is possible to have more complex rules generating your BUILD definitions.</p>
    32  
    33      <p>For example:
    34  
    35      <pre><code>subinclude('//build_defs:my_build_rules')</code></pre>
    36      </p>
    37  
    38      <p>Note that you can also <code>subinclude</code> remote files as follows:
    39  
    40      <pre><code>subinclude('https://raw.githubusercontent.com/thought-machine/pleasings/master/grm/grm.build_defs')</code></pre>
    41  
    42      These are deduplicated internally so they're only downloaded & parsed once, so it's reasonably
    43      performant, although it's still often preferable to vendorise them yourself.</p>
    44  
    45      <h3><a name="glob">glob</a></h3>
    46  
    47      <p><pre class="rule"><code>glob(includes, excludes=None, hidden=False)</code></pre></p>
    48  
    49      <p>Matches all filenames by a pattern, in a manner reminiscent of shell-style pattern
    50        expansion or Python's builtin <a href="https://docs.python.org/3/library/glob.html">glob</a>
    51        module.</p>
    52  
    53      <p>Note that the only expansion patterns accepted are <code>*</code> to match an arbitrary
    54        number of non-separator characters (i.e. not <code>/</code>), and <code>**</code> to
    55        match any number of <b>complete</b> path components.<br/>
    56        These are often referred to as &quot;Ant-style&quot; patterns since Ant introduced them.</p>
    57  
    58      <p>It bears noting that you cannot glob generated files, only source files. Also glob will not
    59        descend into any directories that contain a BUILD file; this maintains an invariant that
    60        each file is owned by exactly one package (or potentially none, but then Please doesn't know
    61        or care about them). If you want to pull in files from another package, export them there
    62        using a <a href="#filegroup"><code>filegroup</code></a> and depend on that in the package
    63        you want it.</p>
    64  
    65      <table>
    66        <thead>
    67        <tr>
    68  	<th>Argument</th>
    69  	<th>Default</th>
    70  	<th>Type</th>
    71  	<th></th>
    72        </tr>
    73        </thead>
    74        <tbody>
    75  
    76        <tr>
    77  	<td>include</td>
    78  	<td></td>
    79  	<td>list</td>
    80  	<td>List of paths to include. Each is globbed separately.</td>
    81        </tr>
    82  
    83        <tr>
    84  	<td>exclude</td>
    85  	<td>None</td>
    86  	<td>list</td>
    87  	<td>List of filenames to exclude from any patterns matched by <code>includes</code>.<br/>
    88        These are subject to glob expansion of <code>*</code> themselves, but
    89        <code>**</code> is <b>not</b> expanded (although since these are applied to each file found
    90        individually, it's not necessary).</td>
    91        </tr>
    92  
    93        <tr>
    94  	<td>hidden</td>
    95  	<td>False</td>
    96  	<td>bool</td>
    97  	<td>Set to True to include hidden files / folders.</td>
    98        </tr>
    99  
   100        </tbody>
   101      </table>
   102  
   103      <h3><a name="get_labels">get_labels</a></h3>
   104  
   105      <p><pre class="rule"><code>get_labels(target, prefix)</code></pre></p>
   106  
   107      <p>Gets the unique set of labels for a rule and all its transitive dependencies.</p>
   108  
   109      <p>Two formats are accepted for target: the first is a string containing just the target name,
   110        which is resolved in the current package. This facilitates calling them from a pre-build
   111        function, which is in fact the only time it's safe to call this way.<br/>
   112        The other is a full build target, which should be a transitive dependency of the target
   113        whose pre/post build function you're in. If the target isn't built when you ask for the
   114        labels the build will terminate.<br/>
   115        In either case this is only safe to call from a pre / post-build function and should
   116        never be called at initial parse time, because at that point you generally don't have
   117        the full set of labels available yet.</p>
   118  
   119      <p>Uses for this are normally fairly language-specific. The clearest example is maybe the
   120        builtin Python rules, where <code>python_binary</code> and <code>python_test</code> use
   121        this to identify if any of their dependencies have marked them as not being zip-safe.</p>
   122  
   123      <table>
   124        <thead>
   125        <tr>
   126  	<th>Argument</th>
   127  	<th>Default</th>
   128  	<th>Type</th>
   129  	<th></th>
   130        </tr>
   131        </thead>
   132        <tbody>
   133  
   134        <tr>
   135  	<td>target</td>
   136  	<td></td>
   137  	<td>str</td>
   138  	<td>Label of the target to get labels for.</td>
   139        </tr>
   140  
   141        <tr>
   142  	<td>prefix</td>
   143  	<td>None</td>
   144  	<td>str</td>
   145  	<td>Filters the returned labels to only ones starting with this prefix.</td>
   146        </tr>
   147  
   148        </tbody>
   149      </table>
   150  
   151      <h3><a name="has_label">has_label</a></h3>
   152  
   153      <p><pre class="rule"><code>has_label(target, prefix)</code></pre></p>
   154  
   155      <p>Returns True if the target has any matching label that would be returned by get_labels.</p>
   156  
   157      <table>
   158        <thead>
   159        <tr>
   160  	<th>Argument</th>
   161  	<th>Default</th>
   162  	<th>Type</th>
   163  	<th></th>
   164        </tr>
   165        </thead>
   166        <tbody>
   167  
   168        <tr>
   169  	<td>target</td>
   170  	<td></td>
   171  	<td>str</td>
   172  	<td>Label of the target to get labels for.</td>
   173        </tr>
   174  
   175        <tr>
   176  	<td>prefix</td>
   177  	<td>None</td>
   178  	<td>str</td>
   179  	<td>Checks only labels that start with this prefix.</td>
   180        </tr>
   181  
   182        </tbody>
   183      </table>
   184  
   185      <h3><a name="package">package</a></h3>
   186  
   187      <p><pre class="rule"><code>package(...)</code></pre></p>
   188  
   189      <p>Defines settings affecting the current package - for example, default visibility.</p>
   190  
   191      <p>With this you can override any current value in <code>CONFIG</code> by name for all
   192        subsequent targets in the current package. Only existing values can be replaced.</p>
   193  
   194      <p>There are also a couple of special values which aren't normally in <code>CONFIG</code>:
   195        <code>default_licences</code> and <code>default_visibility</code>. As the names suggest
   196        these set defaults for those attributes for all following targets that don't set them.</p>
   197  
   198      <p>This function must be called <b>before</b> any targets are defined.</p>
   199  
   200      <h3><a name="log">log</a></h3>
   201  
   202      <p><pre class="rule"><code>log.warning(message, [args...])</code></pre></p>
   203  
   204      <p>Logs an arbitrary message at some given level. The available levels, from most quiet
   205        to most severe, are:
   206        <ul>
   207          <li><code>log.debug</code></li>
   208          <li><code>log.info</code></li>
   209          <li><code>log.notice</code></li>
   210          <li><code>log.warning</code></li>
   211          <li><code>log.error</code></li>
   212          <li><code>log.fatal</code></li>
   213        </ul>
   214        These correspond to Please's built in messages and are controlled as usual by the
   215        <code>-v</code> command-line argument.</p>
   216  
   217      <p>As the name suggests, <code>log.fatal</code> immediately terminates the program with
   218        a fatal error. The others have no side effect other than showing the message.</p>
   219  
   220      <p>The message and arguments together are interpolated like Python's normal string
   221        interpolation, similar to the builtin <code>logging</code> package.</p>
   222  
   223      <h3><a name="decompose">decompose</a></h3>
   224  
   225      <p><pre class="rule"><code>decompose(label)</code></pre></p>
   226  
   227      <p>Decomposes a build label into the package and name parts.</p>
   228  
   229      <p>Consider carefully when you should use this - command replacements may be more appropriate.<br/>
   230        Most rules should be able to accept labels without having to know anything about their structure.</p>
   231  
   232      <p>The input label can be given in either relative or absolute form. It will fail with an error
   233        if it's not a structurally valid label.</p>
   234  
   235  
   236      <h2><a name="cc">C and C++ rules</a></h2>
   237  
   238      <p>Rules to build C and C++ targets.</p>
   239      <p>The process of building C or C++ code is fairly complex so while these rules do their best
   240        to keep things reasonably simple, there's a lot of scope for things going wrong.</p>
   241      <p>There is very little difference between the <code>c_</code> and <code>cc_</code> variants
   242        of each rule, in nearly all cases they simply use different tools and flags in the
   243        <a href="config.html#cpp">relevant config section</a>.</p>
   244  
   245      {{ template "lexicon_entry.html" .Named "c_library" }}
   246      {{ template "lexicon_entry.html" .Named "c_static_library" }}
   247      {{ template "lexicon_entry.html" .Named "c_shared_object" }}
   248      {{ template "lexicon_entry.html" .Named "c_binary" }}
   249      {{ template "lexicon_entry.html" .Named "c_test" }}
   250      {{ template "lexicon_entry.html" .Named "c_embed_binary" }}
   251  
   252  
   253      <h2><a name="go">Go rules</a></h2>
   254  
   255      <p>Rules to build Go code.</p>
   256      <p>Go has a strong built-in concept of packages so while it's not 100% required to strictly
   257        keep to 1 package per dir as in classic Go, it's still probably a good idea to match Please
   258        rules to Go packages.</p>
   259  
   260      {{ template "lexicon_entry.html" .Named "go_library" }}
   261      {{ template "lexicon_entry.html" .Named "go_generate" }}
   262      {{ template "lexicon_entry.html" .Named "cgo_library" }}
   263      {{ template "lexicon_entry.html" .Named "go_binary" }}
   264      {{ template "lexicon_entry.html" .Named "go_test" }}
   265      {{ template "lexicon_entry.html" .Named "cgo_test" }}
   266      {{ template "lexicon_entry.html" .Named "go_get" }}
   267  
   268  
   269      <h2><a name="java">Java rules</a></h2>
   270  
   271      <p>Built-in rules to compile Java code.</p>
   272  
   273      {{ template "lexicon_entry.html" .Named "java_library" }}
   274      {{ template "lexicon_entry.html" .Named "java_module" }}
   275      {{ template "lexicon_entry.html" .Named "java_binary" }}
   276      {{ template "lexicon_entry.html" .Named "java_runtime_image" }}
   277      {{ template "lexicon_entry.html" .Named "java_test" }}
   278      {{ template "lexicon_entry.html" .Named "maven_jar" }}
   279      {{ template "lexicon_entry.html" .Named "maven_jars" }}
   280  
   281  
   282      <h2><a name="misc">Misc rules</a></h2>
   283  
   284      <p>Miscellaneous rules that aren't language-specific.</p>
   285  
   286      {{ template "lexicon_entry.html" .Named "genrule" }}
   287      {{ template "lexicon_entry.html" .Named "gentest" }}
   288      {{ template "lexicon_entry.html" .Named "export_file" }}
   289      {{ template "lexicon_entry.html" .Named "filegroup" }}
   290      {{ template "lexicon_entry.html" .Named "hash_filegroup" }}
   291      {{ template "lexicon_entry.html" .Named "system_library" }}
   292      {{ template "lexicon_entry.html" .Named "remote_file" }}
   293      {{ template "lexicon_entry.html" .Named "tarball" }}
   294  
   295  
   296      <h2><a name="proto">Proto rules</a></h2>
   297  
   298      <p>Build rules for compiling protocol buffers & gRPC service stubs.</p>
   299      <p>Note that these are some of the most complex of our built-in build rules,
   300        because of their cross-language nature. Each proto_library rule declares a set of
   301        sub-rules to run protoc & the appropriate java_library, go_library rules etc. Users
   302        shouldn't worry about those sub-rules and just declare a dependency directly on
   303        the proto_library rule to get its appropriate outputs.</p>
   304  
   305      {{ template "lexicon_entry.html" .Named "proto_library" }}
   306      {{ template "lexicon_entry.html" .Named "grpc_library" }}
   307  
   308  
   309      <h2><a name="python">Python rules</a></h2>
   310  
   311      <p>Rules to build Python code.</p>
   312      <p>The output artifacts for Python rules are .zip files similar to
   313        <a href="https://github.com/pantsbuild/pex">.pex</a> files (and are suffixed as such).</p>
   314      <p>This combines all the in-repo dependencies into a single file that can be deployed and run
   315        on any system that has a suitable Python interpreter. Please optimises this process to
   316        improve incrementality and includes some hooks to make running from a zipfile as transparent
   317        as possible; nonetheless certain file-based operations may not work since there is not a
   318        real file system available. If necessary various Python rules can be annotated with
   319        <code>zip_safe</code> to control this; if a binary is zip-unsafe it will extract itself before
   320        running.</p>
   321  
   322      {{ template "lexicon_entry.html" .Named "python_library" }}
   323      {{ template "lexicon_entry.html" .Named "python_binary" }}
   324      {{ template "lexicon_entry.html" .Named "python_test" }}
   325      {{ template "lexicon_entry.html" .Named "pip_library" }}
   326      {{ template "lexicon_entry.html" .Named "python_wheel" }}
   327  
   328  
   329      <h2><a name="sh">Shell rules</a></h2>
   330  
   331      <p>Rules to 'build' shell scripts.</p>
   332      <p>Note that these do pretty much nothing beyond collecting the files. In future we might
   333        implement something more advanced (ala .sar files or whatever).</p>
   334  
   335      {{ template "lexicon_entry.html" .Named "sh_library" }}
   336      {{ template "lexicon_entry.html" .Named "sh_binary" }}
   337      {{ template "lexicon_entry.html" .Named "sh_cmd" }}
   338      {{ template "lexicon_entry.html" .Named "sh_test" }}