copy_to_bundle.sh 2.5 KB

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