Browse code

Simplify compiling for windows in the Makefile example (using toolchain-zig/compile.sh and toolchain-zig/link.sh ).

Dario Rodriguez authored on 02/07/2023 14:41:21
Showing 2 changed files
... ...
@@ -12,18 +12,6 @@ notosans_regular_ttf.c: fonts/NotoSans-Regular.ttf
12 12
 notosans_regular_ttf.h: fonts/NotoSans-Regular.ttf
13 13
 	(cd fonts && xxd -i NotoSans-Regular.ttf | sed "s/^\(.*[^ ]\) *=.*/extern \1\;/g" | grep extern | tr A-Z a-z ) > notosans_regular_ttf.h
14 14
 
15
-
16
-# windows build icon
17
-
18
-boxui-icon.ico: boxui-icon_256x256.png
19
-	convert boxui-icon_256x256.png boxui-icon.ico
20
-
21
-boxui-icon.rc: boxui-icon.ico
22
-	echo "1 ICON \"boxui-icon.ico\"" > boxui-icon.rc
23
-
24
-boxui-icon.o: boxui-icon.rc boxui-icon.ico
25
-	x86_64-w64-mingw32-windres --preprocessor /usr/bin/cpp boxui-icon.rc boxui-icon.o
26
-
27 15
 # boxui.o: linux
28 16
 
29 17
 boxui.o: boxui.c boxui.h notosans_regular_ttf.c notosans_regular_ttf.h
... ...
@@ -31,7 +19,7 @@ boxui.o: boxui.c boxui.h notosans_regular_ttf.c notosans_regular_ttf.h
31 19
 # boxui-windows.o: windows
32 20
 
33 21
 boxui-windows.o: boxui.c boxui.h notosans_regular_ttf.c notosans_regular_ttf.h
34
-	sh -c ". toolchain-zig/env.sh >/dev/null ; zig cc -I./toolchain-zig/windows-msys2-mingw/include/ -I./toolchain-zig/windows-msys2-mingw/include/SDL2 boxui.c -c -o boxui-windows.o -target x86_64-windows-gnu"
22
+	toolchain-zig/compile.sh boxui.c -c -o boxui-windows.o
35 23
 
36 24
 # boxui-html.o: html5
37 25
 
... ...
@@ -47,10 +35,10 @@ boxui_demo_helloworld: boxui.o boxui_demo_helloworld.o
47 35
 # boxui_demo_helloworld: windows
48 36
 
49 37
 boxui_demo_helloworld-windows.o: boxui_demo_helloworld.c boxui.h
50
-	sh -c ". toolchain-zig/env.sh >/dev/null ; zig cc -I./toolchain-zig/windows-msys2-mingw/include/ -I./toolchain-zig/windows-msys2-mingw/include/SDL2 boxui_demo_helloworld.c -c -o boxui_demo_helloworld-windows.o -target x86_64-windows-gnu"
38
+	toolchain-zig/compile.sh boxui_demo_helloworld.c -c -o boxui_demo_helloworld-windows.o
51 39
 
52
-boxui_demo_helloworld.exe: boxui-icon.o boxui-windows.o boxui_demo_helloworld-windows.o
53
-	sh -c ". toolchain-zig/env.sh >/dev/null ; zig build-exe --subsystem windows boxui_demo_helloworld-windows.o boxui-windows.o boxui-icon.o -Ltoolchain-zig/windows-msys2-mingw/lib/ -Ltoolchain-zig/windows-msys2-mingw/lib/w32api -lSDL2 -lSDL2main -lSDL2_ttf -lfreetype -lgdi32 -lwinmm -lsetupapi -lmincore -lwindowsapp -limm32 -lversion -lgraphite2 -lpng -lbrotlidec -lbrotlicommon -lharfbuzz -lstdc++ -lbz2 -lz --name boxui_demo_helloworld -target x86_64-windows-gnu && rm boxui_demo_helloworld.lib boxui_demo_helloworld.pdb"
40
+boxui_demo_helloworld.exe: boxui-icon_256x256.png boxui-windows.o boxui_demo_helloworld-windows.o
41
+	toolchain-zig/link.sh -icon boxui-icon_256x256.png boxui_demo_helloworld.exe boxui-windows.o boxui_demo_helloworld-windows.o
54 42
 
