Skip to content

Commit

Permalink
ios_device attribute "xcode", which takes an "xcode_version" target t…
Browse files Browse the repository at this point in the history
…o select a specific xcode.

RELNOTES: ios_device attribute "xcode", for declaring a specific xcode to use when selecting iOS simulators.

--
MOS_MIGRATED_REVID=119317344
  • Loading branch information
c-parsons authored and kchodorow committed Apr 8, 2016
1 parent d985242 commit 3ab018d
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
@SkylarkModule(name = "apple", doc = "A configuration fragment for Apple platforms")
@Immutable
public class AppleConfiguration extends BuildConfiguration.Fragment {
/**
* Environment variable name for the xcode version. The value of this environment variable should
* be set to the version (for example, "7.2") of xcode to use when invoking part of the apple
* toolkit in action execution.
**/
public static final String XCODE_VERSION_ENV_NAME = "XCODE_VERSION_OVERRIDE";
/**
* Environment variable name for the apple SDK version. If unset, uses the system default of the
Expand Down Expand Up @@ -158,11 +163,20 @@ public Map<String, String> getTargetAppleEnvironment(Platform platform) {
)
public Map<String, String> getAppleHostSystemEnv() {
Optional<DottedVersion> xcodeVersion = getXcodeVersion();
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
if (xcodeVersion.isPresent()) {
builder.put(AppleConfiguration.XCODE_VERSION_ENV_NAME, xcodeVersion.get().toString());
return getXcodeVersionEnv(xcodeVersion.get());
} else {
return ImmutableMap.of();
}
return builder.build();
}

/**
* Returns a map of environment variables that should be propagated for actions that require
* a version of xcode to be explicitly declared. Keys are variable names and values are their
* corresponding values.
*/
public Map<String, String> getXcodeVersionEnv(DottedVersion xcodeVersion) {
return ImmutableMap.of(AppleConfiguration.XCODE_VERSION_ENV_NAME, xcodeVersion.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ public class XcodeVersion implements RuleConfiguredTargetFactory {

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException {
XcodeVersionRuleData ruleData =
new XcodeVersionRuleData(ruleContext.getLabel(), ruleContext.getRule());

return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY)
.addProvider(XcodeVersionProperties.class, ruleData.getXcodeVersionProperties())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.devtools.build.lib.analysis.TransitiveInfoProvider;

import javax.annotation.Nullable;

/**
* A tuple containing information about a version of xcode and its properties.
*/
public class XcodeVersionProperties {
public class XcodeVersionProperties implements TransitiveInfoProvider {
@VisibleForTesting public static final String DEFAULT_IOS_SDK_VERSION = "8.4";
@VisibleForTesting public static final String DEFAULT_WATCHOS_SDK_VERSION = "2.0";
@VisibleForTesting public static final String DEFAULT_MACOSX_SDK_VERSION = "10.10";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,50 @@

import com.google.common.base.Strings;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode;
import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.apple.DottedVersion;
import com.google.devtools.build.lib.rules.apple.Platform;
import com.google.devtools.build.lib.rules.apple.XcodeVersionProperties;

/**
* Implementation for the "ios_device" rule.
*/
public final class IosDevice implements RuleConfiguredTargetFactory {
@Override
public ConfiguredTarget create(RuleContext context) throws InterruptedException {
String iosVersionAttribute = context.attributes().get("ios_version", STRING);
AppleConfiguration appleConfiguration = context.getFragment(AppleConfiguration.class);
String iosVersionAttribute =
context.attributes().get(IosDeviceRule.IOS_VERSION_ATTR_NAME, STRING);
XcodeVersionProperties xcodeVersionProperties = context.getPrerequisite(
IosDeviceRule.XCODE_ATTR_NAME, Mode.TARGET, XcodeVersionProperties.class);

DottedVersion xcodeVersion = null;
if (xcodeVersionProperties != null && xcodeVersionProperties.getXcodeVersion().isPresent()) {
xcodeVersion = xcodeVersionProperties.getXcodeVersion().get();
} else if (appleConfiguration.getXcodeVersion().isPresent()) {
xcodeVersion = appleConfiguration.getXcodeVersion().get();
}

DottedVersion iosVersion;
if (!Strings.isNullOrEmpty(iosVersionAttribute)) {
iosVersion = DottedVersion.fromString(iosVersionAttribute);
} else if (xcodeVersionProperties != null) {
iosVersion = xcodeVersionProperties.getDefaultIosSdkVersion();
} else {
iosVersion = context.getFragment(AppleConfiguration.class)
.getSdkVersionForPlatform(Platform.IOS_SIMULATOR);
iosVersion = appleConfiguration.getSdkVersionForPlatform(Platform.IOS_SIMULATOR);
}

IosDeviceProvider provider =
new IosDeviceProvider.Builder()
.setType(context.attributes().get("type", STRING))
.setType(context.attributes().get(IosDeviceRule.TYPE_ATTR_NAME, STRING))
.setIosVersion(iosVersion)
.setLocale(context.attributes().get("locale", STRING))
.setLocale(context.attributes().get(IosDeviceRule.LOCALE_ATTR_NAME, STRING))
.setXcodeVersion(xcodeVersion)
.build();

return new RuleConfiguredTargetBuilder(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.devtools.build.lib.rules.apple.DottedVersion;
import com.google.devtools.build.lib.util.Preconditions;

import javax.annotation.Nullable;

/**
* Provider that describes a simulator device.
*/
Expand All @@ -31,17 +33,35 @@ public static final class Builder {
private String type;
private DottedVersion iosVersion;
private String locale;
@Nullable
private DottedVersion xcodeVersion;

/**
* Sets the hardware type of the device, corresponding to the {@code simctl} device type.
*/
public Builder setType(String type) {
this.type = type;
return this;
}

/**
* Sets the iOS version of the simulator to use. This may be different than the iOS sdk version
* used to build the application.
*/
public Builder setIosVersion(DottedVersion iosVersion) {
this.iosVersion = iosVersion;
return this;
}

/**
* Sets the xcode version to obtain the iOS simulator from. This may be different than the
* xcode version with which the application was built.
*/
public Builder setXcodeVersion(@Nullable DottedVersion xcodeVersion) {
this.xcodeVersion = xcodeVersion;
return this;
}

public Builder setLocale(String locale) {
this.locale = locale;
return this;
Expand All @@ -54,12 +74,14 @@ public IosDeviceProvider build() {

private final String type;
private final DottedVersion iosVersion;
private final DottedVersion xcodeVersion;
private final String locale;

private IosDeviceProvider(Builder builder) {
this.type = Preconditions.checkNotNull(builder.type);
this.iosVersion = Preconditions.checkNotNull(builder.iosVersion);
this.locale = Preconditions.checkNotNull(builder.locale);
this.xcodeVersion = builder.xcodeVersion;
}

public String getType() {
Expand All @@ -70,6 +92,11 @@ public DottedVersion getIosVersion() {
return iosVersion;
}

@Nullable
public DottedVersion getXcodeVersion() {
return xcodeVersion;
}

public String getLocale() {
return locale;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.devtools.build.lib.rules.objc;

import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
import static com.google.devtools.build.lib.syntax.Type.STRING;

import com.google.devtools.build.lib.analysis.BaseRuleClasses;
Expand All @@ -28,22 +29,36 @@
* Rule definition for ios_device.
*/
public final class IosDeviceRule implements RuleDefinition {
static final String IOS_VERSION_ATTR_NAME = "ios_version";
static final String XCODE_ATTR_NAME = "xcode";
static final String TYPE_ATTR_NAME = "type";
static final String LOCALE_ATTR_NAME = "locale";

@Override
public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
return builder
.requiresConfigurationFragments(AppleConfiguration.class)
/* <!-- #BLAZE_RULE(ios_device).ATTRIBUTE(ios_version) -->
The operating system version of the device. This corresponds to the
<code>simctl</code> runtime. Defaults to the ios sdk version configuration value.
<code>simctl</code> runtime. Defaults to the default ios sdk version for the xcode
version defined in the <code>xcode</code> attribute, or uses the ios sdk version
configuration value, if <code>xcode</code> is unspecified.
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
.add(attr(IOS_VERSION_ATTR_NAME, STRING))
/* <!-- #BLAZE_RULE(ios_device).ATTRIBUTE(xcode) -->
The version of xcode to use to run the simulator. Defaults to the xcode version build
configuration value.
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
.add(attr("ios_version", STRING))
.add(attr(XCODE_ATTR_NAME, LABEL)
.allowedRuleClasses("xcode_version")
.allowedFileTypes())
/* <!-- #BLAZE_RULE(ios_device).ATTRIBUTE(type) -->
The hardware type. This corresponds to the <code>simctl</code> device
type.
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
.add(attr("type", STRING)
.add(attr(TYPE_ATTR_NAME, STRING)
.mandatory())
.add(attr("locale", STRING)
.add(attr(LOCALE_ATTR_NAME, STRING)
.undocumented("this is not yet supported by any test runner")
.value("en"))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.rules.apple.AppleConfiguration;
import com.google.devtools.build.lib.rules.apple.DottedVersion;
import com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider;
import com.google.devtools.build.lib.rules.test.TestEnvironmentProvider;
import com.google.devtools.build.lib.syntax.Type;
Expand Down Expand Up @@ -251,12 +252,16 @@ public TestSupport addRunfiles(
* builder.
*/
public Map<Class<? extends TransitiveInfoProvider>, TransitiveInfoProvider> getExtraProviders() {
IosDeviceProvider deviceProvider =
ruleContext.getPrerequisite(IosTest.TARGET_DEVICE, Mode.TARGET, IosDeviceProvider.class);
DottedVersion xcodeVersion = deviceProvider.getXcodeVersion();
AppleConfiguration configuration = ruleContext.getFragment(AppleConfiguration.class);

ImmutableMap.Builder<String, String> envBuilder = ImmutableMap.builder();

envBuilder.putAll(configuration.getTargetAppleEnvironment(configuration.getIosCpuPlatform()));
envBuilder.putAll(configuration.getAppleHostSystemEnv());
if (xcodeVersion != null) {
envBuilder.putAll(configuration.getXcodeVersionEnv(xcodeVersion));
}

if (ruleContext.getConfiguration().isCodeCoverageEnabled()) {
envBuilder.put("COVERAGE_GCOV_PATH",
Expand Down

0 comments on commit 3ab018d

Please sign in to comment.