summary refs log tree commit diff stats
path: root/tools/vccexe/vccvswhere.nim
diff options
context:
space:
mode:
authorToshiyuki-Tega <tega@softgate.co.jp>2019-06-26 17:52:22 +0900
committerAndreas Rumpf <rumpf_a@web.de>2019-06-26 10:52:22 +0200
commitb7f8031e98ea0d6e5be4c07eb096eda68294ed40 (patch)
treebbfcefb64615aac9a88acdf72a73ca77552c23d3 /tools/vccexe/vccvswhere.nim
parent206f2478b876854153f158132a48e026bb3444d8 (diff)
downloadNim-b7f8031e98ea0d6e5be4c07eb096eda68294ed40.tar.gz
VCC discovery using vswhere (#6540) (#11559)
Diffstat (limited to 'tools/vccexe/vccvswhere.nim')
-rw-r--r--tools/vccexe/vccvswhere.nim47
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/vccexe/vccvswhere.nim b/tools/vccexe/vccvswhere.nim
new file mode 100644
index 000000000..11ad39ce9
--- /dev/null
+++ b/tools/vccexe/vccvswhere.nim
@@ -0,0 +1,47 @@
+## VCC compiler discovery using vswhere (https://github.com/Microsoft/vswhere)
+
+import os, osproc, strformat, strutils
+
+const
+  vswhereRelativePath = joinPath("Microsoft Visual Studio", "Installer", "vswhere.exe")
+  vswhereArgs = "-latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath"
+  vcvarsRelativePath = joinPath("VC", "Auxiliary", "Build", "vcvarsall.bat")
+
+proc vccVswhereExtractVcVarsAllPath(vswherePath: string): string =
+  ## Returns the path to `vcvarsall.bat` if Visual Studio location was obtained by executing `vswhere.exe`.
+  ##
+  ## `vcvarsall.bat` is supposed to exist under {vsPath}\VC\Auxiliary\Build directory.
+  ##
+  ## Returns "" if no appropriate `vcvarsall.bat` file was found.
+
+  # For more detail on vswhere arguments, refer to https://github.com/microsoft/vswhere/wiki/Find-VC
+  let vsPath = execProcess(&"\"{vswherePath}\" {vswhereArgs}").strip()
+  if vsPath.len > 0:
+    let vcvarsallPath = joinPath(vsPath, vcvarsRelativePath)
+    if existsFile(vcvarsallPath):
+      return vcvarsallPath
+
+proc vccVswhereGeneratePath(envName: string): string =
+  ## Generate a path to vswhere.exe under "Program Files" or "Program Files (x86)" depending on envName.
+  ##
+  ## Returns "" if an environment variable specified by envName was not available.
+
+  let val = getEnv(envName)
+  if val.len > 0:
+    result = try: expandFilename(joinPath(val, vswhereRelativePath)) except OSError: ""
+
+proc vccVswhereVcVarsAllPath*(): string = 
+  ## Returns the path to `vcvarsall.bat` for the latest Visual Studio (2017 and later).
+  ##
+  ## Returns "" if no recognizable Visual Studio installation was found.
+  ## 
+  ## Note: Beginning with Visual Studio 2017, the installers no longer set environment variables to allow for
+  ## multiple side-by-side installations of Visual Studio. Therefore, `vccEnvVcVarsAllPath` cannot be used
+  ## to detect the VCC Developer Command Prompt executable path for Visual Studio 2017 and later.
+
+  for tryEnv in ["ProgramFiles(x86)", "ProgramFiles"]:
+    let vswherePath = vccVswhereGeneratePath(tryEnv)
+    if vswherePath.len > 0 and existsFile(vswherePath):
+      let vcVarsAllPath = vccVswhereExtractVcVarsAllPath(vswherePath)
+      if vcVarsAllPath.len > 0:
+        return vcVarsAllPath