Skip to content

Commit

Permalink
Merge pull request bazelbuild#12 from Wyverald/git
Browse files Browse the repository at this point in the history
Patch support for overrides & git_override support
  • Loading branch information
meteorcloudy authored May 11, 2021
2 parents 8e76ade + 5ea0ca9 commit c660ce5
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static ArchiveOverride create(ImmutableList<String> urls, ImmutableList<S
@Override
public RepoSpec getRepoSpec(String repoName) {
return IndexRegistry.getRepoSpecForArchive(
repoName, getUrls(), getPatches(), ImmutableMap.of(), getIntegrity(), getStripPrefix(), getPatchStrip());
repoName, getUrls(), getPatches(), ImmutableMap.of(), getIntegrity(), getStripPrefix(),
getPatchStrip());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ java_library(
"ArchiveOverride.java",
"DiscoveryFunction.java",
"DiscoveryValue.java",
"GitOverride.java",
"LocalPathOverride.java",
"Module.java",
"ModuleFileFunction.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.bzlmod.repo.RepoSpec;

@AutoValue
public abstract class GitOverride implements NonRegistryOverride {

private static final String GIT_REPOSITORY_RULE_CLASS =
"@bazel_tools//tools/build_defs/repo:git.bzl%git_repository";

public static GitOverride create(String remote, String commit, ImmutableList<String> patches, int patchStrip) {
return new AutoValue_GitOverride(remote, commit, patches, patchStrip);
}

public abstract String getRemote();
public abstract String getCommit();
public abstract ImmutableList<String> getPatches();
public abstract int getPatchStrip();

@Override
public RepoSpec getRepoSpec(String repoName) {
ImmutableMap.Builder<String, Object> attrBuilder = ImmutableMap.builder();
attrBuilder.put("name", repoName)
.put("remote", getRemote())
.put("commit", getCommit())
.put("patches", getPatches())
.put("patch_args", ImmutableList.of("-p" + getPatchStrip()));
return new RepoSpec(GIT_REPOSITORY_RULE_CLASS, attrBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import net.starlark.java.eval.StarlarkInt;

public class ModuleFileGlobals implements ModuleFileGlobalsApi<ModuleFileFunctionException> {

Expand Down Expand Up @@ -56,13 +57,16 @@ public void overrideDep(String name, StarlarkOverrideApi override)
}

@Override
public StarlarkOverrideApi singleVersionOverride(String version, String registry) {
return SingleVersionOverride.create(version, registry);
public StarlarkOverrideApi singleVersionOverride(String version, String registry,
Iterable<?> patches, StarlarkInt patchStrip) throws EvalException {
return SingleVersionOverride.create(version, registry,
ImmutableList.copyOf((Iterable<String>) patches),
patchStrip.toInt("single_version_override.patch_strip"));
}

@Override
public StarlarkOverrideApi archiveOverride(Object urls, String integrity,
String stripPrefix) {
String stripPrefix, Iterable<?> patches, StarlarkInt patchStrip) throws EvalException {
ImmutableList.Builder<String> urlList = new ImmutableList.Builder<>();
if (urls instanceof String) {
urlList.add((String) urls);
Expand All @@ -71,8 +75,17 @@ public StarlarkOverrideApi archiveOverride(Object urls, String integrity,
urlList.add(urlString);
}
}
// TODO: add patch file support here as well
return ArchiveOverride.create(urlList.build(), ImmutableList.of(), integrity, stripPrefix, 0);
return ArchiveOverride.create(
urlList.build(), ImmutableList.copyOf((Iterable<String>) patches), integrity, stripPrefix,
patchStrip.toInt("archive_override.patch_strip"));
}

@Override
public StarlarkOverrideApi gitOverride(String remote, String commit, Iterable<?> patches,
StarlarkInt patchStrip) throws EvalException {
return GitOverride.create(
remote, commit, ImmutableList.copyOf((Iterable<String>) patches),
patchStrip.toInt("git_override.patch_strip"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public SkyValue compute(SkyKey skyKey, Environment env)
collectDeps(ModuleKey.create(discovery.getRootModuleName(), ""), newDepGraph, referenced);
ImmutableMap<ModuleKey, Module> finalDepGraph =
ImmutableMap.copyOf(Maps.filterKeys(newDepGraph, referenced::contains));
return SelectionValue.create(discovery.getRootModuleName(), finalDepGraph);
return SelectionValue.create(
discovery.getRootModuleName(), finalDepGraph, discovery.getOverrides());
}

private void collectDeps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.skyframe.SkyFunctions;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.lib.starlarkbuildapi.repository.StarlarkOverrideApi;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;

Expand All @@ -14,12 +15,15 @@ public abstract class SelectionValue implements SkyValue {
public static final SkyKey KEY = () -> SkyFunctions.SELECTION;

public static SelectionValue create(String rootModuleName,
ImmutableMap<ModuleKey, Module> depGraph) {
return new AutoValue_SelectionValue(rootModuleName, depGraph);
ImmutableMap<ModuleKey, Module> depGraph,
ImmutableMap<String, StarlarkOverrideApi> overrides) {
return new AutoValue_SelectionValue(rootModuleName, depGraph, overrides);
}

public abstract String getRootModuleName();

public abstract ImmutableMap<ModuleKey, Module> getDepGraph();

public abstract ImmutableMap<String, StarlarkOverrideApi> getOverrides();

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;

@AutoValue
public abstract class SingleVersionOverride implements RegistryOverride {

public static SingleVersionOverride create(String version, String registry) {
return new AutoValue_SingleVersionOverride(version, registry);
public static SingleVersionOverride create(String version, String registry,
ImmutableList<String> patches, int patchStrip) {
return new AutoValue_SingleVersionOverride(version, registry, patches, patchStrip);
}

public abstract String getVersion();

@Override
public abstract String getRegistry();

public abstract ImmutableList<String> getPatches();

public abstract int getPatchStrip();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.FileValue;
import com.google.devtools.build.lib.bazel.bzlmod.Module;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileValue;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleKey;
import com.google.devtools.build.lib.bazel.bzlmod.NonRegistryOverride;
import com.google.devtools.build.lib.bazel.bzlmod.RegistryOverride;
import com.google.devtools.build.lib.bazel.bzlmod.SelectionValue;
import com.google.devtools.build.lib.bazel.bzlmod.SingleVersionOverride;
import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
import com.google.devtools.build.lib.skyframe.PrecomputedValue;
import com.google.devtools.build.lib.starlarkbuildapi.repository.StarlarkOverrideApi;
Expand All @@ -30,6 +32,7 @@
import javax.annotation.Nullable;

public class RepoSpecsFunction implements SkyFunction {

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
Expand Down Expand Up @@ -95,6 +98,10 @@ private SkyValue computeForBazelModule(Environment env)
} else {
repoSpec = module.getRegistry().getRepoSpec(moduleKey, repoName, env.getListener());
}
// We may need to apply an extra set of patches here when the module has a single version
// override with patches.
repoSpec = maybeAppendAdditionalPatches(repoSpec, moduleKey,
selectionValue.getOverrides().get(moduleKey.getName()));
repositories.put(repoName, repoSpec);
} catch (IOException e) {
throw new RepoSpecsFunctionException(e, Transience.PERSISTENT);
Expand All @@ -103,6 +110,36 @@ private SkyValue computeForBazelModule(Environment env)
return new RepoSpecsValue(repositories.build());
}

private RepoSpec maybeAppendAdditionalPatches(RepoSpec repoSpec, ModuleKey moduleKey,
StarlarkOverrideApi override)
throws RepoSpecsFunctionException {
if (!(override instanceof SingleVersionOverride)) {
return repoSpec;
}
SingleVersionOverride singleVersion = (SingleVersionOverride) override;
if (singleVersion.getPatches().isEmpty()) {
return repoSpec;
}
if (!ImmutableList.of("-p" + singleVersion.getPatchStrip())
.equals(repoSpec.getAttributes().get("patch_args"))) {
// The registry specifies a different patch_strip than the single version override. We
// can only throw an exception here.
throw new RepoSpecsFunctionException(new IOException(String.format(
"Module %s has a single_version_override which specifies a different patch_strip"
+ " (%d) than the registry (%s)",
moduleKey.toString(), singleVersion.getPatchStrip(),
repoSpec.getAttributes().get("patch_args"))), Transience.PERSISTENT);
}
// Append the patches from the override.
ImmutableList<String> newPatches = ImmutableList.copyOf(Iterables.concat(
(ImmutableList<String>) repoSpec.getAttributes().get("patches"),
singleVersion.getPatches()));
HashMap<String, Object> newAttrs = new HashMap<>(repoSpec.getAttributes().size());
newAttrs.putAll(repoSpec.getAttributes());
newAttrs.put("patches", newPatches);
return new RepoSpec(repoSpec.getRuleClass(), ImmutableMap.copyOf(newAttrs));
}

@Nullable
private SkyValue computeForModuleRule(Environment env)
throws SkyFunctionException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkInt;

/**
* A collection of global Starlark build API functions that apply to WORKSPACE files.
Expand Down Expand Up @@ -48,7 +49,8 @@ public interface ModuleFileGlobalsApi<ModuleFileFunctionExceptionT extends Excep
// & platforms
},
extraKeywords = @Param(name = "kwargs"))
void module(String name, String version, Dict<String, Object> kwargs) throws EvalException, InterruptedException;
void module(String name, String version, Dict<String, Object> kwargs)
throws EvalException, InterruptedException;

@StarlarkMethod(
name = "bazel_dep",
Expand Down Expand Up @@ -108,9 +110,21 @@ void overrideDep(String name, StarlarkOverrideApi override)
named = true,
positional = false,
defaultValue = "''"),
// TODO: patch_files, patch_strip
@Param(
name = "patches",
doc = "TODO",
named = true,
positional = false,
defaultValue = "[]"),
@Param(
name = "patch_strip",
doc = "TODO",
named = true,
positional = false,
defaultValue = "0"),
})
StarlarkOverrideApi singleVersionOverride(String version, String registry);
StarlarkOverrideApi singleVersionOverride(String version, String registry, Iterable<?> patches,
StarlarkInt patchStrip) throws EvalException;

@StarlarkMethod(
name = "archive_override",
Expand All @@ -137,10 +151,53 @@ void overrideDep(String name, StarlarkOverrideApi override)
named = true,
positional = false,
defaultValue = "''"),
// TODO: patch_files, patch_strip
@Param(
name = "patches",
doc = "TODO",
named = true,
positional = false,
defaultValue = "[]"),
@Param(
name = "patch_strip",
doc = "TODO",
named = true,
positional = false,
defaultValue = "0"),
})
StarlarkOverrideApi archiveOverride(Object urls, String integrity, String stripPrefix,
Iterable<?> patches, StarlarkInt patchStrip)
throws EvalException, ModuleFileFunctionExceptionT;

@StarlarkMethod(
name = "git_override",
doc = "TODO",
parameters = {
@Param(
name = "remote",
doc = "TODO",
named = true,
positional = false),
@Param(
name = "commit",
doc = "TODO",
named = true,
positional = false,
defaultValue = "''"),
@Param(
name = "patches",
doc = "TODO",
named = true,
positional = false,
defaultValue = "[]"),
@Param(
name = "patch_strip",
doc = "TODO",
named = true,
positional = false,
defaultValue = "0"),
})
StarlarkOverrideApi archiveOverride(Object urls, String integrity, String stripPrefix)
throws ModuleFileFunctionExceptionT;
StarlarkOverrideApi gitOverride(String remote, String commit, Iterable<?> patches,
StarlarkInt patchStrip) throws EvalException, ModuleFileFunctionExceptionT;

@StarlarkMethod(
name = "local_path_override",
Expand Down

0 comments on commit c660ce5

Please sign in to comment.