vcvarsall.nim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ## VCC Developer Command Prompt Loader
  2. ##
  3. ## In order for the VCC compiler backend to work properly, it requires numerous
  4. ## environment variables to be set properly for the desired architecture and compile target.
  5. ## For that purpose the VCC compiler ships with the vcvarsall utility which is an executable
  6. ## batch script that can be used to properly set up an Command Prompt environment.
  7. import strtabs, strutils, os, osproc
  8. const
  9. comSpecEnvKey = "ComSpec" ## Environment Variable that specifies the command-line application path in Windows
  10. ## Usually set to cmd.exe
  11. type
  12. VccArch* = enum ## The VCC compile target architectures
  13. vccarchUnspecified = "",
  14. vccarchX86 = "x86", ## VCC for compilation against the x86 architecture.
  15. vccarchAmd64 = "amd64", ## VCC for compilation against the amd64 architecture.
  16. vccarchX86Amd64 = "x86_amd64", ## VCC cross-compilation tools using x86 VCC for compilation against the amd64 architecture.
  17. vccarchX86Arm = "x86_arm", ## VCC cross-compilation tools using x86 VCC for compilation against the ARM architecture.
  18. vccarchX86Arm64 = "x86_arm64", ## VCC cross-compilation tools using x86 VCC for compilation against the ARM (64-bit) architecture.
  19. vccarchAmd64X86 = "amd64_x86", ## VCC cross-compilation tools using amd64 VCC for compilation against the x86 architecture.
  20. vccarchAmd64Arm = "amd64_arm", ## VCC cross-compilation tools using amd64 VCC for compilation against the ARM architecture.
  21. vccarchAmd64Arm64 = "amd64_arm64", ## VCC cross-compilation tools using amd64 VCC for compilation against the ARM (64-bit) architecture.
  22. vccarchX64 = "x64", ## VCC for compilation against the x64 architecture.
  23. vccarchX64X86 = "x64_x86", ## VCC cross-compilation tools using x64 VCC for compilation against the x86 architecture.
  24. vccarchX64Arm = "x64_arm", ## VCC cross-compilation tools using x64 VCC for compilation against the ARM architecture.
  25. vccarchX64Arm64 = "x64_arm64" ## VCC cross-compilation tools using x64 VCC for compilation against the ARM (64-bit) architecture.
  26. VccPlatformType* = enum ## The VCC platform type of the compile target
  27. vccplatEmpty = "", ## Default (i.e. Desktop) Platfor Type
  28. vccplatStore = "store", ## Windows Store Application
  29. vccplatUWP = "uwp", ## Universal Windows Platform (UWP) Application
  30. vccplatOneCore = "onecore" # Undocumented platform type in the Windows SDK, probably XBox One SDK platform type.
  31. proc vccVarsAll*(path: string, arch: VccArch = vccarchUnspecified, platform_type: VccPlatformType = vccplatEmpty, sdk_version, vctoolset: string = "", verbose: bool = false): StringTableRef =
  32. ## Returns a string table containing the proper process environment to successfully execute VCC compile commands for the specified SDK version, CPU architecture and platform type.
  33. ##
  34. ## path
  35. ## The path to the vcvarsall utility for VCC compiler backend.
  36. ## arch
  37. ## The compile target CPU architecture. Starting with Visual Studio 2017, this value must be specified and must not be set to `vccarchUnspecified`.
  38. ## platform_type
  39. ## The compile target Platform Type. Defaults to the Windows Desktop platform, i.e. a regular Windows executable binary.
  40. ## sdk_version
  41. ## The Windows SDK version to use.
  42. ## vctoolset
  43. ## Visual Studio compiler toolset to use.
  44. ## verbose
  45. ## Echo the command-line passed on to the system to load the VCC environment. Defaults to `false`.
  46. if path == "":
  47. return nil
  48. let vccvarsallpath = path
  49. var args: seq[string] = @[]
  50. let archStr: string = $arch
  51. if archStr.len > 0:
  52. args.add(archStr)
  53. let platStr: string = $platform_type
  54. if platStr.len > 0:
  55. args.add(platStr)
  56. if sdk_version.len > 0:
  57. args.add(sdk_version)
  58. if vctoolset.len > 0:
  59. args.add("-vcvars_ver="&vctoolset)
  60. let argStr = args.join " "
  61. var vcvarsExec: string
  62. if argStr.len > 0:
  63. vcvarsExec = "\"$1\" $2" % [vccvarsallpath, argStr]
  64. else:
  65. vcvarsExec = "\"$1\"" % vccvarsallpath
  66. var comSpecCmd = getenv comSpecEnvKey
  67. if comSpecCmd.len < 1:
  68. comSpecCmd = "cmd"
  69. # Run the Windows Command Prompt with the /C argument
  70. # Execute vcvarsall with its command-line arguments
  71. # and then execute the SET command to list all environment variables
  72. let comSpecExec = "\"$1\" /C \"$2 && SET\"" % [comSpecCmd, vcvarsExec]
  73. var comSpecOpts = {poEvalCommand, poDaemon, poStdErrToStdOut}
  74. if verbose:
  75. comSpecOpts.incl poEchoCmd
  76. let comSpecOut = execProcess(comSpecExec, options = comSpecOpts)
  77. result = newStringTable(modeCaseInsensitive)
  78. # Parse the output of the final SET command to construct a String Table
  79. # with the appropriate environment variables
  80. for line in comSpecOut.splitLines:
  81. let idx = line.find('=')
  82. if idx > 0:
  83. result[line[0..(idx - 1)]] = line[(idx + 1)..(line.len - 1)]
  84. elif verbose:
  85. echo line