forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a BzlLoadValue.Key to BazelModuleContext to achieve this. However, since BazelModuleContext is inseparable from Label, which BzlLoadValue.Key depends on, that would create a circular dependency. Adds the BazelModuleKey interface to break the cycle. PiperOrigin-RevId: 702700386 Change-Id: I59c93167b97b86a524f6a62ae4b5ffa991a9b729
- Loading branch information
1 parent
eae9d4d
commit 54237be
Showing
14 changed files
with
266 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/main/java/com/google/devtools/build/lib/cmdline/BazelModuleKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2020 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.cmdline; | ||
|
||
import com.google.devtools.build.lib.skyframe.SkyFunctions; | ||
import com.google.devtools.build.skyframe.SkyFunctionName; | ||
import com.google.devtools.build.skyframe.SkyKey; | ||
|
||
/** | ||
* Interface for {@link com.google.devtools.build.lib.skyframe.BzlLoadValue.Key}. | ||
* | ||
* <p>This exists to break what would otherwise be a circular dependency between {@link Label}, | ||
* {@link BazelModuleContext} and {@link com.google.devtools.build.lib.skyframe.BzlLoadValue.Key}. | ||
*/ | ||
public interface BazelModuleKey extends SkyKey { | ||
/** Absolute label of the .bzl file to be loaded. */ | ||
Label getLabel(); | ||
|
||
@Override | ||
default SkyFunctionName functionName() { | ||
return SkyFunctions.BZL_LOAD; | ||
} | ||
|
||
/** Creates a fake instance for testing. */ | ||
static BazelModuleKey createFakeModuleKeyForTesting(Label label) { | ||
return new FakeModuleKey(label); | ||
} | ||
|
||
/** Key for {@link BazelModuleContext}s created outside of Skyframe for testing */ | ||
static final class FakeModuleKey implements BazelModuleKey { | ||
private final Label label; | ||
|
||
private FakeModuleKey(Label label) { | ||
this.label = label; | ||
} | ||
|
||
@Override | ||
public Label getLabel() { | ||
return label; | ||
} | ||
|
||
@Override | ||
public SkyFunctionName functionName() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/com/google/devtools/build/lib/skyframe/serialization/ModuleCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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.devtools.build.lib.skyframe.serialization; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.google.devtools.build.lib.skyframe.BzlLoadValue.bzlLoadKeyCodec; | ||
|
||
import com.google.devtools.build.lib.cmdline.BazelModuleContext; | ||
import com.google.devtools.build.lib.skyframe.BzlLoadValue; | ||
import com.google.protobuf.CodedInputStream; | ||
import com.google.protobuf.CodedOutputStream; | ||
import java.io.IOException; | ||
import net.starlark.java.eval.Module; | ||
|
||
/** | ||
* Codec for {@link Module}. | ||
* | ||
* <p>Serializes using the module's associated {@link BzlLoadValue.Key}. | ||
*/ | ||
public final class ModuleCodec extends DeferredObjectCodec<Module> { | ||
private static final ModuleCodec INSTANCE = new ModuleCodec(); | ||
|
||
public static ModuleCodec moduleCodec() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public Class<Module> getEncodedClass() { | ||
return Module.class; | ||
} | ||
|
||
@Override | ||
public boolean autoRegister() { | ||
// Unit tests that bypass Skyframe for Module loading cannot use this codec. | ||
return false; | ||
} | ||
|
||
@Override | ||
public void serialize(SerializationContext context, Module obj, CodedOutputStream codedOut) | ||
throws IOException, SerializationException { | ||
var moduleContext = checkNotNull(BazelModuleContext.of(obj), "module %s missing context", obj); | ||
context.serializeLeaf((BzlLoadValue.Key) moduleContext.key(), bzlLoadKeyCodec(), codedOut); | ||
} | ||
|
||
@Override | ||
public DeferredValue<Module> deserializeDeferred( | ||
AsyncDeserializationContext context, CodedInputStream codedIn) | ||
throws IOException, SerializationException { | ||
BzlLoadValue.Key bzlLoadKey = context.deserializeLeaf(codedIn, bzlLoadKeyCodec()); | ||
var builder = new DeserializationBuilder(); | ||
context.getSkyValue(bzlLoadKey, builder, DeserializationBuilder::setBzlLoadValue); | ||
return builder; | ||
} | ||
|
||
private static final class DeserializationBuilder implements DeferredValue<Module> { | ||
private BzlLoadValue loadValue; | ||
|
||
@Override | ||
public Module call() { | ||
return checkNotNull(loadValue, "Skyframe lookup value not set").getModule(); | ||
} | ||
|
||
private static void setBzlLoadValue(DeserializationBuilder builder, Object value) { | ||
builder.loadValue = (BzlLoadValue) value; | ||
} | ||
} | ||
|
||
private ModuleCodec() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.