copy_to_bundle.sh
changeset 0 b112defa99c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/copy_to_bundle.sh	Mon May 19 21:10:35 2014 +0200
     1.3 @@ -0,0 +1,97 @@
     1.4 +#!/bin/bash
     1.5 +
     1.6 +#### Configuration:
     1.7 +# The name of the executable. It is assumed
     1.8 +# that it is in the current working directory.
     1.9 +EXE_NAME=hidapi-testgui
    1.10 +# Path to the executable directory inside the bundle.
    1.11 +# This must be an absolute path, so use $PWD.
    1.12 +EXEPATH=$PWD/TestGUI.app/Contents/MacOS
    1.13 +# Libraries to explicitly bundle, even though they
    1.14 +# may not be in /opt/local. One per line. These
    1.15 +# are used with grep, so only a portion of the name
    1.16 +# is required. eg: libFOX, libz, etc.
    1.17 +LIBS_TO_BUNDLE=libFOX
    1.18 +
    1.19 +
    1.20 +function copydeps {
    1.21 +	local file=$1
    1.22 +	# echo "Copying deps for $file...."
    1.23 +	local BASE_OF_EXE=`basename $file`
    1.24 +
    1.25 +	# A will contain the dependencies of this library
    1.26 +	local A=`otool -LX $file |cut -f 1 -d " "`
    1.27 +	local i
    1.28 +	for i in $A; do
    1.29 +		local BASE=`basename $i`
    1.30 +
    1.31 +		# See if it's a lib we specifically want to bundle
    1.32 +		local bundle_this_lib=0
    1.33 +		local j
    1.34 +		for j in $LIBS_TO_BUNDLE; do
    1.35 +			echo $i |grep -q $j
    1.36 +			if [ $? -eq 0 ]; then
    1.37 +				bundle_this_lib=1
    1.38 +				echo "bundling $i because it's in the list."
    1.39 +				break;
    1.40 +			fi
    1.41 +		done
    1.42 +
    1.43 +		# See if it's in /opt/local. Bundle all in /opt/local
    1.44 +		local isOptLocal=0
    1.45 +		echo $i |grep -q /opt/local
    1.46 +		if [ $? -eq 0 ]; then
    1.47 +			isOptLocal=1
    1.48 +			echo "bundling $i because it's in /opt/local."
    1.49 +		fi
    1.50 +		
    1.51 +		# Bundle the library
    1.52 +		if [ $isOptLocal -ne 0 ] || [ $bundle_this_lib -ne 0 ]; then
    1.53 +
    1.54 +			# Copy the file into the bundle if it exists.
    1.55 +			if [ -f $EXEPATH/$BASE ]; then
    1.56 +				z=0
    1.57 +			else
    1.58 +				cp $i $EXEPATH
    1.59 +				chmod 755 $EXEPATH/$BASE
    1.60 +			fi
    1.61 +			
    1.62 +			
    1.63 +			# echo "$BASE_OF_EXE depends on $BASE"
    1.64 +			
    1.65 +			# Fix the paths using install_name_tool and then
    1.66 +			# call this function recursively for each dependency
    1.67 +			# of this library. 
    1.68 +			if [ $BASE_OF_EXE != $BASE ]; then
    1.69 +			
    1.70 +				# Fix the paths
    1.71 +				install_name_tool -id @executable_path/$BASE $EXEPATH/$BASE
    1.72 +				install_name_tool -change $i @executable_path/$BASE $EXEPATH/$BASE_OF_EXE
    1.73 +
    1.74 +				# Call this function (recursive) on
    1.75 +				# on each dependency of this library.
    1.76 +				copydeps $EXEPATH/$BASE
    1.77 +			fi
    1.78 +		fi
    1.79 +	done
    1.80 +}
    1.81 +
    1.82 +rm -f $EXEPATH/*
    1.83 +
    1.84 +# Copy the binary into the bundle. Use ../libtool to do this if it's
    1.85 +# available beacuse if $EXE_NAME was built with autotools, it will be
    1.86 +# necessary.  If ../libtool not available, just use cp to do the copy, but
    1.87 +# only if $EXE_NAME is a binary.
    1.88 +if [ -x ../libtool ]; then
    1.89 +	../libtool --mode=install cp $EXE_NAME $EXEPATH
    1.90 +else
    1.91 +	file -bI $EXE_NAME |grep binary
    1.92 +	if [ $? -ne 0 ]; then
    1.93 +		echo "There is no ../libtool and $EXE_NAME is not a binary."
    1.94 +		echo "I'm not sure what to do."
    1.95 +		exit 1
    1.96 +	else
    1.97 +		cp $EXE_NAME $EXEPATH
    1.98 +	fi
    1.99 +fi
   1.100 +copydeps $EXEPATH/$EXE_NAME