functions 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # Build functions
  2. # Copyright 2020-2021 orbea
  3. # All rights reserved.
  4. #
  5. # Redistribution and use of this script, with or without modification, is
  6. # permitted provided that the following conditions are met:
  7. #
  8. # 1. Redistributions of this script must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. #
  11. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  12. # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  13. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  14. # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  15. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  16. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  17. # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  18. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  19. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  20. # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. summary=
  22. tab=' '
  23. CWD=$(pwd)
  24. TMP=${TMP:-/tmp/build}
  25. SRCROOT=${SRCROOT:-"$HOME"/src}
  26. GITSRC=${GITSRC:-"$SRCROOT"/git/installed}
  27. TARSRC=${TARSRC:-"$SRCROOT"/tar}
  28. CMAKE_INSTALL=1
  29. DEBUG_RELEASE=
  30. GITPATCHES=
  31. if [ -n "${NOINSTALL:-}" ]; then
  32. DST="$TMP"/install
  33. else
  34. DST=''
  35. fi
  36. _build () {
  37. if [ -r "$1/$1".build ] && [ -x "$1/$1".build ]; then
  38. printf %s\\n '' "Building $1/$1.build" ''
  39. log="$1|:|okay"
  40. BRANCH='' COMMIT='' ./"$1/$1".build || log="$1|:|fail"
  41. summary="${summary} \"${tab}$log\""
  42. [ -z "${2:-}" ] || eval "$2=\"\$${2} $1\""
  43. fi
  44. }
  45. _clone () {
  46. if [ ! -d "$1/.git" ] || [ ! -r "$1/.git" ] || [ ! -x "$1/.git" ]; then
  47. _slash () { case "$1" in */) return 1 ;; *) return 0 ;; esac; }
  48. dir="${1%/"$2"}"
  49. while ! _slash "$dir"; do
  50. dir="${dir%/}"
  51. done
  52. rm -rf -- "$1"
  53. mkdir -p -- "$dir"
  54. git clone "$3" "$1"
  55. unset -f _slash
  56. fi
  57. }
  58. _cmake () {
  59. mkdir -p -- "$TMP"
  60. cd -- "$TMP"
  61. src="$1"
  62. shift
  63. cmake -GNinja "$@" \
  64. -DCMAKE_INSTALL_PREFIX="$PKG" \
  65. -DCMAKE_SKIP_RPATH=TRUE \
  66. -DCMAKE_BUILD_TYPE="$RELEASE" "$src"/
  67. "${NINJA:=ninja}"
  68. [ "$CMAKE_INSTALL" != 1 ] || DESTDIR="$DST" "$NINJA" "$CMAKE_STRIP"
  69. unset src
  70. }
  71. _die () {
  72. ret="$1"; shift
  73. case "$ret" in
  74. : ) printf %s\\n "$@" >&2; return 0 ;;
  75. 0 ) printf %s\\n "$@" ;;
  76. * ) printf %s\\n "$@" >&2 ;;
  77. esac
  78. exit "$ret"
  79. }
  80. _download () {
  81. if [ ! -f "$1" ] || [ ! -r "$1" ]; then
  82. _slash () { case "$1" in */) return 1 ;; *) return 0 ;; esac; }
  83. dir="${1%/"$2"}"
  84. while ! _slash "$dir"; do
  85. dir="${dir%/}"
  86. done
  87. rm -f -- "$1"
  88. mkdir -p -- "$dir"
  89. wget --content-disposition -P "$dir" "$3"
  90. unset -f _slash
  91. fi
  92. }
  93. _exists () {
  94. r=0; cwd="$(pwd)"
  95. while [ $# -gt 0 ]; do
  96. v=1; arg="$1"; shift
  97. case "$arg" in
  98. ''|*/ )
  99. :
  100. ;;
  101. /* )
  102. if [ -f "$arg" ] && [ -x "$arg" ]; then
  103. printf %s\\n "$arg"
  104. v=0
  105. fi
  106. ;;
  107. ./* )
  108. if [ -f "$arg" ] && [ -x "$arg" ]; then
  109. pre="$(cd -- "${arg%%/*}/" && pwd)"
  110. printf %s\\n "${pre%/}/$arg"
  111. v=0
  112. fi
  113. ;;
  114. */* )
  115. if [ -f "$arg" ] && [ -x "$arg" ]; then
  116. printf %s\\n "$(cd -- "${arg%%/*}/.." && pwd)/$arg"
  117. v=0
  118. fi
  119. ;;
  120. * )
  121. if [ -n "${PATH+x}" ]; then
  122. p=":${PATH:-$cwd}"
  123. while [ "$p" != "${p#*:}" ] && [ -n "${p#*:}" ]; do
  124. p="${p#*:}"; x="${p%%:*}"; z="${x:-$cwd}"; d="${z%/}/$arg"
  125. if [ -f "$d" ] && [ -x "$d" ]; then
  126. case "$d" in
  127. /* ) : ;;
  128. ./* ) pre="$(cd -- "${d%/*}/" && pwd)"; d="${pre%/}/$d" ;;
  129. * ) d="$(cd -- "${d%/*}/" && pwd)/$arg" ;;
  130. esac
  131. printf %s\\n "$d"
  132. v=0
  133. break
  134. fi
  135. done
  136. fi
  137. ;;
  138. esac
  139. [ $v = 0 ] || r=1
  140. done
  141. return $r
  142. }
  143. _flags () {
  144. if [ "${DEBUG:=0}" != 0 ]; then
  145. DEBUG=1
  146. BLDFLAGS="${BLDFLAGS:--O0 -pipe}"
  147. case "${1:-}" in
  148. cmake )
  149. CMAKE_STRIP='install'
  150. RELEASE="${DEBUG_RELEASE:-Debug}"
  151. ;;
  152. meson )
  153. NDEBUG=false
  154. RELEASE="${DEBUG_RELEASE:-debug}"
  155. ;;
  156. * )
  157. BLDFLAGS="${BLDFLAGS} -g"
  158. ;;
  159. esac
  160. else
  161. BLDFLAGS="${BLDFLAGS:--O2 -pipe}"
  162. case "${1:-}" in
  163. cmake )
  164. BLDFLAGS="${BLDFLAGS} -DNDEBUG"
  165. CMAKE_STRIP='install/strip'
  166. RELEASE=None
  167. ;;
  168. meson )
  169. NDEBUG=true
  170. RELEASE=plain
  171. ;;
  172. esac
  173. fi
  174. }
  175. _git () {
  176. _clone "$1" "$2" "$3"
  177. _git_update "$4" "$1" "$5" "$6" "${7:-}"
  178. [ "$2" != "${PRGNAM:?}" ] || _git_version
  179. }
  180. _git_update () {
  181. rm -rf -- "$1"
  182. cd -- "$2" || return 1
  183. git reset --hard origin/"$3"
  184. git clean -xdff
  185. git checkout "$3"
  186. sleep 1
  187. [ -n "${NOUPDATE:=}" ] || git pull
  188. [ -z "$4" ] || git checkout "$4"
  189. for patch in $(printf %s "$GITPATCHES"); do
  190. git apply -3 "$CWD/$patch"
  191. done
  192. if [ -n "${5:-}" ]; then
  193. git submodule foreach --recursive git clean -xdff
  194. git submodule foreach --recursive git reset --hard
  195. [ -n "${NOUPDATE}" ] || git submodule update --init --recursive
  196. fi
  197. }
  198. _git_version () {
  199. HEAD="$(git rev-parse --short HEAD)"
  200. DATE="$(git show -s --format=%cd --date=format:%Y.%m.%d)"
  201. VERSION="${DATE}_$HEAD"
  202. : "$VERSION"
  203. }
  204. _install () {
  205. docdir="$3"/share/doc/"$1"
  206. srcdir="$4"
  207. mkdir -p -- "$DST$docdir"
  208. printf %s\\n "$1-$2" > "$DST$docdir/$1".version
  209. cat -- "$CWD/$1".build > "$DST$docdir/$1".build
  210. shift 4
  211. for f do
  212. if [ ! -e "$srcdir/$f" ]; then
  213. _die : "WARNING: File '$f' not found"
  214. elif [ -e "$DST$docdir/${f#*/}" ]; then
  215. _die : "WARNING: File '$f' already installed"
  216. elif [ ! -s "$srcdir/$f" ]; then
  217. _die : "WARNING: Zero length file '$f'"
  218. else
  219. cp -p -- "$srcdir/$f" "$DST$docdir/"
  220. fi
  221. done
  222. unset docdir srcdir
  223. }
  224. _install_strip () {
  225. _strip "$3"
  226. _install "$@"
  227. }
  228. _make () {
  229. mkdir -p -- "$TMP"
  230. cd -- "$TMP"
  231. target="${1:-install}"
  232. shift
  233. make "$target" -f "$@"
  234. unset target
  235. }
  236. _meson () {
  237. out="$1"
  238. shift
  239. meson "$@" \
  240. --prefix="$PKG" \
  241. -Dstrip=$NDEBUG \
  242. -Db_ndebug=$NDEBUG \
  243. -Dbuildtype=$RELEASE \
  244. "$out"
  245. "${NINJA:=ninja}" -C "$out"
  246. DESTDIR="$DST" "$NINJA" -C "$out" install
  247. unset out
  248. }
  249. _strip () {
  250. [ "${DEBUG:-0}" = 0 ] || return 0
  251. case "$DST$1" in
  252. -* ) f="./$DST$1" ;;
  253. * ) f="$DST$1" ;;
  254. esac
  255. find "$f" -print0 | xargs -0 file | grep -e executable -e 'shared object' |
  256. grep ELF | cut -f 1 -d : | xargs strip 2> /dev/null || :
  257. unset f
  258. }
  259. _summary () {
  260. eval "set -- $summary"
  261. printf %s\\n '' 'Summary:' ''
  262. printf %s\\n "$@" | column -t -o ' ' -s '|'
  263. printf %s\\n ''
  264. }
  265. _summary2 () {
  266. { [ -f "$2/${1}_summary" ] && [ -r "$2/${1}_summary" ]; } || return 0
  267. printf %s\\n "Summary for $1:"
  268. cat -- "$2/${1}"_summary
  269. }