Skip to content

Commit

Permalink
Re-arrange PlatformInfo and clean up some code.
Browse files Browse the repository at this point in the history
Work towards platform-based flags: #19409.

PiperOrigin-RevId: 565770775
Change-Id: Ibb75e66f6a73aad565ade28e5cda99ebaa280121
  • Loading branch information
katre authored and copybara-github committed Sep 15, 2023
1 parent d9beda5 commit d8d3593
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ public class PlatformInfo extends NativeInfo
/** Name used in Starlark for accessing this provider. */
public static final String STARLARK_NAME = "PlatformInfo";

/** Empty {@link PlatformInfo} instance representing an invalid or empty platform. */
public static final PlatformInfo EMPTY_PLATFORM_INFO;

static {
try {
EMPTY_PLATFORM_INFO = PlatformInfo.builder().build();
} catch (DuplicateConstraintException | ExecPropertiesException e) {
// This can never happen since we're not passing any values to the builder.
throw new VerifyException(e);
}
}

/** Provider singleton constant. */
public static final BuiltinProvider<PlatformInfo> PROVIDER = new Provider();

/** Provider for {@link ToolchainInfo} objects. */
/** Provider for {@link PlatformInfo} objects. */
private static class Provider extends BuiltinProvider<PlatformInfo>
implements PlatformInfoApi.Provider<
ConstraintSettingInfo, ConstraintValueInfo, PlatformInfo> {
Expand Down Expand Up @@ -101,7 +113,10 @@ public PlatformInfo platformInfo(
private final Label label;
private final ConstraintCollection constraints;
private final String remoteExecutionProperties;

/** execProperties will deprecate and replace remoteExecutionProperties */
// TODO(blaze-configurability): If we want to remove remoteExecutionProperties, we need to fix
// PlatformUtils.getPlatformProto to use the dict-typed execProperties and do a migration.
private final ImmutableMap<String, String> execProperties;

private PlatformInfo(
Expand All @@ -117,18 +132,6 @@ private PlatformInfo(
this.execProperties = execProperties;
}

/** Empty {@link PlatformInfo} instance representing an invalid or empty platform. */
public static final PlatformInfo EMPTY_PLATFORM_INFO;

static {
try {
EMPTY_PLATFORM_INFO = PlatformInfo.builder().build();
} catch (DuplicateConstraintException | ExecPropertiesException e) {
// This can never happen since we're not passing any values to the builder.
throw new VerifyException(e);
}
}

@Override
public BuiltinProvider<PlatformInfo> getProvider() {
return PROVIDER;
Expand Down Expand Up @@ -159,11 +162,6 @@ public void repr(Printer printer) {
printer.append(String.format("PlatformInfo(%s, constraints=%s)", label, constraints));
}

/** Returns a new {@link Builder} for creating a fresh {@link PlatformInfo} instance. */
public static Builder builder() {
return new Builder();
}

/** Add this platform to the given fingerprint. */
public void addTo(Fingerprint fp) {
fp.addString(label.toString());
Expand All @@ -172,6 +170,28 @@ public void addTo(Fingerprint fp) {
constraints.addToFingerprint(fp);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof PlatformInfo)) {
return false;
}
PlatformInfo that = (PlatformInfo) o;
return Objects.equals(label, that.label)
&& Objects.equals(constraints, that.constraints)
&& Objects.equals(remoteExecutionProperties, that.remoteExecutionProperties)
&& Objects.equals(execProperties, that.execProperties);
}

@Override
public int hashCode() {
return Objects.hash(label, constraints, remoteExecutionProperties, execProperties);
}

/** Returns a new {@link Builder} for creating a fresh {@link PlatformInfo} instance. */
public static Builder builder() {
return new Builder();
}

/** Builder class to facilitate creating valid {@link PlatformInfo} instances. */
public static class Builder {

Expand Down Expand Up @@ -380,23 +400,6 @@ private static ImmutableMap<String, String> mergeExecProperties(
}
}

@Override
public boolean equals(Object o) {
if (!(o instanceof PlatformInfo)) {
return false;
}
PlatformInfo that = (PlatformInfo) o;
return Objects.equals(label, that.label)
&& Objects.equals(constraints, that.constraints)
&& Objects.equals(remoteExecutionProperties, that.remoteExecutionProperties)
&& Objects.equals(execProperties, that.execProperties);
}

@Override
public int hashCode() {
return Objects.hash(label, constraints, remoteExecutionProperties);
}

/** Exception that indicates something is wrong in exec_properties configuration. */
public static class ExecPropertiesException extends Exception {
ExecPropertiesException(String message) {
Expand Down
Loading

0 comments on commit d8d3593

Please sign in to comment.