vcvarsall.nim 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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: 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. ## verbose
  43. ## Echo the command-line passed on to the system to load the VCC environment. Defaults to `false`.
  44. if path == "":
  45. return nil
  46. let vccvarsallpath = path
  47. var args: seq[string] = @[]
  48. let archStr: string = $arch
  49. if archStr.len > 0:
  50. args.add(archStr)
  51. let platStr: string = $platform_type
  52. if platStr.len > 0:
  53. args.add(platStr)
  54. if sdk_version.len > 0:
  55. args.add(sdk_version)
  56. let argStr = args.join " "
  57. var vcvarsExec: string
  58. if argStr.len > 0:
  59. vcvarsExec = "\"$1\" $2" % [vccvarsallpath, argStr]
  60. else:
  61. vcvarsExec = "\"$1\"" % vccvarsallpath
  62. var comSpecCmd = getenv comSpecEnvKey
  63. if comSpecCmd.len < 1:
  64. comSpecCmd = "cmd"
  65. # Run the Windows Command Prompt with the /C argument
  66. # Execute vcvarsall with its command-line arguments
  67. # and then execute the SET command to list all environment variables
  68. let comSpecExec = "\"$1\" /C \"$2 && SET\"" % [comSpecCmd, vcvarsExec]
  69. var comSpecOpts = {poEvalCommand, poDaemon, poStdErrToStdOut}
  70. if verbose:
  71. comSpecOpts.incl poEchoCmd
  72. let comSpecOut = execProcess(comSpecExec, options = comSpecOpts)
  73. result = newStringTable(modeCaseInsensitive)
  74. # Parse the output of the final SET command to construct a String Table
  75. # with the appropriate environment variables
  76. for line in comSpecOut.splitLines:
  77. let idx = line.find('=')
  78. if idx > 0:
  79. result[line[0..(idx - 1)]] = line[(idx + 1)..(line.len - 1)]
  80. elif verbose:
  81. echo line