aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/packages/SkylarkNativeModule.java
blob: 43220320f5e788780af92783494ddf1bb94222d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.packages;

import com.google.devtools.build.lib.cmdline.PackageIdentifier;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.skylarkinterface.Param;
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.syntax.FuncallExpression;
import com.google.devtools.build.lib.syntax.Runtime;
import com.google.devtools.build.lib.syntax.SkylarkDict;
import com.google.devtools.build.lib.syntax.SkylarkList;
import com.google.devtools.build.lib.syntax.Type.ConversionException;

/**
 * A class for the Skylark native module. TODO(laurentlb): Some definitions are duplicated from
 * PackageFactory.
 */
@SkylarkModule(
  name = "native",
  category = SkylarkModuleCategory.BUILTIN,
  doc =
      "A built-in module to support native rules and other package helper functions. "
          + "All native rules appear as functions in this module, e.g. "
          + "<code>native.cc_library</code>. "
          + "Note that the native module is only available in the loading phase "
          + "(i.e. for macros, not for rule implementations). Attributes will ignore "
          + "<code>None</code> values, and treat them as if the attribute was unset.<br>"
          + "The following functions are also available:"
)
public class SkylarkNativeModule {

  @SkylarkCallable(
    name = "glob",
    doc =
        "Glob returns a list of every file in the current package that:<ul>\n"
            + "<li>Matches at least one pattern in <code>include</code>.</li>\n"
            + "<li>Does not match any of the patterns in <code>exclude</code> "
            + "(default <code>[]</code>).</li></ul>\n"
            + "If the <code>exclude_directories</code> argument is enabled (set to <code>1</code>),"
            + " files of type directory will be omitted from the results (default <code>1</code>).",
    parameters = {
      @Param(
        name = "include",
        type = SkylarkList.class,
        generic1 = String.class,
        defaultValue = "[]",
        doc = "The list of glob patterns to include."
      ),
      @Param(
        name = "exclude",
        type = SkylarkList.class,
        generic1 = String.class,
        defaultValue = "[]",
        doc = "The list of glob patterns to exclude."
      ),
      // TODO(bazel-team): accept booleans as well as integers? (and eventually migrate?)
      @Param(
        name = "exclude_directories",
        type = Integer.class,
        defaultValue = "1",
        doc = "A flag whether to exclude directories or not."
      )
    },
    useAst = true,
    useEnvironment = true
  )
  public SkylarkList<?> glob(
      SkylarkList<?> include,
      SkylarkList<?> exclude,
      Integer excludeDirectories,
      FuncallExpression ast,
      Environment env)
      throws EvalException, ConversionException, InterruptedException {
    env.checkLoadingPhase("native.glob", ast.getLocation());
    return PackageFactory.callGlob(null, include, exclude, excludeDirectories != 0, ast, env);
  }

  @SkylarkCallable(
    name = "existing_rule",
    doc =
        "Returns a dictionary representing the attributes of a previously defined rule, "
            + "or None if the rule does not exist.",
    parameters = {
      @Param(name = "name", type = String.class, doc = "The name of the rule.")
    },
    useAst = true,
    useEnvironment = true
  )
  public Object existingRule(String name, FuncallExpression ast, Environment env)
      throws EvalException, InterruptedException {
    env.checkLoadingOrWorkspacePhase("native.existing_rule", ast.getLocation());
    SkylarkDict<String, Object> rule = PackageFactory.callGetRuleFunction(name, ast, env);
    if (rule != null) {
      return rule;
    }

    return Runtime.NONE;
  }

  /*
    If necessary, we could allow filtering by tag (anytag, alltags), name (regexp?), kind ?
    For now, we ignore this, since users can implement it in Skylark.
  */
  @SkylarkCallable(
    name = "existing_rules",
    doc =
        "Returns a dict containing all the rules instantiated so far. "
            + "The map key is the name of the rule. The map value is equivalent to the "
            + "existing_rule output for that rule.",
    useAst = true,
    useEnvironment = true
  )
  public SkylarkDict<String, SkylarkDict<String, Object>> existingRules(
      FuncallExpression ast, Environment env)
      throws EvalException, InterruptedException {
    env.checkLoadingOrWorkspacePhase("native.existing_rules", ast.getLocation());
    return PackageFactory.callGetRulesFunction(ast, env);
  }

