Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
load("@rules_java//java:defs.bzl", "java_library")

package(
default_visibility = ["//src:__subpackages__"],
)

filegroup(
name = "srcs",
srcs = glob(["*"]),
visibility = ["//src:__subpackages__"],
)

java_library(
name = "repo_rule_value",
srcs = [
"BzlmodRepoRuleValue.java",
],
deps = [
"//src/main/java/com/google/devtools/build/lib/concurrent",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:guava",
],
)

java_library(
name = "repo_spec_values",
srcs = [
"RepoSpec.java",
"RepoSpecsValue.java",
],
deps = [
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:autocodec-annotation",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:guava",
"//third_party:auto_value",
],
)

java_library(
name = "repo_spec_functions",
srcs = [
"RepoSpecsFunction.java",
],
deps = [
":repo_spec_values",
"//src/main/java/com/google/devtools/build/lib/actions:file_metadata",
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader",
"//src/main/java/com/google/devtools/build/lib/pkgcache",
"//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/repository",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:gson",
"//third_party:guava",
],
)

java_library(
name = "repo_rule_creator",
srcs = ["BzlmodRepoRuleCreator.java"],
deps = [
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/net/starlark/java/eval",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 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.devtools.build.lib.bazel.bzlmod;

import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.packages.Package;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException;

import net.starlark.java.eval.StarlarkSemantics;

import java.util.Map;

public interface BzlmodRepoRuleCreator {
Rule createRule(Package.Builder pkg, StarlarkSemantics semantics, Map<String, Object> kwargs, EventHandler handler)
throws InterruptedException, InvalidRuleException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2021 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.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.Interner;
import com.google.devtools.build.lib.concurrent.BlazeInterners;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.skyframe.AbstractSkyKey;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.build.skyframe.SkyValue;

public class BzlmodRepoRuleValue implements SkyValue {
public static final SkyFunctionName BZLMOD_REPO_RULE =
SkyFunctionName.createHermetic("BZLMOD_REPO_RULE");

private final Rule rule;

public BzlmodRepoRuleValue(Rule rule) {
this.rule = rule;
}

public Rule getRule() {
return rule;
}

public static Key key(String repositoryName) {
return Key.create(repositoryName);
}

/** Represents an unsuccessful repository lookup. */
public static final class RepoRuleNotFoundValue extends BzlmodRepoRuleValue {
private RepoRuleNotFoundValue() {
super(null);
}

@Override
public Rule getRule() {
throw new IllegalStateException();
}
}

public static final RepoRuleNotFoundValue REPO_RULE_NOT_FOUND_VALUE = new RepoRuleNotFoundValue();

/** Argument for the SkyKey to request a BzlmodRepoRuleValue. */
@AutoCodec
public static class Key extends AbstractSkyKey<String> {
private static final Interner<Key> interner = BlazeInterners.newWeakInterner();

private Key(String arg) {
super(arg);
}

@AutoCodec.VisibleForSerialization
@AutoCodec.Instantiator
static Key create(String arg) {
return interner.intern(new Key(arg));
}

@Override
public SkyFunctionName functionName() {
return BZLMOD_REPO_RULE;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021 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.devtools.build.lib.bazel.bzlmod;

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

import javax.annotation.Nullable;

@AutoValue
public abstract class RepoSpec {

public static RepoSpec create(String bzlFile, String ruleClassName, ImmutableMap<String, Object> attributes) {
return new AutoValue_RepoSpec(bzlFile, ruleClassName, attributes);
}

public static RepoSpec create(String ruleClassName, ImmutableMap<String, Object> attributes) {
return new AutoValue_RepoSpec(null, ruleClassName, attributes);
}

// The label string for the bzl file this repository rule is defined in, null for native repo rule
@Nullable
public abstract String getBzlFile();

public abstract String getRuleClassName();

public abstract ImmutableMap<String, Object> getAttributes();

public boolean isNativeRepoRule() {
return getBzlFile() == null;
}

// Return a string representing the rule class
// eg. Native repo rule: local_repository
// Starlark repo rule: //:repo.bzl%my_repo
public String getRuleClass() {
return (isNativeRepoRule() ? "" : getBzlFile() + "%") + getRuleClassName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 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.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;

import javax.annotation.Nullable;

public class RepoSpecsFunction implements SkyFunction {

@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {

if (skyKey == RepoSpecsValue.KEY_FOR_OVERRIDE_DEP) {
return computeForOverrideDep(env);
} else if (skyKey == RepoSpecsValue.KEY_FOR_BAZEL_MODULE) {
return computeForBazelModule(env);
} else if (skyKey == RepoSpecsValue.KEY_FOR_MODULE_RULE) {
return computeForModuleRule(env);
}
throw new IllegalArgumentException("Unrecognized key: " + skyKey.toString());
}

@Nullable
private SkyValue computeForOverrideDep(Environment env) {
ImmutableMap.Builder<String, RepoSpec> repositories = ImmutableMap.builder();
return new RepoSpecsValue(repositories.build());
}

@Nullable
private SkyValue computeForBazelModule(Environment env) {
ImmutableMap.Builder<String, RepoSpec> repositories = ImmutableMap.builder();
return new RepoSpecsValue(repositories.build());
}

@Nullable
private SkyValue computeForModuleRule(Environment env) {
ImmutableMap.Builder<String, RepoSpec> repositories = ImmutableMap.builder();
return new RepoSpecsValue(repositories.build());
}

@Nullable
@Override
public String extractTag(SkyKey skyKey) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 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.devtools.build.lib.bazel.bzlmod;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;

public class RepoSpecsValue implements SkyValue {
public static final SkyFunctionName REPO_SPECS =
SkyFunctionName.createHermetic("REPO_SPECS");

@AutoCodec
public static final SkyKey KEY_FOR_OVERRIDE_DEP = () -> REPO_SPECS;

@AutoCodec
public static final SkyKey KEY_FOR_BAZEL_MODULE = () -> REPO_SPECS;

@AutoCodec
public static final SkyKey KEY_FOR_MODULE_RULE = () -> REPO_SPECS;

private final ImmutableMap<String, RepoSpec> repositories;

public RepoSpecsValue(ImmutableMap<String, RepoSpec> repositories) {
this.repositories = repositories;
}

public RepoSpec getRepository(String repositoryName) {
return repositories.get(repositoryName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/actions:file_metadata",
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:blaze_directories",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:repo_rule_creator",
"//src/main/java/com/google/devtools/build/lib/bazel/debug:workspace-rule-event",
"//src/main/java/com/google/devtools/build/lib/bazel/repository",
"//src/main/java/com/google/devtools/build/lib/bazel/repository/cache",
Expand Down
Loading