-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompile-mingw32.sh
More file actions
344 lines (293 loc) · 10.2 KB
/
compile-mingw32.sh
File metadata and controls
344 lines (293 loc) · 10.2 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
set -e
##################
# Configurations #
##################
BINUTILS_VER=2.40
GCC_VER=13.1.0
MINGW_VER=12.0.0
THREADS="--enable-threads=win32"
TC_ARCH="win32"
BUILD_DIR="/opt/$TC_ARCH"
PREFIX="$BUILD_DIR"
TARGET="i586-w64-mingw32" # Pentium baseline (safe for Win95/NT4)
I686_TARGET="i686-w64-mingw32" # i686 overlay for 200/XP+
JOBS="$(nproc)"
SOURCE="src-$TC_ARCH"
###########################################################################
# Default to NT4/Win95 headersl override per-project of you need 2000/XP+ #
###########################################################################
CFLAGS+="-DWINVER=0x0400 -D_WIN32_WINNT=0x0400"
CXXFLAGS+="$CFLAGS"
#########################
# URLs of sources files #
#########################
BINUTILS_URL="https://gnu.c3sl.ufpr.br/ftp/binutils/binutils-${BINUTILS_VER}.tar.xz"
GCC_URL="https://gnu.c3sl.ufpr.br/ftp/gcc/gcc-13.1.0/gcc-${GCC_VER}.tar.xz"
MINGW_URL="https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v${MINGW_VER}.tar.bz2/download"
##################################################
# Generic function to download and extract files #
##################################################
download_and_extract() {
if [ "$#" -ne 3 ]; then
echo "Use: download_and_extract <url> <archive> <extraction-directory>"
return 1
fi
local url="$1"
local archive="$2"
local extract_dir="$3"
local lockfile="${archive}.lock"
# If lockfile exists and extract_dir exists, skip all
if [ -f "$lockfile" ] && [ -d "$extract_dir" ]; then
echo "Lockfile '$lockfile' and directory '$extract_dir' exist. Skipping download and extraction."
return 0
fi
# If lockfile exists but archive is missing (user deleted archive manually)
if [ -f "$lockfile" ] && [ ! -f "$archive" ]; then
echo "Lockfile exists but archive '$archive' missing. Removing stale lockfile."
rm -f "$lockfile"
fi
# Function to test archive integrity for .xz compressed tarballs
test_archive() {
if [[ "$archive" == *.tar.xz ]]; then
xz -t "$archive" 2>/dev/null
return $?
else
# Could add tests for other archive types if needed
return 0
fi
}
# If archive doesn't exist, download and create lockfile
if [ ! -f "$archive" ]; then
echo "Archive '$archive' not found. Downloading..."
wget -c "$url" -O "$archive" || { echo "Download error, check the script!"; exit 1; }
echo "$$" > "$lockfile" || { echo "Failed to create lockfile."; exit 1; }
if [ -d "$extract_dir" ]; then
echo "Directory '$extract_dir' exists. Removing before extraction..."
rm -rf "$extract_dir" || { echo "Failed to remove directory '$extract_dir'"; exit 1; }
fi
echo "Extracting '$archive'..."
tar xf "$archive" || { echo "Extraction error for file '$archive'. Possibly corrupted."; exit 1; }
return 0
fi
# Archive exists but no lockfile, test archive integrity
if [ ! -f "$lockfile" ]; then
echo "Lockfile missing but archive '$archive' exists. Testing archive integrity..."
if ! test_archive; then
echo "Archive '$archive' corrupted or incomplete. Re-downloading..."
wget -c "$url" -O "$archive" || { echo "Download error, check the script!"; exit 1; }
fi
# After download or if test passed, recreate lockfile
echo "$$" > "$lockfile" || { echo "Failed to create lockfile."; exit 1; }
if [ -d "$extract_dir" ]; then
echo "Directory '$extract_dir' exists. Removing before extraction..."
rm -rf "$extract_dir" || { echo "Failed to remove directory '$extract_dir'"; exit 1; }
fi
echo "Extracting '$archive'..."
tar xf "$archive" || { echo "Extraction error for file '$archive'. Possibly corrupted."; exit 1; }
return 0
fi
# If here, lockfile missing, archive missing — just in case (shouldn't happen)
echo "Unexpected state. Please check manually."
return 1
}
###############################
# Download and Extract source #
###############################
mkdir -p ~/$SOURCE && cd ~/$SOURCE
echo "Binutils $BINUTILS_VER"
download_and_extract "$BINUTILS_URL" "binutils-${BINUTILS_VER}.tar.xz" "binutils-${BINUTILS_VER}"
echo "GCC $GCC_VER"
download_and_extract "$GCC_URL" "gcc-${GCC_VER}.tar.xz" "gcc-${GCC_VER}"
echo "mingw-w64 $MINGW_VER"
download_and_extract "$MINGW_URL" "mingw-w64v-${MINGW_VER}.tar.bz2" "mingw-w64-v${MINGW_VER}"
HOST=$(~/$SOURCE/gcc-13.1.0/config.guess)
###################################
# Built and install Binutils 2.40 #
###################################
echo "Starting Binutils build..."
cd binutils-${BINUTILS_VER}
mkdir -p build && cd build
../configure \
--target=$TARGET \
--host=$HOST \
--prefix=$PREFIX \
--with-sysroot=$PREFIX \
--disable-multilib \
--disable-nls
make -j$JOBS
make install
##################################
# Install Mingw-w64 Headers Only #
##################################
echo "Starting Mingw Headers Install"
cd ~/$SOURCE/mingw-w64-v${MINGW_VER}/mingw-w64-headers
mkdir -p build && cd build
################################################
# Configure with the $Prefix/$Target in Prefix #
# and --host=$TARGET in -host #
# as per Mingw/GCC documentation for Toolchain #
################################################
../configure \
--prefix=$PREFIX/$TARGET \
--host=$TARGET \
--with-sysroot=$PREFIX \
--enable-sdk=all \
--enable-idl \
--with-default-msvcrt=msvcrt
make install
echo "Creating Symlinks"
ln -sf $PREFIX/$TARGET $PREFIX/mingw
ln -sf $PREFIX/$TARGET/lib $PREFIX/$TARGET/lib64
echo ""
echo "Now mingw and lib64 is symlinked"
echo "to correct paths accepted by GCC"
echo ""
echo "Refreshing PATH" #note: this change is valid during the script use only
PATH=$PATH:$PREFIX/bin:$PREFIX/$TARGET/bin
echo ""
echo "Verify if PATH is the correct one - desired output"
echo "is $PREFIX/bin:$PREFIX/$TARGET/bin:/usr/bin etc"
echo "Building PATH is -> $PATH"
echo ""
echo "if PATH is empty or wrong, abort script and check"
echo ""
#read -p "Press Enter to continue..."
##################################################
# Built GCC Phase 1 (Only compiler without libs) #
##################################################
echo "Preparing GCC ${GCC_VER} to build"
cd ~/$SOURCE/gcc-${GCC_VER}
# Download GCC Pre Requisites
echo "Downloading GCC Pre Requisites"
./contrib/download_prerequisites
mkdir -p build-phase-1 && cd build-phase-1
##########################################
# Configure GGC for Phase 1 #
# as per GCC documentation for Toolchain #
##########################################
echo "Building GCC ${GCC_VERSION} with C only"
../configure \
--target=$TARGET \
--host=$HOST \
--prefix=$PREFIX \
--with-sysroot=$PREFIX \
--disable-multilib \
--enable-languages=c \
--disable-nls \
--without-headers \
--with-default-msvcrt=msvcrt \
--disable-shared \
--with-arch=pentium \
--with-tune=generic \
--enable-sjlj-exceptions \
$THREADS
make all-gcc -j$JOBS
make install-gcc
###############################################
# Built and Install Mingw-w64 CRT with MSVCRT #
###############################################
echo "Building mingw-w64 CRT with MSVCRT"
cd ~/$SOURCE/mingw-w64-v${MINGW_VER}/mingw-w64-crt
mkdir -p build && cd build
################################################
# Configure with the $Prefix/$Target in Prefix #
# and --host=$TARGET in -host #
# as per Mingw/GCC documentation for Toolchain #
################################################
../configure \
--host=$TARGET \
--prefix=$PREFIX/$TARGET \
--with-sysroot=$PREFIX \
--with-default-msvcrt=msvcrt
make -j$JOBS
make install
############################################
# Built GCC Phase 2 (Continues the Phase 1 #
############################################
echo "Building GCC ${GCC_VER} Phase 2"
cd ~/$SOURCE/gcc-13.1.0
mkdir -p build-phase-2 && cd build-phase-2
../configure \
--target=$TARGET \
--host=$HOST \
--prefix=$PREFIX \
--with-sysroot=$PREFIX \
--disable-multilib \
--enable-languages=c,c++ \
--disable-nls \
--disable-shared \
--with-arch=pentium \
--with-tune=generic \
--enable-sjlj-exceptions \
$THREADS
##########################################
# Configure GGC for Phase 2 #
# as per GCC documentation for Toolchain #
##########################################
make -j$JOBS
make install
#################################
# Building Mingw-W64 Tools like #
# Gendef #
# Genidl #
# Genpeigm #
# Widl #
#################################
echo "Building Mingw-w64 tools"
build_tool() {
local tool=$1
echo "Building $tool"
cd ~/$SOURCE/mingw-w64-v12.0.0/mingw-w64-tools/$tool
if [ "$tool" == "widl" ]; then
./configure --target=$TARGET --prefix=$PREFIX --bindir=$PREFIX/bin
else
./configure
fi
make -j$JOBS
make install
}
build_tool gendef
build_tool genidl
build_tool genpeimg
build_tool widl
#########################
# Optional i686 overlay #
#########################
################################################
# Build and install Binutils 2.40 i686 overlay #
################################################
echo "Starting Binutils i686 build..."
cd ~/$SOURCE/binutils-${BINUTILS_VER}
mkdir -p build-i686 && cd build-i686
../configure \
--target=$I686_TARGET \
--host=$HOST \
--prefix=$PREFIX \
--disable-multilib \
--disable-nls
make -j$JOBS
make install
#############################################
# Build and install GCC 13.1.0 i686 overlay #
#############################################
echo "Starting GCC ${GCC_VER} i686 build ..."
cd ~/$SOURCE/gcc-${GCC_VER}
mkdir build-i686 && cd build-i686
../configure \
--target=$I686_TARGET \
--host=$HOST \
--prefix=$PREFIX \
--with-sysroot=$PREFIX \
--disable-multilib \
--enable-languages=c,c++ \
--disable-nls \
--disable-shared \
--with-arch=pentiumpro \
--with-tune=generic \
--enable-sjlj-exceptions \
$THREADS
make all-gcc -j$JOBS
make install-gcc
echo "Built all done! Toolchain is located in $TARGET"
echo "Don't forget to add the $TARGET in PATH to use"