harmonyos-tool.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/bash
  2. HVIGORWPATH=`which hvigorw 2>/dev/null`
  3. if [ "x$HVIGORWPATH" == "x" ]; then
  4. echo "Couldn't find hvigorw!"
  5. echo "Please make sure the HarmonyOS command line tools are in your PATH." 1>&2
  6. exit 1
  7. fi
  8. HDCPATH=`which hdc 2>/dev/null`
  9. if [ "x$HDCPATH" == "x" ]; then
  10. echo "Couldn't find hdc!"
  11. echo "Please make sure the HarmonyOS command line tools are in your PATH." 1>&2
  12. exit 1
  13. fi
  14. CLTBINPATH=`dirname "$HVIGORWPATH"`
  15. CLTPATH=`dirname "$CLTBINPATH"`
  16. CLT=`realpath "$CLTPATH"`
  17. #echo "CLT is '$CLT'"
  18. need_usage=0
  19. APPJSON=""
  20. PROJDIR="$1"
  21. if [ "x$PROJDIR" == "x" ]; then
  22. need_usage=1
  23. else
  24. PROJDIR=`realpath "$PROJDIR"`
  25. APPJSON="$PROJDIR/AppScope/app.json5"
  26. if [ ! -f "$APPJSON" ]; then
  27. echo "Can't find '$APPJSON'. Is project directory correct?" 1>&2
  28. echo "" 1>&2
  29. need_usage=1
  30. fi
  31. fi
  32. do_clean=0
  33. do_build=0
  34. do_uninstall=0
  35. do_install=0
  36. do_kill=0
  37. do_launch=0
  38. do_log=0
  39. do_debug=0
  40. do_anything=0
  41. shift # dump projectdir
  42. while [ $# -gt 0 ]; do
  43. do_anything=1
  44. case $1 in
  45. --clean)
  46. do_clean=1
  47. shift
  48. ;;
  49. --build)
  50. do_build=1
  51. shift
  52. ;;
  53. --install)
  54. do_install=1
  55. shift
  56. ;;
  57. --uninstall)
  58. do_uninstall=1
  59. shift
  60. ;;
  61. --kill)
  62. do_kill=1
  63. shift
  64. ;;
  65. --launch)
  66. do_launch=1
  67. shift
  68. ;;
  69. --log)
  70. do_log=1
  71. shift
  72. ;;
  73. --debug)
  74. do_launch=1
  75. do_debug=1
  76. shift
  77. ;;
  78. *)
  79. echo "Unknown option '$1'" 1>&2
  80. need_usage=1
  81. shift
  82. ;;
  83. esac
  84. done
  85. if [ "$do_anything" == "0" ]; then
  86. need_usage=1
  87. fi
  88. if [ "$need_usage" == "1" ]; then
  89. echo "USAGE: $0 <project_dir> [--kill] [--uninstall] [--build] [--install] [--launch] [--debug] [--log]" 1>&2
  90. echo "" 1>&2
  91. exit 1
  92. fi
  93. if [ "$do_build" == "1" ]; then
  94. if [ "$do_debug" == "1" ]; then
  95. do_install=1
  96. elif [ "$do_launch" == "1" ]; then
  97. do_install=1
  98. fi
  99. fi
  100. # Here we go!
  101. BUNDLE=`grep -F bundleName "$PROJDIR/AppScope/app.json5" |perl -w -p -e 's/\A\s*\"bundleName\"\s*\:\s*\"(.*?)\".*?\Z/$1/i;'`
  102. if [ "x$BUNDLE" == "x" ]; then
  103. echo "Failed to determine bundle name! Aborting!" 1>&2
  104. exit 1
  105. fi
  106. echo "BUNDLE is '$BUNDLE'"
  107. #set -x
  108. cd "$PROJDIR"
  109. if [ "$do_kill" == "1" ]; then
  110. echo "KILLING..."
  111. hdc shell aa force-stop $BUNDLE || exit 1
  112. fi
  113. if [ "$do_uninstall" == "1" ]; then
  114. echo "UNINSTALLING..."
  115. hdc uninstall $BUNDLE || exit 1
  116. fi
  117. if [ "$do_clean" == "1" ]; then
  118. echo "CLEANING..."
  119. hvigorw clean || exit 1
  120. fi
  121. if [ "$do_build" == "1" ]; then
  122. echo "BUILDING..."
  123. hvigorw assembleHap || exit 1
  124. fi
  125. if [ "$do_debug" == "1" ]; then
  126. hdc shell mkdir -p data/local/tmp/debugserver/$BUNDLE
  127. hdc file send "$CLT/sdk/default/hms/native/lldb/aarch64-linux-ohos/lldb-server" data/local/tmp/debugserver/$BUNDLE
  128. hdc shell chmod 755 data/local/tmp/debugserver/$BUNDLE/lldb-server
  129. fi
  130. if [ "$do_install" == "1" ]; then
  131. echo "INSTALLING..."
  132. hdc install "$PROJDIR/entry/build/default/outputs/default/entry-default-signed.hap" || exit 1
  133. fi
  134. # Catch sigint, kill the backgrounded `hdc hilog` then exit.
  135. trap_sigint() {
  136. if [ "$HILOGPID" != "" ]; then
  137. kill -9 "$HILOGPID"
  138. fi
  139. wait "$HILOGPID" 2>/dev/null
  140. echo ""
  141. #echo "Killed $HILOGPID"
  142. exit 0
  143. }
  144. HILOGPID=""
  145. if [ "$do_log" == "1" ]; then
  146. trap trap_sigint INT
  147. # This wild bash syntax puts `hdc` in the background, and pipes its output through `grep`, but `$!` will have hdc's process ID.
  148. # The ridiculous perl regular expression basically filters out a ton of garbage from each line of the log. You can remove it entirely and just grep for the bundle name, though!
  149. hdc hilog > >(grep -F --line-buffered "/$BUNDLE/" |perl -w -p -e 's/\A\d+\-\d+\s+(\d+\:\d+\:\d+)\.\d+\s+\d+\s+\d+\s+(.)\s+.*?\/.*?\/(.*?)\:\s+/$1 $2 $3: /;') & HILOGPID="$!"
  150. fi
  151. if [ "$do_launch" == "1" ]; then
  152. echo "LAUNCHING..."
  153. # !!! FIXME: maybe this makes the process wait on launch until debugger is attached?
  154. #hdc shell aa appdebug -b $BUNDLE
  155. hdc shell aa start -a EntryAbility -b $BUNDLE || exit 1
  156. fi
  157. if [ "$do_debug" == "1" ]; then
  158. echo "DEBUGGING..."
  159. hdc shell aa attach -b $BUNDLE
  160. hdc shell aa process -a EntryAbility -b $BUNDLE -D "/data/local/tmp/debugserver/$BUNDLE/lldb-server platform --listen unix-abstract:///lldb-server/platform.sock"
  161. PID=`hdc shell ps -w -o PID,ARGS |grep -F "$BUNDLE" |head -n 1 |awk '{print $1}'`
  162. if [ "x$PID" == "x" ]; then
  163. echo "Failed to determine PID! Aborting!" 1>&2
  164. exit 1
  165. fi
  166. echo "PID is $PID"
  167. trap - INT
  168. "$CLT/sdk/default/openharmony/native/llvm/bin/lldb" \
  169. -O "platform select remote-ohos" \
  170. -O "platform connect unix-abstract-connect:///lldb-server/platform.sock" \
  171. -O "settings append target.exec-search-paths \"$PROJDIR/entry/build/default/intermediates/cmake/default/obj/arm64-v8a\"" \
  172. -O "attach $PID"
  173. trap trap_sigint INT
  174. fi
  175. if [ "$HILOGPID" != "" ]; then
  176. #echo "$0: WAITING ON $HILOGPID"
  177. wait "$HILOGPID"
  178. fi