summary refs log tree commit diff stats
path: root/tools/vccenv/vccenv.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2016-10-24 21:55:57 +0200
committerAraq <rumpf_a@web.de>2016-10-24 21:55:57 +0200
commit11dd0b3b5107a784bcb12c2d504a48a9a57884c5 (patch)
tree5de97895ab68a926c69c28d7ec9642544cf0050c /tools/vccenv/vccenv.nim
parentc15cef7d2d1198ac1b552dcead0f1a65f366549a (diff)
downloadNim-11dd0b3b5107a784bcb12c2d504a48a9a57884c5.tar.gz
simplified code of the upcoming vccexe and vcclinkerexe tools
Diffstat (limited to 'tools/vccenv/vccenv.nim')
-rw-r--r--tools/vccenv/vccenv.nim58
1 files changed, 58 insertions, 0 deletions
diff --git a/tools/vccenv/vccenv.nim b/tools/vccenv/vccenv.nim
new file mode 100644
index 000000000..a335efd10
--- /dev/null
+++ b/tools/vccenv/vccenv.nim
@@ -0,0 +1,58 @@
+import strtabs, os, osproc, streams, strutils
+
+const
+  comSpecEnvKey = "ComSpec"
+  vsComnToolsEnvKeys = [
+    "VS140COMNTOOLS",
+    "VS130COMNTOOLS",
+    "VS120COMNTOOLS",
+    "VS110COMNTOOLS",
+    "VS100COMNTOOLS",
+    "VS90COMNTOOLS"
+  ]
+  vcvarsallRelativePath = joinPath("..", "..", "VC", "vcvarsall")
+
+proc getVsComnToolsPath*(): TaintedString =
+  for vsComnToolsEnvKey in vsComnToolsEnvKeys:
+    let vsComnToolsEnvVal = getEnv vsComnToolsEnvKey
+    if vsComnToolsEnvVal.len > 0:
+      return vsComnToolsEnvVal
+
+proc getVccEnv*(platform: string, windowsStoreSdk: bool = false,
+                sdkVersion: string = nil): StringTableRef =
+  var comSpecCommandString = getEnv comSpecEnvKey
+  if comSpecCommandString.len == 0:
+    comSpecCommandString = "cmd"
+
+  let vsComnToolsPath = getVsComnToolsPath()
+  if vsComnToolsPath.len < 1:
+    return nil
+  let vcvarsallPath = expandFilename joinPath(vsComnToolsPath, vcvarsallRelativePath)
+
+  var vcvarsallArgs: seq[string] = @[]
+  if platform.len > 0:
+    vcvarsallArgs.add(platform)
+  if windowsStoreSdk:
+    vcvarsallArgs.add("store")
+  if sdkVersion.len > 0:
+    vcvarsallArgs.add(sdkVersion)
+  let vcvarsallArgString = vcvarsallArgs.join(" ")
+
+  var vcvarsallCommandString: string
+  if vcvarsallArgString.len > 0:
+    vcvarsallCommandString = "\"$1\" $2" % [vcvarsallPath, vcvarsallArgString]
+  else:
+    vcvarsallCommandString = vcvarsallPath
+
+  let vcvarsallExecCommand = "\"$1\" /C \"$2 && SET\"" %
+                             [comSpecCommandString, vcvarsallCommandString]
+  when defined(release):
+    let vccvarsallOptions = {poEvalCommand, poDemon}
+  else:
+    let vccvarsallOptions = {poEchoCmd, poEvalCommand, poDemon}
+  let vcvarsallStdOut = execProcess(vcvarsallExecCommand, options = vccvarsallOptions)
+  result = newStringTable(modeCaseInsensitive)
+  for line in vcvarsallStdOut.splitLines:
+    let idx = line.find('=')
+    if idx > 0:
+      result[line[0..(idx - 1)]] = line[(idx + 1)..(line.len - 1)]