Skip to content

Commit

Permalink
Make the "Xcode unavailable" error message product-specific.
Browse files Browse the repository at this point in the history
RELNOTES: None.
PiperOrigin-RevId: 580426915
Change-Id: If89c286f5626c1acbce6726198de0110b6b9b746
  • Loading branch information
lberki authored and copybara-github committed Nov 8, 2023
1 parent 6a08b2f commit b6f55bb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.devtools.build.lib.rules.objc.AppleCcToolchainRule;
import com.google.devtools.build.lib.rules.objc.AppleStarlarkCommon;
import com.google.devtools.build.lib.rules.objc.AppleToolchain;
import com.google.devtools.build.lib.rules.objc.BazelXcodeConfig;
import com.google.devtools.build.lib.rules.objc.J2ObjcConfiguration;
import com.google.devtools.build.lib.rules.objc.ObjcConfiguration;
import com.google.devtools.build.lib.rules.objc.ObjcImportBaseRule;
Expand Down Expand Up @@ -69,7 +70,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
builder.addRuleDefinition(new ObjcRuleClasses.SdkFrameworksDependerRule());
builder.addRuleDefinition(new ObjcRuleClasses.CompileDependencyRule());
builder.addRuleDefinition(new ObjcRuleClasses.CrosstoolRule());
builder.addRuleDefinition(new XcodeConfigRule());
builder.addRuleDefinition(new XcodeConfigRule(BazelXcodeConfig.class));
builder.addRuleDefinition(new XcodeConfigAliasRule());
builder.addRuleDefinition(new AvailableXcodesRule());
builder.addRuleDefinition(new XcodeVersionRule());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2023 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.objc;

/** An {@code xcode_config} rule with the default "Xcode unavailable" error message. */
public class BazelXcodeConfig extends XcodeConfig {
private static final String UNAVAILABLE_XCODE_COMMAND = "bazel sync --configure";

public BazelXcodeConfig() {
super(UNAVAILABLE_XCODE_COMMAND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public static class XcodeConfigException extends Exception {
}
}

private final String unavailableXcodeCommand;

protected XcodeConfig(String unavailableXcodeCommand) {
this.unavailableXcodeCommand = unavailableXcodeCommand;
}

@Override
@Nullable
public ConfiguredTarget create(RuleContext ruleContext)
Expand Down Expand Up @@ -290,7 +296,7 @@ private static XcodeVersionProperties resolveExplicitlyDefinedVersion(
* If {@code --xcode_version} is unspecified, then this will return the newest mutually available
* version if possibls, otherwise the default local version.
*/
private static Map.Entry<XcodeVersionRuleData, Availability> resolveXcodeFromLocalAndRemote(
private Map.Entry<XcodeVersionRuleData, Availability> resolveXcodeFromLocalAndRemote(
AvailableXcodesInfo localVersions,
AvailableXcodesInfo remoteVersions,
RuleContext ruleContext,
Expand Down Expand Up @@ -397,22 +403,22 @@ private static Map.Entry<XcodeVersionRuleData, Availability> resolveXcodeFromLoc
String.format(
"--xcode_version=%1$s specified, but it is not available locally. Your build will"
+ " fail if any actions require a local Xcode. If you believe you have '%1$s'"
+ " installed, try running \"sudo python3 /usr/local/bin/xcode_configure.py"
+ " --verbose\", and then re-run your command. Locally available versions:"
+ " [%2$s]. Remotely available versions: [%3$s]",
+ " installed, try running \"%2$s\", and then re-run your command. Locally"
+ " available versions: [%3$s]. Remotely available versions: [%4$s]",
versionOverrideFlag,
unavailableXcodeCommand,
printableXcodeVersions(localVersions.getAvailableVersions()),
printableXcodeVersions(remoteVersions.getAvailableVersions())));
return Maps.immutableEntry(specifiedVersionFromRemote, Availability.REMOTE);
} else { // if (specifiedVersionFromRemote == null && specifiedVersionFromLocal == null)
ruleContext.throwWithRuleError(
String.format(
"--xcode_version=%1$s specified, but '%1$s' is not an available Xcode version."
+ " Locally available versions: [%2$s]. Remotely available versions: [%3$s]. If"
+ " you believe you have '%1$s' installed, try running \"sudo python3"
+ " /usr/local/bin/xcode_configure.py --verbose\", and then re-run your"
+ " command.",
+ " Locally available versions: [%3$s]. Remotely available versions: [%4$s]. If"
+ " you believe you have '%1$s' installed, try running \"%2$s\", and then"
+ " re-run your command.",
versionOverrideFlag,
unavailableXcodeCommand,
printableXcodeVersions(localVersions.getAvailableVersions()),
printableXcodeVersions(remoteVersions.getAvailableVersions())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public class XcodeConfigRule implements RuleDefinition {
static final String REMOTE_VERSIONS_ATTR_NAME = "remote_versions";
static final String LOCAL_VERSIONS_ATTR_NAME = "local_versions";

private final Class<? extends XcodeConfig> xcodeConfigFactory;

public XcodeConfigRule(Class<? extends XcodeConfig> xcodeConfigFactory) {
this.xcodeConfigFactory = xcodeConfigFactory;
}

@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
return builder
Expand Down Expand Up @@ -93,7 +99,7 @@ public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("xcode_config")
.ancestors(BaseRuleClasses.NativeBuildRule.class)
.factoryClass(XcodeConfig.class)
.factoryClass(xcodeConfigFactory)
.build();
}
}
Expand Down

0 comments on commit b6f55bb

Please sign in to comment.