Browse code

Update the zig toolchain to crosscompile to windows for SDL3 and SDL3_ttf

Dario Rodriguez authored on 25/10/2024 15:56:57
Showing 1 changed files
... ...
@@ -24,14 +24,25 @@ chmod 755 env.sh
24 24
 # Convenience scripts
25 25
 cat > compile.sh  <<EOF
26 26
 #!/bin/bash
27
-if [ ! -e "\$1" ] || [ "m\$2" = "m" ] ; then
27
+if [ "m\$2" = "m--help" ] ; then
28 28
         echo "Syntax: \$0 source.c -c -o result.o [flags]"
29 29
         exit 1
30 30
 fi
31 31
 . "$parentdir/toolchain-zig/env.sh" >/dev/null
32
-exec zig cc "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/" "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/SDL2" "\$@" -target x86_64-windows-gnu
32
+exec zig cc "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/" "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/w32api/" "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/SDL2" "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/SDL" "\$@" -target x86_64-windows-gnu
33 33
 EOF
34 34
 chmod 755 compile.sh
35
+cat > compile-cpp.sh  <<EOF
36
+#!/bin/bash
37
+if [ "m$2" = "m--help" ] ; then
38
+        echo "Syntax: $0 source.cpp -c -o result.o [flags]"
39
+        exit 1
40
+fi
41
+. "$parentdir/toolchain-zig/env.sh" >/dev/null
42
+exec zig c++ "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/" "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/w32api/" "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/SDL2" "-I$parentdir/toolchain-zig/windows-msys2-mingw/include/SDL" "\$@" -target x86_64-windows-gnu
43
+EOF
44
+chmod 755 compile-cpp.sh
45
+
35 46
 cat > link.sh  <<EOF
36 47
 #!/bin/bash
37 48
 tmpicon=0
... ...
@@ -132,8 +143,13 @@ https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-wineditline-2.205-3-any.
132 143
 https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-xz-5.2.9-1-any.pkg.tar.zst
133 144
 https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-zlib-1.2.13-2-any.pkg.tar.zst
134 145
 https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-zstd-1.5.2-2-any.pkg.tar.zst
146
+https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-winpthreads-git-12.0.0.r351.gcdf6b16b8-1-any.pkg.tar.zst
147
+https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-cmake-3.30.5-1-any.pkg.tar.zst
148
+https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-ninja-1.12.1-1-any.pkg.tar.zst
149
+https://mirror.msys2.org/mingw/mingw64/mingw-w64-x86_64-pkg-config-0.29.2-6-any.pkg.tar.zst
135 150
 https://mirror.msys2.org/msys/x86_64/msys2-w32api-headers-10.0.0.r16.g49a56d453-1-x86_64.pkg.tar.zst
136 151
 https://mirror.msys2.org/msys/x86_64/msys2-w32api-runtime-10.0.0.r16.g49a56d453-1-x86_64.pkg.tar.zst
152
+https://mirror.msys2.org/msys/x86_64/perl-5.38.2-2-x86_64.pkg.tar.zst
137 153
 EOF
138 154
 for i in  $(cat deps.txt) ; do
139 155
         if [ ! -e ../toolchain-zig-dl/$(basename $i) ] ; then
... ...
@@ -167,6 +183,7 @@ i="$1"
167 183
 EOF
168 184
 echo " (cd $l && tar -cf - . | (cd $parentdir/toolchain-zig/windows-msys2-mingw && tar -xvf - ) ) ; done ) ; rm -rf t " >> add-xz.sh
169 185
 chmod a+x add-xz.sh add-zst.sh
186
+cd ..
170 187
 # Check for windres
171 188
 if ! which x86_64-w64-mingw32-windres >/dev/null 2>/dev/null; then
172 189
         echo "Please, install windres manually using your distribution, i.e.:"                
... ...
@@ -176,7 +193,85 @@ if ! which convert >/dev/null 2>/dev/null; then
176 193
         echo "Please, install imagemagick manually using your distribution, i.e.:"                
177 194
         echo "sudo apt-get install imagemagick"
178 195
 fi
