6 export LANGUAGE="${LANG}"
9 case "${system_name}" in
20 case "${system_name}" in
25 cp -R --preserve=timestamps ${@}
35 local file_path="${1}"
37 if command -v cygpath >/dev/null
39 if [ "${file_path}" = '-' ]
42 | xargs -0 -n1 -P1 -I{} \
45 cygpath --unix "${file_path}"
48 if [ "${file_path}" = '-' ]
52 printf '%s\n' "${file_path}"
59 case "${system_name}" in
70 case "${system_name}" in
72 _sed -e 's/^\t\(.*\) (compatibility version .*/\1/'
75 _sed -e 's/ (0x[0-9a-f]*)$//;s/^.* => //'
80 Multi::excludeLdd () {
81 case "${system_name}" in
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
95 if echo "${ldd_line}" | egrep '/builtins/'
98 elif echo "${ldd_line}" \
99 | egrep -q '/libc\.|/libstdc\+\+\.|/libdl\.|/libm\.|/libX|/libxcb|/libGL|/libICE\.|/libSM\.|/libpthread\.'
102 elif echo "${ldd_line}" \
103 | egrep -q '/libatk|/libgdk|/libgtk|/libgio|/libglib|/libgmodule|/libgobject|/libcairo|/libpango|/libfontconfig|/libfreetype'
112 egrep -i '\.dll => [A-Z]:\\msys64\\'
115 egrep -v '^\t/System/|^\t/usr/lib/'
120 Multi::filterLib () {
122 | Multi::excludeLdd \
128 local exe_file="${1}"
130 case "${system_name}" in
135 ntldd --recursive "${exe_file}"
138 otool -L "${exe_file}"
142 Multi::getGtkThemeName () {
143 case "${system_name}" in
156 Multi::getGtkLibName () {
157 case "${system_name}" in
159 echo 'libgtk-x11-2.0.so.0'
162 echo 'libgtk-win32-2.0-0.dll'
165 echo 'libgtk-quartz-2.0.0.dylib'
170 Multi::getRootPrefix () {
171 local lib_file="${1}"
173 case "${system_name}" in
179 basename "${lib_file}" \
180 | xargs -n1 -P1 which \
188 Multi::getLibPrefix () {
189 local lib_file="${1}"
191 case "${system_name}" in
193 dirname "${lib_file}" \
205 Multi::getGtkDeps () {
206 local lib_prefix="${1}"
207 local gtk_theme_name="${2}"
209 case "${system_name}" in
212 share/themes/${gtk_theme_name}/gtk-2.0
214 ${lib_prefix}/gdk-pixbuf-2.0
215 ${lib_prefix}/gtk-2.0
221 share/themes/${gtk_theme_name}/gtk-2.0
225 ${lib_prefix}/gdk-pixbuf-2.0
226 ${lib_prefix}/gtk-2.0
231 case "${system_name}" in
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
243 Multi::rewriteLoadersCache () {
244 local bundle_component_path="${1}"
247 find "${bundle_component_path}" \
250 -name 'loaders.cache' \
251 -o -name 'immodules.cache' \
253 | while read cache_file
256 -e 's|^"/[^"]*/lib/|"lib/|;s| "/[^"]*/share/| "share/|;/^# ModulesPath = /d;/^# Created by /d;/^#$/d' \
261 Multi::bundleGtkDepsFromFile () {
262 local lib_file="${1}"
264 local real_component_dir
265 local bundle_component_dir
267 lib_basename="$(basename "${lib_file}")"
269 gtk_lib_name="$(Multi::getGtkLibName)"
270 if [ "${lib_basename}" = "${gtk_lib_name}" ]
272 root_prefix="$(Multi::getRootPrefix "${lib_file}")"
273 lib_prefix="$(Multi::getLibPrefix "${lib_file}")"
274 gtk_theme_name="$(Multi::getGtkThemeName)"
276 for component_dir in $(Multi::getGtkDeps "${lib_prefix}" "${gtk_theme_name}")
278 bundle_component_dir="$(echo "${component_dir}" | _sed -e 's|^'"${lib_prefix}"'|lib|')"
279 if ! [ -e "${bundle_dir}/${bundle_component_dir}" ]
281 real_component_dir="$(realpath "/${root_prefix}/${component_dir}")"
283 mkdir -p "${bundle_dir}/$(dirname "${bundle_component_dir}")"
286 "${real_component_dir}" \
287 "${bundle_dir}/${bundle_component_dir}"
290 "${real_component_dir}" \
291 "${bundle_dir}/${bundle_component_dir}"
293 Multi::rewriteLoadersCache "${bundle_dir}/${bundle_component_dir}"
299 Multi::bundleLibFromFile () {
300 local exe_file="${1}"
303 Multi::printLdd "${exe_file}" \
305 | while read lib_file
307 if [ "${lib_file}" = 'not found' ]
309 printf 'ERROR: library not found while bundling %s (but link worked)\n' "${exe_file}" >&2
310 Multi::printLdd "${exe_file}" | grep 'not found'
313 lib_basename="$(basename "${lib_file}")"
315 if [ -f "${lib_dir}/${lib_basename}" ]
322 "${lib_dir}/${lib_basename}"
326 "${lib_dir}/${lib_basename}"
328 Multi::bundleGtkDepsFromFile "${lib_file}"
330 case "${system_name}" in
332 Multi::bundleLibFromFile "${lib_file}"
339 find "${bundle_dir}/lib" \
344 find "${bundle_dir}/lib" \
349 find "${bundle_dir}/lib" \
357 local exe_file="${1}"
359 local exe_dir="$(dirname "${exe_file}")"
360 local path_start="$(printf '%s' "${bundle_dir}" | wc -c)"
361 path_start="$((${path_start} + 1))"
363 local exe_subdir="$(echo "${exe_dir}" | cut -c "${path_start}-" | _sed -e 's|//*|/|;s|^/||')"
365 local rpath_origin='$ORIGIN'
367 if [ "${exe_subdir}" = '' ]
369 printf '%s/lib\n' "${rpath_origin}"
371 if [ "${exe_subdir}" = 'lib' ]
373 printf '%s\n' "${rpath_origin}"
375 local num_parent_dir="$(echo "${exe_subdir}" | tr '/' '\n' | wc -l)"
378 while [ "${i}" -lt "${num_parent_dir}" ]
380 rpath_subdir="${rpath_subdir}/.."
383 printf '%s%s/lib\n' "${rpath_origin}" "${rpath_subdir}"
389 local exe_file="${1}"
391 local linux_rpath_string=$"$(Linux::getRpath "${exe_file}")"
392 patchelf --set-rpath "${linux_rpath_string}" "${exe_file}"
402 | while read exe_file
404 Linux::patchExe "${exe_file}"
408 Darwin::patchExe () {
409 local exe_file="${1}"
411 Multi::printLdd "${exe_file}" \
413 | while read lib_file
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}"
423 Darwin::patchLib () {
433 | while read exe_file
435 Darwin::patchExe "${exe_file}"
436 chmod ugo-x "${exe_file}"
440 Windows::listLibForManifest () {
447 -exec basename {} \; \
449 | xargs -0 -n1 -P1 -I{} \
450 printf ' <file name="{}"/>\n'
453 Windows::writeManifest () {
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}")
464 system_name="${1}"; shift
465 bundle_dir="${1}"; shift
469 exe_file="${1}"; shift
472 bundle_dir="$(Common::getPath "${bundle_dir}")"
473 registry_dir="${bundle_dir}/registry"
474 lib_dir="${bundle_dir}/lib"
476 manifest_file="${lib_dir}/lib.manifest"
478 exe_action='Common::noOp'
479 lib_action='Common::noOp'
481 case "${system_name}" in
483 mkdir -p "${registry_dir}"
484 Common::getPath "${exe_file}" > "${registry_dir}/$(uuidgen)"
488 exe_action='Linux::patchExe'
489 lib_action='Linux::patchLib'
492 lib_action='Windows::writeManifest'
495 exe_action='Darwin::patchExe'
496 lib_action='Darwin::patchLib'
499 printf 'ERROR: unsupported system: %s\n' "${system_name}" >&2
504 mkdir -p "${lib_dir}"
506 if [ -d "${registry_dir}" ]
508 for registry_entry in "${registry_dir}"/*
510 exe_file="$(cat "${registry_entry}")"
512 Multi::bundleLibFromFile "${exe_file}"
514 "${exe_action}" "${exe_file}"
516 rm "${registry_entry}"
518 "${exe_action}" "${exe_file}"
521 rmdir "${registry_dir}"
524 "${lib_action}" "${lib_dir}"