find-sdk-path.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/bash
  2. # Function to output the path in Windows format (convert from MSYS2/Unix format using cygpath)
  3. convert_to_windows_format() {
  4. cygpath -w -a "$1"
  5. }
  6. # Function to display help message
  7. display_help() {
  8. echo "Usage: $0 [-s|--sdk] [-v|--visualstudio] [-w|--windows] [--help]"
  9. echo
  10. echo "Options:"
  11. echo " -s, --sdk Search for tools in the Windows SDK."
  12. echo " -v, --visualstudio Search for tools in Visual Studio."
  13. echo " -w, --windows Output the path in Windows format (using cygpath)."
  14. echo " --help Display this help message."
  15. exit 0
  16. }
  17. # Function to find tools in the Windows SDK
  18. find_sdk_tools() {
  19. sdk_base_path="/c/Program Files (x86)/Windows Kits/10/bin"
  20. if [ ! -d "$sdk_base_path" ]; then
  21. echo "ERROR: SDK base path \"$sdk_base_path\" does not exist. No SDK installations found." >&2
  22. echo "$system_root"
  23. return 1
  24. fi
  25. echo "SDK base path exists: \"$sdk_base_path\"" >&2
  26. # Find all SDK versions
  27. sdk_versions=($(ls "$sdk_base_path" | tr ' ' '\n' | grep -E "^[0-9]+\..*$"))
  28. echo "Found SDK versions: ${sdk_versions[*]}" >&2
  29. if [ ${#sdk_versions[@]} -eq 0 ]; then
  30. echo "ERROR: No valid Windows SDK versions found in \"$sdk_base_path\"." >&2
  31. echo "$system_root"
  32. return 1
  33. fi
  34. # Sort versions and pick the latest
  35. sorted_versions=$(printf '%s\n' "${sdk_versions[@]}" | sort -V)
  36. latest_sdk_version=$(echo "$sorted_versions" | tail -n 1)
  37. sdk_tool_path="$sdk_base_path/$latest_sdk_version/x64"
  38. echo "Latest SDK version: \"$latest_sdk_version\"" >&2
  39. if [ ! -d "$sdk_tool_path" ]; then
  40. echo "ERROR: Tool path \"$sdk_tool_path\" does not exist." >&2
  41. echo "$system_root"
  42. return 1
  43. fi
  44. # Check if required tools exist
  45. tools=("mc.exe" "rc.exe")
  46. for tool in "${tools[@]}"; do
  47. if [ ! -f "$sdk_tool_path/$tool" ]; then
  48. echo "ERROR: $tool not found in \"$sdk_tool_path\"" >&2
  49. echo "$system_root"
  50. return 1
  51. else
  52. echo "$tool found in \"$sdk_tool_path\"" >&2
  53. fi
  54. done
  55. echo >&2
  56. echo "DONE: All required tools found in \"$sdk_tool_path\"" >&2
  57. echo >&2
  58. echo "$sdk_tool_path"
  59. }
  60. # Function to find tools in Visual Studio
  61. find_visual_studio_tools() {
  62. studio_base_path="/c/Program Files/Microsoft Visual Studio/2022"
  63. echo "Checking for Visual Studio installations in: \"$studio_base_path\"" >&2
  64. if [ ! -d "$studio_base_path" ]; then
  65. echo "ERROR: Visual Studio base path \"$studio_base_path\" does not exist. No Visual Studio installations found." >&2
  66. echo "$system_root"
  67. return 1
  68. fi
  69. # Visual Studio editions we want to check
  70. editions=("Enterprise" "Professional" "Community")
  71. available_editions=()
  72. # Loop through each edition and check for tools
  73. for edition in "${editions[@]}"; do
  74. edition_path="$studio_base_path/$edition/VC/Tools/MSVC"
  75. if [ -d "$edition_path" ]; then
  76. available_editions+=("$edition")
  77. echo "Checking edition: $edition in $studio_base_path" >&2
  78. # Find all MSVC versions and sort them
  79. msvc_versions=($(ls "$edition_path" | tr ' ' '\n' | grep -E "^[0-9]+\..*$"))
  80. echo "Found MSVC versions in $edition: ${msvc_versions[*]}" >&2
  81. if [ ${#msvc_versions[@]} -gt 0 ]; then
  82. sorted_versions=$(printf '%s\n' "${msvc_versions[@]}" | sort -V)
  83. latest_msvc_version=$(echo "${sorted_versions[@]}" | tail -n 1)
  84. vs_tool_path="$edition_path/$latest_msvc_version/bin/Hostx64/x64"
  85. echo "Latest MSVC version: \"$latest_msvc_version\" in $edition" >&2
  86. if [ ! -d "$vs_tool_path" ]; then
  87. echo "WARNING: Tool path \"$vs_tool_path\" does not exist." >&2
  88. continue
  89. fi
  90. # Check if required tools exist
  91. tools=("link.exe")
  92. missing_tool=0
  93. for tool in "${tools[@]}"; do
  94. if [ ! -f "$vs_tool_path/$tool" ]; then
  95. echo "WARNING: $tool not found in \"$vs_tool_path\" for $edition" >&2
  96. missing_tool=1
  97. else
  98. echo "$tool found in \"$vs_tool_path\"" >&2
  99. fi
  100. done
  101. if [ $missing_tool -eq 0 ]; then
  102. echo >&2
  103. echo "All required tools found in \"$vs_tool_path\"" >&2
  104. echo >&2
  105. echo "$vs_tool_path"
  106. return 0
  107. else
  108. echo "WARNING: skipping edition '$edition', directory does not exist." >&2
  109. fi
  110. else
  111. echo "WARNING: skipping edition '$edition', MSVC directory does not exist." >&2
  112. fi
  113. else
  114. echo "WARNING: skipping edition '$edition', directory does not exist." >&2
  115. fi
  116. done
  117. echo "ERROR: No valid Visual Studio editions found in \"$studio_base_path\"." >&2
  118. echo "$system_root"
  119. return 1
  120. }
  121. # Parse options using getopt
  122. TEMP=$(getopt -o svwh --long sdk,visualstudio,windows,help -- "$@")
  123. if [ $? != 0 ]; then
  124. echo "ERROR: Invalid options provided." >&2
  125. exit 1
  126. fi
  127. eval set -- "$TEMP"
  128. search_mode="sdk"
  129. windows_format=0
  130. system_root="/usr/bin"
  131. # Process getopt options
  132. while true; do
  133. case "$1" in
  134. -s|--sdk)
  135. search_mode="sdk"
  136. shift
  137. ;;
  138. -v|--visualstudio)
  139. search_mode="visualstudio"
  140. shift
  141. ;;
  142. -w|--windows)
  143. system_root="%SYSTEMROOT%"
  144. windows_format=1
  145. shift
  146. ;;
  147. --help|-h)
  148. display_help
  149. ;;
  150. --)
  151. shift
  152. break
  153. ;;
  154. *)
  155. echo "ERROR: Invalid option: $1" >&2
  156. exit 1
  157. ;;
  158. esac
  159. done
  160. # Ensure that one of --sdk or --visualstudio is selected
  161. if [ -z "$search_mode" ]; then
  162. echo "ERROR: You must specify either --sdk or --visualstudio." >&2
  163. display_help
  164. fi
  165. # Determine which function to call based on the search mode
  166. if [ "$search_mode" = "sdk" ]; then
  167. tool_path=$(find_sdk_tools)
  168. else
  169. tool_path=$(find_visual_studio_tools)
  170. fi
  171. # If a valid path is found, output it
  172. if [ "$tool_path" != "$system_root" ]; then
  173. if [ "$windows_format" -eq 1 ]; then
  174. windows_tool_path=$(convert_to_windows_format "$tool_path")
  175. echo "$windows_tool_path"
  176. else
  177. echo "$tool_path"
  178. fi
  179. else
  180. echo "$system_root"
  181. exit 1
  182. fi
  183. exit 0