179
-
196
+# SDL3 for mingw
197
+mkdir -p sdl3-windows
198
+cat > sdl3-windows/setup.sh <<'SeTuPeOf'
199
+#!/bin/bash
200
+#SDL3
201
+if [ ! -e SDL3-devel-3.1.3-mingw.tar.xz ] ; then
202
+	wget https://github.com/libsdl-org/SDL/releases/download/preview-3.1.3/SDL3-devel-3.1.3-mingw.tar.xz
203
+fi
204
+xz -d < SDL3-devel-3.1.3-mingw.tar.xz | tar -xvf -
205
+cp ./SDL3-3.1.3/x86_64-w64-mingw32/bin/SDL3.dll .
206
+#SDL3_ttf
207
+rm -rf SDL_ttf
208
+if [ ! -e SDL_ttf.tgz ] ; then
209
+	git clone --recurse-submodules -j4 https://github.com/libsdl-org/SDL_ttf.git
210
+	tar -cf SDL_ttf.tgz SDL_ttf
211
+else
212
+	tar -xf SDL_ttf.tgz
213
+fi
214
+cd SDL_ttf
215
+ln -s ~/Programacion/sources/boxui/toolchain-zig/compile.sh x86_64-w64-mingw32-gcc
216
+ln -s ~/Programacion/sources/boxui/toolchain-zig/compile-cpp.sh x86_64-w64-mingw32-g++
217
+mkdir -p build/external/freetype/include
218
+cat >> build/external/freetype/include/windows.h <<'EOF'
219
+#define	VS_FF_DEBUG	0x01L
220
+#define	VFT_DLL	0x00000002L
221
+#define	VFT_STATIC_LIB	0x00000007L
222
+#define	VS_FF_DEBUG	0x01L
223
+#define	VS_FFI_FILEFLAGSMASK	0x0000003fL
224
+EOF
225
+cat >> build/external/freetype/include/winresrc.h <<'EOF'
226
+#define LANG_ENGLISH    0x09
227
+#define SUBLANG_ENGLISH_US      0x01
228
+EOF
229
+cat >> freetype_Use_SDL_StepBackUTF8.patch <<'EOF'
230
+diff --git a/examples/editbox.c b/examples/editbox.c
231
+index f96d5087..b2028178 100644
232
+--- a/examples/editbox.c
233
+@@ -633,19 +633,14 @@ void EditBox_Backspace(EditBox *edit)
234
+         return;
235
+     }
236
+ 
237
+-    const char *start = &edit->text->text[edit->cursor];
238
+-    const char *current = start;
239
+-    /* Step back over the previous UTF-8 character */
240
+-    do {
241
+-        if (current == edit->text->text) {
242
+-            break;
243
+-        }
244
+-        --current;
245
+-    } while ((*current & 0xC0) == 0x80);
246
+-
247
+-    int length = (int)(start - current);
248
+-    TTF_DeleteTextString(edit->text, edit->cursor - length, length);
249
+-    edit->cursor -= length;
250
++    if (edit->cursor > 0) {
251
++        const char *start = &edit->text->text[edit->cursor];
252
++        const char *next = start;
253
++        SDL_StepBackUTF8(edit->text->text, &next);
254
++        int length = (int)(uintptr_t)(start - next);
255
++        TTF_DeleteTextString(edit->text, edit->cursor - length, length);
256
++        edit->cursor -= length;
257
++    }
258
+ }
259
+EOF
260
+# revert patch https://discourse.libsdl.org/t/sdl-ttf-use-sdl-stepbackutf8/54854
261
+( patch -R -p1 < ./freetype_Use_SDL_StepBackUTF8.patch )
262
+sed -i "s/vendored_default FALSE/vendored_default TRUE/g" CMakeLists.txt
263
+PATH=$(pwd):$PATH cmake -S . -B build -DBUILD_SHARED_LIBS=ON -DCMAKE_TOOLCHAIN_FILE=build-scripts/cmake-toolchain-mingw64-x86_64.cmake -DCMAKE_MAKE_PROGRAM=make -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DSDL3_DIR=/home/bit/Programacion/sources/boxui/sdl3-windows/SDL3-3.1.3/x86_64-w64-mingw32/lib/cmake/SDL3/ && cmake --build build
264
+cd ..
265
+cp SDL_ttf/build/SDL3_ttf.dll .
266
+cp SDL_ttf/build/libSDL3_ttf.dll.a .
267
+cp SDL_ttf/build/external/freetype/libfreetype.a .
268
+SeTuPeOf
269
+cd sdl3-windows
270
+chmod a+x setup.sh
271
+./setup.sh
272
+cd ..
273
+# Success message
180 274
 echo ""
181 275
 echo "Installation complete."
182 276
 echo "To use the zig toolchain, first include env.sh with \". toolchain-zig/env.sh\""