<commit_before>package com.github.maven_nar;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Comparator;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.tools.ant.types.Environment.Variable;
import org.codehaus.plexus.util.StringUtils;

import com.github.maven_nar.cpptasks.CCTask;
import com.github.maven_nar.cpptasks.CompilerDef;
import com.github.maven_nar.cpptasks.LinkerDef;
import com.github.maven_nar.cpptasks.types.SystemIncludePath;

public class Msvc {
  @Parameter
  private File home;

  private AbstractNarMojo mojo;

  private final Set<String> paths = new LinkedHashSet<String>();

  @Parameter
  private String version;

  @Parameter
  private File windowsSdkHome;

  @Parameter
  private String windowsSdkVersion;

  private File windowsHome;
  private String toolPathWindowsSDK;
  private String versionPathWindowsSDK = "";
  private String toolPathLinker;

  private boolean addIncludePath(final CCTask task, final File home, final String subDirectory)
      throws MojoExecutionException {
    if (home == null) {
      return false;
    }
    final File file = new File(home, subDirectory);
    try {
      if (file.exists()) {
        final SystemIncludePath includePath = task.createSysIncludePath();
        final String fullPath = file.getCanonicalPath();
        includePath.setPath(fullPath);
        return true;
      }
      return false;
    } catch (final IOException e) {
      throw new MojoExecutionException("Unable to add system include: " + file.getAbsolutePath(), e);
    }
  }

  private boolean addPath(final File home, final String path) {
    if (home != null) {
      final File directory = new File(home, path);
      if (directory.exists()) {
        try {
          final String fullPath = directory.getCanonicalPath();
          this.paths.add(fullPath);
          return true;
        } catch (final IOException e) {
          throw new IllegalArgumentException("Unable to get path: " + directory, e);
        }
      }
    }
    return false;
  }

  public void configureCCTask(final CCTask task) throws MojoExecutionException {
    final String os = mojo.getOS();
    if (os.equals(OS.WINDOWS)) {
      addIncludePath(task, this.home, "VC/include");
      addIncludePath(task, this.home, "VC/atlmfc/include");
      if (compareVersion(this.windowsSdkVersion, "7.1A") <= 0) {
        addIncludePath(task, this.windowsSdkHome, "include");
      } else {
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/ucrt");
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/um");
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/shared");
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/winrt");
      }
      task.addEnv(getPathVariable());
      // TODO: supporting running with clean environment - addEnv sets
      // newEnvironemnt by default
      // task.setNewenvironment(false);
      Variable envVariable = new Variable();
      // cl needs SystemRoot env var set, otherwise D8037 is raised (bogus
      // message)
      // - https://msdn.microsoft.com/en-us/library/bb385201.aspx
      // -
      // http://stackoverflow.com/questions/10560779/cl-exe-when-launched-via-createprocess-does-not-seem-to-have-write-permissions
      envVariable.setKey("SystemRoot");
      envVariable.setValue(this.windowsHome.getAbsolutePath());
      task.addEnv(envVariable);
      // cl needs TMP otherwise D8050 is raised c1xx.dll
      envVariable = new Variable();
      envVariable.setKey("TMP");
      envVariable.setValue("C:\\Temp");
      task.addEnv(envVariable);
    }
  }

