-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-wine-prefix
executable file
·266 lines (232 loc) · 10.5 KB
/
create-wine-prefix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/sh
set -o errexit
set -o nounset
#placeholders for template files
PLACEHOLDER_EXECUTABLE="__EXECUTABLE__";
PLACEHOLDER_WINEPREFIX="__WINEPREFIX__";
PLACEHOLDER_WINEDLLOVERRIDES="__WINEDLLOVERRIDES__";
PLACEHOLDER_WINEARCH="__WINEARCH__";
PLACEHOLDER_INCLUDE_DEFAULT_PATHS="__INCLUDE_DEFAULT_PATHS__";
PLACEHOLDER_LIB_DEFAULT_PATHS="__LIB_DEFAULT_PATHS__";
DEFAULT_BIN_DIR="$HOME/bin";
DEFAULT_WINEPREFIX="$HOME/.wine-cl";
help(){
printf '%s\n\n' "${0##*/}";
printf '\t--help prints this message\n';
printf '\t--bin directory where to "install" wrapper scripts, default is "%s"\n' "$DEFAULT_BIN_DIR";
printf '\t--vs directory where to search for cl.exe and all other compontents\n';
printf '\t--wine-prefix wine prefix to use, default is "%s". Is also set with WINEPREFIX environment variable\n' "$DEFAULT_WINEPREFIX";
printf '\t--suffix append suffix to generated executables, useful if testing multiple versions of cl.exe\n';
printf '\t--no-copy do not copy vs directory in wine prefix, useful if you have multiple users and do not want to create a copy for everyone.\n';
printf '\t--do-not-adapt-wine do not modify prefix (disable desktop integration, use real tmp folder, read-onl, ...)\n'
printf '\t--arch x86 and x64 are valid values\n'
printf '\nEnvironment variables:\n';
printf '\tWINEPREFIX can be used instead of --wine-prefix, if both are present, the command line parameter takes precedence\n'
}
check_value() {
if [ "$1" -lt 2 ]; then :;
printf 'Missing value for %s\n\n' "$2"; help; exit 1;
fi
}
append_win0_paths(){
sort -z | uniq -z | xargs -0 winepath -0 --windows | sed -e 's|\\|\\\\|g' | xargs --no-run-if-empty -0 printf ';%s';
}
main() {
[ $# -eq 0 ] && set -- "$@" '?';
BIN_DIR="$DEFAULT_BIN_DIR";
if [ -n "${WINEPREFIX+x}" ]; then :;
# do nothing, take global variable
else :;
WINEPREFIX="$DEFAULT_WINEPREFIX";
fi
COPY_VS=true;
ADAPT_WINE=true;
SUFFIX="";
ARCH="x64";
while test "$#" -gt 0; do :;
key="$1";
case "$key" in
-h|--help|h|help|\?)
help; exit 0;
;;
--bin)
check_value "$#" "$key"; BIN_DIR="$2"; shift;
;;
--wine-prefix)
check_value "$#" "$key"; WINEPREFIX="$2"; shift;
;;
--vs)
check_value "$#" "$key"; VS_DIR="$2"; shift;
;;
--suffix)
check_value "$#" "$key"; SUFFIX="$2"; shift;
;;
--arch)
check_value "$#" "$key"; ARCH="$2"; shift;
;;
--no-copy)
COPY_VS=false;
;;
--do-not-adapt-wine)
ADAPT_WINE=false;
;;
*)
{ printf 'Unrecognized value: %s\n\n' "$key"; help; } >&2;
exit 1;
;;
esac
shift;
done
if [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ]; then :;
printf 'Only x86 and x64 are supported architectures\n' >&2;
elif [ "$ARCH" = "x86" ] ; then :;
export WINEARCH=win32;
else :;
export WINEARCH=win64; # FIXME: if system ix x86, trigger error with ARCH = x64
fi
export WINEPREFIX;
#export WINEDEBUG=-all;
WINEDLLOVERRIDES="mscoree,mshtml=";
if $ADAPT_WINE; then :;
# https://www.winehq.org/pipermail/wine-bugs/2012-July/329872.html
# https://wiki.archlinux.org/index.php/Wine_package_guidelines#Gecko_and_Mono
export WINEDLLOVERRIDES;
env -u DISPLAY WINEDEBUG=-all wine regedit env.reg;
find "$WINEPREFIX" -type d -iname temp -exec rmdir {} + ;
find "$WINEPREFIX" -type d -iname tmp -exec rmdir {} + ;
#rm -rf "$WINEPREFIX/drive_c/users" || rm -rf "$WINEPREFIX/drive_c/users"
if ! command -v hardlink > /dev/null 2>&1; then :;
hardlink --ignore-mode --ignore-owner --ignore-time "$WINEPREFIX" >/dev/null;
fi
fi
env -u DISPLAY WINEDEBUG=-all wine wineboot;
# https://askubuntu.com/questions/338758/how-to-quickly-extract-all-kinds-of-archived-files-from-command-line
# FIXME: in case of an archive, extract it, and override msvc_dir
microsoft_visual_studio="$(find "$VS_DIR" -iname 'Microsoft Visual Studio' -type d -print -quit)"
windows_kits="$(find "$VS_DIR" -iname 'Windows Kits' -not -ipath '*Microsoft SDKs*' -type d -print -quit)"
progfiles="$WINEPREFIX/drive_c/Program Files (x86)/";
if [ ! -d "$progfiles" ]; then :;
progfiles="$WINEPREFIX/drive_c/Program Files/";
fi
[ -d "$progfiles" ] || { printf "%s ist not a directory" "$progfiles" >&2; exit 1; }
if $COPY_VS; then :;
cp -r "$windows_kits" --target-directory "$progfiles"
cp -r "$microsoft_visual_studio" --target-directory "$progfiles"
msvc_dir="$progfiles/Microsoft Visual Studio"; # fixme, should use old name as find was not case sensitive
winsdk_dir="$progfiles/Windows Kits";
else
msvc_dir="$microsoft_visual_studio";
winsdk_dir="$windows_kits";
fi
if $ADAPT_WINE; then :;
# no write, only read+exec
# enusres no accumulating of tmp files, registry is excluded because wineserver writes to it
fi
CL="$(find "$msvc_dir" -iname cl.exe -type f -ipath "*Host$ARCH/$ARCH*" -ipath '*bin*' -print -quit)";
# Telemetry event upload failed: 'Failed to open connection to VCTIP' -> maybe remove file?, test with verbose linker
LINK="$(find "$msvc_dir" -iname link.exe -type f -ipath "*Host$ARCH/$ARCH*" -ipath '*bin*' -print -quit)";
LIB="$(find "$msvc_dir" -iname lib.exe -type f -ipath "*Host$ARCH/$ARCH*" -ipath '*bin*' -print -quit)";
RC="$(find "$winsdk_dir" -iname rc.exe -type f -ipath "*/$ARCH/*" -ipath '*bin*' -print -quit)";
# recheck: link.exe tries to execute rc.exe and has problems if it is not in path or same folder of link.exe -> copy rc.exe and rcdll.dll in folder of link.exe
MT="$(find "$winsdk_dir" -iname mt.exe -type f -ipath "*/$ARCH/*" -ipath '*bin*' -print -quit)";
MC="$(find "$winsdk_dir" -iname mc.exe -type f -ipath "*/$ARCH/*" -ipath '*bin*' -print -quit)";
#optional
DUMPBIN="$(find "$msvc_dir" -iname dumpbin.exe -type f -ipath "*Host$ARCH/$ARCH*" -ipath '*bin*' -print -quit)";
INCLUDE_DEFAULT_PATHS="";
LIB_DEFAULT_PATHS="";
include_path="$(find "$msvc_dir" -type f -ipath '*include*' -not -ipath '*onecore*' \( -iname 'afxwin.h' -or -iname 'cstdio' -or -iname 'winapifamily.h' \) -printf '%h\0' | append_win0_paths )";
INCLUDE_DEFAULT_PATHS="$INCLUDE_DEFAULT_PATHS${include_path:+$include_path}";
lib_path="$(find "$msvc_dir" -type f -ipath '*lib*' -ipath "*/$ARCH/*" -not -ipath '*onecore*' \( -iname 'libcmt.lib' -or -iname 'mfc140.lib' \) -printf '%h\0' | append_win0_paths )";
LIB_DEFAULT_PATHS="$LIB_DEFAULT_PATHS${lib_path:+$lib_path}";
include_path="$(find "$winsdk_dir" -type f -ipath '*include*' -not -ipath '*onecore*' \( -iname 'corecrt.h' -or -iname 'windows.h' -or -iname 'winapifamily.h' \) -printf '%h\0' | append_win0_paths )";
INCLUDE_DEFAULT_PATHS="$INCLUDE_DEFAULT_PATHS${include_path:+$include_path}";
lib_path="$(find "$winsdk_dir" -type f -ipath '*lib*' -ipath "*/$ARCH/*" -not -ipath '*onecore*' \( -iname 'kernel32.lib' -or -iname 'libucrt.lib' \) -printf '%h\0' | append_win0_paths )";
LIB_DEFAULT_PATHS="$LIB_DEFAULT_PATHS${lib_path:+$lib_path}";
winecl="$BIN_DIR/wine-cl$SUFFIX"
cp "wine-cl-template" "$winecl";
ex \
-sc "%s#${PLACEHOLDER_WINEPREFIX}#${WINEPREFIX}#g" \
-sc "%s#${PLACEHOLDER_WINEDLLOVERRIDES}#${WINEDLLOVERRIDES}#g" \
-sc "%s#${PLACEHOLDER_WINEARCH}#${WINEARCH}#g" \
-sc "%s#${PLACEHOLDER_EXECUTABLE}#${CL}#g" \
-sc "%s#${PLACEHOLDER_INCLUDE_DEFAULT_PATHS}#$INCLUDE_DEFAULT_PATHS#g" \
-sc "%s#${PLACEHOLDER_LIB_DEFAULT_PATHS}#$LIB_DEFAULT_PATHS#g" \
-cx "$winecl";
chmod +x "$winecl";
winelink="$BIN_DIR/wine-link$SUFFIX"
cp "wine-link-template" "$winelink";
ex \
-sc "%s#${PLACEHOLDER_WINEPREFIX}#${WINEPREFIX}#g" \
-sc "%s#${PLACEHOLDER_WINEDLLOVERRIDES}#${WINEDLLOVERRIDES}#g" \
-sc "%s#${PLACEHOLDER_WINEARCH}#${WINEARCH}#g" \
-sc "%s#${PLACEHOLDER_EXECUTABLE}#${LINK}#g" \
-sc "%s#${PLACEHOLDER_INCLUDE_DEFAULT_PATHS}#$INCLUDE_DEFAULT_PATHS#g" \
-sc "%s#${PLACEHOLDER_LIB_DEFAULT_PATHS}#$LIB_DEFAULT_PATHS#g" \
-cx "$winelink"
chmod +x "$winelink";
winelib="$BIN_DIR/wine-lib$SUFFIX"
cp "wine-lib-template" "$winelib";
ex \
-sc "%s#${PLACEHOLDER_WINEPREFIX}#${WINEPREFIX}#g" \
-sc "%s#${PLACEHOLDER_WINEDLLOVERRIDES}#${WINEDLLOVERRIDES}#g" \
-sc "%s#${PLACEHOLDER_WINEARCH}#${WINEARCH}#g" \
-sc "%s#${PLACEHOLDER_EXECUTABLE}#${LIB}#g" \
-sc "%s#${PLACEHOLDER_INCLUDE_DEFAULT_PATHS}#$INCLUDE_DEFAULT_PATHS#g" \
-sc "%s#${PLACEHOLDER_LIB_DEFAULT_PATHS}#$LIB_DEFAULT_PATHS#g" \
-cx "$winelib"
chmod +x "$winelib";
winemt="$BIN_DIR/wine-mt$SUFFIX"
cp "wine-mt-template" "$winemt";
ex \
-sc "%s#${PLACEHOLDER_WINEPREFIX}#${WINEPREFIX}#g" \
-sc "%s#${PLACEHOLDER_WINEDLLOVERRIDES}#${WINEDLLOVERRIDES}#g" \
-sc "%s#${PLACEHOLDER_WINEARCH}#${WINEARCH}#g" \
-sc "%s#${PLACEHOLDER_EXECUTABLE}#${MT}#g" \
-sc "%s#${PLACEHOLDER_INCLUDE_DEFAULT_PATHS}#$INCLUDE_DEFAULT_PATHS#g" \
-sc "%s#${PLACEHOLDER_LIB_DEFAULT_PATHS}#$LIB_DEFAULT_PATHS#g" \
-cx "$winemt"
chmod +x "$winemt";
cp "$winemt" "$BIN_DIR/mt"; # for cmake
winemc="$BIN_DIR/wine-mc$SUFFIX"
cp "wine-mc-template" "$winemc";
ex \
-sc "%s#${PLACEHOLDER_WINEPREFIX}#${WINEPREFIX}#g" \
-sc "%s#${PLACEHOLDER_WINEDLLOVERRIDES}#${WINEDLLOVERRIDES}#g" \
-sc "%s#${PLACEHOLDER_WINEARCH}#${WINEARCH}#g" \
-sc "%s#${PLACEHOLDER_EXECUTABLE}#${MC}#g" \
-sc "%s#${PLACEHOLDER_INCLUDE_DEFAULT_PATHS}#$INCLUDE_DEFAULT_PATHS#g" \
-sc "%s#${PLACEHOLDER_LIB_DEFAULT_PATHS}#$LIB_DEFAULT_PATHS#g" \
-cx "$winemc"
chmod +x "$winemc";
winerc="$BIN_DIR/wine-rc$SUFFIX"
cp "wine-rc-template" "$winerc";
ex \
-sc "%s#${PLACEHOLDER_WINEPREFIX}#${WINEPREFIX}#g" \
-sc "%s#${PLACEHOLDER_WINEDLLOVERRIDES}#${WINEDLLOVERRIDES}#g" \
-sc "%s#${PLACEHOLDER_WINEARCH}#${WINEARCH}#g" \
-sc "%s#${PLACEHOLDER_EXECUTABLE}#${RC}#g" \
-sc "%s#${PLACEHOLDER_INCLUDE_DEFAULT_PATHS}#$INCLUDE_DEFAULT_PATHS#g" \
-sc "%s#${PLACEHOLDER_LIB_DEFAULT_PATHS}#$LIB_DEFAULT_PATHS#g" \
-cx "$winerc"
chmod +x "$winerc";
cp "$winerc" "$BIN_DIR/rc"; # for cmake
winedumpbin="$BIN_DIR/wine-dumpbin$SUFFIX"
cp "wine-dumpbin-template" "$winedumpbin";
ex \
-sc "%s#${PLACEHOLDER_WINEPREFIX}#${WINEPREFIX}#g" \
-sc "%s#${PLACEHOLDER_WINEDLLOVERRIDES}#${WINEDLLOVERRIDES}#g" \
-sc "%s#${PLACEHOLDER_WINEARCH}#${WINEARCH}#g" \
-sc "%s#${PLACEHOLDER_EXECUTABLE}#${DUMPBIN}#g" \
-cx "$winedumpbin";
chmod +x "$winedumpbin";
toolchain="$BIN_DIR/wine-toolchain$SUFFIX"
cp "wine-toolchain-template" "$toolchain";
ex \
-sc "%s#__WINE_CL__#${winecl}#g" \
-sc "%s#__WINE_LINK__#${winelink}#g" \
-sc "%s#__WINE_RC__#${winerc}#g" \
-sc "%s#__WINE_MC__#${winemc}#g" \
-sc "%s#__WINE_MT__#${winemt}#g" \
-cx "$toolchain";
}
main "$@"