use of com.google.idea.blaze.base.bazel.BuildSystem.BuildInvoker in project intellij by bazelbuild.
the class BuildPhaseSyncTask method doRun.
private void doRun(BlazeContext context) throws SyncFailedException, SyncCanceledException {
List<TargetExpression> targets = Lists.newArrayList();
ProjectViewSet viewSet = projectState.getProjectViewSet();
if (syncParams.addWorkingSet() && projectState.getWorkingSet() != null) {
Collection<TargetExpression> workingSetTargets = getWorkingSetTargets(context);
if (!workingSetTargets.isEmpty()) {
targets.addAll(workingSetTargets);
printTargets(context, "working set", workingSetTargets);
}
}
if (syncParams.addProjectViewTargets()) {
ProjectTargets projectTargets = SyncProjectTargetsHelper.getProjectTargets(project, context, viewSet, projectState.getWorkspacePathResolver(), projectState.getLanguageSettings());
if (!projectTargets.derivedTargets.isEmpty()) {
buildStats.setTargetsDerivedFromDirectories(true);
printTargets(context, "project view directories", projectTargets.derivedTargets);
}
if (!projectTargets.explicitTargets.isEmpty()) {
printTargets(context, "project view targets", projectTargets.explicitTargets);
}
targets.addAll(projectTargets.getTargetsToSync());
}
if (!syncParams.sourceFilesToSync().isEmpty()) {
Collection<TargetExpression> targetsFromSources = findTargetsBuildingSourceFiles(syncParams.sourceFilesToSync(), context);
if (!targetsFromSources.isEmpty()) {
targets.addAll(targetsFromSources);
printTargets(context, syncParams.title() + " (targets derived from query)", targetsFromSources);
}
}
if (!syncParams.targetExpressions().isEmpty()) {
targets.addAll(syncParams.targetExpressions());
printTargets(context, syncParams.title(), syncParams.targetExpressions());
}
buildStats.setTargets(targets);
notifyBuildStarted(context, syncParams.addProjectViewTargets(), ImmutableList.copyOf(targets));
BuildInvoker localInvoker = buildSystem.getBuildInvoker(project, context);
ShardedTargetsResult shardedTargetsResult = BlazeBuildTargetSharder.expandAndShardTargets(project, context, workspaceRoot, viewSet, projectState.getWorkspacePathResolver(), targets, localInvoker, buildSystem.getSyncStrategy(project));
if (shardedTargetsResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
throw new SyncFailedException();
}
ShardedTargetList shardedTargets = shardedTargetsResult.shardedTargets;
boolean parallel;
SyncStrategy strategy = buildSystem.getSyncStrategy(project);
switch(strategy) {
case PARALLEL:
parallel = true;
break;
case DECIDE_AUTOMATICALLY:
parallel = shardedTargets.shardCount() > 1;
break;
case SERIAL:
parallel = false;
break;
default:
throw new IllegalStateException("Invalid sync strategy: " + strategy);
}
if (!shardedTargetsResult.shardedTargets.shardStats().shardingApproach().equals(ShardingApproach.PARTITION_WITHOUT_EXPANDING)) {
int targetCount = shardedTargets.shardStats().actualTargetSizePerShard().stream().mapToInt(Integer::intValue).sum();
printShardingSummary(context, targetCount, shardedTargets.shardCount(), parallel);
}
BuildInvoker syncBuildInvoker = null;
if (parallel) {
syncBuildInvoker = buildSystem.getParallelBuildInvoker(project, context).orElse(null);
}
if (syncBuildInvoker == null) {
syncBuildInvoker = localInvoker;
}
resultBuilder.setBlazeInfo(syncBuildInvoker.getBlazeInfo());
buildStats.setSyncSharded(shardedTargets.shardCount() > 1).setShardCount(shardedTargets.shardCount()).setShardStats(shardedTargets.shardStats()).setParallelBuilds(syncBuildInvoker.supportsParallelism());
BlazeBuildOutputs blazeBuildResult = getBlazeBuildResult(context, viewSet, shardedTargets, syncBuildInvoker);
resultBuilder.setBuildResult(blazeBuildResult);
buildStats.setBuildResult(blazeBuildResult.buildResult).setBuildIds(blazeBuildResult.buildIds).setBuildBinaryType(syncBuildInvoker.getType()).setBepBytesConsumed(blazeBuildResult.bepBytesConsumed);
if (context.isCancelled()) {
throw new SyncCanceledException();
}
String invocationResultMsg = "Build invocation result: " + blazeBuildResult.buildResult.status;
if (blazeBuildResult.buildResult.status == BuildResult.Status.FATAL_ERROR) {
context.setHasError();
if (blazeBuildResult.buildResult.outOfMemory()) {
SuggestBuildShardingNotification.syncOutOfMemoryError(project, context);
}
context.output(PrintOutput.error(invocationResultMsg));
throw new SyncFailedException();
}
context.output(PrintOutput.log(invocationResultMsg));
}
use of com.google.idea.blaze.base.bazel.BuildSystem.BuildInvoker in project intellij by bazelbuild.
the class BlazeIdeInterfaceAspectsImpl method build.
@Override
public BlazeBuildOutputs build(Project project, BlazeContext context, WorkspaceRoot workspaceRoot, BlazeVersionData blazeVersion, BuildInvoker invoker, ProjectViewSet projectViewSet, ShardedTargetList shardedTargets, WorkspaceLanguageSettings workspaceLanguageSettings, ImmutableSet<OutputGroup> outputGroups, BlazeInvocationContext blazeInvocationContext) {
AspectStrategy aspectStrategy = AspectStrategy.getInstance(blazeVersion);
final Ref<BlazeBuildOutputs> combinedResult = new Ref<>();
// The build is a sync iff INFO output group is present
boolean isSync = outputGroups.contains(OutputGroup.INFO);
Function<Integer, String> progressMessage = count -> String.format("Building targets for shard %s of %s...", count, shardedTargets.shardCount());
final ShardedBuildProgressTracker progressTracker = new ShardedBuildProgressTracker(shardedTargets.shardCount());
// Sync only flags (sync_only) override build_flags, so log them to warn the users
List<String> syncOnlyFlags = BlazeFlags.expandBuildFlags(projectViewSet.listItems(SyncFlagsSection.KEY));
if (!syncOnlyFlags.isEmpty()) {
String message = String.format("Sync flags (`%s`) specified in the project view file will override the build" + " flags set in blazerc configurations or general build flags in the" + " project view file.", String.join(" ", syncOnlyFlags));
// Print to both summary and print outputs (i.e. main and subtask window of blaze console)
context.output(SummaryOutput.output(Prefix.INFO, message));
context.output(PrintOutput.log(message));
}
// Fetching blaze flags here using parent context, to avoid duplicate fetch for every shard.
List<String> additionalBlazeFlags = BlazeFlags.blazeFlags(project, projectViewSet, BlazeCommandName.BUILD, context, blazeInvocationContext);
Function<List<? extends TargetExpression>, BuildResult> invocation = targets -> Scope.push(context, (childContext) -> {
Task task = createTask(project, context, "Build shard " + StringUtil.first(UUID.randomUUID().toString(), 8, true), isSync);
// we use context (rather than childContext) here since the shard state relates to
// the parent task (which encapsulates all the build shards).
setupToolWindow(project, childContext, workspaceRoot, task);
progressTracker.onBuildStarted(context);
BlazeBuildOutputs result = runBuildForTargets(project, childContext, workspaceRoot, invoker, projectViewSet, workspaceLanguageSettings.getActiveLanguages(), targets, aspectStrategy, outputGroups, additionalBlazeFlags);
// TODO(b/216104482) track failures
progressTracker.onBuildCompleted(context);
printShardFinishedSummary(context, task.getName(), result);
if (!result.buildResult.outOfMemory()) {
synchronized (combinedResult) {
combinedResult.set(combinedResult.isNull() ? result : combinedResult.get().updateOutputs(result));
}
}
return result.buildResult;
});
BuildResult buildResult = shardedTargets.runShardedCommand(project, context, progressMessage, invocation, invoker);
if (combinedResult.isNull() || buildResult.status == Status.FATAL_ERROR) {
return BlazeBuildOutputs.noOutputs(buildResult);
}
return combinedResult.get();
}
use of com.google.idea.blaze.base.bazel.BuildSystem.BuildInvoker in project intellij by bazelbuild.
the class BlazeBuildTargetSharder method doExpandWildcardTargets.
private static ExpandedTargetsResult doExpandWildcardTargets(Project project, BlazeContext context, WorkspaceRoot workspaceRoot, BuildInvoker buildBinary, ProjectViewSet projectViewSet, WorkspacePathResolver pathResolver, List<TargetExpression> targets) {
List<WildcardTargetPattern> includes = getWildcardPatterns(targets);
if (includes.isEmpty()) {
return new ExpandedTargetsResult(targets, BuildResult.SUCCESS);
}
Map<TargetExpression, List<TargetExpression>> expandedTargets = WildcardTargetExpander.expandToNonRecursiveWildcardTargets(project, context, pathResolver, includes);
if (expandedTargets == null) {
return new ExpandedTargetsResult(ImmutableList.of(), BuildResult.FATAL_ERROR);
}
// replace original recursive targets with expanded list, retaining relative ordering
List<TargetExpression> fullList = new ArrayList<>();
for (TargetExpression target : targets) {
List<TargetExpression> expanded = expandedTargets.get(target);
if (expanded == null) {
fullList.add(target);
} else {
fullList.addAll(expanded);
}
}
ExpandedTargetsResult result = WildcardTargetExpander.expandToSingleTargets(project, context, workspaceRoot, buildBinary, projectViewSet, fullList);
// finally add back any explicitly-specified, unexcluded single targets which may have been
// removed by the query (for example, because they have the 'manual' tag)
TargetExpressionList helper = TargetExpressionList.create(targets);
List<TargetExpression> singleTargets = targets.stream().filter(t -> !t.isExcluded()).filter(t -> !isWildcardPattern(t)).filter(t -> t instanceof Label).filter(t -> helper.includesTarget((Label) t)).collect(toImmutableList());
return ExpandedTargetsResult.merge(result, new ExpandedTargetsResult(singleTargets, result.buildResult));
}
Aggregations