  @SkylarkCallable(name = "package_group",
      doc = "This function defines a set of packages and assigns a label to the group. "
          + "The label can be referenced in <code>visibility</code> attributes.",
      parameters = {
      @Param(name = "name", type = String.class, named = true, positional = false,
          doc = "The unique name for this rule."),
      @Param(name = "packages", type = SkylarkList.class, generic1 = String.class,
          defaultValue = "[]", named = true, positional = false,
          doc = "A complete enumeration of packages in this group."),
      @Param(name = "includes", type = SkylarkList.class, generic1 = String.class,
          defaultValue = "[]", named = true, positional = false,
          doc = "Other package groups that are included in this one.")},
      useAst = true, useEnvironment = true)
  public Runtime.NoneType packageGroup(String name, SkylarkList<?> packages,
      SkylarkList<?> includes,
      FuncallExpression ast, Environment env) throws EvalException {
    env.checkLoadingPhase("native.package_group", ast.getLocation());
    return PackageFactory.callPackageFunction(name, packages, includes, ast, env);
  }

  @SkylarkCallable(name = "exports_files",
    doc = "Specifies a list of files belonging to this package that are exported to other "
        + "packages but not otherwise mentioned.",
    parameters = {
      @Param(name = "srcs", type = SkylarkList.class, generic1 = String.class,
          doc = "The list of files to export."),
      // TODO(bazel-team): make it possible to express the precise type ListOf(LabelDesignator)
      @Param(name = "visibility", type = SkylarkList.class, defaultValue = "None", noneable = true,
          doc = "A visibility declaration can to be specified. The files will be visible to the "
              + "targets specified. If no visibility is specified, the files will be visible to "
              + "every package."),
      @Param(name = "licenses", type = SkylarkList.class, generic1 = String.class, noneable = true,
          defaultValue = "None", doc = "Licenses to be specified.")},
    useAst = true, useEnvironment = true)
  public Runtime.NoneType exportsFiles(SkylarkList<?> srcs, Object visibility, Object licenses,
      FuncallExpression ast, Environment env)
      throws EvalException {
    env.checkLoadingPhase("native.exports_files", ast.getLocation());
    return PackageFactory.callExportsFiles(srcs, visibility, licenses, ast, env);
  }

  @SkylarkCallable(
    name = "package_name",
    doc =
        "The name of the package being evaluated. "
            + "For example, in the BUILD file <code>some/package/BUILD</code>, its value "
            + "will be <code>some/package</code>. "
            + "If the BUILD file calls a function defined in a .bzl file, "
            + "<code>package_name()</code> will match the caller BUILD file package. "
            + "This function is equivalent to the deprecated variable <code>PACKAGE_NAME</code>.",
    parameters = {},
    useAst = true,
    useEnvironment = true
  )
  public String packageName(FuncallExpression ast, Environment env)
      throws EvalException {
    env.checkLoadingPhase("native.package_name", ast.getLocation());
    PackageIdentifier packageId =
        PackageFactory.getContext(env, ast.getLocation()).getBuilder().getPackageIdentifier();
    return packageId.getPackageFragment().getPathString();
  }

  @SkylarkCallable(
    name = "repository_name",
    doc =
        "The name of the repository the rule or build extension is called from. "
            + "For example, in packages that are called into existence by the WORKSPACE stanza "
            + "<code>local_repository(name='local', path=...)</code> it will be set to "
            + "<code>@local</code>. In packages in the main repository, it will be set to "
            + "<code>@</code>. This function is equivalent to the deprecated variable "
            + "<code>REPOSITORY_NAME</code>.",
    parameters = {},
    useLocation = true,
    useEnvironment = true
  )
  public String repositoryName(Location location, Environment env)
      throws EvalException {
    env.checkLoadingPhase("native.repository_name", location);
    PackageIdentifier packageId =
        PackageFactory.getContext(env, location).getBuilder().getPackageIdentifier();
    return packageId.getRepository().toString();
  }
}