  public void configureLinker(final LinkerDef linker) throws MojoExecutionException {
    final String os = mojo.getOS();
    if (os.equals(OS.WINDOWS)) {
      final String arch = mojo.getArchitecture();

      // Visual Studio
      if ("x86".equals(arch)) {
        linker.addLibraryDirectory(this.home, "VC/lib");
        linker.addLibraryDirectory(this.home, "VC/atlmfc/lib");
      } else {
        linker.addLibraryDirectory(this.home, "VC/lib/" + arch);
        linker.addLibraryDirectory(this.home, "VC/atlmfc/lib/" + arch);
      }
      // Windows SDK
      String sdkArch = arch;
      if ("amd64".equals(arch)) {
        sdkArch = "x64";
      }
      // 6 lib ?+ lib/x86 or lib/x64
      if (compareVersion(this.windowsSdkVersion, "8.0") < 0) {
        if ("x86".equals(arch)) {
          linker.addLibraryDirectory(this.windowsSdkHome, "lib");
        } else {
          linker.addLibraryDirectory(this.windowsSdkHome, "lib/" + sdkArch);
        }
      } else if (compareVersion(this.windowsSdkVersion, "8.0") == 0) {
        linker.addLibraryDirectory(this.windowsSdkHome, "Lib/Win8/um/" + sdkArch); // 8+
      } else {
        linker.addLibraryDirectory(this.windowsSdkHome, "Lib/Winv6.3/um/" + sdkArch); // win
                                                                                      // 8
                                                                                      // sdk
                                                                                      // compatible
                                                                                      // with
                                                                                      // XP
      }
      linker.addLibraryDirectory(this.windowsSdkHome, "Lib" + versionPathWindowsSDK + "/ucrt/" + sdkArch); // 10+
      linker.addLibraryDirectory(this.windowsSdkHome, "Lib" + versionPathWindowsSDK + "/um/" + sdkArch); // 10+
    }
  }

