Guest User

fix_naoqi

a guest
Oct 18th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.29 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if test "$1" = "" -o "$1" = "-h" -o "$1" = "--help" ; then
  4.     echo "usage: $0 [-h] path/to/naoqi-bin"
  5.     exit 1
  6. fi
  7.  
  8. naoqipath=$1
  9. bin_folder=""
  10. top_folder=""
  11.  
  12. function detectSDK {
  13.     bin_folder=$(dirname "$naoqipath")
  14.     bin_folder=$(cd "$bin_folder";pwd)
  15.     naoqi_file="$bin_folder/naoqi-bin"
  16.     if [ ! -x "$naoqi_file" ] ; then
  17.         echo "Error: file not found: $naoqi_file"
  18.         exit 1
  19.     fi
  20.     echo "Found naoqi-bin:         $naoqi_file"
  21.     lib_folder=$(cd "$bin_folder/../lib";pwd)
  22.     if [ ! -x "$lib_folder/libinaoqi.dylib" ] ; then
  23.         echo "Error: lib folder not found: $lib_folder"
  24.         exit 1
  25.     fi
  26.     echo "Found lib folder:        $lib_folder"
  27.     top_folder=$(cd "$bin_folder/..";pwd)
  28.     if [ ! -e "$top_folder/QtXml.framework" ] ; then
  29.         echo "Error: framework folder not found: $top_folder"
  30.         exit 1
  31.     fi
  32.     echo "Found frameworks folder: $top_folder"
  33. }
  34.  
  35. strindex() {
  36.   awk -v a="$1" -v b="$2" 'BEGIN{print index(a,b)}'
  37. }
  38.  
  39. function correctLink {
  40.     link="$1"
  41.     file="$2"
  42.     # 3 cases:
  43.     # /Users/dmerejkowsky/Qt5.4.1/5.4/clang_64/lib/QtXml.framework/Versions/5/QtXml : replace /Users/dmerejkowsky/Qt5.4.1/5.4/clang_64/lib/ by $top_folder
  44.     size=${#link}
  45.     if [ $size > 45 ] ; then
  46.         cleanDmerej="${link:45}"
  47.     if [ -f "$top_folder"/$cleanDmerej ] ; then
  48. #            echo "$top_folder/$cleanDmerej exists!"
  49.             install_name_tool -change $link @rpath/$cleanDmerej "$file"
  50. #            echo "$link cleaned"
  51.             return
  52.         fi
  53.     fi
  54.  
  55.     # /Users/opennao/wget-1.15/tinyxml/build/libtinyxml.dylib : replace /Users/opennao/wget-1.15/tinyxml/build/ by $lib_folder
  56.     if [ $size > 39 ] ; then
  57.         cleanOpennao="${link:39}"
  58.         if [ -f "$lib_folder"/$cleanOpennao ] ; then
  59. #            echo "$lib_folder/$cleanOpennao exists!"
  60.             install_name_tool -change $link @rpath/lib/$cleanOpennao "$file"
  61. #            echo "$link cleaned"
  62.             return
  63.         fi
  64.     fi
  65.  
  66.  
  67.     # libboost_python.dylib : add $lib_folder
  68.     if [ -f "$lib_folder"/$link ] ; then
  69. #        echo "$lib_folder/$link exists!"
  70.         install_name_tool -change $link @rpath/lib/$link "$file"
  71. #        echo "$link cleaned"
  72.         return
  73.     fi
  74.  
  75. #     echo "Could not clean $link"
  76.  
  77. }
  78.  
  79. detectSDK
  80.  
  81. echo ""
  82. echo "going through $top_folder"
  83.  
  84. file_list=$(find "$top_folder" ! \( -name "*.py" -o -name "*.h" -o -name "*.hpp" \) -type f -perm +111 -print)
  85.  
  86. count=0
  87. total=$(find "$top_folder" ! \( -name "*.py" -o -name "*.h" -o -name "*.hpp" \) -type f -perm +111 -print | wc -l)
  88. echo $total
  89. pstr="[=======================================================================]"
  90.  
  91. echo "$file_list" | while read current_file; do
  92.     count=$(( $count + 1 ))
  93.     pd=$(( $count * 73 / $total ))
  94. #   valid when no fatal error
  95. #    echo "\r%3d.%1d%% %.${pd}s" $(( $count * 100 / $total )) $(( ($count * 1000 /
  96. #$total) % 10 )) $pstr
  97.     printf "\r%3d.%1d%% %.${pd}s" $(( $count * 100 / $total )) $(( ($count * 1000 / $total) % 10 )) $pstr
  98.  
  99.  
  100.     if [ -d "$current_file" ] ; then
  101.         continue
  102.     fi
  103.     if [ ! -f "$current_file" ] ; then
  104.         continue
  105.     fi
  106.     if [ ! -x "$current_file" ] ; then
  107.         continue
  108.     fi
  109.  
  110.     file_links=$(otool -LX "$current_file" 2> /dev/null)
  111.  
  112.     echo "$file_links" | while read a; do
  113.         end_index=$(strindex "$a" " (compatibility")
  114.         original_path="${a:0:$end_index}"
  115.         actual_path="$original_path"
  116.         relative=false
  117.         if [[ $actual_path == "@rpath"* ]] ; then
  118.             actual_path="$top_folder/${actual_path:7}"
  119.             relative=true
  120.         fi
  121.         if [ -e "$actual_path" ] ; then
  122.             continue
  123.         fi
  124.  
  125.         if  $relative  ; then
  126.             #echo "$original_path is not correct but it is relative: cannot fix."
  127.             continue
  128.         fi
  129.         correctLink "$original_path" "$current_file"
  130.  
  131.     done
  132.  
  133.     install_name_tool -change /usr/local/lib/libsndfile.1.dylib @rpath/lib/libsndfile.1.dylib "$bin_folder"/../lib/naoqi/libmodularityperception.dylib
  134.     install_name_tool -change /usr/local/lib/libsndfile.1.dylib @rpath/lib/libsndfile.1.dylib "$bin_folder"/../lib/libaudiodevicemanager.dylib
  135.     install_name_tool -change /usr/local/lib/libsndfile.1.dylib @rpath/lib/libsndfile.1.dylib "$bin_folder"/../lib/libalmodularity_audio.dylib
  136.  
  137. done
Advertisement
Add Comment
Please, Sign In to add comment