sl@0
|
1 |
#!/bin/bash
|
sl@0
|
2 |
|
sl@0
|
3 |
#### Configuration:
|
sl@0
|
4 |
# The name of the executable. It is assumed
|
sl@0
|
5 |
# that it is in the current working directory.
|
sl@0
|
6 |
EXE_NAME=hidapi-testgui
|
sl@0
|
7 |
# Path to the executable directory inside the bundle.
|
sl@0
|
8 |
# This must be an absolute path, so use $PWD.
|
sl@0
|
9 |
EXEPATH=$PWD/TestGUI.app/Contents/MacOS
|
sl@0
|
10 |
# Libraries to explicitly bundle, even though they
|
sl@0
|
11 |
# may not be in /opt/local. One per line. These
|
sl@0
|
12 |
# are used with grep, so only a portion of the name
|
sl@0
|
13 |
# is required. eg: libFOX, libz, etc.
|
sl@0
|
14 |
LIBS_TO_BUNDLE=libFOX
|
sl@0
|
15 |
|
sl@0
|
16 |
|
sl@0
|
17 |
function copydeps {
|
sl@0
|
18 |
local file=$1
|
sl@0
|
19 |
# echo "Copying deps for $file...."
|
sl@0
|
20 |
local BASE_OF_EXE=`basename $file`
|
sl@0
|
21 |
|
sl@0
|
22 |
# A will contain the dependencies of this library
|
sl@0
|
23 |
local A=`otool -LX $file |cut -f 1 -d " "`
|
sl@0
|
24 |
local i
|
sl@0
|
25 |
for i in $A; do
|
sl@0
|
26 |
local BASE=`basename $i`
|
sl@0
|
27 |
|
sl@0
|
28 |
# See if it's a lib we specifically want to bundle
|
sl@0
|
29 |
local bundle_this_lib=0
|
sl@0
|
30 |
local j
|
sl@0
|
31 |
for j in $LIBS_TO_BUNDLE; do
|
sl@0
|
32 |
echo $i |grep -q $j
|
sl@0
|
33 |
if [ $? -eq 0 ]; then
|
sl@0
|
34 |
bundle_this_lib=1
|
sl@0
|
35 |
echo "bundling $i because it's in the list."
|
sl@0
|
36 |
break;
|
sl@0
|
37 |
fi
|
sl@0
|
38 |
done
|
sl@0
|
39 |
|
sl@0
|
40 |
# See if it's in /opt/local. Bundle all in /opt/local
|
sl@0
|
41 |
local isOptLocal=0
|
sl@0
|
42 |
echo $i |grep -q /opt/local
|
sl@0
|
43 |
if [ $? -eq 0 ]; then
|
sl@0
|
44 |
isOptLocal=1
|
sl@0
|
45 |
echo "bundling $i because it's in /opt/local."
|
sl@0
|
46 |
fi
|
sl@0
|
47 |
|
sl@0
|
48 |
# Bundle the library
|
sl@0
|
49 |
if [ $isOptLocal -ne 0 ] || [ $bundle_this_lib -ne 0 ]; then
|
sl@0
|
50 |
|
sl@0
|
51 |
# Copy the file into the bundle if it exists.
|
sl@0
|
52 |
if [ -f $EXEPATH/$BASE ]; then
|
sl@0
|
53 |
z=0
|
sl@0
|
54 |
else
|
sl@0
|
55 |
cp $i $EXEPATH
|
sl@0
|
56 |
chmod 755 $EXEPATH/$BASE
|
sl@0
|
57 |
fi
|
sl@0
|
58 |
|
sl@0
|
59 |
|
sl@0
|
60 |
# echo "$BASE_OF_EXE depends on $BASE"
|
sl@0
|
61 |
|
sl@0
|
62 |
# Fix the paths using install_name_tool and then
|
sl@0
|
63 |
# call this function recursively for each dependency
|
sl@0
|
64 |
# of this library.
|
sl@0
|
65 |
if [ $BASE_OF_EXE != $BASE ]; then
|
sl@0
|
66 |
|
sl@0
|
67 |
# Fix the paths
|
sl@0
|
68 |
install_name_tool -id @executable_path/$BASE $EXEPATH/$BASE
|
sl@0
|
69 |
install_name_tool -change $i @executable_path/$BASE $EXEPATH/$BASE_OF_EXE
|
sl@0
|
70 |
|
sl@0
|
71 |
# Call this function (recursive) on
|
sl@0
|
72 |
# on each dependency of this library.
|
sl@0
|
73 |
copydeps $EXEPATH/$BASE
|
sl@0
|
74 |
fi
|
sl@0
|
75 |
fi
|
sl@0
|
76 |
done
|
sl@0
|
77 |
}
|
sl@0
|
78 |
|
sl@0
|
79 |
rm -f $EXEPATH/*
|
sl@0
|
80 |
|
sl@0
|
81 |
# Copy the binary into the bundle. Use ../libtool to do this if it's
|
sl@0
|
82 |
# available beacuse if $EXE_NAME was built with autotools, it will be
|
sl@0
|
83 |
# necessary. If ../libtool not available, just use cp to do the copy, but
|
sl@0
|
84 |
# only if $EXE_NAME is a binary.
|
sl@0
|
85 |
if [ -x ../libtool ]; then
|
sl@0
|
86 |
../libtool --mode=install cp $EXE_NAME $EXEPATH
|
sl@0
|
87 |
else
|
sl@0
|
88 |
file -bI $EXE_NAME |grep binary
|
sl@0
|
89 |
if [ $? -ne 0 ]; then
|
sl@0
|
90 |
echo "There is no ../libtool and $EXE_NAME is not a binary."
|
sl@0
|
91 |
echo "I'm not sure what to do."
|
sl@0
|
92 |
exit 1
|
sl@0
|
93 |
else
|
sl@0
|
94 |
cp $EXE_NAME $EXEPATH
|
sl@0
|
95 |
fi
|
sl@0
|
96 |
fi
|
sl@0
|
97 |
copydeps $EXEPATH/$EXE_NAME
|