55 43
 # boxui_demo_helloworld: html5
56 44
 
... ...
@@ -21,6 +21,47 @@ echo "Reminder: this script has to be used with source (.) instead of exec"
21 21
 EOF
22 22
 echo "export PATH=\$PATH:$parentdir/toolchain-zig/zig-$zigversion" >> env.sh
23 23
 chmod 755 env.sh
24
+# Convenience scripts
25
+cat > compile.sh  <<EOF
26
+#!/bin/bash
27
+if [ ! -e "\$1" ] || [ "m\$2" = "m" ] ; then
28
+        echo "Syntax: \$0 source.c -c -o result.o [flags]"
29
+        exit 1
30
+fi
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
33
+EOF
34
+chmod 755 compile.sh
35
+cat > link.sh  <<EOF
36
+#!/bin/bash
37
+tmpicon=0
38
+if [ "m\$1" = "m-icon" ] ; then
39
+        shift
40
+        icon="\$1"
41
+        shift
42
+else
43
+        tmpicon=1
44
+        icon=\$(mktemp -p . --suffix=.png)
45
+        convert -size 256x256 xc:white \$icon
46
+fi
47
+if [ "\${icon/.png/}" = "\$icon" ] || [ "\${1/.exe/}" = "\$1" ] || [ ! -e "\$2" ] ; then
48
+        echo "Syntax: \$0 [-icon myicon.png] result.exe source1.o [source2.o [...]"
49
+        if [ \$tmpicon -eq 1 ] ; then rm "\$icon" ; fi
50
+        exit 1
51
+fi
52
+. "$parentdir/toolchain-zig/env.sh" >/dev/null
53
+name="\${1/.exe/}"
54
+shift
55
+convert "\$icon" "\${icon/.png/-buildiconobject.ico}" && \
56
+ echo "1 ICON \"\${icon/.png/-buildiconobject.ico}\"" > "\${icon/.png/-buildiconobject.rc}" && \
57
+ x86_64-w64-mingw32-windres --preprocessor /usr/bin/cpp "\${icon/.png/-buildiconobject.rc}" "\${icon/.png/-buildiconobject.o}" && \
58
+ zig build-exe --subsystem windows "\$@" \${icon/.png/-buildiconobject.o} -L$parentdir/toolchain-zig/windows-msys2-mingw/lib/ -L$parentdir/toolchain-zig/windows-msys2-mingw/lib/w32api -lSDL2 -lSDL2main -lSDL2_ttf -lfreetype -lgdi32 -lwinmm -lsetupapi -lmincore -lwindowsapp -limm32 -lversion -lgraphite2 -lpng -lbrotlidec -lbrotlicommon -lharfbuzz -lstdc++ -lbz2 -lz --name \$name -target x86_64-windows-gnu && rm \${name}.lib \${name}.pdb
59
+res=\$?
60
+rm "\${icon/.png/-buildiconobject.ico}" "\${icon/.png/-buildiconobject.rc}" "\${icon/.png/-buildiconobject.o}"
61
+if [ \$tmpicon -eq 1 ] ; then rm "\$icon" ; fi
62
+exit \$res
63
+EOF
64
+chmod 755 link.sh
24 65
 # msys2 install
25 66
 mkdir -p windows-msys2-mingw
26 67
 cat > deps.txt <<'EOF'
... ...
@@ -139,3 +180,4 @@ fi
139 180
 echo ""
140 181
 echo "Installation complete."
141 182
 echo "To use the zig toolchain, first include env.sh with \". toolchain-zig/env.sh\""
183
+echo "( alternative: use toolchain-zig/compile.sh and toolchain-zig/link.sh )"