aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/apple/AppleHostInfo.java
blob: dd0023a23c89b8099957a3b2a1217cd10c04874f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2015 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.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.UserExecException;
import com.google.devtools.build.lib.analysis.BlazeDirectories;
import com.google.devtools.build.lib.shell.AbnormalTerminationException;
import com.google.devtools.build.lib.shell.Command;
import com.google.devtools.build.lib.shell.CommandException;
import com.google.devtools.build.lib.shell.CommandResult;
import com.google.devtools.build.lib.shell.TerminationStatus;
import com.google.devtools.build.lib.vfs.Path;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * Obtains information pertaining to Apple host machines required for using Apple toolkits in
 * local action execution.
 */
public class AppleHostInfo {

  private static final String XCRUN_CACHE_FILENAME = "__xcruncache";
  private static final String XCODE_LOCATOR_CACHE_FILENAME = "__xcodelocatorcache";

  /**
   * Returns the absolute root path of the target Apple SDK on the host system for a given
   * version of xcode (as defined by the given {@code developerDir}). This may spawn a
   * process and use the {@code /usr/bin/xcrun} binary to locate the target SDK. This uses a local
   * cache file under {@code bazel-out}, and will only spawn a new {@code xcrun} process in the case
   * of a cache miss.
   *
   * @param execRoot the execution root path, used to locate the cache file
   * @param developerDir the value of {@code DEVELOPER_DIR} for the target version of xcode
   * @param sdkVersion the sdk version, for example, "9.1"
   * @param appleSdkPlatform the sdk platform, for example, "iPhoneOS"
   * @param productName the product name
   * @throws UserExecException if there is an issue with obtaining the root from the spawned
   *     process, either because the SDK platform/version pair doesn't exist, or there was an
   *     unexpected issue finding or running the tool
   */
  public static String getSdkRoot(Path execRoot, String developerDir,
      String sdkVersion, String appleSdkPlatform, String productName) throws UserExecException {
    try {
      CacheManager cacheManager =
          new CacheManager(execRoot.getRelative(
              BlazeDirectories.getRelativeOutputPath(productName)),
              XCRUN_CACHE_FILENAME);

      String sdkString = appleSdkPlatform.toLowerCase() + sdkVersion;
      String cacheResult = cacheManager.getValue(developerDir, sdkString);
      if (cacheResult != null) {
        return cacheResult;
      } else {
        Map<String, String> env = Strings.isNullOrEmpty(developerDir)
            ? ImmutableMap.<String, String>of() : ImmutableMap.of("DEVELOPER_DIR", developerDir);
        CommandResult xcrunResult = new Command(
            new String[] {"/usr/bin/xcrun", "--sdk", sdkString, "--show-sdk-path"},
            env, null).execute();

        // calling xcrun via Command returns a value with a newline on the end.
        String sdkRoot = new String(xcrunResult.getStdout(), StandardCharsets.UTF_8).trim();

        cacheManager.writeEntry(ImmutableList.of(developerDir, sdkString), sdkRoot);
        return sdkRoot;
      }
    } catch (AbnormalTerminationException e) {
      TerminationStatus terminationStatus = e.getResult().getTerminationStatus();

      if (terminationStatus.exited()) {
        throw new UserExecException(
            String.format("xcrun failed with code %s.\n"
                + "This most likely indicates that SDK version [%s] for platform [%s] is "
                + "unsupported for the target version of xcode.\n"
                + "%s\n"
                + "Stderr: %s",
                terminationStatus.getExitCode(),
                sdkVersion, appleSdkPlatform,
                terminationStatus.toString(),
                new String(e.getResult().getStderr(), StandardCharsets.UTF_8)));
      }
      String message = String.format("xcrun failed.\n%s\n%s",
          e.getResult().getTerminationStatus(),
          new String(e.getResult().getStderr(), StandardCharsets.UTF_8));
      throw new UserExecException(message, e);
    } catch (CommandException | IOException e) {
      throw new UserExecException(e);
    }
  }

  /**
   * Returns the absolute root path of the xcode developer directory on the host system for
   * the given xcode version. This may spawn a process and use the {@code xcode-locator} binary.
   * This uses a local cache file under {@code bazel-out}, and will only spawn a new process in the
   * case of a cache miss.
   *
   * @param execRoot the execution root path, used to locate the cache file
   * @param version the xcode version number to look up
   * @param productName the product name
   * @throws UserExecException if there is an issue with obtaining the path from the spawned
   *     process, either because there is no installed xcode with the given version, or
   *     there was an unexpected issue finding or running the tool
   */
  public static String getDeveloperDir(Path execRoot, DottedVersion version, String productName)
      throws UserExecException {
    try {
      CacheManager cacheManager =
          new CacheManager(execRoot.getRelative(BlazeDirectories.getRelativeOutputPath(productName)),
              XCODE_LOCATOR_CACHE_FILENAME);

      String cacheResult = cacheManager.getValue(version.toString());
      if (cacheResult != null) {
        return cacheResult;
      } else {
        CommandResult xcodeLocatorResult = new Command(new String[] {
            execRoot.getRelative("_bin/xcode-locator").getPathString(), version.toString()})
            .execute();

        String developerDir =
            new String(xcodeLocatorResult.getStdout(), StandardCharsets.UTF_8).trim();

        cacheManager.writeEntry(ImmutableList.of(version.toString()), developerDir);
        return developerDir;
      }
    } catch (AbnormalTerminationException e) {
      TerminationStatus terminationStatus = e.getResult().getTerminationStatus();

      String message;
      if (e.getResult().getTerminationStatus().exited()) {
        message = String.format("xcode-locator failed with code %s.\n"
            + "This most likely indicates that xcode version %s is not available on the host "
            + "machine.\n"
            + "%s\n"
            + "stderr: %s",
            terminationStatus.getExitCode(),
            version,
            terminationStatus.toString(),
            new String(e.getResult().getStderr(), StandardCharsets.UTF_8));
      } else {
        message = String.format("xcode-locator failed. %s\nstderr: %s",
            e.getResult().getTerminationStatus(),
            new String(e.getResult().getStderr(), StandardCharsets.UTF_8));
      }
      throw new UserExecException(message, e);
    } catch (CommandException | IOException e) {
      throw new UserExecException(e);
    }
  }
}