summaryrefslogtreecommitdiff
path: root/pkg/osx/cp-with-libs
blob: 182741724490f27b3970bcb80b6d35bfc5692ef0 (plain)
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
#!/bin/bash
#
# Copy a program to the specified destination, along
# with libraries it depends upon.

src_bin=$1
dest_dir=$2

# Returns true if the specified file is a dylib.

is_dylib() {
	case "$1" in
		*.dylib)
			true
			;;
		*)
			false
			;;
	esac
}

# Returns true if the specified file is in a system location
# (/System or /usr):

is_sys_lib() {
	case "$1" in
		/System/*)
			true
			;;
		/usr/*)
			true
			;;
		*)
			false
			;;
	esac
}

# Install the specified file to the location in dest_dir, along with
# any libraries it depends upon (recursively):

install_with_deps() {
	local src_file
	local bin_name
	local dest_file
	local lib_name

	src_file=$1
	bin_name=$(basename "$src_file")
	dest_file="$dest_dir/$bin_name"

	# Already copied into the package? Don't copy again.
	# Prevents endless recursion.

	if [ -e "$dest_file" ]; then
		return
	fi	

	echo "Installing $bin_name..."

	# Copy file into package.

	cp "$src_file" "$dest_file"

	# Copy libraries that this file depends on:

	otool -L "$src_file" | tail -n +2 | sed 's/^.//; s/ (.*//' | while read; do
		
		# Don't copy system libraries

		if is_sys_lib "$REPLY"; then
			continue
		fi

		#echo "    - $bin_name depends on $REPLY"

		# Copy this library into the package, and:
		# recursively install any libraries that _this_ library depends on:

		install_with_deps "$REPLY"

		# Change destination binary to depend on the
		# copy inside the package:

		lib_name=$(basename "$REPLY")
		install_name_tool -change "$REPLY" "@executable_path@/$lib_name" \
		                  "$dest_file"
	done

	# If this is a library that we have installed, change its id:

	if is_dylib "$dest_file"; then
		install_name_tool -id "@executable_path@/$bin_name" "$dest_file"
	fi
}

# Install the file, and recursively install any libraries:

install_with_deps "$src_bin"