能源管控程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

221 lines
6.6 KiB

  1. #!/bin/bash
  2. #OSTYPE: 'linux-gnu', ARCH: 'x86_64' => linux64
  3. #OSTYPE: 'msys', ARCH: 'x86_64' => win32
  4. #OSTYPE: 'darwin18', ARCH: 'i386' => macos
  5. OSBITS=`arch`
  6. if [[ "$OSTYPE" == "linux"* ]]; then
  7. export OS_IS_LINUX="1"
  8. ARCHIVE_FORMAT="tar.xz"
  9. if [[ "$OSBITS" == "i686" ]]; then
  10. OS_NAME="linux32"
  11. elif [[ "$OSBITS" == "x86_64" ]]; then
  12. OS_NAME="linux64"
  13. elif [[ "$OSBITS" == "armv7l" || "$OSBITS" == "aarch64" ]]; then
  14. OS_NAME="linuxarm"
  15. else
  16. OS_NAME="$OSTYPE-$OSBITS"
  17. echo "Unknown OS '$OS_NAME'"
  18. exit 1
  19. fi
  20. elif [[ "$OSTYPE" == "darwin"* ]]; then
  21. export OS_IS_MACOS="1"
  22. ARCHIVE_FORMAT="zip"
  23. OS_NAME="macosx"
  24. elif [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
  25. export OS_IS_WINDOWS="1"
  26. ARCHIVE_FORMAT="zip"
  27. OS_NAME="windows"
  28. else
  29. OS_NAME="$OSTYPE-$OSBITS"
  30. echo "Unknown OS '$OS_NAME'"
  31. exit 1
  32. fi
  33. export OS_NAME
  34. ARDUINO_BUILD_DIR="$HOME/.arduino/build.tmp"
  35. ARDUINO_CACHE_DIR="$HOME/.arduino/cache.tmp"
  36. if [ "$OS_IS_MACOS" == "1" ]; then
  37. export ARDUINO_IDE_PATH="/Applications/Arduino.app/Contents/Java"
  38. export ARDUINO_USR_PATH="$HOME/Documents/Arduino"
  39. elif [ "$OS_IS_WINDOWS" == "1" ]; then
  40. export ARDUINO_IDE_PATH="$HOME/arduino_ide"
  41. export ARDUINO_USR_PATH="$HOME/Documents/Arduino"
  42. else
  43. export ARDUINO_IDE_PATH="$HOME/arduino_ide"
  44. export ARDUINO_USR_PATH="$HOME/Arduino"
  45. fi
  46. if [ ! -d "$ARDUINO_IDE_PATH" ]; then
  47. echo "Installing Arduino IDE on $OS_NAME ..."
  48. echo "Downloading 'arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT' to 'arduino.$ARCHIVE_FORMAT' ..."
  49. if [ "$OS_IS_LINUX" == "1" ]; then
  50. wget -O "arduino.$ARCHIVE_FORMAT" "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
  51. echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
  52. tar xf "arduino.$ARCHIVE_FORMAT" > /dev/null
  53. mv arduino-nightly "$ARDUINO_IDE_PATH"
  54. else
  55. curl -o "arduino.$ARCHIVE_FORMAT" -L "https://www.arduino.cc/download.php?f=/arduino-nightly-$OS_NAME.$ARCHIVE_FORMAT" > /dev/null 2>&1
  56. echo "Extracting 'arduino.$ARCHIVE_FORMAT' ..."
  57. unzip "arduino.$ARCHIVE_FORMAT" > /dev/null
  58. if [ "$OS_IS_MACOS" == "1" ]; then
  59. mv "Arduino.app" "/Applications/Arduino.app"
  60. else
  61. mv arduino-nightly "$ARDUINO_IDE_PATH"
  62. fi
  63. fi
  64. rm -rf "arduino.$ARCHIVE_FORMAT"
  65. mkdir -p "$ARDUINO_USR_PATH/libraries"
  66. mkdir -p "$ARDUINO_USR_PATH/hardware"
  67. echo "Arduino IDE Installed in '$ARDUINO_IDE_PATH'"
  68. echo ""
  69. fi
  70. function build_sketch(){ # build_sketch <fqbn> <path-to-ino> [extra-options]
  71. if [ "$#" -lt 2 ]; then
  72. echo "ERROR: Illegal number of parameters"
  73. echo "USAGE: build_sketch <fqbn> <path-to-ino> [extra-options]"
  74. return 1
  75. fi
  76. local fqbn="$1"
  77. local sketch="$2"
  78. local xtra_opts="$3"
  79. local win_opts=""
  80. if [ "$OS_IS_WINDOWS" == "1" ]; then
  81. local ctags_version=`ls "$ARDUINO_IDE_PATH/tools-builder/ctags/"`
  82. local preprocessor_version=`ls "$ARDUINO_IDE_PATH/tools-builder/arduino-preprocessor/"`
  83. win_opts="-prefs=runtime.tools.ctags.path=$ARDUINO_IDE_PATH/tools-builder/ctags/$ctags_version -prefs=runtime.tools.arduino-preprocessor.path=$ARDUINO_IDE_PATH/tools-builder/arduino-preprocessor/$preprocessor_version"
  84. fi
  85. echo ""
  86. echo "Compiling '"$(basename "$sketch")"' ..."
  87. mkdir -p "$ARDUINO_BUILD_DIR"
  88. mkdir -p "$ARDUINO_CACHE_DIR"
  89. $ARDUINO_IDE_PATH/arduino-builder -compile -logger=human -core-api-version=10810 \
  90. -fqbn=$fqbn \
  91. -warnings="all" \
  92. -tools "$ARDUINO_IDE_PATH/tools-builder" \
  93. -tools "$ARDUINO_IDE_PATH/tools" \
  94. -built-in-libraries "$ARDUINO_IDE_PATH/libraries" \
  95. -hardware "$ARDUINO_IDE_PATH/hardware" \
  96. -hardware "$ARDUINO_USR_PATH/hardware" \
  97. -libraries "$ARDUINO_USR_PATH/libraries" \
  98. -build-cache "$ARDUINO_CACHE_DIR" \
  99. -build-path "$ARDUINO_BUILD_DIR" \
  100. $win_opts $xtra_opts "$sketch"
  101. }
  102. function count_sketches() # count_sketches <examples-path>
  103. {
  104. local examples="$1"
  105. rm -rf sketches.txt
  106. if [ ! -d "$examples" ]; then
  107. touch sketches.txt
  108. return 0
  109. fi
  110. local sketches=$(find $examples -name *.ino)
  111. local sketchnum=0
  112. for sketch in $sketches; do
  113. local sketchdir=$(dirname $sketch)
  114. local sketchdirname=$(basename $sketchdir)
  115. local sketchname=$(basename $sketch)
  116. if [[ "${sketchdirname}.ino" != "$sketchname" ]]; then
  117. continue
  118. fi;
  119. if [[ -f "$sketchdir/.test.skip" ]]; then
  120. continue
  121. fi
  122. echo $sketch >> sketches.txt
  123. sketchnum=$(($sketchnum + 1))
  124. done
  125. return $sketchnum
  126. }
  127. function build_sketches() # build_sketches <fqbn> <examples-path> <chunk> <total-chunks> [extra-options]
  128. {
  129. local fqbn=$1
  130. local examples=$2
  131. local chunk_idex=$3
  132. local chunks_num=$4
  133. local xtra_opts=$5
  134. if [ "$#" -lt 2 ]; then
  135. echo "ERROR: Illegal number of parameters"
  136. echo "USAGE: build_sketches <fqbn> <examples-path> [<chunk> <total-chunks>] [extra-options]"
  137. return 1
  138. fi
  139. if [ "$#" -lt 4 ]; then
  140. chunk_idex="0"
  141. chunks_num="1"
  142. xtra_opts=$3
  143. fi
  144. if [ "$chunks_num" -le 0 ]; then
  145. echo "ERROR: Chunks count must be positive number"
  146. return 1
  147. fi
  148. if [ "$chunk_idex" -ge "$chunks_num" ]; then
  149. echo "ERROR: Chunk index must be less than chunks count"
  150. return 1
  151. fi
  152. set +e
  153. count_sketches "$examples"
  154. local sketchcount=$?
  155. set -e
  156. local sketches=$(cat sketches.txt)
  157. rm -rf sketches.txt
  158. local chunk_size=$(( $sketchcount / $chunks_num ))
  159. local all_chunks=$(( $chunks_num * $chunk_size ))
  160. if [ "$all_chunks" -lt "$sketchcount" ]; then
  161. chunk_size=$(( $chunk_size + 1 ))
  162. fi
  163. local start_index=$(( $chunk_idex * $chunk_size ))
  164. if [ "$sketchcount" -le "$start_index" ]; then
  165. echo "Skipping job"
  166. return 0
  167. fi
  168. local end_index=$(( $(( $chunk_idex + 1 )) * $chunk_size ))
  169. if [ "$end_index" -gt "$sketchcount" ]; then
  170. end_index=$sketchcount
  171. fi
  172. local start_num=$(( $start_index + 1 ))
  173. echo "Found $sketchcount Sketches";
  174. echo "Chunk Count : $chunks_num"
  175. echo "Chunk Size : $chunk_size"
  176. echo "Start Sketch: $start_num"
  177. echo "End Sketch : $end_index"
  178. local sketchnum=0
  179. for sketch in $sketches; do
  180. local sketchdir=$(dirname $sketch)
  181. local sketchdirname=$(basename $sketchdir)
  182. local sketchname=$(basename $sketch)
  183. if [ "${sketchdirname}.ino" != "$sketchname" ] \
  184. || [ -f "$sketchdir/.test.skip" ]; then
  185. continue
  186. fi
  187. sketchnum=$(($sketchnum + 1))
  188. if [ "$sketchnum" -le "$start_index" ] \
  189. || [ "$sketchnum" -gt "$end_index" ]; then
  190. continue
  191. fi
  192. build_sketch "$fqbn" "$sketch" "$xtra_opts"
  193. local result=$?
  194. if [ $result -ne 0 ]; then
  195. return $result
  196. fi
  197. done
  198. return 0
  199. }