aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/packages/SkylarkNativeModule.java
blob: 4c9e7d864197ad557b276cec908ee8897c1f05bf (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
// 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.skylarkbuildapi.SkylarkNativeModuleApi;
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.
 */
public class SkylarkNativeModule implements SkylarkNativeModuleApi {

  @Override
  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);
  }

  @Override
  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.
  */
  @Override
  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);
  }

  @Override
  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);
  }

  @Override
  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);
  }

  @Override
  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();
  }

  @Override
  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();
  }
}