Search in sources :

Example 1 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class PackageFactory method callGetRulesFunction.

static SkylarkDict<String, SkylarkDict<String, Object>> callGetRulesFunction(FuncallExpression ast, Environment env) throws EvalException {
    PackageContext context = getContext(env, ast);
    Collection<Target> targets = context.pkgBuilder.getTargets();
    Location loc = ast.getLocation();
    SkylarkDict<String, SkylarkDict<String, Object>> rules = SkylarkDict.of(env);
    for (Target t : targets) {
        if (t instanceof Rule) {
            SkylarkDict<String, Object> m = targetDict(t, loc, env);
            Preconditions.checkNotNull(m);
            rules.put(t.getName(), m, loc, env);
        }
    }
    return rules;
}
Also used : ClassObject(com.google.devtools.build.lib.syntax.ClassObject) SkylarkDict(com.google.devtools.build.lib.syntax.SkylarkDict) Location(com.google.devtools.build.lib.events.Location)

Example 2 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class BuiltinFunction method call.

@Override
@Nullable
public Object call(Object[] args, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
    Preconditions.checkNotNull(env);
    // ast is null when called from Java (as there's no Skylark call site).
    Location loc = ast == null ? Location.BUILTIN : ast.getLocation();
    // Add extra arguments, if needed
    if (extraArgs != null) {
        int i = args.length - extraArgs.length;
        for (BuiltinFunction.ExtraArgKind extraArg : extraArgs) {
            switch(extraArg) {
                case LOCATION:
                    args[i] = loc;
                    break;
                case SYNTAX_TREE:
                    args[i] = ast;
                    break;
                case ENVIRONMENT:
                    args[i] = env;
                    break;
            }
            i++;
        }
    }
    Profiler.instance().startTask(ProfilerTask.SKYLARK_BUILTIN_FN, getName());
    // Last but not least, actually make an inner call to the function with the resolved arguments.
    try {
        env.enterScope(this, ast, env.getGlobals());
        return invokeMethod.invoke(this, args);
    } catch (InvocationTargetException x) {
        Throwable e = x.getCause();
        if (e instanceof EvalException) {
            throw ((EvalException) e).ensureLocation(loc);
        } else if (e instanceof IllegalArgumentException) {
            throw new EvalException(loc, "illegal argument in call to " + getName(), e);
        }
        // TODO(bazel-team): replace with Throwables.throwIfInstanceOf once Guava 20 is released.
        Throwables.propagateIfInstanceOf(e, InterruptedException.class);
        // TODO(bazel-team): replace with Throwables.throwIfUnchecked once Guava 20 is released.
        Throwables.propagateIfPossible(e);
        throw badCallException(loc, e, args);
    } catch (IllegalArgumentException e) {
        // Either this was thrown by Java itself, or it's a bug
        // To cover the first case, let's manually check the arguments.
        final int len = args.length - ((extraArgs == null) ? 0 : extraArgs.length);
        final Class<?>[] types = invokeMethod.getParameterTypes();
        for (int i = 0; i < args.length; i++) {
            if (args[i] != null && !types[i].isAssignableFrom(args[i].getClass())) {
                String paramName = i < len ? signature.getSignature().getNames().get(i) : extraArgs[i - len].name();
                int extraArgsCount = (extraArgs == null) ? 0 : extraArgs.length;
                throw new EvalException(loc, String.format("method %s is not applicable for arguments %s: " + "'%s' is '%s', but should be '%s'", getShortSignature(true), printTypeString(args, args.length - extraArgsCount), paramName, EvalUtils.getDataTypeName(args[i]), EvalUtils.getDataTypeNameFromClass(types[i])));
            }
        }
        throw badCallException(loc, e, args);
    } catch (IllegalAccessException e) {
        throw badCallException(loc, e, args);
    } finally {
        Profiler.instance().completeTask(ProfilerTask.SKYLARK_BUILTIN_FN);
        env.exitScope();
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) Location(com.google.devtools.build.lib.events.Location) Nullable(javax.annotation.Nullable)

Example 3 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class BaseFunction method call.

/**
   * The outer calling convention to a BaseFunction.
   *
   * @param args a list of all positional arguments (as in *starArg)
   * @param kwargs a map for key arguments (as in **kwArgs)
   * @param ast the expression for this function's definition
   * @param env the Environment in the function is called
   * @return the value resulting from evaluating the function with the given arguments
   * @throws EvalException-s containing source information.
   */
public Object call(List<Object> args, @Nullable Map<String, Object> kwargs, @Nullable FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
    Preconditions.checkState(isConfigured(), "Function %s was not configured", getName());
    // ast is null when called from Java (as there's no Skylark call site).
    Location loc = ast == null ? Location.BUILTIN : ast.getLocation();
    Object[] arguments = processArguments(args, kwargs, loc, env);
    canonicalizeArguments(arguments, loc);
    return call(arguments, ast, env);
}
Also used : Location(com.google.devtools.build.lib.events.Location)

Example 4 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class CircularDependencyTest method testThreeLongPackageGroupCycle.

@Test
public void testThreeLongPackageGroupCycle() throws Exception {
    String expectedEvent = "cycle in dependency graph:\n" + "    //cycle:superman\n" + ".-> //cycle:rock\n" + "|   //cycle:paper\n" + "|   //cycle:scissors\n" + "`-- //cycle:rock";
    checkError("cycle", "superman", expectedEvent, "# dummy line", "package_group(name='paper', includes=['//cycle:scissors'])", "package_group(name='rock', includes=['//cycle:paper'])", "package_group(name='scissors', includes=['//cycle:rock'])", "sh_library(name='superman', visibility=[':rock'])");
    Event foundEvent = null;
    for (Event event : eventCollector) {
        if (event.getMessage().contains(expectedEvent)) {
            foundEvent = event;
            break;
        }
    }
    assertNotNull(foundEvent);
    Location location = foundEvent.getLocation();
    assertEquals(3, location.getStartLineAndColumn().getLine());
    assertEquals("/workspace/cycle/BUILD", location.getPath().toString());
}
Also used : Event(com.google.devtools.build.lib.events.Event) Location(com.google.devtools.build.lib.events.Location) Test(org.junit.Test)

Example 5 with Location

use of com.google.devtools.build.lib.events.Location in project bazel by bazelbuild.

the class SkylarkRuleConfiguredTargetBuilder method parseProviderKeys.

private static void parseProviderKeys(SkylarkClassObject provider, Boolean isDefaultProvider, RuleContext ruleContext, Location loc, Artifact executable, Map<String, Class<? extends TransitiveInfoProvider>> registeredProviderTypes, RuleConfiguredTargetBuilder builder) throws EvalException {
    Runfiles statelessRunfiles = null;
    Runfiles dataRunfiles = null;
    Runfiles defaultRunfiles = null;
    for (String key : provider.getKeys()) {
        if (key.equals("files")) {
            // If we specify files_to_build we don't have the executable in it by default.
            builder.setFilesToBuild(cast("files", provider, SkylarkNestedSet.class, Artifact.class, loc).getSet(Artifact.class));
        } else if (key.equals("runfiles")) {
            statelessRunfiles = cast("runfiles", provider, Runfiles.class, loc);
        } else if (key.equals("data_runfiles")) {
            dataRunfiles = cast("data_runfiles", provider, Runfiles.class, loc);
        } else if (key.equals("default_runfiles")) {
            defaultRunfiles = cast("default_runfiles", provider, Runfiles.class, loc);
        } else if (key.equals("output_groups")) {
            addOutputGroups(provider.getValue(key), loc, builder);
        } else if (key.equals("instrumented_files")) {
            SkylarkClassObject insStruct = cast("instrumented_files", provider, SkylarkClassObject.class, loc);
            Location insLoc = insStruct.getCreationLoc();
            FileTypeSet fileTypeSet = FileTypeSet.ANY_FILE;
            if (insStruct.getKeys().contains("extensions")) {
                @SuppressWarnings("unchecked") List<String> exts = cast("extensions", insStruct, SkylarkList.class, String.class, insLoc);
                if (exts.isEmpty()) {
                    fileTypeSet = FileTypeSet.NO_FILE;
                } else {
                    FileType[] fileTypes = new FileType[exts.size()];
                    for (int i = 0; i < fileTypes.length; i++) {
                        fileTypes[i] = FileType.of(exts.get(i));
                    }
                    fileTypeSet = FileTypeSet.of(fileTypes);
                }
            }
            List<String> dependencyAttributes = Collections.emptyList();
            if (insStruct.getKeys().contains("dependency_attributes")) {
                dependencyAttributes = cast("dependency_attributes", insStruct, SkylarkList.class, String.class, insLoc);
            }
            List<String> sourceAttributes = Collections.emptyList();
            if (insStruct.getKeys().contains("source_attributes")) {
                sourceAttributes = cast("source_attributes", insStruct, SkylarkList.class, String.class, insLoc);
            }
            InstrumentationSpec instrumentationSpec = new InstrumentationSpec(fileTypeSet).withSourceAttributes(sourceAttributes.toArray(new String[0])).withDependencyAttributes(dependencyAttributes.toArray(new String[0]));
            InstrumentedFilesProvider instrumentedFilesProvider = InstrumentedFilesCollector.collect(ruleContext, instrumentationSpec, InstrumentedFilesCollector.NO_METADATA_COLLECTOR, Collections.<Artifact>emptySet());
            builder.addProvider(InstrumentedFilesProvider.class, instrumentedFilesProvider);
        } else if (registeredProviderTypes.containsKey(key)) {
            Class<? extends TransitiveInfoProvider> providerType = registeredProviderTypes.get(key);
            TransitiveInfoProvider providerField = cast(key, provider, providerType, loc);
            builder.addProvider(providerType, providerField);
        } else if (isDefaultProvider) {
            // Custom keys are not allowed for default providers
            throw new EvalException(loc, "Invalid key for default provider: " + key);
        } else if (key.equals("providers")) {
            Iterable iterable = cast(key, provider, Iterable.class, loc);
            for (Object o : iterable) {
                SkylarkClassObject declaredProvider = SkylarkType.cast(o, SkylarkClassObject.class, loc, "The value of 'providers' should be a sequence of declared providers");
                builder.addSkylarkDeclaredProvider(declaredProvider, loc);
            }
        } else if (!key.equals("executable")) {
            // We handled executable already.
            builder.addSkylarkTransitiveInfo(key, provider.getValue(key), loc);
        }
    }
    addSimpleProviders(builder, ruleContext, loc, executable, statelessRunfiles, dataRunfiles, defaultRunfiles, (isDefaultProvider ? provider : null));
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) InstrumentationSpec(com.google.devtools.build.lib.rules.test.InstrumentedFilesCollector.InstrumentationSpec) InstrumentedFilesProvider(com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider) EvalException(com.google.devtools.build.lib.syntax.EvalException) Artifact(com.google.devtools.build.lib.actions.Artifact) Runfiles(com.google.devtools.build.lib.analysis.Runfiles) FileTypeSet(com.google.devtools.build.lib.util.FileTypeSet) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) SkylarkList(com.google.devtools.build.lib.syntax.SkylarkList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ClassObject(com.google.devtools.build.lib.syntax.ClassObject) TransitiveInfoProvider(com.google.devtools.build.lib.analysis.TransitiveInfoProvider) Location(com.google.devtools.build.lib.events.Location)

Aggregations

Location (com.google.devtools.build.lib.events.Location)20 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)4 Test (org.junit.Test)4 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)3 EvalException (com.google.devtools.build.lib.syntax.EvalException)2 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 List (java.util.List)2 ImmutableList (com.google.common.collect.ImmutableList)1 DummyOrigin (com.google.copybara.testing.DummyOrigin)1 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)1 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)1 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 ConfiguredAspect (com.google.devtools.build.lib.analysis.ConfiguredAspect)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 TransitiveInfoProvider (com.google.devtools.build.lib.analysis.TransitiveInfoProvider)1 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)1 RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)1 Event (com.google.devtools.build.lib.events.Event)1