Skip to content

Commit

Permalink
Fix Fast Run for Bazel 7
Browse files Browse the repository at this point in the history
(cherry picked from commit 96c0892)
  • Loading branch information
mai93 committed Feb 12, 2024
1 parent 4e4d1aa commit a61a256
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.idea.blaze.base.model.BlazeVersionData;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.settings.BuildSystemName;
Expand All @@ -29,22 +30,37 @@ public ImmutableSet<BuildSystemName> getSupportedBuildSystems() {
}

@Override
public ImmutableList<? extends TargetExpression> getBuildTargets(Label label) {
return ImmutableList.of(createDeployJarLabel(label), label);
public ImmutableList<? extends TargetExpression> getBuildTargets(
Label label, BlazeVersionData versionData) {
if (versionData.bazelIsAtLeastVersion(7, 0, 1)) {
return ImmutableList.of(label);
}
return ImmutableList.of(createDeployJarLabel(label, versionData), label);
}

@Override
public ImmutableList<String> getBuildFlags() {
public ImmutableList<String> getBuildFlags(BlazeVersionData versionData) {
if (versionData.bazelIsAtLeastVersion(7, 0, 1)) {
return ImmutableList.of(
"--experimental_java_test_auto_create_deploy_jar",
"--output_groups=+_hidden_top_level_INTERNAL_");
}
return ImmutableList.of();
}

@Override
public Label createDeployJarLabel(Label label) {
public Label createDeployJarLabel(Label label, BlazeVersionData versionData) {
if (versionData.bazelIsAtLeastVersion(7, 0, 1)) {
return Label.create(label + "_auto_deploy.jar");
}
return Label.create(label + "_deploy.jar");
}

@Override
public Label deployJarOwnerLabel(Label label) {
return createDeployJarLabel(label);
public Label deployJarOwnerLabel(Label label, BlazeVersionData versionData) {
if (versionData.bazelIsAtLeastVersion(7, 0, 1)) {
return label;
}
return createDeployJarLabel(label, versionData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.idea.blaze.java.fastbuild;

import com.google.common.collect.ImmutableList;
import com.google.idea.blaze.base.model.BlazeVersionData;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.settings.BuildSystemName;
Expand All @@ -30,11 +31,12 @@ static FastBuildDeployJarStrategy getInstance(BuildSystemName buildSystemName) {
return BuildSystemExtensionPoint.getInstance(EP_NAME, buildSystemName);
}

public abstract ImmutableList<? extends TargetExpression> getBuildTargets(Label label);
public abstract ImmutableList<? extends TargetExpression> getBuildTargets(
Label label, BlazeVersionData versionData);

public abstract ImmutableList<String> getBuildFlags();
public abstract ImmutableList<String> getBuildFlags(BlazeVersionData versionData);

public abstract Label createDeployJarLabel(Label label);
public abstract Label createDeployJarLabel(Label label, BlazeVersionData versionData);

public abstract Label deployJarOwnerLabel(Label label);
public abstract Label deployJarOwnerLabel(Label label, BlazeVersionData versionData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,33 +269,34 @@ private FastBuildState.BuildOutput buildDeployJar(
Label label,
FastBuildParameters buildParameters,
BuildResultHelper resultHelper) {
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
BlazeInfo blazeInfo = getBlazeInfo(context, buildParameters);

BlazeVersionData blazeVersionData =
BlazeVersionData.build(
Blaze.getBuildSystemProvider(project).getBuildSystem(), workspaceRoot, blazeInfo);

FastBuildDeployJarStrategy deployJarStrategy =
FastBuildDeployJarStrategy.getInstance(Blaze.getBuildSystemName(project));
Label deployJarLabel = deployJarStrategy.createDeployJarLabel(label);
Label deployJarLabel = deployJarStrategy.createDeployJarLabel(label, blazeVersionData);
context.output(
new StatusOutput(
"Building base deploy jar for fast builds: " + deployJarLabel.targetName()));

BlazeInfo blazeInfo = getBlazeInfo(context, buildParameters);
FastBuildAspectStrategy aspectStrategy =
FastBuildAspectStrategy.getInstance(Blaze.getBuildSystemName(project));

Stopwatch timer = Stopwatch.createStarted();

BlazeCommand.Builder command =
BlazeCommand.builder(buildParameters.blazeBinary(), BlazeCommandName.BUILD)
.addTargets(deployJarStrategy.getBuildTargets(label))
.addBlazeFlags(deployJarStrategy.getBuildFlags())
.addTargets(deployJarStrategy.getBuildTargets(label, blazeVersionData))
.addBlazeFlags(deployJarStrategy.getBuildFlags(blazeVersionData))
.addBlazeFlags(buildParameters.buildFlags())
.addBlazeFlags(resultHelper.getBuildFlags());

WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);

aspectStrategy.addAspectAndOutputGroups(
command,
BlazeVersionData.build(
Blaze.getBuildSystemProvider(project).getBuildSystem(), workspaceRoot, blazeInfo),
/* additionalOutputGroups...= */ "default");
command, blazeVersionData, /* additionalOutputGroups...= */ "default");

int exitCode =
ExternalTask.builder(workspaceRoot)
Expand All @@ -318,7 +319,7 @@ private FastBuildState.BuildOutput buildDeployJar(
ImmutableList<File> deployJarArtifacts =
BlazeArtifact.getLocalFiles(
resultHelper.getBuildArtifactsForTarget(
deployJarStrategy.deployJarOwnerLabel(label), jarPredicate));
deployJarStrategy.deployJarOwnerLabel(label, blazeVersionData), jarPredicate));
checkState(deployJarArtifacts.size() == 1);
File deployJar = deployJarArtifacts.get(0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ final class BazelFastBuildTestEnvironmentCreator extends FastBuildTestEnvironmen
// Bazel adds the Java launcher to the runfiles path when building a Java test target.
private static final File STANDARD_JAVA_BINARY = new File("../local_jdk/bin/java");

// TODO: b/295221112 - remove LAUNCHER_ALIAS once label_flag is used
private static final String LAUNCHER_ALIAS = "@@bazel_tools//tools/jdk:launcher_flag_alias";

@Override
String getTestClassProperty() {
return "bazel.test_suite";
Expand All @@ -44,13 +47,20 @@ File getJavaBinFromLauncher(
@Nullable Label javaLauncher,
boolean swigdeps,
String runfilesPath) {
if (javaLauncher == null) {
if (javaLauncher == null || isDefaultLauncher(javaLauncher)) {
return getStandardJavaBinary(runfilesPath);
} else {
return new File(getTestBinary(label) + "_nativedeps");
}
}

private static boolean isDefaultLauncher(Label label) {
// Use com.google.idea.blaze.common.Label to handle both cases of `@` and `@@` correctly
com.google.idea.blaze.common.Label canonicalLabel =
new com.google.idea.blaze.common.Label(label.toString());
return canonicalLabel.toString().equals(LAUNCHER_ALIAS);
}

/**
* Look for the directory containing Bazel local jdk and return the java binary.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright 2024 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.idea.blaze.java.fastbuild;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.idea.blaze.base.bazel.BazelVersion;
import com.google.idea.blaze.base.model.BlazeVersionData;
import com.google.idea.blaze.base.model.primitives.Label;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link BazelFastBuildDeployJarStrategy}. */
@RunWith(JUnit4.class)
public final class BazelFastBuildDeployJarStrategyTest {

private BazelFastBuildDeployJarStrategy bazelFastBuildDeployJarStrategy;

private static final BlazeVersionData BAZEL_7_1_0 =
BlazeVersionData.builder().setBazelVersion(new BazelVersion(7, 1, 0)).build();
private static final BlazeVersionData BAZEL_6_5_0 =
BlazeVersionData.builder().setBazelVersion(new BazelVersion(6, 5, 0)).build();

@Before
public void setUp() {
bazelFastBuildDeployJarStrategy = new BazelFastBuildDeployJarStrategy();
}

@Test
public void buildTargetsBazel_7_1_0() {
Label testTarget = Label.create("//pkg:foo_test");

ImmutableList<String> buildTargets =
bazelFastBuildDeployJarStrategy.getBuildTargets(testTarget, BAZEL_7_1_0).stream()
.map(t -> t.toString())
.collect(toImmutableList());

assertThat(buildTargets).containsExactly("//pkg:foo_test");
}

@Test
public void buildTargetsBeforeBazel_6_5_0() {
Label testTarget = Label.create("//pkg:foo_test");

ImmutableList<String> buildTargets =
bazelFastBuildDeployJarStrategy.getBuildTargets(testTarget, BAZEL_6_5_0).stream()
.map(t -> t.toString())
.collect(toImmutableList());

assertThat(buildTargets).containsExactly("//pkg:foo_test_deploy.jar", "//pkg:foo_test");
}

@Test
public void buildFlagsBazel_7_1_0() {
ImmutableList<String> buildFlags = bazelFastBuildDeployJarStrategy.getBuildFlags(BAZEL_7_1_0);

assertThat(buildFlags)
.containsExactly(
"--experimental_java_test_auto_create_deploy_jar",
"--output_groups=+_hidden_top_level_INTERNAL_");
}

@Test
public void buildFlagsBeforeBazel_6_5_0() {
ImmutableList<String> buildFlags = bazelFastBuildDeployJarStrategy.getBuildFlags(BAZEL_6_5_0);

assertThat(buildFlags).isEmpty();
}

@Test
public void deployJarLabelBazel_7_1_0() {
Label testTarget = Label.create("//pkg:foo_test");

Label deployJarLabel =
bazelFastBuildDeployJarStrategy.createDeployJarLabel(testTarget, BAZEL_7_1_0);

assertThat(deployJarLabel.toString()).isEqualTo("//pkg:foo_test_auto_deploy.jar");
}

@Test
public void deployJarLabelBeforeBazel_6_5_0() {
Label testTarget = Label.create("//pkg:foo_test");

Label deployJarLabel =
bazelFastBuildDeployJarStrategy.createDeployJarLabel(testTarget, BAZEL_6_5_0);

assertThat(deployJarLabel.toString()).isEqualTo("//pkg:foo_test_deploy.jar");
}

@Test
public void deployJarOwnerLabelBazel_7_1_0() {
Label testTarget = Label.create("//pkg:foo_test");

Label deployJarOwnerLabel =
bazelFastBuildDeployJarStrategy.deployJarOwnerLabel(testTarget, BAZEL_7_1_0);

assertThat(deployJarOwnerLabel).isEqualTo(testTarget);
}

@Test
public void deployJarOwnerLabelBeforeBazel_6_5_0() {
Label testTarget = Label.create("//pkg:foo_test");

Label deployJarOwnerLabel =
bazelFastBuildDeployJarStrategy.deployJarOwnerLabel(testTarget, BAZEL_6_5_0);

assertThat(deployJarOwnerLabel.toString()).isEqualTo("//pkg:foo_test_deploy.jar");
}
}

0 comments on commit a61a256

Please sign in to comment.