Blame autobuild/osx-relocate-binary.sh

47e223
#!/bin/bash
47e223
47e223
set -e
47e223
47e223
[ -z "$3" ] && echo "usage: \"$0\" <file> <source-prefix> <destination-dir>" && echo "This utility relocates binary <file> from <source-prefix> to <destonation-dir> together with all dependencies and adds rpath support." && exit 1</destonation-dir></source-prefix></file></destination-dir></source-prefix></file>
47e223
47e223
PREFIX="$2"
47e223
PREFIXLEN=${#PREFIX}
47e223
47e223
DEST="$3"
47e223
47e223
process_lib() {
47e223
47e223
FILESRC=$1
47e223
if [[ "${FILESRC}" == ${PREFIX}* ]]; then
47e223
	#FILEDEST_SHORT=`echo "${FILESRC}" | cut -c1-$PREFIXLEN`
47e223
	local FILEDEST_SHORT="${FILESRC:$PREFIXLEN}"
47e223
	local FILEDEST="${DEST}/${FILEDEST_SHORT}"
47e223
	
47e223
	if [ ! -f "${FILEDEST}" ]; then
47e223
		
47e223
		
47e223
		if [ ! -d `dirname "${FILEDEST}"` ]; then
47e223
			mkdir -p `dirname "${FILEDEST}"`
47e223
		fi
47e223
		cp "${FILESRC}" "${FILEDEST}"
47e223
		chmod a+rw "$FILEDEST"
47e223
		install_name_tool -add_rpath ./ "$FILEDEST" > /dev/null 2>&1 || true
47e223
		
47e223
		
47e223
		echo "Relinking ${FILEDEST_SHORT} ..."
47e223
		local FIRST=true
47e223
		local LINE=
47e223
		local LINE2=
47e223
		otool -L "${FILEDEST}" | while read -r LINE; do
47e223
47e223
			if $FIRST; then
47e223
				FIRST=false
47e223
			else
47e223
 		       	LINE=`echo "$LINE" | sed -e 's/^[ \t]*//' | sed -e 's/ \(.*\)$//'`
47e223
            
47e223
            	# make sure file isn't referencing itself
47e223
            	#A=$(basename "$FILEDEST")
47e223
            	#B=$(basename "$LINE")
47e223
            	if [ ! "$LINE" == "$FILESRC" ] && [[ "$LINE" == $PREFIX* ]]; then
47e223
            		#LINE2=`echo "${LINE}" | cut -c1-$PREFIXLEN`
47e223
            		LINE2="${LINE:$PREFIXLEN}"
47e223
            		install_name_tool -change "$LINE" "@rpath/$LINE2" "$FILEDEST"
47e223
            		process_lib "$LINE"
47e223
            	fi
47e223
            	
47e223
			fi
47e223
    	done
47e223
		#echo "... finished ${FILEDEST_SHORT}"
47e223
	fi
47e223
fi
47e223
47e223
}
47e223
47e223
echo "Gathering deps for: $1"
47e223
47e223
#scan "$BASE_FILE"
47e223
#scan "$BASE_FILE" subscan
47e223
47e223
process_lib "$1"
47e223
47e223
echo "Success."
47e223
echo ""
47e223