vccenv.nim 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import strtabs, os, osproc, streams, strutils
  2. const
  3. comSpecEnvKey = "ComSpec"
  4. vsComnToolsEnvKeys = [
  5. "VS140COMNTOOLS",
  6. "VS130COMNTOOLS",
  7. "VS120COMNTOOLS",
  8. "VS110COMNTOOLS",
  9. "VS100COMNTOOLS",
  10. "VS90COMNTOOLS"
  11. ]
  12. vcvarsallRelativePath = joinPath("..", "..", "VC", "vcvarsall")
  13. proc getVsComnToolsPath*(): TaintedString =
  14. for vsComnToolsEnvKey in vsComnToolsEnvKeys:
  15. let vsComnToolsEnvVal = getEnv vsComnToolsEnvKey
  16. if vsComnToolsEnvVal.len > 0:
  17. return vsComnToolsEnvVal
  18. proc getVccEnv*(platform: string, windowsStoreSdk: bool = false,
  19. sdkVersion: string = ""): StringTableRef =
  20. var comSpecCommandString = getEnv comSpecEnvKey
  21. if comSpecCommandString.len == 0:
  22. comSpecCommandString = "cmd"
  23. let vsComnToolsPath = getVsComnToolsPath()
  24. if vsComnToolsPath.len < 1:
  25. return nil
  26. let vcvarsallPath = expandFilename joinPath(vsComnToolsPath, vcvarsallRelativePath)
  27. var vcvarsallArgs: seq[string] = @[]
  28. if platform.len > 0:
  29. vcvarsallArgs.add(platform)
  30. if windowsStoreSdk:
  31. vcvarsallArgs.add("store")
  32. if sdkVersion.len > 0:
  33. vcvarsallArgs.add(sdkVersion)
  34. let vcvarsallArgString = vcvarsallArgs.join(" ")
  35. var vcvarsallCommandString: string
  36. if vcvarsallArgString.len > 0:
  37. vcvarsallCommandString = "\"$1\" $2" % [vcvarsallPath, vcvarsallArgString]
  38. else:
  39. vcvarsallCommandString = vcvarsallPath
  40. let vcvarsallExecCommand = "\"$1\" /C \"$2 && SET\"" %
  41. [comSpecCommandString, vcvarsallCommandString]
  42. when defined(release):
  43. let vccvarsallOptions = {poEvalCommand, poDemon}
  44. else:
  45. let vccvarsallOptions = {poEchoCmd, poEvalCommand, poDemon}
  46. let vcvarsallStdOut = execProcess(vcvarsallExecCommand, options = vccvarsallOptions)
  47. result = newStringTable(modeCaseInsensitive)
  48. for line in vcvarsallStdOut.splitLines:
  49. let idx = line.find('=')
  50. if idx > 0:
  51. result[line[0..(idx - 1)]] = line[(idx + 1)..(line.len - 1)]