-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix-libraries-INDIWebManager.sh
executable file
·217 lines (169 loc) · 6.75 KB
/
fix-libraries-INDIWebManager.sh
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#/bin/zsh
# This script has three goals:
# 1) It makes sure the DMG folder is set up, INDIWebManagerApp is copied there, and the variables aree correct.
# 2) identify programs that use libraries outside of the package (that meet certain criteria)
# 3) copy those libraries to the blah/Frameworks dir
# 4) Update those programs to know where to look for said libraries
DIR=$(dirname "$0")
#This adds a file to the list so it can be copied to Frameworks
function addFileToCopy
{
for e in "${FILES_TO_COPY[@]}"
do
if [[ "$e" == "$1" ]]
then
return 0
fi
done
FILES_TO_COPY+=($1)
}
#This Function processes a given file using otool to see what files it is using
#Then it uses install_name_tool to change that target to be a file in Frameworks
#Finally, it adds the file that it changed to the list of files to copy there.
function processTarget
{
target=$1
#This hard coded rpath needs to be removed from any files that have it for packaged apps because later there could be rpath conflicts
#if the program is run on a computer with the same paths as the build computer
install_name_tool -delete_rpath ${CRAFT_DIR}/lib $target
entries=$(otool -L $target | sed '1d' | awk '{print $1}' | egrep -v "$IGNORED_OTOOL_OUTPUT")
echo "Processing $target"
relativeRoot="${INDI_WEB_MANAGER_APP}/Contents"
pathDiff=${target#${relativeRoot}*}
#This is a Framework file
if [[ "$pathDiff" == /Frameworks/* ]]
then
newname="@rpath/$(basename $target)"
install_name_tool -add_rpath "@loader_path/" $target
echo " This is a Framework, change its own id $target -> $newname"
install_name_tool -id \
$newname \
$target
else
pathToFrameworks=$(echo $(dirname "${pathDiff}") | awk -F/ '{for (i = 1; i < NF ; i++) {printf("../")} }')
pathToFrameworks="${pathToFrameworks}Frameworks/"
install_name_tool -add_rpath "@loader_path/${pathToFrameworks}" $target
fi
for entry in $entries
do
baseEntry=$(basename $entry)
newname=""
newname="@rpath/${baseEntry}"
echo " change reference $entry -> $newname"
install_name_tool -change \
$entry \
$newname \
$target
addFileToCopy "$entry"
done
echo ""
echo " otool for $target after"
otool -L $target | egrep -v "$IGNORED_OTOOL_OUTPUT" | awk '{printf("\t%s\n", $0)}'
}
#This copies all of the files in the list into Frameworks
function copyFilesToFrameworks
{
FILES_COPIED=0
for libFile in "${FILES_TO_COPY[@]}"
do
# if it starts with a / then easy.
#
base=$(basename $libFile)
if [[ $libFile == /* ]]
then
filename=$libFile
else
# see if I can find it, NOTE: I had to add | cut -d" " -f1 because the find produced multiple results breaking the file copy.
# I also had to add | awk -F '.dSYM' '{print $1}' because it sometimes found a file with the same name inside the .dSYM file
filename=$(echo $(find "${CRAFT_DIR}/lib" -name "${base}")| cut -d" " -f1| awk -F '.dSYM' '{print $1}')
if [[ "$filename" == "" ]]
then
filename=$(echo $(find $(brew --prefix)/lib -name "${base}")| cut -d" " -f1| awk -F '.dSYM' '{print $1}')
fi
fi
if [ ! -f "${FRAMEWORKS_DIR}/${base}" ]
then
echo "HAVE TO COPY [$base] from [${filename}] to Frameworks"
cp -fL "${filename}" "${FRAMEWORKS_DIR}"
FILES_COPIED=$((FILES_COPIED+1))
# Seem to need this for the macqtdeploy
#
chmod +w "${FRAMEWORKS_DIR}/${base}"
else
echo ""
echo "Skipping Copy: $libFile already in Frameworks "
fi
done
}
function processDirectory
{
directoryName=$1
directory=$2
statusBanner "Processing all of the $directoryName files in $directory"
FILES_TO_COPY=()
for file in ${directory}/*
do
base=$(basename $file)
statusBanner "Processing $directoryName file $base"
processTarget $file
done
statusBanner "Copying required files for $directoryName into frameworks"
copyFilesToFrameworks
}
#########################################################################
#This is where the main part of the script starts!!
#
#This code should only run if the user is running the fix-libraries script without running build-indiwebmanagerapp.sh or generate-dmg.sh
if [ -z "${ASTRO_ROOT}" ]
then
source ${DIR}/build-env.sh
fi
#This sets some important variables.
DMG_DIR="${ASTRO_ROOT}/INDIWebManagerAppDMG"
INDI_WEB_MANAGER_APP="${DMG_DIR}/INDIWebManagerApp.app"
FRAMEWORKS_DIR="${INDI_WEB_MANAGER_APP}/Contents/Frameworks"
#This should stop the script so that it doesn't run if these paths are blank.
#That way it doesn't try to edit /Applications instead of ${CRAFT_DIR}/Applications for example
if [ -z "${DIR}" ] || [ -z "${DMG_DIR}" ] || [ -z "${CRAFT_DIR}" ]
then
echo "directory error! aborting Fix Libraries Script"
exit 9
fi
#This code makes sure the craft directory exists. This won't work too well if it doesn't
if [ ! -e ${CRAFT_DIR} ]
then
"Craft directory does not exist. You have to build INDIWebManager with Craft first. Use build-INDIWebManagerApp.sh"
exit
fi
#This code should make sure the INDI_WEB_MANAGER_APP and the DMG Directory are set correctly.
if [ ! -e ${DMG_DIR} ] || [ ! -e ${INDI_WEB_MANAGER_APP} ]
then
"INDIWebManager.app does not exist in the DMG Directory. Please run build-indiwebmanagerapp.sh first!"
exit
fi
announce "Running Fix Libraries Script"
FILES_TO_COPY=()
#Files in these locations do not need to be copied into the Frameworks folder.
IGNORED_OTOOL_OUTPUT="/Qt|${INDI_WEB_MANAGER_APP}/|/usr/lib/|/System/"
cd ${DMG_DIR}
statusBanner "Processing INDIWebManager.app executable and other things in the MacOS directory"
processDirectory MacOS "${INDI_WEB_MANAGER_APP}/Contents/MacOS"
statusBanner "Copying first round of files"
copyFilesToFrameworks
statusBanner "Processing Needed plugins and resources"
processDirectory GPHOTO_IOLIBS "${INDI_WEB_MANAGER_APP}/Contents/Resources/DriverSupport/gphoto/IOLIBS"
processDirectory GPHOTO_CAMLIBS "${INDI_WEB_MANAGER_APP}/Contents/Resources/DriverSupport/gphoto/CAMLIBS"
processDirectory MathPlugins "${INDI_WEB_MANAGER_APP}/Contents/Resources/MathPlugins"
statusBanner "Processing possibly needed plugins"
#I am not sure if we need the following plugins, but if we are going to include these plugins, they should not be linked to craft-root?
processDirectory Platforms "${INDI_WEB_MANAGER_APP}/Contents/Plugins/platforms"
processDirectory bearer "${KSTARS_APP}/Contents/Plugins/bearer"
statusBanner "Processing Frameworks"
processDirectory Frameworks "${FRAMEWORKS_DIR}"
while [ ${FILES_COPIED} -gt 0 ]
do
statusBanner "${FILES_COPIED} more files were copied into Frameworks, we need to process it again."
processDirectory Frameworks "${FRAMEWORKS_DIR}"
done
statusBanner "The following files are now in Frameworks:"
ls -lF ${FRAMEWORKS_DIR}