Blame autobuild/osx/relocate-binary.sh

47e223
#!/bin/bash
47e223
47e223
set -e
faf931
#set -x
47e223
b9cca3
[ -z "$2" ] && 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
b9cca3
readlink_f() {
b9cca3
	TARGET_FILE=$1
47e223
b9cca3
	cd `dirname $TARGET_FILE`
b9cca3
	TARGET_FILE=`basename $TARGET_FILE`
b9cca3
b9cca3
	# Iterate down a (possible) chain of symlinks
b9cca3
	while [ -L "$TARGET_FILE" ]
b9cca3
	do
b9cca3
		TARGET_FILE=`readlink $TARGET_FILE`
b9cca3
		cd `dirname $TARGET_FILE`
b9cca3
		TARGET_FILE=`basename $TARGET_FILE`
b9cca3
	done
b9cca3
b9cca3
	# Compute the canonicalized name by finding the physical path 
b9cca3
	# for the directory we're in and appending the target file.
b9cca3
	PHYS_DIR=`pwd -P`
b9cca3
	RESULT=$PHYS_DIR/$TARGET_FILE
b9cca3
	echo $RESULT
b9cca3
}
47e223
faf931
remove_prefix() {
faf931
	local INPUT="$1"
faf931
	local OUTPUT=""
faf931
	if [[ "$INPUT" == /usr/local/Cellar/* ]]; then
faf931
		TMP="${INPUT#*/}" # usr/local/Cellar/pkgname/pkgversion/path
faf931
		TMP="${TMP#*/}"  # local/Cellar/pkgname/pkgversion/path
faf931
		TMP="${TMP#*/}"  # Cellar/pkgname/pkgversion/path
faf931
		TMP="${TMP#*/}"  # pkgname/pkgversion/path
faf931
		TMP="${TMP#*/}"  # pkgversion/path
faf931
		TMP="${TMP#*/}"  # path
faf931
		OUTPUT="${TMP}"
faf931
	elif [[ "$INPUT" == /usr/local/opt/* ]]; then
faf931
		TMP="${INPUT#*/}" # usr/local/opt/pkgname/path
faf931
		TMP="${TMP#*/}"  # local/opt/pkgname/path
faf931
		TMP="${TMP#*/}"  # opt/pkgname/path
faf931
		TMP="${TMP#*/}"  # pkgname/path
faf931
		TMP="${TMP#*/}"  # path
faf931
		OUTPUT="${TMP}"
faf931
	elif [[ "$INPUT" == /usr/local/lib/* ]]; then
faf931
		TMP="${INPUT#*/}" # usr/local/lib/file
faf931
		TMP="${TMP#*/}"  # local/lib/file
faf931
		TMP="${TMP#*/}"  # lib/file = path
faf931
		OUTPUT="${TMP}"
faf931
	elif [[ "$INPUT" == /usr/local/share/* ]]; then
faf931
		TMP="${INPUT#*/}" # usr/local/share/file
faf931
		TMP="${TMP#*/}"  # local/share/file
faf931
		TMP="${TMP#*/}"  # share/file = path
faf931
		OUTPUT="${TMP}"
faf931
	elif [[ "$INPUT" == $PREFIX* ]]; then
faf931
		#LINE2=`echo "${INPUT}" | cut -c1-$PREFIXLEN`
faf931
		OUTPUT="${INPUT:$PREFIXLEN}"
faf931
	fi
faf931
	echo "$OUTPUT"
faf931
}
faf931
47e223
process_lib() {
47e223
faf931
local FILESRC="$1"
faf931
local FILEDEST_SHORT="$2"
faf931
faf931
#local FILE_PARENT="$2"
faf931
b9cca3
b9cca3
47e223
	local FILEDEST="${DEST}/${FILEDEST_SHORT}"
faf931
47e223
	if [ ! -f "${FILEDEST}" ]; then
47e223
		
47e223
		
47e223
		if [ ! -d `dirname "${FILEDEST}"` ]; then
47e223
			mkdir -p `dirname "${FILEDEST}"`
47e223
		fi
b9cca3
		REALPATH=`readlink_f "${FILESRC}"`
b9cca3
		cp "${REALPATH}" "${FILEDEST}"
47e223
		chmod a+rw "$FILEDEST"
faf931
		
faf931
		
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
b9cca3
			#echo "$LINE"
47e223
			if $FIRST; then
47e223
				FIRST=false
47e223
			else
b9cca3
				LINE=`echo "$LINE" | sed -e 's/^[ \t]*//' | sed -e 's/ \(.*\)$//'`
b9cca3
		    
b9cca3
				# make sure file isn't referencing itself
b9cca3
				#A=$(basename "$FILEDEST")
b9cca3
				#B=$(basename "$LINE")
b9cca3
				if [ ! "$LINE" == "$FILESRC" ]; then
faf931
					LINE2=`remove_prefix "$LINE"`
faf931
					if [ ! -z "$LINE2" ]; then
faf931
					#if [ ! -z "$FILE_PARENT" ]; then
faf931
						#echo "   install_name_tool -change \"$LINE\" \"@rpath/$LINE2\" \"$FILEDEST\""
b9cca3
						install_name_tool -change "$LINE" "@rpath/$LINE2" "$FILEDEST"
faf931
						process_lib "$LINE" "$LINE2"
faf931
					#fi
faf931
					
b9cca3
					fi
b9cca3
				fi
47e223
            	
47e223
			fi
47e223
    	done
47e223
		#echo "... finished ${FILEDEST_SHORT}"
47e223
	fi
47e223
47e223
b9cca3
}
47e223
47e223
#scan "$BASE_FILE"
47e223
#scan "$BASE_FILE" subscan
47e223
b9cca3
#TARGET_FILE=`readlink_f "$1"`
b9cca3
TARGET_FILE="$1"
b9cca3
echo "Gathering deps for: $TARGET_FILE"
b9cca3
b9cca3
export DEST="$3"
b9cca3
#echo "DESTINATION: $DEST"
b9cca3
b9cca3
export PREFIX="$2"
b9cca3
export PREFIXLEN=${#PREFIX}
b9cca3
faf931
if [ ! -f "${TARGET_FILE}" ]; then
faf931
	echo "ERROR: File not found."
faf931
	exit 1
faf931
fi
faf931
faf931
FILEDEST_SHORT=`remove_prefix "${TARGET_FILE}"`
faf931
b9cca3
if [[ "${TARGET_FILE}" == ${PREFIX}* ]]; then
faf931
	process_lib "${TARGET_FILE}" "${FILEDEST_SHORT}"
b9cca3
fi
47e223
47e223
echo "Success."
47e223
echo ""
47e223