]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - library-bundler
macos: also call to hide the gl widget (even if that does not work, at least we ask to)
[xonotic/netradiant.git] / library-bundler
1 #! /usr/bin/env bash
2
3 set -e
4
5 export LANG='C.UTF-8'
6 export LANGUAGE="${LANG}"
7
8 _sed () {
9         case "${system_name}" in
10                 'macos')
11                         gsed "${@}"
12                         ;;
13                 *)
14                         sed "${@}"
15                         ;;
16         esac
17 }
18
19 _cp () {
20         case "${system_name}" in
21                 'macos')
22                         gcp -R --preserve=timestamps -H -L "${@}"
23                         ;;
24                 *)
25                         cp -R --preserve=timestamps -H -L "${@}"
26                         ;;
27         esac
28 }
29
30 Common::noOp () {
31         true
32 }
33
34 Common::getPath () {
35         local file_path="${1}"
36
37         if command -v cygpath >/dev/null
38         then
39                 if [ "${file_path}" = '-' ]
40                 then
41                         tr '\n' '\0' \
42                         | xargs -0 -n1 -P1 -I{} \
43                                 cygpath --unix '{}'
44                 else
45                         cygpath --unix "${file_path}"
46                 fi
47         else
48                 if [ "${file_path}" = '-' ]
49                 then
50                         cat
51                 else
52                         printf '%s\n' "${file_path}"
53                 fi
54         fi \
55         | _sed -e 's|/*$||'
56 }
57
58 Common::grepLdd () {
59         case "${system_name}" in
60                 'macos')
61                         egrep '^\t/'
62                         ;;
63                 *)
64                         egrep ' => '
65                         ;;
66         esac
67 }
68
69 Common::stripLdd () {
70         case "${system_name}" in
71                 'macos')
72                         _sed -e 's/^\t\(.*\) (compatibility version .*/\1/'
73                         ;;
74                 *)
75                         _sed -e 's/ (0x[0-9a-f]*)$//;s/^.* => //'
76                         ;;
77         esac
78 }
79
80 Multi::excludeLdd () {
81         case "${system_name}" in
82                 'linux')
83                         # - always bundle built-in libraries
84                         # - always rely on up-to-date x11 and gl libraries, bundling them will break on future distros
85                         # - gtk is not easily bundlable on linux because it looks for harcoded system path to optional
86                         #   shared libraries like image codecs, theme engines, sound notification system, etc.
87                         #   so expect user to install gtk first
88                         # - since we ask user to instal gtk, we can also ask them to install gtkglext,
89                         #   which is likely to pull gtk itself, x11 and gl dependencies
90                         # - old fontconfig does not work correctly if newer fontconfig configuration is installed
91                         # - if gtk and fontconfig is installed, pango and freetype are
92                         local ldd_line
93                         while read ldd_line
94                         do
95                                 if echo "${ldd_line}" | egrep '/builtins/'
96                                 then
97                                         echo "${ldd_line}"
98                                 elif echo "${ldd_line}" \
99                                         | egrep -q '/libc\.|/libstdc\+\+\.|/libdl\.|/libm\.|/libX|/libxcb|/libGL|/libICE\.|/libSM\.|/libpthread\.'
100                                 then
101                                         Common::noOp
102                                 elif echo "${ldd_line}" \
103                                         | egrep -q '/libatk|/libgdk|/libgtk|/libgio|/libglib|/libgmodule|/libgobject|/libcairo|/libpango|/libfontconfig|/libfreetype'
104                                 then
105                                         Common::noOp
106                                 else
107                                         echo "${ldd_line}"
108                                 fi
109                         done
110                         ;;
111                 'windows')
112                         egrep -i '\.dll => [A-Z]:\\msys64\\'
113                         ;;
114                 'macos')
115                         egrep -v '^\t/System/|^\t/usr/lib/'
116                         ;;
117         esac
118 }
119
120 Multi::filterLib () {
121         Common::grepLdd \
122         | Multi::excludeLdd \
123         | Common::stripLdd \
124         | Common::getPath -
125 }
126
127 Multi::printLdd () {
128         local exe_file="${1}"
129
130         case "${system_name}" in
131                 'linux')
132                         ldd "${exe_file}"
133                         ;;
134                 'windows')
135                         ntldd --recursive "${exe_file}"
136                         ;;
137                 'macos')
138                         otool -L "${exe_file}"
139         esac
140 }
141
142 Multi::getGtkThemeName () {
143         case "${system_name}" in
144                 'linux')
145                         echo 'Adwaita'
146                         ;;
147                 'windows')
148                         echo 'MS-Windows'
149                         ;;
150                 *)
151                         echo 'Raleigh'
152                         ;;
153         esac
154 }
155
156 Multi::getGtkLibName () {
157         case "${system_name}" in
158                 'linux')
159                         echo 'libgtk-x11-2.0.so.0'
160                         ;;
161                 'windows')
162                         echo 'libgtk-win32-2.0-0.dll'
163                         ;;
164                 'macos')
165                         echo 'libgtk-quartz-2.0.0.dylib'
166                         ;;
167         esac
168 }
169
170 Multi::getRootPrefix () {
171         local lib_file="${1}"
172
173         case "${system_name}" in
174                 'linux')
175                         echo "${lib_file}" \
176                         | cut -f2 -d'/'
177                         ;;
178                 'windows')
179                         basename "${lib_file}" \
180                         | xargs -n1 -P1 which \
181                         | cut -f2 -d'/'
182                         ;;
183                 'macos')
184                         echo 'usr/local'
185         esac
186 }
187
188 Multi::getLibPrefix () {
189         local lib_file="${1}"
190
191         case "${system_name}" in
192                 'linux')
193                         dirname "${lib_file}" \
194                         | cut -f3- -d'/'
195                         ;;
196                 'windows')
197                         echo 'lib'
198                         ;;
199                 'macos')
200                         echo 'lib'
201                         ;;
202         esac
203 }
204
205 Multi::getGtkDeps () {
206         local lib_prefix="${1}"
207         local gtk_theme_name="${2}"
208
209         case "${system_name}" in
210                 'linux'|'windows')
211                         cat <<-EOF
212                         share/themes/${gtk_theme_name}/gtk-2.0
213                         share/icons/hicolor
214                         ${lib_prefix}/gdk-pixbuf-2.0
215                         ${lib_prefix}/gtk-2.0
216                         EOF
217                         ;;
218                 'macos')
219                         cat <<-EOF
220                         etc/fonts
221                         share/themes/${gtk_theme_name}/gtk-2.0
222                         share/fontconfig
223                         share/icons/hicolor
224                         share/locale
225                         ${lib_prefix}/gdk-pixbuf-2.0
226                         ${lib_prefix}/gtk-2.0
227                         EOF
228                         ;;
229         esac
230
231         case "${system_name}" in
232                 'linux')
233                         cat <<-EOF
234                         ${lib_prefix}/libatk-bridge-2.0.so.0
235                         ${lib_prefix}/libcanberra-0.30
236                         ${lib_prefix}/libcanberra.so.0
237                         ${lib_prefix}/libcanberra-gtk.so.0
238                         EOF
239                         ;;
240         esac
241 }
242
243 Multi::rewriteLoadersCache () {
244         local bundle_component_path="${1}"
245         local cache_file
246
247         find "${bundle_component_path}" \
248                 -type f \
249                 \( \
250                 -name 'loaders.cache' \
251                 -o -name 'immodules.cache' \
252                 \) \
253         | while read cache_file
254         do
255                 _sed \
256                         -e 's|^"/[^"]*/lib/|"lib/|;s| "/[^"]*/share/| "share/|;/^# ModulesPath = /d;/^# Created by /d;/^#$/d' \
257                         -i "${cache_file}"
258         done
259 }
260
261 Multi::bundleGtkDepsFromFile () {
262         local lib_file="${1}"
263         local component_dir
264         local real_component_dir
265         local bundle_component_dir
266
267         lib_basename="$(basename "${lib_file}")"
268
269         gtk_lib_name="$(Multi::getGtkLibName)"
270         if [ "${lib_basename}" = "${gtk_lib_name}" ]
271         then
272                 root_prefix="$(Multi::getRootPrefix "${lib_file}")"
273                 lib_prefix="$(Multi::getLibPrefix "${lib_file}")"
274                 gtk_theme_name="$(Multi::getGtkThemeName)"
275
276                 for component_dir in $(Multi::getGtkDeps "${lib_prefix}" "${gtk_theme_name}")
277                 do
278                         bundle_component_dir="$(echo "${component_dir}" | _sed -e 's|^'"${lib_prefix}"'|lib|')"
279                         if ! [ -e "${bundle_dir}/${bundle_component_dir}" ]
280                         then
281                                 real_component_dir="$(realpath "/${root_prefix}/${component_dir}")"
282
283                                 mkdir -p "${bundle_dir}/$(dirname "${bundle_component_dir}")"
284
285                                 _cp \
286                                         "${real_component_dir}" \
287                                         "${bundle_dir}/${bundle_component_dir}"
288
289                                 Multi::rewriteLoadersCache "${bundle_dir}/${bundle_component_dir}"
290                         fi
291                 done
292         fi
293 }
294
295 Multi::bundleLibFromFile () {
296         local exe_file="${1}"
297         local lib_file
298
299         Multi::printLdd "${exe_file}" \
300         | Multi::filterLib \
301         | while read lib_file
302         do
303                 if [ "${lib_file}" = 'not found' ]
304                 then
305                         printf 'ERROR: library not found while bundling %s (but link worked)\n' "${exe_file}" >&2
306                         Multi::printLdd "${exe_file}" | grep 'not found'
307                         exit 1
308                 fi
309                 lib_basename="$(basename "${lib_file}")"
310
311                 if [ -f "${lib_dir}/${lib_basename}" ]
312                 then
313                         continue
314                 fi
315
316                 _cp \
317                         "${lib_file}" \
318                         "${lib_dir}/${lib_basename}"
319
320                 Multi::bundleGtkDepsFromFile "${lib_file}"
321
322                 case "${system_name}" in
323                         'macos')
324                                 Multi::bundleLibFromFile "${lib_file}"
325                                 ;;
326                 esac
327         done
328 }
329
330 Multi::cleanUp () {
331         find "${bundle_dir}/lib" \
332                 -type f \
333                 -name '*.a' \
334                 -exec rm -f {} \;
335
336         find "${bundle_dir}/lib" \
337                 -type f \
338                 -name '*.h' \
339                 -exec rm -f {} \;
340
341         find "${bundle_dir}/lib" \
342                 -depth \
343                 -type d \
344                 -exec rmdir {} \; \
345         || true
346 }
347
348 Linux::getRpath () {
349         local exe_file="${1}"
350
351         local exe_dir="$(dirname "${exe_file}")"
352         local path_start="$(printf '%s' "${bundle_dir}" | wc -c)"
353         path_start="$((${path_start} + 1))"
354
355         local exe_subdir="$(echo "${exe_dir}" | cut -c "${path_start}-" | _sed -e 's|//*|/|;s|^/||')"
356
357         local rpath_origin='$ORIGIN'
358
359         if [ "${exe_subdir}" = '' ]
360         then
361                 printf '%s/lib\n' "${rpath_origin}"
362         else
363                 if [ "${exe_subdir}" = 'lib' ]
364                 then
365                         printf '%s\n' "${rpath_origin}"
366                 else
367                         local num_parent_dir="$(echo "${exe_subdir}" | tr '/' '\n' | wc -l)"
368                         local rpath_subdir
369                         local i=0
370                         while [ "${i}" -lt "${num_parent_dir}" ]
371                         do
372                                 rpath_subdir="${rpath_subdir}/.."
373                                 i="$((${i} + 1))"
374                         done
375                         printf '%s%s/lib\n' "${rpath_origin}" "${rpath_subdir}"
376                 fi
377         fi
378 }
379
380 Linux::patchExe () {
381         local exe_file="${1}"
382
383         local linux_rpath_string=$"$(Linux::getRpath "${exe_file}")"
384         patchelf --set-rpath "${linux_rpath_string}" "${exe_file}"
385 }
386
387 Linux::patchLib () {
388         local lib_dir="${1}"
389         local exe_file
390
391         find "${lib_dir}" \
392                 -type f \
393                 -name '*.so*' \
394         | while read exe_file
395         do
396                 Linux::patchExe "${exe_file}"
397         done
398 }
399
400 Darwin::patchExe () {
401         local exe_file="${1}"
402
403         Multi::printLdd "${exe_file}" \
404         | Multi::filterLib \
405         | while read lib_file
406         do
407                 new_path="$(echo "${lib_file}" | _sed -e 's|^/.*/lib/|@executable_path/lib/|')"
408                 id_name="$(echo "${lib_file}" | _sed -e 's|.*/||g')"
409                 chmod u+w,go-w "${exe_file}"
410                 install_name_tool -change "${lib_file}" "${new_path}" "${exe_file}"
411                 install_name_tool -id "${id_name}" "${exe_file}"
412         done
413 }
414
415 Darwin::patchLib () {
416         local lib_dir="${1}"
417         local exe_file
418
419         find "${lib_dir}" \
420                 -type f \
421                 \( \
422                 -name '*.dylib' \
423                 -o -name '*.so' \
424                 \) \
425         | while read exe_file
426         do
427                 Darwin::patchExe "${exe_file}"
428                 chmod ugo-x "${exe_file}"
429         done
430 }
431
432 Windows::listLibForManifest () {
433         local lib_dir="${1}"
434
435         find "${lib_dir}" \
436                 -maxdepth 1 \
437                 -type f \
438                 -name '*.dll' \
439                 -exec basename {} \; \
440         | tr '\n' '\0' \
441         | xargs -0 -n1 -P1 -I{} \
442                 printf '  <file name="{}"/>\n'
443 }
444
445 Windows::writeManifest () {
446         local lib_dir="${1}"
447
448         cat > "${manifest_file}" <<-EOF
449         <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
450           <assemblyIdentity type="win32" name="lib" version="1.0.0.0"/>
451         $(Windows::listLibForManifest "${lib_dir}")
452         </assembly>
453         EOF
454 }
455
456 system_name="${1}"; shift
457 bundle_dir="${1}"; shift
458
459 if ! [ -z "${1}" ]
460 then
461         exe_file="${1}"; shift
462 fi
463
464 bundle_dir="$(Common::getPath "${bundle_dir}")"
465 registry_dir="${bundle_dir}/registry"
466 lib_dir="${bundle_dir}/lib"
467
468 manifest_file="${lib_dir}/lib.manifest"
469
470 exe_action='Common::noOp'
471 lib_action='Common::noOp'
472
473 case "${system_name}" in
474         'register')
475                 mkdir -p "${registry_dir}"
476                 Common::getPath "${exe_file}" > "${registry_dir}/$(uuidgen)"
477                 exit
478                 ;;
479         'linux')
480                 exe_action='Linux::patchExe'
481                 lib_action='Linux::patchLib'
482                 ;;
483         'windows')
484                 lib_action='Windows::writeManifest'
485                 ;;
486         'macos')
487                 exe_action='Darwin::patchExe'
488                 lib_action='Darwin::patchLib'
489                 ;;
490         *)
491                 printf 'ERROR: unsupported system: %s\n' "${system_name}" >&2
492                 exit 1
493                 ;;
494 esac
495
496 mkdir -p "${lib_dir}"
497
498 if [ -d "${registry_dir}" ]
499 then
500         for registry_entry in "${registry_dir}"/*
501         do
502                 exe_file="$(cat "${registry_entry}")"
503
504                 Multi::bundleLibFromFile "${exe_file}"
505
506                 "${exe_action}" "${exe_file}"
507
508                 rm "${registry_entry}"
509
510                 "${exe_action}" "${exe_file}"
511         done
512
513         rmdir "${registry_dir}"
514 fi
515
516 "${lib_action}" "${lib_dir}"
517
518 Multi::cleanUp