Search in sources :

Example 16 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRuleContext method getBuildFileRelativePath.

@SkylarkCallable(name = "build_file_path", structField = true, documented = true, doc = "Returns path to the BUILD file for this rule, relative to the source root.")
public String getBuildFileRelativePath() {
    Package pkg = ruleContext.getRule().getPackage();
    Root root = Root.asSourceRoot(pkg.getSourceRoot(), pkg.getPackageIdentifier().getRepository().isMain());
    return pkg.getBuildFile().getPath().relativeTo(root.getPath()).getPathString();
}
Also used : Root(com.google.devtools.build.lib.actions.Root) Package(com.google.devtools.build.lib.packages.Package) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 17 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRuleContext method newFile.

@SkylarkCallable(doc = "Creates a new file object in the same directory as the original file. " + DOC_NEW_FILE_TAIL)
public Artifact newFile(Artifact baseArtifact, String newBaseName) {
    PathFragment original = baseArtifact.getRootRelativePath();
    PathFragment fragment = original.replaceName(newBaseName);
    return ruleContext.getDerivedArtifact(fragment, newFileRoot());
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 18 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class SkylarkRuleContext method instrumentCoverage.

@SkylarkCallable(name = "coverage_instrumented", doc = "Returns whether code coverage instrumentation should be generated when performing " + "compilation actions for this rule or, if <code>target</code> is provided, the rule " + "specified by that Target. (If a non-rule Target is provided, this returns False.) This " + "differs from <code>coverage_enabled</code> in the <a href=\"configuration.html\">" + "configuration</a>, which notes whether coverage data collection is enabled for the " + "entire run, but not whether a specific target should be instrumented.", parameters = { @Param(name = "target", type = TransitiveInfoCollection.class, defaultValue = "None", noneable = true, named = true, doc = "A Target specifying a rule. If not provided, defaults to the current rule.") })
public boolean instrumentCoverage(Object targetUnchecked) {
    BuildConfiguration config = ruleContext.getConfiguration();
    if (!config.isCodeCoverageEnabled()) {
        return false;
    }
    if (targetUnchecked == Runtime.NONE) {
        return InstrumentedFilesCollector.shouldIncludeLocalSources(ruleContext);
    }
    TransitiveInfoCollection target = (TransitiveInfoCollection) targetUnchecked;
    return (target.getProvider(InstrumentedFilesProvider.class) != null) && InstrumentedFilesCollector.shouldIncludeLocalSources(config, target);
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) InstrumentedFilesProvider(com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider) TransitiveInfoCollection(com.google.devtools.build.lib.analysis.TransitiveInfoCollection) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 19 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class CcSkylarkApiProvider method getCcFlags.

@SkylarkCallable(name = "compile_flags", structField = true, doc = "Returns the immutable set of flags used to compile this target " + "(possibly empty but never None).")
public ImmutableList<String> getCcFlags() {
    CppCompilationContext ccContext = getInfo().getProvider(CppCompilationContext.class);
    ImmutableList.Builder<String> options = ImmutableList.builder();
    for (String define : ccContext.getDefines()) {
        options.add("-D" + define);
    }
    for (PathFragment path : ccContext.getSystemIncludeDirs()) {
        options.add("-isystem " + path.getSafePathString());
    }
    for (PathFragment path : ccContext.getIncludeDirs()) {
        options.add("-I " + path.getSafePathString());
    }
    for (PathFragment path : ccContext.getQuoteIncludeDirs()) {
        options.add("-iquote " + path.getSafePathString());
    }
    return options.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 20 with SkylarkCallable

use of com.google.devtools.build.lib.skylarkinterface.SkylarkCallable in project bazel by bazelbuild.

the class JavaSkylarkCommon method createJavaCompileAction.

@SkylarkCallable(name = "compile", doc = "Compiles Java source files/jars from the implementation of a Skylark rule and returns a " + "provider that represents the results of the compilation and can be added to the set of " + "providers emitted by this rule.", // There is one mandatory positional: the Skylark rule context.
mandatoryPositionals = 1, parameters = { @Param(name = "source_jars", positional = false, named = true, type = SkylarkList.class, generic1 = Artifact.class, defaultValue = "[]", doc = "A list of the jars to be compiled. At least one of source_jars or source_files" + " should be specified."), @Param(name = "source_files", positional = false, named = true, type = SkylarkList.class, generic1 = Artifact.class, defaultValue = "[]", doc = "A list of the Java source files to be compiled. At least one of source_jars or " + "source_files should be specified."), @Param(name = "output", positional = false, named = true, type = Artifact.class), @Param(name = "javac_opts", positional = false, named = true, type = SkylarkList.class, generic1 = String.class, doc = "A list of the desired javac options. Optional."), @Param(name = "deps", positional = false, named = true, type = SkylarkList.class, generic1 = JavaProvider.class, doc = "A list of dependencies. Optional."), @Param(name = "strict_deps", defaultValue = "OFF", positional = false, named = true, type = String.class, doc = "A string that specifies how to handle strict deps. Possible values: 'OFF' (silently" + " allowing referencing transitive dependencies) and 'ERROR' (failing to build when" + " transitive dependencies are used directly). By default 'OFF'."), @Param(name = "java_toolchain", positional = false, named = true, type = ConfiguredTarget.class, doc = "A label pointing to a java_toolchain rule to be used for this compilation. " + "Mandatory."), @Param(name = "host_javabase", positional = false, named = true, type = ConfiguredTarget.class, doc = "A label pointing to a JDK to be used for this compilation. Mandatory.") })
public JavaProvider createJavaCompileAction(SkylarkRuleContext skylarkRuleContext, SkylarkList<Artifact> sourceJars, SkylarkList<Artifact> sourceFiles, Artifact outputJar, SkylarkList<String> javacOpts, SkylarkList<JavaProvider> deps, String strictDepsMode, ConfiguredTarget javaToolchain, ConfiguredTarget hostJavabase) {
    JavaLibraryHelper helper = new JavaLibraryHelper(skylarkRuleContext.getRuleContext()).setOutput(outputJar).addSourceJars(sourceJars).addSourceFiles(sourceFiles).setJavacOpts(javacOpts);
    List<JavaCompilationArgsProvider> compilationArgsProviders = JavaProvider.fetchProvidersFromList(deps, JavaCompilationArgsProvider.class);
    helper.addAllDeps(compilationArgsProviders);
    helper.setCompilationStrictDepsMode(getStrictDepsMode(strictDepsMode));
    MiddlemanProvider hostJavabaseProvider = hostJavabase.getProvider(MiddlemanProvider.class);
    NestedSet<Artifact> hostJavabaseArtifacts = hostJavabaseProvider == null ? NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER) : hostJavabaseProvider.getMiddlemanArtifact();
    JavaToolchainProvider javaToolchainProvider = checkNotNull(javaToolchain.getProvider(JavaToolchainProvider.class));
    JavaCompilationArgs artifacts = helper.build(javaSemantics, javaToolchainProvider, hostJavabaseArtifacts, SkylarkList.createImmutable(ImmutableList.<Artifact>of()));
    JavaRuleOutputJarsProvider javaRuleOutputJarsProvider = JavaRuleOutputJarsProvider.builder().addOutputJar(new JavaRuleOutputJarsProvider.OutputJar(outputJar, /* ijar */
    null, sourceJars)).build();
    return JavaProvider.Builder.create().addProvider(JavaCompilationArgsProvider.class, helper.buildCompilationArgsProvider(artifacts, true)).addProvider(JavaSourceJarsProvider.class, createJavaSourceJarsProvider(sourceJars)).addProvider(JavaRuleOutputJarsProvider.class, javaRuleOutputJarsProvider).build();
}
Also used : MiddlemanProvider(com.google.devtools.build.lib.analysis.MiddlemanProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Aggregations

SkylarkCallable (com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)27 Method (java.lang.reflect.Method)8 Test (org.junit.Test)7 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)6 ImmutableList (com.google.common.collect.ImmutableList)5 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)5 IOException (java.io.IOException)5 ImmutableMap (com.google.common.collect.ImmutableMap)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)2 Param (com.google.devtools.build.lib.skylarkinterface.Param)2 OutputStream (java.io.OutputStream)2 URL (java.net.URL)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SkylarkJavaMethodDoc (com.google.devtools.build.docgen.skylark.SkylarkJavaMethodDoc)1 SkylarkModuleDoc (com.google.devtools.build.docgen.skylark.SkylarkModuleDoc)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 Root (com.google.devtools.build.lib.actions.Root)1 MiddlemanProvider (com.google.devtools.build.lib.analysis.MiddlemanProvider)1