]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - library-bundler
bf8622c4932969406a85d312429957867969cd17
[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 _cpr () {
20         case "${system_name}" in
21                 'macos')
22                         cp -R ${@}
23                         ;;
24                 *)
25                         cp -R --preserve=timestamps ${@}
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                                 _cpr -H -L \
286                                         "${real_component_dir}" \
287                                         "${bundle_dir}/${bundle_component_dir}"
288
289                                 touch -r \
290                                         "${real_component_dir}" \
291                                         "${bundle_dir}/${bundle_component_dir}"
292
293                                 Multi::rewriteLoadersCache "${bundle_dir}/${bundle_component_dir}"
294                         fi
295                 done
296         fi
297 }
298
299 Multi::bundleLibFromFile () {
300         local exe_file="${1}"
301         local lib_file
302
303         Multi::printLdd "${exe_file}" \
304         | Multi::filterLib \
305         | while read lib_file
306         do
307                 if [ "${lib_file}" = 'not found' ]
308                 then
309                         printf 'ERROR: library not found while bundling %s (but link worked)\n' "${exe_file}" >&2
310                         Multi::printLdd "${exe_file}" | grep 'not found'
311                         exit 1
312                 fi
313                 lib_basename="$(basename "${lib_file}")"
314
315                 if [ -f "${lib_dir}/${lib_basename}" ]
316                 then
317                         continue
318                 fi
319
320                 cp -H \
321                         "${lib_file}" \
322                         "${lib_dir}/${lib_basename}"
323
324                 touch -r \
325                         "${lib_file}" \
326                         "${lib_dir}/${lib_basename}"
327
328                 Multi::bundleGtkDepsFromFile "${lib_file}"
329
330                 case "${system_name}" in
331                         'macos')
332                                 Multi::bundleLibFromFile "${lib_file}"
333                                 ;;
334                 esac
335         done
336 }
337
338 Multi::cleanUp () {
339         find "${bundle_dir}/lib" \
340                 -type f \
341                 -name '*.a' \
342                 -exec rm -f {} \;
343
344         find "${bundle_dir}/lib" \
345                 -type f \
346                 -name '*.h' \
347                 -exec rm -f {} \;
348
349         find "${bundle_dir}/lib" \
350                 -depth \
351                 -type d \
352                 -exec rmdir {} \; \
353         || true
354 }
355
356 Linux::getRpath () {
357         local exe_file="${1}"
358
359         local exe_dir="$(dirname "${exe_file}")"
360         local path_start="$(printf '%s' "${bundle_dir}" | wc -c)"
361         path_start="$((${path_start} + 1))"
362
363         local exe_subdir="$(echo "${exe_dir}" | cut -c "${path_start}-" | _sed -e 's|//*|/|;s|^/||')"
364
365         local rpath_origin='$ORIGIN'
366
367         if [ "${exe_subdir}" = '' ]
368         then
369                 printf '%s/lib\n' "${rpath_origin}"
370         else
371                 if [ "${exe_subdir}" = 'lib' ]
372                 then
373                         printf '%s\n' "${rpath_origin}"
374                 else
375                         local num_parent_dir="$(echo "${exe_subdir}" | tr '/' '\n' | wc -l)"
376                         local rpath_subdir
377                         local i=0
378                         while [ "${i}" -lt "${num_parent_dir}" ]
379                         do
380                                 rpath_subdir="${rpath_subdir}/.."
381                                 i="$((${i} + 1))"
382                         done
383                         printf '%s%s/lib\n' "${rpath_origin}" "${rpath_subdir}"
384                 fi
385         fi
386 }
387
388 Linux::patchExe () {
389         local exe_file="${1}"
390
391         local linux_rpath_string=$"$(Linux::getRpath "${exe_file}")"
392         patchelf --set-rpath "${linux_rpath_string}" "${exe_file}"
393 }
394
395 Linux::patchLib () {
396         local lib_dir="${1}"
397         local exe_file
398
399         find "${lib_dir}" \
400                 -type f \
401                 -name '*.so*' \
402         | while read exe_file
403         do
404                 Linux::patchExe "${exe_file}"
405         done
406 }
407
408 Darwin::patchExe () {
409         local exe_file="${1}"
410
411         Multi::printLdd "${exe_file}" \
412         | Multi::filterLib \
413         | while read lib_file
414         do
415                 new_path="$(echo "${lib_file}" | _sed -e 's|^/.*/lib/|@executable_path/lib/|')"
416                 id_name="$(echo "${lib_file}" | _sed -e 's|.*/||g')"
417                 chmod u+w,go-w "${exe_file}"
418                 install_name_tool -change "${lib_file}" "${new_path}" "${exe_file}"
419                 install_name_tool -id "${id_name}" "${exe_file}"
420         done
421 }
422
423 Darwin::patchLib () {
424         local lib_dir="${1}"
425         local exe_file
426
427         find "${lib_dir}" \
428                 -type f \
429                 \( \
430                 -name '*.dylib' \
431                 -o -name '*.so' \
432                 \) \
433         | while read exe_file
434         do
435                 Darwin::patchExe "${exe_file}"
436                 chmod ugo-x "${exe_file}"
437         done
438 }
439
440 Windows::listLibForManifest () {
441         local lib_dir="${1}"
442
443         find "${lib_dir}" \
444                 -maxdepth 1 \
445                 -type f \
446                 -name '*.dll' \
447                 -exec basename {} \; \
448         | tr '\n' '\0' \
449         | xargs -0 -n1 -P1 -I{} \
450                 printf '  <file name="{}"/>\n'
451 }
452
453 Windows::writeManifest () {
454         local lib_dir="${1}"
455
456         cat > "${manifest_file}" <<-EOF
457         <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
458           <assemblyIdentity type="win32" name="lib" version="1.0.0.0"/>
459         $(Windows::listLibForManifest "${lib_dir}")
460         </assembly>
461         EOF
462 }
463
464 system_name="${1}"; shift
465 bundle_dir="${1}"; shift
466
467 if ! [ -z "${1}" ]
468 then
469         exe_file="${1}"; shift
470 fi
471
472 bundle_dir="$(Common::getPath "${bundle_dir}")"
473 registry_dir="${bundle_dir}/registry"
474 lib_dir="${bundle_dir}/lib"
475
476 manifest_file="${lib_dir}/lib.manifest"
477
478 exe_action='Common::noOp'
479 lib_action='Common::noOp'
480
481 case "${system_name}" in
482         'register')
483                 mkdir -p "${registry_dir}"
484                 Common::getPath "${exe_file}" > "${registry_dir}/$(uuidgen)"
485                 exit
486                 ;;
487         'linux')
488                 exe_action='Linux::patchExe'
489                 lib_action='Linux::patchLib'
490                 ;;
491         'windows')
492                 lib_action='Windows::writeManifest'
493                 ;;
494         'macos')
495                 exe_action='Darwin::patchExe'
496                 lib_action='Darwin::patchLib'
497                 ;;
498         *)
499                 printf 'ERROR: unsupported system: %s\n' "${system_name}" >&2
500                 exit 1
501                 ;;
502 esac
503
504 mkdir -p "${lib_dir}"
505
506 if [ -d "${registry_dir}" ]
507 then
508         for registry_entry in "${registry_dir}"/*
509         do
510                 exe_file="$(cat "${registry_entry}")"
511
512                 Multi::bundleLibFromFile "${exe_file}"
513
514                 "${exe_action}" "${exe_file}"
515
516                 rm "${registry_entry}"
517
518                 "${exe_action}" "${exe_file}"
519         done
520
521         rmdir "${registry_dir}"
522 fi
523
524 "${lib_action}" "${lib_dir}"
525
526 Multi::cleanUp