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
222 ${lib_prefix}/gdk-pixbuf-2.0
223 ${lib_prefix}/gtk-2.0
228 case "${system_name}" in
231 ${lib_prefix}/libatk-bridge-2.0.so.0
232 ${lib_prefix}/libcanberra-0.30
233 ${lib_prefix}/libcanberra.so.0
234 ${lib_prefix}/libcanberra-gtk.so.0
240 Multi::rewriteLoadersCache () {
241 local bundle_component_path="${1}"
244 find "${bundle_component_path}" \
247 -name 'loaders.cache' \
248 -o -name 'immodules.cache' \
250 | while read cache_file
253 -e 's|^"/[^"]*/lib/|"lib/|;s| "/[^"]*/share/| "share/|;/^# ModulesPath = /d;/^# Created by /d;/^#$/d' \
258 Multi::bundleGtkDepsFromFile () {
259 local lib_file="${1}"
261 local real_component_dir
262 local bundle_component_dir
264 lib_basename="$(basename "${lib_file}")"
266 gtk_lib_name="$(Multi::getGtkLibName)"
267 if [ "${lib_basename}" = "${gtk_lib_name}" ]
269 root_prefix="$(Multi::getRootPrefix "${lib_file}")"
270 lib_prefix="$(Multi::getLibPrefix "${lib_file}")"
271 gtk_theme_name="$(Multi::getGtkThemeName)"
273 for component_dir in $(Multi::getGtkDeps "${lib_prefix}" "${gtk_theme_name}")
275 bundle_component_dir="$(echo "${component_dir}" | _sed -e 's|^'"${lib_prefix}"'|lib|')"
276 if ! [ -e "${bundle_dir}/${bundle_component_dir}" ]
278 real_component_dir="$(realpath "/${root_prefix}/${component_dir}")"
280 mkdir -p "${bundle_dir}/$(dirname "${bundle_component_dir}")"
283 "${real_component_dir}" \
284 "${bundle_dir}/${bundle_component_dir}"
287 "/${real_component_dir}" \
288 "${bundle_dir}/${bundle_component_dir}"
290 Multi::rewriteLoadersCache "${bundle_dir}/${bundle_component_dir}"
296 Multi::bundleLibFromFile () {
297 local exe_file="${1}"
300 Multi::printLdd "${exe_file}" \
302 | while read lib_file
304 if [ "${lib_file}" = 'not found' ]
306 printf 'ERROR: library not found while bundling %s (but link worked)\n' "${exe_file}" >&2
307 Multi::printLdd "${exe_file}" | grep 'not found'
310 lib_basename="$(basename "${lib_file}")"
312 if [ -f "${lib_dir}/${lib_basename}" ]
319 "${lib_dir}/${lib_basename}"
323 "${lib_dir}/${lib_basename}"
325 Multi::bundleGtkDepsFromFile "${lib_file}"
327 case "${system_name}" in
329 Multi::bundleLibFromFile "${lib_file}"
336 find "${bundle_dir}/lib" \
341 find "${bundle_dir}/lib" \
346 find "${bundle_dir}/lib" \
354 local exe_file="${1}"
356 local exe_dir="$(dirname "${exe_file}")"
357 local path_start="$(printf '%s' "${bundle_dir}" | wc -c)"
358 path_start="$((${path_start} + 1))"
360 local exe_subdir="$(echo "${exe_dir}" | cut -c "${path_start}-" | _sed -e 's|//*|/|;s|^/||')"
362 local rpath_origin='$ORIGIN'
364 if [ "${exe_subdir}" = '' ]
366 printf '%s/lib\n' "${rpath_origin}"
368 if [ "${exe_subdir}" = 'lib' ]
370 printf '%s\n' "${rpath_origin}"
372 local num_parent_dir="$(echo "${exe_subdir}" | tr '/' '\n' | wc -l)"
375 while [ "${i}" -lt "${num_parent_dir}" ]
377 rpath_subdir="${rpath_subdir}/.."
380 printf '%s%s/lib\n' "${rpath_origin}" "${rpath_subdir}"
386 local exe_file="${1}"
388 local linux_rpath_string=$"$(Linux::getRpath "${exe_file}")"
389 patchelf --set-rpath "${linux_rpath_string}" "${exe_file}"
399 | while read exe_file
401 Linux::patchExe "${exe_file}"
405 Darwin::patchExe () {
406 local exe_file="${1}"
408 Multi::printLdd "${exe_file}" \
410 | while read lib_file
412 new_path="$(echo "${lib_file}" | _sed -e 's|^/.*/lib/|@executable_path/lib/|')"
413 id_name="$(echo "${lib_file}" | _sed -e 's|.*/||g')"
414 chmod u+w,go-w "${exe_file}"
415 install_name_tool -change "${lib_file}" "${new_path}" "${exe_file}"
416 install_name_tool -id "${id_name}" "${exe_file}"
420 Darwin::patchLib () {
430 | while read exe_file
432 Darwin::patchExe "${exe_file}"
433 chmod ugo-x "${exe_file}"
437 Windows::listLibForManifest () {
444 -exec basename {} \; \
446 | xargs -0 -n1 -P1 -I{} \
447 printf ' <file name="{}"/>\n'
450 Windows::writeManifest () {
453 cat > "${manifest_file}" <<-EOF
454 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
455 <assemblyIdentity type="win32" name="lib" version="1.0.0.0"/>
456 $(Windows::listLibForManifest "${lib_dir}")
461 system_name="${1}"; shift
462 bundle_dir="${1}"; shift
466 exe_file="${1}"; shift
469 bundle_dir="$(Common::getPath "${bundle_dir}")"
470 registry_dir="${bundle_dir}/registry"
471 lib_dir="${bundle_dir}/lib"
473 manifest_file="${lib_dir}/lib.manifest"
475 exe_action='Common::noOp'
476 lib_action='Common::noOp'
478 case "${system_name}" in
480 mkdir -p "${registry_dir}"
481 Common::getPath "${exe_file}" > "${registry_dir}/$(uuidgen)"
485 exe_action='Linux::patchExe'
486 lib_action='Linux::patchLib'
489 lib_action='Windows::writeManifest'
492 exe_action='Darwin::patchExe'
493 lib_action='Darwin::patchLib'
496 printf 'ERROR: unsupported system: %s\n' "${system_name}" >&2
501 mkdir -p "${lib_dir}"
503 if [ -d "${registry_dir}" ]
505 for registry_entry in "${registry_dir}"/*
507 exe_file="$(cat "${registry_entry}")"
509 Multi::bundleLibFromFile "${exe_file}"
511 "${exe_action}" "${exe_file}"
513 rm "${registry_entry}"
515 "${exe_action}" "${exe_file}"
518 rmdir "${registry_dir}"
521 "${lib_action}" "${lib_dir}"