Replace utils_make_human_readable_str() with g_format_size()
[geany-mirror.git] / scripts / gtk-bundle-from-msys2.sh
blob40c865ab8f1a8c413d928bd5a03146dc9b645180
1 #!/bin/bash
3 # Fetch and extract Geany dependencies for Windows/MSYS2
4 # This script will download (or use Pacman's cache) to extract
5 # dependencies as defined below needed to run Geany.
6 # To be run within a MSYS2 shell. The extracted files will be
7 # placed into the current directory.
9 ABI=x86_64 # do not change, i686 is not supported any longer
10 use_cache="no"
11 make_zip="no"
12 gtkv="3"
13 run_pi="y"
14 cross="no"
16 UNX_UTILS_URL="https://download.geany.org/contrib/UnxUpdates.zip"
17 # we use the Prof-Gnome GTK theme from the Geany macOS repository
18 GTK_THEME_URL="https://github.com/geany/geany-osx/archive/refs/heads/master.zip"
20 # Wine commands for 32bit and 64bit binaries (we still need 32bit for UnxUtils sort.exe)
21 # Used only when "-x" is set
22 EXE_WRAPPER_32="mingw-w64-i686-wine"
23 EXE_WRAPPER_64="mingw-w64-x86_64-wine"
25 package_urls=""
26 gtk3_dependency_pkgs=""
27 gtk4_dependency_pkgs=""
29 packages="
30 adwaita-icon-theme
31 atk
32 brotli
33 bzip2
34 cairo
35 expat
36 fontconfig
37 freetype
38 fribidi
39 gcc-libs
40 gdk-pixbuf2
41 gettext
42 glib2
43 graphite2
44 grep
45 gtk-update-icon-cache
46 harfbuzz
47 hicolor-icon-theme
48 libdatrie
49 libepoxy
50 libffi
51 libiconv
52 libpng
53 librsvg
54 libthai
55 libwinpthread-git
56 libxml2
57 pango
58 pcre2
59 pixman
61 zlib
64 handle_command_line_options() {
65 for opt in "$@"; do
66 case "$opt" in
67 "-c"|"--cache")
68 use_cache="yes"
70 "-z"|"--zip")
71 make_zip="yes"
73 "-3")
74 gtkv="3"
76 "-4")
77 gtkv="4"
79 "-n")
80 run_pi=""
82 "-x")
83 cross="yes"
85 "-h"|"--help")
86 echo "gtk-bundle-from-msys2.sh [-c] [-h] [-n] [-z] [-3 | -4] [CACHEDIR]"
87 echo " -c Use pacman cache. Otherwise pacman will download"
88 echo " archive files"
89 echo " -h Show this help screen"
90 echo " -n Do not run post install scripts of the packages"
91 echo " -z Create a zip afterwards"
92 echo " -3 Prefer gtk3"
93 echo " -4 Prefer gtk4"
94 echo " -x Set when the script is executed in a cross-compilation context (e.g. to use wine)"
95 echo "CACHEDIR Directory where to look for cached packages (default: /var/cache/pacman/pkg)"
96 exit 1
99 cachedir="$opt"
101 esac
102 done
105 set -e # stop on errors
106 # enable extended globbing as we need it in _getpkg
107 shopt -s extglob
109 initialize() {
110 if [ -z "$cachedir" ]; then
111 cachedir="/var/cache/pacman/pkg"
114 if [ "$use_cache" = "yes" ] && ! [ -d "$cachedir" ]; then
115 echo "Cache dir \"$cachedir\" not a directory"
116 exit 1
119 if [ "$cross" != "yes" ]; then
120 # if running natively, we do not need wine or any other wrappers
121 EXE_WRAPPER_32=""
122 EXE_WRAPPER_64=""
125 gtk="gtk$gtkv"
126 eval "gtk_dependency_pkgs=\${${gtk}_dependency_pkgs}"
128 pkgs="
129 ${packages}
130 ${gtk_dependency_pkgs}
131 ${gtk}
135 _getpkg() {
136 if [ "$use_cache" = "yes" ]; then
137 package_info=$(pacman -Qi mingw-w64-$ABI-$1)
138 package_version=$(echo "$package_info" | grep "^Version " | cut -d':' -f 2 | tr -d '[[:space:]]')
139 # use @(gz|xz|zst) to filter out signature files (e.g. mingw-w64-x86_64-...-any.pkg.tar.zst.sig)
140 ls $cachedir/mingw-w64-${ABI}-${1}-${package_version}-*.tar.@(gz|xz|zst) | sort -V | tail -n 1
141 else
142 # -dd to ignore dependencies as we listed them already above in $packages and
143 # make pacman ignore its possibly existing cache (otherwise we would get an URL to the cache)
144 pacman -Sddp --cachedir /nonexistent mingw-w64-${ABI}-${1}
148 _remember_package_source() {
149 if [ "$use_cache" = "yes" ]; then
150 package_url=$(pacman -Sddp mingw-w64-${ABI}-${2})
151 else
152 package_url="${1}"
154 package_urls="${package_urls}\n${package_url}"
157 extract_packages() {
158 for i in $pkgs; do
159 pkg=$(_getpkg $i)
160 _remember_package_source $pkg $i
161 if [ "$use_cache" = "yes" ]; then
162 if [ -e "$pkg" ]; then
163 echo "Extracting $pkg from cache"
164 tar xaf $pkg
165 else
166 echo "ERROR: File $pkg not found"
167 exit 1
169 else
170 echo "Download $pkg using curl"
171 filename=$(basename "$pkg")
172 curl --silent --location --output "$filename" "$pkg"
173 tar xf "$filename"
174 rm "$filename"
176 if [ "$make_zip" = "yes" -a "$i" = "$gtk" ]; then
177 VERSION=$(grep ^pkgver .PKGINFO | sed -e 's,^pkgver = ,,' -e 's,-.*$,,')
179 rm -f .INSTALL .MTREE .PKGINFO .BUILDINFO
180 done
183 move_extracted_files() {
184 echo "Move extracted data to destination directory"
185 if [ -d mingw64 ]; then
186 for d in bin etc home include lib locale share var; do
187 if [ -d "mingw64/$d" ]; then
188 rm -rf $d
189 # prevent sporadic 'permission denied' errors on my system, not sure why they happen
190 sleep 0.5
191 mv mingw64/$d .
193 done
194 rmdir mingw64
198 delayed_post_install() {
199 if [ "$run_pi" ]; then
200 echo "Execute delayed post install tasks"
201 # Commands have been collected manually from the various .INSTALL scripts
202 ${EXE_WRAPPER_64} bin/fc-cache.exe -f
203 ${EXE_WRAPPER_64} bin/gdk-pixbuf-query-loaders.exe --update-cache
204 ${EXE_WRAPPER_64} bin/gtk-update-icon-cache-3.0.exe -q -t -f share/icons/hicolor
205 ${EXE_WRAPPER_64} bin/gtk-update-icon-cache-3.0.exe -q -t -f share/icons/Adwaita
206 ${EXE_WRAPPER_64} bin/glib-compile-schemas share/glib-2.0/schemas/
210 cleanup_unnecessary_files() {
211 echo "Cleanup unnecessary files"
212 # cleanup temporary files
213 rm -rf var/cache/fontconfig
214 rmdir var/cache
215 rmdir var
216 # cleanup development and other unnecessary files
217 rm -rf include
218 rm -rf lib/cmake
219 rm -rf lib/gettext
220 rm -rf lib/libffi-*
221 rm -rf lib/pkgconfig
222 rm -rf lib/python3.9
223 find lib -name '*.a' -delete
224 find lib -name '*.typelib' -delete
225 find lib -name '*.def' -delete
226 find lib -name '*.h' -delete
227 find lib -name '*.sh' -delete
228 # cleanup other unnecessary files
229 rm -rf share/aclocal
230 rm -rf share/applications
231 rm -rf share/bash-completion
232 rm -rf share/doc
233 rm -rf share/gdb
234 rm -rf share/gettext
235 rm -rf share/gettext-*
236 rm -rf share/gir-1.0
237 rm -rf share/glib-2.0/codegen
238 rm -rf share/glib-2.0/gdb
239 rm -rf share/glib-2.0/gettext
240 rm -rf share/graphite2
241 rm -rf share/gtk-3.0
242 rm -rf share/gtk-doc
243 rm -rf share/icons/Adwaita/cursors
244 rm -rf share/info
245 rm -rf share/man
246 rm -rf share/thumbnailers
247 rm -rf share/vala
248 rm -rf share/xml
249 rm -rf usr/share/libalpm
250 # cleanup binaries and libs (delete anything except *.dll and GSpawn helper binaries)
251 find bin ! -name '*.dll' ! -name 'grep.exe' ! -name 'gspawn-win32-helper*.exe' -type f -delete
252 # cleanup empty directories
253 find . -type d -empty -delete
256 download_and_extract_sort() {
257 echo "Download and unpack 'sort'"
258 # add sort to the bin directory
259 unxutils_archive="unxutilsupdates.zip"
260 wget --no-verbose -O ${unxutils_archive} ${UNX_UTILS_URL}
261 unzip ${unxutils_archive} sort.exe -d bin/
262 rm ${unxutils_archive}
265 download_and_extract_gtk_theme() {
266 echo "Download and unpack GTK theme 'Prof-Gnome'"
267 geany_osx_archive="geany_osx.zip"
268 wget --no-verbose -O ${geany_osx_archive} ${GTK_THEME_URL}
269 unzip ${geany_osx_archive} "geany-osx-master/Prof-Gnome/**" "geany-osx-master/prof_gnome_windows_changes.patch"
270 mkdir -p share/themes/
271 mv geany-osx-master/Prof-Gnome share/themes/
272 patch --dir share/themes/Prof-Gnome -p2 < geany-osx-master/prof_gnome_windows_changes.patch
273 rm ${geany_osx_archive}
274 rm geany-osx-master/prof_gnome_windows_changes.patch
275 rmdir geany-osx-master
276 # create GTK settings to enable the theme
277 if [ -f "etc/gtk-3.0/settings.ini" ]; then
278 echo "etc/gtk-3.0/settings.ini already exists. Aborting."
279 exit 1
281 echo -e "[Settings]\r\ngtk-theme-name=Prof-Gnome" > etc/gtk-3.0/settings.ini
284 create_bundle_dependency_info_file() {
285 # sort.exe from UnxUtils is a 32bit binary, so use $EXE_WRAPPER_32
286 sort_version="$(${EXE_WRAPPER_32} bin/sort.exe --version | sed -n 1p)"
287 # use "sed -n 1p" instead of "head -n1" as grep will otherwise prints a weird error,
288 # probably because the output pipe is closed prematurely
289 grep_version="$(${EXE_WRAPPER_64} bin/grep.exe --version | sed -n 1p)"
290 filename="ReadMe.Dependencies.Geany.txt"
291 cat << EOF > "${filename}"
292 This installation contains dependencies for Geany which are distributed
293 as binaries (usually .dll files) as well as additional files
294 necessary for these dependencies.
295 Following is a list of all included binary packages with their
296 full download URL as used to create this installation.
298 sort.exe is extracted from the ZIP archive at
299 ${UNX_UTILS_URL}.
300 Sort version: ${sort_version}
302 grep.exe is taken from a 64bit MSYS2 installation and
303 is bundled together with its dependencies.
304 Grep version: ${grep_version}
306 GTK theme "Prof-Gnome" was downloaded from:
307 ${GTK_THEME_URL}
309 Other dependencies are provided by the MSYS2 project
310 (https://msys2.github.io) and were downloaded from:
312 echo -e "${package_urls}" >> "${filename}"
315 create_zip_archive() {
316 if [ "$make_zip" = "yes" ]; then
317 if [ -z "$VERSION" ]; then
318 VERSION="unknown-version"
320 echo "Packing the bundle"
321 zip -r gtk-$VERSION.zip bin etc include lib locale share var
326 # main()
327 handle_command_line_options $@
328 initialize
329 extract_packages
330 move_extracted_files
331 delayed_post_install
332 cleanup_unnecessary_files
333 download_and_extract_sort
334 download_and_extract_gtk_theme
335 create_bundle_dependency_info_file
336 create_zip_archive