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