Skip to content

Commit

Permalink
Replace xcode_version native rule with common Starlark Builtin.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 608721046
Change-Id: If1152928385a5fc0f9957b2413c4dc42e2d7f570
  • Loading branch information
susinmotion authored and copybara-github committed Feb 20, 2024
1 parent 28495ea commit cd2a09b
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.apple.AvailableXcodesRule;
import com.google.devtools.build.lib.rules.apple.XcodeVersionRule;
import com.google.devtools.build.lib.rules.apple.XcodeVersionProperties;
import com.google.devtools.build.lib.rules.apple.XcodeVersionRuleData;
import com.google.devtools.build.lib.rules.core.CoreRules;
import com.google.devtools.build.lib.rules.objc.AppleStarlarkCommon;
import com.google.devtools.build.lib.rules.objc.AppleToolchain;
Expand Down Expand Up @@ -57,7 +58,10 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
builder.addRuleDefinition(new XcodeConfigRule(BazelXcodeConfig.class));
builder.addRuleDefinition(new XcodeConfigAliasRule());
builder.addRuleDefinition(new AvailableXcodesRule());
builder.addRuleDefinition(new XcodeVersionRule());
builder.addRuleDefinition(new EmptyRule("xcode_version") {});

builder.addStarlarkBuiltinsInternal("XcodeProperties", XcodeVersionProperties.PROVIDER);
builder.addStarlarkBuiltinsInternal("XcodeVersionRuleData", XcodeVersionRuleData.PROVIDER);

