1
0

do 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env bash
  2. #
  3. # You may redistribute this program and/or modify it under the terms of
  4. # the GNU General Public License as published by the Free Software Foundation,
  5. # either version 3 of the License, or (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. #
  15. [[ -n "$PLATFORM" ]] \
  16. || PLATFORM=$(uname | tr '[:upper:]' '[:lower:]')
  17. [[ -n "$MARCH" ]] \
  18. || MARCH=$(uname -m | sed 's/i./x/g')
  19. build_dir="build_$PLATFORM"
  20. node_min_ver='v0.8.15'
  21. node_dl_ver='v0.10.24'
  22. read -d '' version_test <<"EOF"
  23. var currentVersion = process.version;
  24. var verArray = currentVersion.substring(1).split(".");
  25. var minVerArray = process.argv[process.argv.length-1].substring(1).split(".");
  26. for (var i = 0; i < minVerArray.length; i++) {
  27. if (Number(verArray[i]) < Number(minVerArray[i])) {
  28. process.exit(1);
  29. } else if (Number(verArray[i]) > Number(minVerArray[i])) {
  30. process.exit(0);
  31. }
  32. }
  33. EOF
  34. # return true if the input command exists in $PATH
  35. function cmd_exists() {
  36. type -P "$1" >/dev/null
  37. }
  38. # output an error and exit with a failed status
  39. function die() {
  40. printf '%s\n' "ERROR: $1" >&2
  41. exit 1
  42. }
  43. # detect and configure a copy of node.js to use with at least the minimum version
  44. function check_node_tool() {
  45. for node_tool in "$build_dir/nodejs/node/bin/node" 'nodejs' 'node'; do
  46. cmd_exists "$node_tool"
  47. if [ $? = 0 -o -f "$node_tool" ]; then
  48. "$node_tool" '' "$node_min_ver" <<< "$version_test" && {
  49. node_cmd="$node_tool"
  50. return 0
  51. }
  52. printf '%s\n' "You have a version of node [$node_tool] but it is too old [$($node_tool -v)]"
  53. fi
  54. done
  55. return 1
  56. }
  57. # detect and configure a sha256sum implementation to use that produces the expected sum for an empty string
  58. function get_shasum_tool() {
  59. expected_sum='01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b'
  60. for shasum_tool in 'sha256sum' 'gsha256sum' 'sha256' 'shasum -a 256' 'openssl sha256'; do
  61. if cmd_exists "${shasum_tool/ *}"; then
  62. [[ $($shasum_tool <<< '') =~ "$expected_sum" ]] && {
  63. shasum_cmd="$shasum_tool"
  64. return 0
  65. }
  66. fi
  67. done
  68. return 1
  69. }
  70. # download and configure a copy of node.js to use based on the current system
  71. function get_node_tool() {
  72. printf '%s %s\n\n' '###' "Installing node.js (you can bypass this step by manually installing node.js $node_min_ver or newer)"
  73. case "$PLATFORM-$MARCH" in
  74. linux-x86_64)
  75. node_download="http://nodejs.org/dist/$node_dl_ver/node-$node_dl_ver-linux-x64.tar.gz"
  76. node_sha='6ef93f4a5b53cdd4471786dfc488ba9977cb3944285ed233f70c508b50f0cb5f'
  77. ;;
  78. linux-x86)
  79. node_download="http://nodejs.org/dist/$node_dl_ver/node-$node_dl_ver-linux-x86.tar.gz"
  80. node_sha='fb6487e72d953451d55e28319c446151c1812ed21919168b82ab1664088ecf46'
  81. ;;
  82. linux-armv6l|linux-armv7l)
  83. # Raspberry Pi or Cubieboard
  84. node_download="http://nodejs.org/dist/$node_dl_ver/node-$node_dl_ver-linux-arm-pi.tar.gz"
  85. node_sha='bdd5e253132c363492fa24ed9985873733a10558240fd45b0a4a15989ab8da90'
  86. ;;
  87. darwin-x86_64)
  88. node_download="http://nodejs.org/dist/$node_dl_ver/node-$node_dl_ver-darwin-x64.tar.gz"
  89. node_sha='c1c523014124a0327d71ba5d6f737a4c866a170f1749f8895482c5fa8be877b0'
  90. ;;
  91. darwin-x86)
  92. node_download="http://nodejs.org/dist/$node_dl_ver/node-$node_dl_ver-darwin-x86.tar.gz"
  93. node_sha='8b8d2bf9828804c3f8027d7d442713318814a36df12dea97dceda8f4aff42b3c'
  94. ;;
  95. sunos-x86_64)
  96. node_download="http://nodejs.org/dist/$node_dl_ver/node-$node_dl_ver-sunos-x64.tar.gz"
  97. node_sha='7cb714df92055b93a908b3b6587ca388a2884b1a9b5247c708a867516994a373'
  98. ;;
  99. sunos-x86)
  100. node_download="http://nodejs.org/dist/$node_dl_ver/node-$node_dl_ver-sunos-x86.tar.gz"
  101. node_sha='af69ab26aae42b05841c098f5d11d17e21d22d980cd32666e2db45a53ddffe34'
  102. ;;
  103. *)
  104. printf '%s\n%s\n' \
  105. "No nodejs executable available for $PLATFORM-$MARCH" \
  106. "Please install nodejs (>= $node_min_ver) from your distribution package repository or from source"
  107. return 1
  108. ;;
  109. esac
  110. [[ -d "$build_dir/nodejs" ]] \
  111. && rm -r "$build_dir/nodejs"
  112. install -d "$build_dir/nodejs"
  113. pushd "$build_dir/nodejs" >/dev/null
  114. node_dl="node-${node_dl_ver}.tar.gz"
  115. if cmd_exists wget; then
  116. printf '\n%s %s ' '==>' "Downloading $node_download with wget..."
  117. wget -q "$node_download" -O "$node_dl"
  118. elif cmd_exists curl; then
  119. printf '%s %s ' '==>' "Downloading $node_download with curl..."
  120. curl -s "$node_download" > "$node_dl"
  121. elif cmd_exists fetch; then
  122. printf '%s %s ' '==>' "Downloading $node_download with fetch..."
  123. fetch "$node_download" -o "$node_dl"
  124. else
  125. die 'wget, curl or fetch is required download node.js but you have none!'
  126. fi
  127. [[ -f "$node_dl" ]] \
  128. || die 'Failed to download node.js'
  129. printf '%s\n' 'DONE!'
  130. printf '%s %s ' '==>' "Verifying the checksum of the downloaded archive..."
  131. [[ $($shasum_cmd "$node_dl") =~ $node_sha ]] \
  132. || die 'The downloaded file is damaged! Aborting'
  133. printf '%s\n' 'DONE!'
  134. printf '%s %s ' '==>' "Extracting the downloaded archive..."
  135. install -d node
  136. tar xzf "$node_dl" -C node --strip-components=1
  137. [[ -d 'node' ]] \
  138. || die 'An error prevented the archive from being extracted'
  139. printf '%s\n\n' 'DONE!'
  140. popd >/dev/null
  141. # Return with the success status of the check_node_tool function
  142. check_node_tool
  143. }
  144. cd "$(dirname $0)" \
  145. || die 'failed to set directory'
  146. [[ -d "$build_dir" ]] \
  147. || install -d "$build_dir" \
  148. || die "failed to create build dir $build_dir"
  149. get_shasum_tool \
  150. || die "couldn't find working sha256 shasum_tool";
  151. check_node_tool \
  152. || get_node_tool \
  153. || die "couldn't get working node.js implementation";
  154. "$node_cmd" ./node_build/make.js "$@"