/*
 * Copyright 2015-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.cxx.toolchain;

import com.facebook.buck.rules.DelegatingTool;
import com.facebook.buck.rules.Tool;
import com.google.common.collect.Iterables;
import java.nio.file.Path;
import java.util.function.Function;

/** Preprocessor implementation for the Windows toolchain. */
public class WindowsPreprocessor extends DelegatingTool implements Preprocessor {

  private static Function<String, String> prependIncludeFlag = "/I"::concat;

  public WindowsPreprocessor(Tool tool) {
    super(tool);
  }

  @Override
  public boolean supportsHeaderMaps() {
    return false;
  }

  @Override
  public boolean supportsPrecompiledHeaders() {
    // TODO(steveo) Should be easy to add support; will try @ later time,
    // when I can test w/ Windows.
    // https://msdn.microsoft.com/en-us/library/z0atkd6c.aspx
    return false;
  }

  @Override
  public Iterable<String> localIncludeArgs(Iterable<String> includeRoots) {
    return Iterables.transform(includeRoots, prependIncludeFlag::apply);
  }

  @Override
  public Iterable<String> systemIncludeArgs(Iterable<String> includeRoots) {
    return Iterables.transform(includeRoots, prependIncludeFlag::apply);
  }

  @Override
  public Iterable<String> quoteIncludeArgs(Iterable<String> includeRoots) {
    return Iterables.transform(includeRoots, prependIncludeFlag::apply);
  }

  @Override
  public Iterable<String> prefixHeaderArgs(Path prefixHeader) {
    throw new UnsupportedOperationException("prefix header not supported by " + getClass());
    // TODO(steveo) Should be easy to add support; will try @ later time,
    // when I can test w/ Windows.
    // "Forced Include": https://msdn.microsoft.com/en-us/library/8c5ztk84.aspx
    // Space is allowed between flag and its pathname argument.
    // E.g. something like this (space allowed between flag and its argument):
    // return ImmutableList.of("/FI", resolver.getAbsolutePath(prefixHeader).toString());
  }

  @Override
  public Iterable<String> precompiledHeaderArgs(Path pchOutputPath) {
    throw new UnsupportedOperationException("precompiled header not supported by " + getClass());
    // TODO(steveo) Should be easy to add support; will try @ later time,
    // when I can test w/ Windows.
    // https://msdn.microsoft.com/en-us/library/z0atkd6c.aspx
    // E.g. something like this flag (no space between "/Yu" and its argument):
    // return ImmutableList.of("/Yu" + pchOutputPath);
  }
}

@@ -800,44 +800,8 @@
 ath;
-%0Aimport java.util.function.Function;
 %0A%0A/*
@@ -946,87 +946,8 @@
 r %7B%0A
-%0A  private static Function%3CString, String%3E prependIncludeFlag = %22/I%22::concat;%0A%0A
   pu
@@ -1327,32 +1327,133 @@
 urn false;%0A  %7D%0A%0A
+  private static String prependIncludeFlag(String includeRoot) %7B%0A    return %22/I%22 + includeRoot;%0A  %7D%0A%0A
   @Override%0A  pu
@@ -1561,32 +1561,53 @@
 m(includeRoots, 
+WindowsPreprocessor::
 prependIncludeFl
@@ -1600,39 +1600,32 @@
 ependIncludeFlag
-::apply
 );%0A  %7D%0A%0A  @Overr
@@ -1742,32 +1742,53 @@
 m(includeRoots, 
+WindowsPreprocessor::
 prependIncludeFl
@@ -1781,39 +1781,32 @@
 ependIncludeFlag
-::apply
 );%0A  %7D%0A%0A  @Overr
@@ -1930,16 +1930,37 @@
 eRoots, 
+WindowsPreprocessor::
 prependI
@@ -1973,15 +1973,8 @@
 Flag
-::apply
 );%0A 

Convert Function to method reference.