diff options
author | 2017-10-16 11:25:18 +0200 | |
---|---|---|
committer | 2017-10-16 17:48:57 +0200 | |
commit | 22670a4e5abb879ebd5e41294f6aa0960346f4cf (patch) | |
tree | dc27a2a43eec21fea912a57d1a1d6ba0783a51ae /src/main/java/com/google/devtools/build/lib/rules/apple | |
parent | 95e41675f94bff0dcec027b5516ae07a045dfbfa (diff) |
Export apple_host_system_env and target_apple_env through apple_common (as opposed to only through ctx.fragments.apple)
Progress towards #3424.
RELNOTES: None.
PiperOrigin-RevId: 172299240
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/apple')
4 files changed, 81 insertions, 60 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java index 752bf64e48..cc77b96018 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/apple/AppleConfiguration.java @@ -233,62 +233,74 @@ public class AppleConfiguration extends BuildConfiguration.Fragment { * Returns a map of environment variables (derived from configuration) that should be propagated * for actions pertaining to the given apple platform. Keys are variable names and values are * their corresponding values. + * + * @deprecated use {@link #appleTargetPlatformEnv(ApplePlatform, DottedVersion) instead)}. */ + @Deprecated @SkylarkCallable( - name = "target_apple_env", - doc = "Returns a <code>dict</code> of environment variables that should be set for actions " - + "that build targets of the given Apple platform type. For example, this dictionary " - + "contains variables that denote the platform name and SDK version with which to " - + "build. The keys are variable names and the values are their corresponding values.") + name = "target_apple_env", + doc = + "Returns a <code>dict</code> of environment variables that should be set for actions " + + "that build targets of the given Apple platform type. For example, this dictionary " + + "contains variables that denote the platform name and SDK version with which to " + + "build. The keys are variable names and the values are their corresponding values." + ) + // Bug tracking the removal of this method: https://github.com/bazelbuild/bazel/issues/3424 public ImmutableMap<String, String> getTargetAppleEnvironment(ApplePlatform platform) { - ImmutableMap.Builder<String, String> mapBuilder = ImmutableMap.builder(); - mapBuilder.putAll(appleTargetPlatformEnv(platform)); - return mapBuilder.build(); + return appleTargetPlatformEnv(platform, getSdkVersionForPlatform(platform)); + } + + /** + * Returns a map of environment variables (derived from configuration) that should be propagated + * for actions pertaining to building applications for apple platforms. These environment + * variables are needed to use apple toolkits. Keys are variable names and values are their + * corresponding values. + */ + public static ImmutableMap <String, String> appleTargetPlatformEnv( + ApplePlatform platform, DottedVersion sdkVersion) { + ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); + + builder + .put(AppleConfiguration.APPLE_SDK_VERSION_ENV_NAME, + sdkVersion.toStringWithMinimumComponents(2)) + .put(AppleConfiguration.APPLE_SDK_PLATFORM_ENV_NAME, + platform.getNameInPlist()); + + return builder.build(); } /** * Returns a map of environment variables that should be propagated for actions that build on an * apple host system. These environment variables are needed by the apple toolchain. Keys are * variable names and values are their corresponding values. + * + * @deprecated use {@link #getXcodeVersionEnv(DottedVersion)} instead}. */ @SkylarkCallable( - name = "apple_host_system_env", - doc = "Returns a <a href='dict.html'>dict</a> of environment variables that should be set " - + "for actions that need to run build tools on an Apple host system, such as the version " - + "of Xcode that should be used. The keys are variable names and the values are their " - + "corresponding values.") + name = "apple_host_system_env", + doc = + "Returns a <a href='dict.html'>dict</a> of environment variables that should be set " + + "for actions that need to run build tools on an Apple host system, such as the " + + "version of Xcode that should be used. The keys are variable names and the values " + + "are their corresponding values." + ) + @Deprecated + // Bug tracking the removal of this method: https://github.com/bazelbuild/bazel/issues/3424 public ImmutableMap<String, String> getAppleHostSystemEnv() { - if (xcodeVersion != null) { - return getXcodeVersionEnv(xcodeVersion); - } else { - return ImmutableMap.of(); - } + return getXcodeVersionEnv(xcodeVersion); } /** - * 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 ImmutableMap<String, String> getXcodeVersionEnv(DottedVersion xcodeVersion) { - return ImmutableMap.of(AppleConfiguration.XCODE_VERSION_ENV_NAME, xcodeVersion.toString()); - } - - /** - * Returns a map of environment variables (derived from configuration) that should be propagated - * for actions pertaining to building applications for apple platforms. These environment - * variables are needed to use apple toolkits. Keys are variable names and values are their + * 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> appleTargetPlatformEnv(ApplePlatform platform) { - ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); - - String sdkVersion = getSdkVersionForPlatform(platform).toStringWithMinimumComponents(2); - builder - .put(AppleConfiguration.APPLE_SDK_VERSION_ENV_NAME, sdkVersion) - .put(AppleConfiguration.APPLE_SDK_PLATFORM_ENV_NAME, platform.getNameInPlist()); - - return builder.build(); + public static ImmutableMap<String, String> getXcodeVersionEnv(DottedVersion xcodeVersion) { + if (xcodeVersion != null) { + return ImmutableMap.of(AppleConfiguration.XCODE_VERSION_ENV_NAME, xcodeVersion.toString()); + } else { + return ImmutableMap.of(); + } } /** diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java b/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java index 9749a9eb80..7444c45bc0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java +++ b/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfig.java @@ -45,6 +45,7 @@ import javax.annotation.Nullable; * Implementation for the {@code xcode_config} rule. */ public class XcodeConfig implements RuleConfiguredTargetFactory { + private static ImmutableList<XcodeVersionRuleData> getAvailableVersions( ConfigurationEnvironment env, Rule xcodeConfigTarget) throws InvalidConfigurationException, InterruptedException { @@ -92,6 +93,7 @@ public class XcodeConfig implements RuleConfiguredTargetFactory { * An exception that signals that an Xcode config setup was invalid. */ public static class XcodeConfigException extends Exception { + XcodeConfigException(String reason) { super(reason); } @@ -149,18 +151,17 @@ public class XcodeConfig implements RuleConfiguredTargetFactory { .addNativeDeclaredProvider(xcodeVersionProperties) .build(); } - + /** * Uses the {@link AppleCommandLineOptions#xcodeVersion} and {@link * AppleCommandLineOptions#xcodeVersionConfig} command line options to determine and return the * effective xcode version and its properties. * - * @param requireDefinedVersions whether the version config requires an explicitly defined version * @param xcodeVersionOverrideFlag the value of the {@code --xcode_version} command line flag * @param xcodeVersions the Xcode versions listed in the {@code xcode_config} rule * @param defaultVersion the default Xcode version in the {@code xcode_config} rule. - * @throws XcodeConfigException if the options given (or configuration targets) were - * malformed and thus the xcode version could not be determined + * @throws XcodeConfigException if the options given (or configuration targets) were malformed and + * thus the xcode version could not be determined */ static XcodeVersionProperties resolveXcodeVersion( String xcodeVersionOverrideFlag, @@ -168,9 +169,10 @@ public class XcodeConfig implements RuleConfiguredTargetFactory { XcodeVersionRuleData defaultVersion) throws XcodeConfigException { if (defaultVersion != null - && Iterables.isEmpty(Iterables.filter( - xcodeVersions, - ruleData -> ruleData.getLabel().equals(defaultVersion.getLabel())))) { + && Iterables.isEmpty( + Iterables.filter( + xcodeVersions, + ruleData -> ruleData.getLabel().equals(defaultVersion.getLabel())))) { throw new XcodeConfigException( String.format("default label '%s' must be contained in versions attribute", defaultVersion.getLabel())); @@ -246,12 +248,11 @@ public class XcodeConfig implements RuleConfiguredTargetFactory { * Returns a map where keys are "names" of xcode versions as defined by the configuration target, * and values are the rule data objects which contain information regarding that xcode version. * - * @throws XcodeConfigException if there are duplicate aliases (if two xcode versions - * were registered to the same alias) + * @throws XcodeConfigException if there are duplicate aliases (if two xcode versions were + * registered to the same alias) */ private static Map<String, XcodeVersionRuleData> aliasesToVersionMap( - Iterable<XcodeVersionRuleData> xcodeVersionRules) - throws XcodeConfigException { + Iterable<XcodeVersionRuleData> xcodeVersionRules) throws XcodeConfigException { Map<String, XcodeVersionRuleData> aliasesToXcodeRules = Maps.newLinkedHashMap(); for (XcodeVersionRuleData xcodeVersionRule : xcodeVersionRules) { for (String alias : xcodeVersionRule.getAliases()) { @@ -271,13 +272,13 @@ public class XcodeConfig implements RuleConfiguredTargetFactory { } return aliasesToXcodeRules; } - + /** - * Convenience method for throwing an {@link XcodeConfigException} due to presence - * of duplicate aliases in an {@code xcode_config} target definition. + * Convenience method for throwing an {@link XcodeConfigException} due to presence of duplicate + * aliases in an {@code xcode_config} target definition. */ - private static void configErrorDuplicateAlias(String alias, - Iterable<XcodeVersionRuleData> xcodeVersionRules) throws XcodeConfigException { + private static void configErrorDuplicateAlias( + String alias, Iterable<XcodeVersionRuleData> xcodeVersionRules) throws XcodeConfigException { ImmutableList.Builder<Label> labelsContainingAlias = ImmutableList.builder(); for (XcodeVersionRuleData xcodeVersionRule : xcodeVersionRules) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfigProvider.java b/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfigProvider.java index d711bd4506..913e367cd3 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfigProvider.java +++ b/src/main/java/com/google/devtools/build/lib/rules/apple/XcodeConfigProvider.java @@ -15,6 +15,8 @@ package com.google.devtools.build.lib.rules.apple; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.analysis.RuleContext; +import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; import com.google.devtools.build.lib.packages.NativeInfo; import com.google.devtools.build.lib.packages.NativeProvider; @@ -115,4 +117,9 @@ public class XcodeConfigProvider extends NativeInfo { throw new IllegalArgumentException("Unhandled platform: " + platform); } } + + public static XcodeConfigProvider fromRuleContext(RuleContext ruleContext) { + return ruleContext.getPrerequisite( + XcodeConfigRule.XCODE_CONFIG_ATTR_NAME, Mode.TARGET, XcodeConfigProvider.PROVIDER); + } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/apple/cpp/AppleCcToolchain.java b/src/main/java/com/google/devtools/build/lib/rules/apple/cpp/AppleCcToolchain.java index 5c78577ead..a2c6120e45 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/apple/cpp/AppleCcToolchain.java +++ b/src/main/java/com/google/devtools/build/lib/rules/apple/cpp/AppleCcToolchain.java @@ -25,6 +25,7 @@ import com.google.devtools.build.lib.rules.apple.AppleConfiguration; import com.google.devtools.build.lib.rules.apple.ApplePlatform; import com.google.devtools.build.lib.rules.apple.AppleToolchain; import com.google.devtools.build.lib.rules.apple.XcodeConfig; +import com.google.devtools.build.lib.rules.apple.XcodeConfigProvider; import com.google.devtools.build.lib.rules.cpp.CcToolchain; import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.Variables; import com.google.devtools.build.lib.rules.cpp.CppConfiguration; @@ -120,12 +121,12 @@ public class AppleCcToolchain extends CcToolchain { private ImmutableMap<String, String> getEnvironmentBuildVariables(RuleContext ruleContext) { Map<String, String> builder = new LinkedHashMap<>(); CppConfiguration cppConfiguration = ruleContext.getFragment(CppConfiguration.class); - AppleConfiguration appleConfiguration = ruleContext.getFragment(AppleConfiguration.class); - builder.putAll(appleConfiguration.getAppleHostSystemEnv()); + XcodeConfigProvider xcodeConfig = XcodeConfigProvider.fromRuleContext(ruleContext); + builder.putAll(AppleConfiguration.getXcodeVersionEnv(xcodeConfig.getXcodeVersion())); if (ApplePlatform.isApplePlatform(cppConfiguration.getTargetCpu())) { - builder.putAll( - appleConfiguration.appleTargetPlatformEnv( - ApplePlatform.forTargetCpu(cppConfiguration.getTargetCpu()))); + ApplePlatform platform = ApplePlatform.forTargetCpu(cppConfiguration.getTargetCpu()); + builder.putAll(AppleConfiguration.appleTargetPlatformEnv( + platform, xcodeConfig.getSdkVersionForPlatform(platform))); } return ImmutableMap.copyOf(builder); } |