From c5442570ecba9a540a552825c8aed7ac9c62c321 Mon Sep 17 00:00:00 2001 From: bbaren Date: Wed, 15 Aug 2018 13:58:00 -0700 Subject: Always use exactly two components in Xcode version feature When setting the standard Xcode version feature (see commit eb952d08f76afa907bb34eaf7e1a69899102c523), always use exactly two components for the version number. Grouping all point releases together simplifies writing CROSSTOOLs. Also fix a bug in MockObjcSupport that prevented Xcode version selection in tests from working properly. RELNOTES: The standard `xcode_VERSION` feature now always uses exactly two components in the version, even if you specify `--xcode_version` with more or fewer than two. PiperOrigin-RevId: 208877588 --- .../build/lib/rules/apple/DottedVersion.java | 35 +++++++++++++++++----- .../build/lib/rules/objc/CompilationSupport.java | 5 +++- 2 files changed, 31 insertions(+), 9 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java b/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java index 9919ff757d..e063f3e085 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java +++ b/src/main/java/com/google/devtools/build/lib/rules/apple/DottedVersion.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.rules.apple; import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.ComparisonChain; @@ -168,6 +169,31 @@ public final class DottedVersion implements DottedVersionApi { return compareTo(other); } + /** + * Returns the string representation of this dotted version, padded or truncated to the specified + * number of components. + * + *

For example, a dotted version of "7.3.0" will return "7" if one is requested, "7.3" if two + * are requested, "7.3.0" if three are requested, and "7.3.0.0" if four are requested. + * + * @param numComponents a positive number of dot-separated numbers that should be present in the + * returned string representation + */ + public String toStringWithComponents(int numComponents) { + Preconditions.checkArgument(numComponents > 0, + "Can't serialize as a version with %s components", numComponents); + ImmutableList.Builder stringComponents = ImmutableList.builder(); + if (numComponents <= components.size()) { + stringComponents.addAll(components.subList(0, numComponents)); + } else { + stringComponents.addAll(components); + for (int i = components.size(); i < numComponents; i++) { + stringComponents.add(ZERO_COMPONENT); + } + } + return Joiner.on('.').join(stringComponents.build()); + } + /** * Returns the string representation of this dotted version, padded to a minimum number of * components if the string representation does not already contain that many components. @@ -183,14 +209,7 @@ public final class DottedVersion implements DottedVersionApi { * the returned string representation */ public String toStringWithMinimumComponents(int numMinComponents) { - ImmutableList.Builder stringComponents = ImmutableList.builder(); - stringComponents.addAll(components); - int numComponents = Math.max(this.numOriginalComponents, numMinComponents); - int zeroesToPad = numComponents - components.size(); - for (int i = 0; i < zeroesToPad; i++) { - stringComponents.add(ZERO_COMPONENT); - } - return Joiner.on('.').join(stringComponents.build()); + return toStringWithComponents(Math.max(this.numOriginalComponents, numMinComponents)); } /** diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java index f7e7a4a401..40938048a1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/CompilationSupport.java @@ -553,8 +553,11 @@ public class CompilationSupport { // next release. activatedCrosstoolSelectables.add(NO_DSYM_ZIPS_FEATURE_NAME); + // Add a feature identifying the Xcode version so CROSSTOOL authors can enable flags for + // particular versions of Xcode. To ensure consistency across platforms, use exactly two + // components in the version number. activatedCrosstoolSelectables.add(XCODE_VERSION_FEATURE_NAME_PREFIX - + XcodeConfig.getXcodeVersion(ruleContext).toStringWithMinimumComponents(2)); + + XcodeConfig.getXcodeVersion(ruleContext).toStringWithComponents(2)); activatedCrosstoolSelectables.addAll(ruleContext.getFeatures()); -- cgit v1.2.3