vccenv.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ## VCC compiler backend installation discovery using Visual Studio common tools
  2. ## environment variables.
  3. import os
  4. type
  5. VccEnvVersion* = enum ## The version of the Visual Studio C/C++ Developer Environment to load
  6. ## Valid versions are Versions of Visual Studio that permanently set a COMNTOOLS
  7. ## environment variable. That includes Visual Studio version up to and including
  8. ## Visual Studio 2015
  9. vsUndefined = (0, ""), ## Version not specified, use latest recognized version on the system
  10. vs90 = (90, "VS90COMNTOOLS"), ## Visual Studio 2008
  11. vs100 = (100, "VS100COMNTOOLS"), ## Visual Studio 2010
  12. vs110 = (110, "VS110COMNTOOLS"), ## Visual Studio 2012
  13. vs120 = (120, "VS120COMNTOOLS"), ## Visual Studio 2013
  14. vs140 = (140, "VS140COMNTOOLS") ## Visual Studio 2015
  15. const
  16. vcvarsallRelativePath = joinPath("..", "..", "VC", "vcvarsall.bat") ## Relative path from the COMNTOOLS path to the vcvarsall file.
  17. proc vccEnvVcVarsAllPath*(version: VccEnvVersion = vsUndefined): string =
  18. ## Returns the path to the VCC Developer Command Prompt executable for the specified VCC version.
  19. ##
  20. ## Returns `nil` if the specified VCC compiler backend installation was not found.
  21. ##
  22. ## If the `version` parameter is omitted or set to `vsUndefined`, `vccEnvVcVarsAllPath` searches
  23. ## for the latest recognizable version of the VCC tools it can find.
  24. ##
  25. ## `vccEnvVcVarsAllPath` uses the COMNTOOLS environment variables to find the Developer Command Prompt
  26. ## executable path. The COMNTOOLS environment variable are permanently set when Visual Studio is installed.
  27. ## Each version of Visual Studio has its own COMNTOOLS environment variable. E.g.: Visual Studio 2015 sets
  28. ## The VS140COMNTOOLS environment variable.
  29. ##
  30. ## Note: Beginning with Visual Studio 2017, the installers no longer set environment variables to allow for
  31. ## multiple side-by-side installations of Visual Studio. Therefore, `vccEnvVcVarsAllPath` cannot be used
  32. ## to detect the VCC Developer Command Prompt executable path for Visual Studio 2017 and later.
  33. if version == vsUndefined:
  34. for tryVersion in [vs140, vs120, vs110, vs100, vs90]:
  35. let tryPath = vccEnvVcVarsAllPath(tryVersion)
  36. if tryPath.len > 0:
  37. return tryPath
  38. else: # Specific version requested
  39. let key = $version
  40. let val = getEnv key
  41. if val.len > 0:
  42. result = try: expandFilename(joinPath(val, vcvarsallRelativePath)) except OSError: ""