  public Variable getPathVariable() {
    if (this.paths.isEmpty()) {
      return null;
    }
    final Variable pathVariable = new Variable();
    pathVariable.setKey("PATH");
    pathVariable.setValue(StringUtils.join(this.paths.iterator(), File.pathSeparator));
    return pathVariable;<commit_msg>Make msvc build TMP path configurable<commit_after>package com.github.maven_nar;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Comparator;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.tools.ant.types.Environment.Variable;
import org.codehaus.plexus.util.StringUtils;

import com.github.maven_nar.cpptasks.CCTask;
import com.github.maven_nar.cpptasks.CompilerDef;
import com.github.maven_nar.cpptasks.LinkerDef;
import com.github.maven_nar.cpptasks.types.SystemIncludePath;

public class Msvc {
  @Parameter
  private File home;

  private AbstractNarMojo mojo;

  private final Set<String> paths = new LinkedHashSet<String>();

  @Parameter
  private String version;

  @Parameter
  private File windowsSdkHome;

  @Parameter
  private String windowsSdkVersion;

  @Parameter
  private String tempPath;

  private File windowsHome;
  private String toolPathWindowsSDK;
  private String versionPathWindowsSDK = "";
  private String toolPathLinker;

  private boolean addIncludePath(final CCTask task, final File home, final String subDirectory)
      throws MojoExecutionException {
    if (home == null) {
      return false;
    }
    final File file = new File(home, subDirectory);
    try {
      if (file.exists()) {
        final SystemIncludePath includePath = task.createSysIncludePath();
        final String fullPath = file.getCanonicalPath();
        includePath.setPath(fullPath);
        return true;
      }
      return false;
    } catch (final IOException e) {
      throw new MojoExecutionException("Unable to add system include: " + file.getAbsolutePath(), e);
    }
  }

  private boolean addPath(final File home, final String path) {
    if (home != null) {
      final File directory = new File(home, path);
      if (directory.exists()) {
        try {
          final String fullPath = directory.getCanonicalPath();
          this.paths.add(fullPath);
          return true;
        } catch (final IOException e) {
          throw new IllegalArgumentException("Unable to get path: " + directory, e);
        }
      }
    }
    return false;
  }

  public void configureCCTask(final CCTask task) throws MojoExecutionException {
    final String os = mojo.getOS();
    if (os.equals(OS.WINDOWS)) {
      addIncludePath(task, this.home, "VC/include");
      addIncludePath(task, this.home, "VC/atlmfc/include");
      if (compareVersion(this.windowsSdkVersion, "7.1A") <= 0) {
        addIncludePath(task, this.windowsSdkHome, "include");
      } else {
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/ucrt");
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/um");
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/shared");
        addIncludePath(task, this.windowsSdkHome, "include" + versionPathWindowsSDK + "/winrt");
      }
      task.addEnv(getPathVariable());
      // TODO: supporting running with clean environment - addEnv sets
      // newEnvironemnt by default
      // task.setNewenvironment(false);
      Variable envVariable = new Variable();
      // cl needs SystemRoot env var set, otherwise D8037 is raised (bogus
      // message)
      // - https://msdn.microsoft.com/en-us/library/bb385201.aspx
      // -
      // http://stackoverflow.com/questions/10560779/cl-exe-when-launched-via-createprocess-does-not-seem-to-have-write-permissions
      envVariable.setKey("SystemRoot");
      envVariable.setValue(this.windowsHome.getAbsolutePath());
      task.addEnv(envVariable);
      // cl needs TMP otherwise D8050 is raised c1xx.dll
      envVariable = new Variable();
      envVariable.setKey("TMP");
      envVariable.setValue(getTempPath());
      task.addEnv(envVariable);
    }
  }

  public void configureLinker(final LinkerDef linker) throws MojoExecutionException {
    final String os = mojo.getOS();
    if (os.equals(OS.WINDOWS)) {
      final String arch = mojo.getArchitecture();

      // Visual Studio
      if ("x86".equals(arch)) {
        linker.addLibraryDirectory(this.home, "VC/lib");
        linker.addLibraryDirectory(this.home, "VC/atlmfc/lib");
      } else {
        linker.addLibraryDirectory(this.home, "VC/lib/" + arch);
        linker.addLibraryDirectory(this.home, "VC/atlmfc/lib/" + arch);
      }
      // Windows SDK
      String sdkArch = arch;
      if ("amd64".equals(arch)) {
        sdkArch = "x64";
      }
      // 6 lib ?+ lib/x86 or lib/x64
      if (compareVersion(this.windowsSdkVersion, "8.0") < 0) {
        if ("x86".equals(arch)) {
          linker.addLibraryDirectory(this.windowsSdkHome, "lib");
        } else {
          linker.addLibraryDirectory(this.windowsSdkHome, "lib/" + sdkArch);
        }
      } else if (compareVersion(this.windowsSdkVersion, "8.0") == 0) {
        linker.addLibraryDirectory(this.windowsSdkHome, "Lib/Win8/um/" + sdkArch); // 8+
      } else {
        linker.addLibraryDirectory(this.windowsSdkHome, "Lib/Winv6.3/um/" + sdkArch); // win
                                                                                      // 8
                                                                                      // sdk
                                                                                      // compatible
                                                                                      // with
                                                                                      // XP
      }
      linker.addLibraryDirectory(this.windowsSdkHome, "Lib" + versionPathWindowsSDK + "/ucrt/" + sdkArch); // 10+
      linker.addLibraryDirectory(this.windowsSdkHome, "Lib" + versionPathWindowsSDK + "/um/" + sdkArch); // 10+
    }
  }

  private String getTempPath(){
    if( null == tempPath ){
      tempPath = System.getenv("TMP");
      if( null == tempPath )
        tempPath = System.getenv("TEMP");
      if( null == tempPath )
        tempPath = "C:\\Temp";
    }
    return tempPath;
  }

  public Variable getPathVariable() {
    if (this.paths.isEmpty()) {
      return null;
    }
    final Variable pathVariable = new Variable();
    pathVariable.setKey("PATH");
    pathVariable.setValue(StringUtils.join(this.paths.iterator(), File.pathSeparator));
    return pathVariable;
{'fraction_non_alphanumeric': 0.11320018723669839, 'fraction_numerical': 0.008425651427679825, 'mean_word_length': 2.3993635640413684, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5704521', 'n_tokens_mistral': 3742, 'n_tokens_neox': 3582, 'n_words': 774}