File: dylibcopy.sh

package info (click to toggle)
filezilla 3.69.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,772 kB
  • sloc: cpp: 97,617; ansic: 54,984; sh: 5,225; makefile: 2,080; xml: 375
file content (155 lines) | stat: -rwxr-xr-x 3,295 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /bin/sh

set -e

bundle="$1"
searchdirs="$2"
executabledir="${3:-${bundle}/Contents/MacOS/}"
relframeworks="${4:-../Frameworks}"

if [ ! -d "$bundle" ]; then
  echo "$bundle is not an application bundle"
  exit 1
fi

frameworks="${bundle}/Contents/Frameworks"

mkdir -p "$frameworks"
rm -f "${frameworks}/"*.processed

absreadlink()
{
  if [ -L "$1" ]; then
    local target=`readlink "$1"`
    if [ "${target:0:1}" != "/" ]; then
      local dir=`dirname "$1"`
      target="$dir/$target"
    fi
    if [ -L "$target" ]; then
      absreadlink "$target"
    else
      echo "$target"
    fi
  else
    echo "$1"
  fi
}

find_dylib()
{
  local path="$1"
  local dylib="$2"

  local name="${dylib##*/}"
  if [ -f "$path/$name" ]; then
    echo "$path/$name"
    exit    
  fi

  local dirs="$LD_LIBRARY_PATH"
  while [ ! -z ${dirs} ]; do
    local dir="${dirs%%:*}"
    dirs="${dirs#*:}"
    if [ -f "$dir/$name" ]; then
      echo "$dir/$name"
      exit    
    fi
  done
  
  echo $dylib
}

process_dylib()
{
  local file="$1"
  local origin="$2"
  local dylib="$3"
  local original_dylib="$dylib"

  case $dylib in
    @rpath/*)
      dylib=`find_dylib "${origin%/*}" "$dylib"`;;
  esac

  case $dylib in
    /usr/*|/System/*)
      exit;;
  esac

  local resolved=`absreadlink "$dylib"`
  local name="${resolved##*/}"

  if [ ! -f "${frameworks}/$name" ] && [ ! -f "${frameworks}/$name.processed" ]; then
    touch "${frameworks}/$name.processed"

    local dirs=$searchdirs
    while [ ! -z "$dirs" ]; do
      local dir=${dirs%%:*}
      dirs=${dirs#*:}
      if [ "$dir" = "$dirs" ]; then
        dirs=
      fi

      if [ -f "$dir/.libs/$name" ]; then
        echo "Found dependency $name"
        cp "$dir/.libs/$name" "${frameworks}/$name"
        break
      elif [ -f "$dir/$name" ]; then
        echo "Found dependency $name"
        cp "$dir/$name" "${frameworks}/$name"
        break
      elif [ -f "$dylib" ]; then
        echo "Found dependency $name"
        cp "$dylib" "${frameworks}/$name"
        break
      fi
    done

    if [ ! -f "${frameworks}/$name" ]; then
      echo "Dependency $name not found"
      exit 1
    fi

    install_name_tool -id "$name" "${frameworks}/$name"

    # dylibs themselves have dependencies. Process them too
    process_file "${frameworks}/$name" "$dylib"
  fi

  if echo "$file" | grep '.\(dylib\|so\)$' >/dev/null 2>&1; then
    install_name_tool -change "${original_dylib}" "@loader_path/$name" "$file"
  else
    install_name_tool -change "${original_dylib}" "@executable_path/${relframeworks}/$name" "$file"
  fi
}

process_dylibs()
{
  local file="$1"
  local origin="$2"
  while [ ! -z "$3" ]; do
    process_dylib "$file" "$origin" "$3"
    shift
  done
}

process_file()
{
  local file="$1"
  local origin="${2:-$1}"

  process_dylibs "$file" "$origin" `otool -L "$file" | grep -v ':$' | grep "dylib\\|\\.so" | sed 's/^[[:blank:]]*//' | sed 's/ .*//' | grep -v '^/usr/\|^/System/'`
  process_dylibs "$file" "$origin" `otool -L "$file" | grep -v ':$' | grep "dylib\\|\\.so" | sed 's/^[[:blank:]]*//' | sed 's/ .*//' | grep '^/usr/local/'`
}

if [ -z "$3" ]; then
  rm -f "${frameworks}/"*.dylib
fi

for file in "${executabledir}"*; do
  process_file "$file"
done

rm -f "${frameworks}/"*.processed

echo Dependencies copied