/*
 * Copyright 2017-present Facebook, Inc.
 *
 * 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.facebook.buck.apple.toolchain.impl;

import com.facebook.buck.apple.AppleConfig;
import com.facebook.buck.apple.toolchain.AbstractAppleToolchainProvider;
import com.facebook.buck.apple.toolchain.AppleDeveloperDirectoryProvider;
import com.facebook.buck.apple.toolchain.AppleSdk;
import com.facebook.buck.apple.toolchain.AppleSdkLocation;
import com.facebook.buck.apple.toolchain.AppleSdkPaths;
import com.facebook.buck.log.Logger;
import com.facebook.buck.toolchain.ToolchainCreationContext;
import com.facebook.buck.toolchain.ToolchainFactory;
import com.facebook.buck.toolchain.ToolchainProvider;
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;

public class AppleSdkLocationFactory implements ToolchainFactory<AppleSdkLocation> {

  private static final Logger LOG = Logger.get(AppleSdkLocationFactory.class);

  @Override
  public Optional<AppleSdkLocation> createToolchain(
      ToolchainProvider toolchainProvider, ToolchainCreationContext context) {
    if (!toolchainProvider.isToolchainPresent(AbstractAppleToolchainProvider.DEFAULT_NAME)) {
      return Optional.empty();
    }

    Optional<Path> appleDeveloperDir =
        toolchainProvider
            .getByNameIfPresent(
                AppleDeveloperDirectoryProvider.DEFAULT_NAME, AppleDeveloperDirectoryProvider.class)
            .map(AppleDeveloperDirectoryProvider::getAppleDeveloperDirectory);

    AppleConfig appleConfig = context.getBuckConfig().getView(AppleConfig.class);
    AbstractAppleToolchainProvider appleToolchainProvider =
        toolchainProvider.getByName(
            AbstractAppleToolchainProvider.DEFAULT_NAME, AbstractAppleToolchainProvider.class);
    try {
      ImmutableMap<AppleSdk, AppleSdkPaths> appleSdkPaths =
          AppleSdkDiscovery.discoverAppleSdkPaths(
              appleDeveloperDir,
              appleConfig.getExtraPlatformPaths(),
              appleToolchainProvider.getAppleToolchains(),
              appleConfig);
      return Optional.of(AppleSdkLocation.of(appleSdkPaths));
    } catch (IOException e) {
      LOG.error(
          e, "Couldn't find the Apple SDK.\nPlease check that the SDK is installed properly.");
      return Optional.empty();
    }
  }
}

@@ -1010,45 +1010,8 @@
 hs;%0A
-import com.facebook.buck.log.Logger;%0A
 impo
@@ -1120,16 +1120,84 @@
 actory;%0A
+import com.facebook.buck.toolchain.ToolchainInstantiationException;%0A
 import c
@@ -1462,88 +1462,8 @@
  %7B%0A%0A
-  private static final Logger LOG = Logger.get(AppleSdkLocationFactory.class);%0A%0A
   @O
@@ -2682,17 +2682,49 @@
     
-LOG.error
+throw new ToolchainInstantiationException
 (%0A  
@@ -2821,39 +2821,8 @@
 %22);%0A
-      return Optional.empty();%0A
     

Throw an exception when there is an error during Apple SDK discovery