builder.addStarlarkBuiltinsInternal("apple_common", new AppleStarlarkCommon());
builder.addStarlarkBootstrap(new AppleBootstrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ public ConfiguredTarget create(RuleContext ruleContext)

Iterable<XcodeVersionRuleData> availableVersions =
ruleContext.getPrerequisites(
AvailableXcodesRule.VERSIONS_ATTR_NAME,
XcodeVersionRuleData.class);
AvailableXcodesRule.VERSIONS_ATTR_NAME, XcodeVersionRuleData.PROVIDER);
XcodeVersionRuleData defaultVersion =
ruleContext.getPrerequisite(
AvailableXcodesRule.DEFAULT_ATTR_NAME,
XcodeVersionRuleData.class);
AvailableXcodesRule.DEFAULT_ATTR_NAME, XcodeVersionRuleData.PROVIDER);
AvailableXcodesInfo availableXcodes =
new AvailableXcodesInfo(availableVersions, defaultVersion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ java_library(
name = "apple",
srcs = glob(["*.java"]),
deps = [
"//src/main/java/com/google/devtools/build/docgen/annot",
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_options",
"//src/main/java/com/google/devtools/build/lib/analysis:config/core_option_converters",
Expand All @@ -26,15 +28,17 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:config/fragment_options",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/analysis:rule_definition_environment",
"//src/main/java/com/google/devtools/build/lib/analysis:transitive_info_provider",
"//src/main/java/com/google/devtools/build/lib/analysis/starlark/annotations",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//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",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/apple",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/core",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/annot",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/syntax",
"//third_party:auto_value",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
import com.google.devtools.build.lib.starlarkbuildapi.apple.XcodePropertiesApi;
import java.util.Objects;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.StarlarkThread;

/** A tuple containing information about a version of xcode and its properties. */
@Immutable
public class XcodeVersionProperties extends NativeInfo implements XcodePropertiesApi {
/** Starlark identifier for XcodeVersionProperties provider. */
public static final Provider PROVIDER = new Provider();

/** Starlark name for the XcodeVersionProperties provider. */
public static final String STARLARK_NAME = "XcodeProperties";

/** Starlark constructor and identifier for XcodeVersionProperties provider. */
public static final BuiltinProvider<XcodeVersionProperties> STARLARK_CONSTRUCTOR =
new BuiltinProvider<XcodeVersionProperties>(STARLARK_NAME, XcodeVersionProperties.class) {};

@VisibleForTesting public static final String DEFAULT_IOS_SDK_VERSION = "8.4";
@VisibleForTesting public static final String DEFAULT_VISIONOS_SDK_VERSION = "1.0";
@VisibleForTesting public static final String DEFAULT_WATCHOS_SDK_VERSION = "2.0";
Expand Down Expand Up @@ -103,8 +103,8 @@ public static XcodeVersionProperties unknownXcodeVersionProperties() {
}

@Override
public BuiltinProvider<XcodeVersionProperties> getProvider() {
return STARLARK_CONSTRUCTOR;
public Provider getProvider() {
return PROVIDER;
}

/** Returns the xcode version, or null if the xcode version is unknown. */
Expand Down Expand Up @@ -209,4 +209,31 @@ public int hashCode() {
defaultTvosSdkVersion,
defaultMacosSdkVersion);
}

/** Provider class for {@link XcodeVersionProperties} objects. */
public static class Provider extends BuiltinProvider<XcodeVersionProperties>
implements XcodePropertiesApi.Provider {
private Provider() {
super(XcodePropertiesApi.NAME, XcodeVersionProperties.class);
}

@Override
public XcodePropertiesApi createInfo(
String starlarkVersion,
String starlarkDefaultIosSdkVersion,
String starlarkDefaultVisionosSdkVersion,
String starlarkDefaultWatchosSdkVersion,
String starlarkDefaultTvosSdkVersion,
String starlarkDefaultMacosSdkVersion,
StarlarkThread thread)
throws EvalException {
return new XcodeVersionProperties(
DottedVersion.fromStringUnchecked(starlarkVersion),
starlarkDefaultIosSdkVersion,
starlarkDefaultVisionosSdkVersion,
starlarkDefaultWatchosSdkVersion,
starlarkDefaultTvosSdkVersion,
starlarkDefaultMacosSdkVersion);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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.rules.apple;

import com.google.devtools.build.docgen.annot.StarlarkConstructor;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import net.starlark.java.annot.Param;
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.NoneType;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.StarlarkThread;

/** Provider structure for {@code XcodeVersionRuleData} */
interface XcodeVersionProviderApi<FileT extends FileApi> extends ProviderApi {

@StarlarkMethod(
name = "XcodeVersionRuleData",
useStarlarkThread = true,
parameters = {
@Param(
name = "label",
positional = false,
named = true,
defaultValue = "None",
documented = false),
@Param(
name = "xcode_properties",
positional = false,
named = true,
defaultValue = "None",
documented = false),
@Param(
name = "aliases",
positional = false,
named = true,
defaultValue = "None",
allowedTypes = {@ParamType(type = NoneType.class), @ParamType(type = Sequence.class)},
documented = false),
},
selfCall = true,
documented = false)
@StarlarkConstructor
public XcodeVersionRuleData createInfo(
Object starlarkLabel,
Object starlarkXcodeProperties,
Object starlarkAliases,
StarlarkThread thread)
throws EvalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.devtools.build.lib.packages.Type.STRING_LIST;

import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.BaseRuleClasses.EmptyRuleConfiguredTargetFactory;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.packages.RuleClass;
Expand Down Expand Up @@ -101,7 +102,7 @@ public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("xcode_version")
.ancestors(BaseRuleClasses.NativeBuildRule.class)
.factoryClass(XcodeVersion.class)
.factoryClass(EmptyRuleConfiguredTargetFactory.class)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,47 @@
package com.google.devtools.build.lib.rules.apple;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.NonconfigurableAttributeMapper;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkThread;

/**
* A tuple containing the information in a single target of the {@code xcode_version} rule.
* A single target of this rule contains an official version label decided by Apple, a number
* of supported aliases one might use to reference this version, and various properties of
* the xcode version (such as default SDK versions).
* A tuple containing the information in a single target of the {@code xcode_version} rule. A single
* target of this rule contains an official version label decided by Apple, a number of supported
* aliases one might use to reference this version, and various properties of the xcode version
* (such as default SDK versions).
*
* <p>For example, one may want to reference official xcode version 7.0.1 using the "7" or
* "7.0" aliases. This official version of xcode may have a default supported iOS SDK of
* 9.0.
* <p>For example, one may want to reference official xcode version 7.0.1 using the "7" or "7.0"
* aliases. This official version of xcode may have a default supported iOS SDK of 9.0.
*/
@Immutable
public class XcodeVersionRuleData implements TransitiveInfoProvider {
public class XcodeVersionRuleData extends NativeInfo {
private static final String NAME = "XcodeVersionRuleData";

private final Label label;
private final DottedVersion version;
private final XcodeVersionProperties xcodeVersionProperties;
private final ImmutableList<String> aliases;
public static final Provider PROVIDER = new Provider();

XcodeVersionRuleData(Label label, Rule rule) {
NonconfigurableAttributeMapper attrMapper =
NonconfigurableAttributeMapper.of(rule);

XcodeVersionRuleData(
Label label, XcodeVersionProperties xcodeVersionProperties, List<String> aliases) {
this.label = label;
DottedVersion xcodeVersion =
DottedVersion.fromStringUnchecked(
attrMapper.get(XcodeVersionRule.VERSION_ATTR_NAME, Type.STRING));
String iosSdkVersionString =
attrMapper.get(XcodeVersionRule.DEFAULT_IOS_SDK_VERSION_ATTR_NAME, Type.STRING);
String visionosSdkVersionString =
attrMapper.get(XcodeVersionRule.DEFAULT_VISIONOS_SDK_VERSION_ATTR_NAME, Type.STRING);
String watchosSdkVersionString =
attrMapper.get(XcodeVersionRule.DEFAULT_WATCHOS_SDK_VERSION_ATTR_NAME, Type.STRING);
String tvosSdkVersionString =
attrMapper.get(XcodeVersionRule.DEFAULT_TVOS_SDK_VERSION_ATTR_NAME, Type.STRING);
String macosxSdkVersionString =
attrMapper.get(XcodeVersionRule.DEFAULT_MACOS_SDK_VERSION_ATTR_NAME, Type.STRING);
this.version = xcodeVersion;
this.xcodeVersionProperties =
new XcodeVersionProperties(
xcodeVersion,
iosSdkVersionString,
visionosSdkVersionString,
watchosSdkVersionString,
tvosSdkVersionString,
macosxSdkVersionString);
this.aliases = ImmutableList.copyOf(
attrMapper.get(XcodeVersionRule.ALIASES_ATTR_NAME, Type.STRING_LIST));
this.xcodeVersionProperties = xcodeVersionProperties;
this.aliases = ImmutableList.copyOf(aliases);
}

@Override
public Provider getProvider() {
return PROVIDER;
}

/**
Expand All @@ -84,7 +69,7 @@ public Label getLabel() {
* Returns the official xcode version the owning {@code xcode_version} target is referencing.
*/
public DottedVersion getVersion() {
return version;
return xcodeVersionProperties.getXcodeVersion().get();
}

/**
Expand All @@ -110,7 +95,7 @@ public boolean equals(Object other) {
return false;
}
XcodeVersionRuleData otherData = (XcodeVersionRuleData) other;
return (version.equals(otherData.getVersion())
return (getVersion().equals(otherData.getVersion())
&& xcodeVersionProperties
.getXcodeVersion()
.equals(otherData.getXcodeVersionProperties().getXcodeVersion())
Expand All @@ -133,6 +118,34 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(version, xcodeVersionProperties);
return Objects.hash(getVersion(), xcodeVersionProperties);
}

/** Provider class for {@link XcodeVersionRuleData} objects. */
public static class Provider extends BuiltinProvider<XcodeVersionRuleData>
implements XcodeVersionProviderApi<Artifact> {
private Provider() {
super(XcodeVersionRuleData.NAME, XcodeVersionRuleData.class);
}

@Override
@SuppressWarnings("unchecked")
public XcodeVersionRuleData createInfo(
Object starlarkLabel,
Object starlarkXcodeProperties,
Object starlarkAliases,
StarlarkThread thread)
throws EvalException {
Sequence<String> aliases = nullIfNone(starlarkAliases, Sequence.class);
return new XcodeVersionRuleData(
nullIfNone(starlarkLabel, Label.class),
nullIfNone(starlarkXcodeProperties, XcodeVersionProperties.class),
Sequence.cast(aliases, String.class, "aliases"));
}

@Nullable
private static <T> T nullIfNone(Object object, Class<T> type) {
return object != Starlark.NONE ? type.cast(object) : null;
}
}
}
Loading

0 comments on commit cd2a09b

Please sign in to comment.