s243a

build-pkg.sh (modified)

Dec 5th, 2020 (edited)
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.31 KB | None | 0 0
  1. #!/bin/ash
  2. # FatdogArm/Fatdog64 package build script
  3. # Copyright (C) James Budiono, 2013, 2014, 2015, 2017, 2018
  4. # License: GNU GPL Version 3 or later
  5. #
  6. # This is the version of build-pkg that will build a single package
  7. # from *within* FatdogArm/Fatdog64.
  8. #
  9. # The usual build pre-requisites (toolchain, make, bison, etc)
  10. # must alread be installed.
  11. #
  12. # $1-pkgname (any variation)
  13.  
  14. [ $(id -u) -ne 0 ] && exit # must run as root
  15. export LANG=C # make it run a bit faster
  16.  
  17. ### configuration settings (overridable)
  18. [ $BOOTSTATE_PATH ] && [ -e $BOOTSTATE_PATH ] &&
  19. . $BOOTSTATE_PATH # AUFS_ROOT_ID
  20.  
  21. BUILD_DIR="/mnt/+aufs+devsave+build+ext4"
  22. BUILD_DIR_PREFIX=${BUILD_DIR_PREFIX:-/tmp}
  23. BUILD_DIR_OLD=${BUILD_DIR:-$(mktemp -d -p $BUILD_DIR_PREFIX build-pkg.XXXXXXXX)}
  24. CHROOT_DIR=/tmp/chroot-${BUILD_DIR_OLD##*/}
  25. #UNIONFS_FUSE=${UNIONFS_FUSE-unionfs} # leave blank to use aufs
  26.  
  27. SANDBOX_ROOT=/mnt/sb # enable unionfs by default if we are inside sandbox
  28. grep -q $SANDBOX_ROOT /sys/fs/aufs/$AUFS_ROOT_ID/br0 &&
  29. UNIONFS_FUSE=${UNIONFS_FUSE-unionfs}
  30.  
  31. DEF_PKGARCH=x86_64
  32. PKG=${1##*/}; PKG=${PKG%/}
  33. PKGARCH=${PKGARCH:-$DEF_PKGARCH}
  34. PKG_TYPE=${PKG_TYPE:-txz}
  35. PKG_DIR=${PKG_DIR:-pkg}
  36. SRC_DIR=$(readlink -f ${SRC_DIR:-src})
  37. PATCHES_DIR=$(readlink -f ${PATCHES_DIR:-patches})
  38. OUTPUT_DIR=$(readlink -f ${OUTPUT_DIR:-output})
  39. #KEEP_DIR=1
  40. PKG_COPY=${PKG_COPY:-usr/src/pkgbuild/source} # keep a copy of the pkg here
  41. PKG_COPY=${PKG_COPY#/} # make sure not absolute path
  42. MAKEPKG=makepkg
  43. #FINDPKG_CACHE= # inherited - to speed up find_pkg
  44.  
  45. ### sanity check
  46. if ! [ "$SRC_DIR" -a "$OUTPUT_DIR" ]; then
  47.     echo >&2 'Error: $SRC_DIR or $OUTPUT_DIR are ""'
  48.     exit 1
  49. fi
  50.  
  51. # load our helper functions
  52. . ${0%/*}/build-lib.sh
  53.  
  54. ########### ACTIONS ###########
  55. ###
  56. trap 'cleanup; exit $RESULT' INT TERM HUP 0
  57. CLEANED="" RESULT=1
  58. cleanup() {
  59.     [ $CLEANED ] && return; CLEANED=yes
  60.     if mountpoint -q $CHROOT_DIR; then
  61.         busybox fuser -m $CHROOT_DIR | xargs kill 2> /dev/null
  62.         sleep 1
  63.         busybox fuser -m $CHROOT_DIR | xargs kill -9 2> /dev/null
  64.         for p in dev/pts dev sys proc /; do
  65.             umount $CHROOT_DIR/$p
  66.         done
  67.     fi
  68.     rmdir $CHROOT_DIR
  69.     [ -z $KEEP_DIR ] && ! [ -e $BUILD_DIR/keep ] &&
  70.     rm -r $BUILD_DIR
  71.     echo done
  72. }
  73.  
  74. ###
  75. init() {
  76.     # clean slate
  77.     PKG_NOMAKEPKG= PKG_NOBUILD= PKG_NOSTRIP=
  78.  
  79.     if [ $UNIONFS_FUSE ]; then
  80.         # check whether we have the binary and can use this mode
  81.         type $UNIONFS_FUSE > /dev/null || UNIONFS_FUSE=""
  82.     fi
  83.  
  84.     chmod 0755 $BUILD_DIR # mktemp always creates 0700
  85.     mkdir -p $PKG_DIR $SRC_DIR $OUTPUT_DIR $CHROOT_DIR
  86.     if [ $UNIONFS_FUSE ]; then
  87.         unionfs -o cow -o hide_meta_files $BUILD_DIR=RW:/ $CHROOT_DIR || exit
  88.         mkdir -p $BUILD_DIR/.unionfs-fuse/tmp_HIDDEN~ $BUILD_DIR/tmp # hide the real /tmp for building
  89.     else
  90.         local robranches
  91.         [ -z "$AUFS_ROOT_ID" ] && AUFS_ROOT_ID=$(
  92. awk '{ if ($2 == "/" && $3 == "aufs") { match($4,/si=[0-9a-f]*/); print "si_" substr($4,RSTART+3,RLENGTH-3) } }' \
  93. /proc/mounts | tail -n 1
  94. )
  95.         if [ $AUFS_ROOT_ID ]; then
  96.             robranches=$(ls -v /sys/fs/aufs/$AUFS_ROOT_ID/br[0-9]* | xargs sed 's|=rw|=ro|' | tr '\n' ':')
  97.         else
  98.             # not using aufs - assuming we can use root as aufs branch
  99.             robranches=/
  100.         fi 
  101.         mount -t aufs aufs -o br:$BUILD_DIR:$robranches $CHROOT_DIR || exit
  102.         rm -rf $CHROOT_DIR/tmp/* # clean /tmp for building
  103.     fi
  104.     for p in sys proc dev dev/pts; do
  105.         mount -o bind /$p $CHROOT_DIR/$p
  106.     done
  107. }
  108.  
  109. ###
  110. prepare() {
  111.     find_pkg $PKG_DIR $PKG $FINDPKG_CACHE || return 1
  112.     echo preparing $PKG
  113.        
  114.     # create generic build script, can be overridden if need be
  115.     cat > $CHROOT_DIR/tmp/build.sh << EOF
  116. #!/bin/sh
  117. [ -e /tmp/functions.sh ] && . /tmp/functions.sh
  118. [ -e /tmp/env.sh ] && . /tmp/env.sh
  119. [ -e /tmp/pkg/recipe ] && . /tmp/pkg/recipe
  120. type setup_arch_vars > /dev/null && setup_arch_vars
  121. type pkg_build > /dev/null && pkg_build ||
  122. { echo "Build error $PKG, dropping to temporary shell."; sh < /dev/tty > /dev/tty 2>/dev/tty; }
  123. EOF
  124.     chmod +x $CHROOT_DIR/tmp/build.sh
  125.    
  126.     # load default functions and env, and reload package info (override ones in function/env)
  127.     [ -e functions.sh ] && . ./functions.sh && cp functions.sh $CHROOT_DIR/tmp
  128.     [ -e env.sh ] && . ./env.sh && cp env.sh $CHROOT_DIR/tmp
  129.     . $PKG_FULL_PATH/recipe
  130.     type setup_arch_vars > /dev/null && setup_arch_vars
  131.    
  132.     # copy pkg files to build dir
  133.     cp -a $PKG_FULL_PATH $CHROOT_DIR/tmp/pkg
  134.    
  135.     # run prepare
  136.     type pkg_prepare > /dev/null && ( cd $CHROOT_DIR; pkg_prepare; )
  137.     [ $? -ne 0 ] && echo "Failed prepare phase." && return 1
  138.     return 0
  139. }
  140.  
  141. ### $@ - commands
  142. enter_chroot() {
  143.     LD_LIBRARY_PATH= chroot $CHROOT_DIR /bin/env -i HOME=/root USER=root TERM=$TERM \
  144.     BUILD32="-m32" BUILD64="-m64" CLFS_TARGET32="i686-pc-linux-gnu" \
  145.     PKG_CONFIG_PATH32=/usr/lib/pkgconfig PKG_CONFIG_PATH64=/usr/lib64/pkgconfig \
  146.     PS1='\u:\w# ' PATH=/usr/bin:/usr/sbin:/bin:/sbin:/tools/bin \
  147.     "$@"
  148. }
  149.  
  150. ###
  151. build() {
  152.     echo building $PKG
  153.     #chroot $CHROOT_DIR /tmp/build.sh
  154.     [ "$PKG_NOBUILD" ] && return 0
  155.    
  156.     enter_chroot /tmp/build.sh
  157.     [ $? -ne 0 ] && echo "Failed build phase." && return 1
  158.     return 0
  159. }
  160.  
  161. ###
  162. package() {
  163.     echo packaging $PKG
  164.     #mkdir -p $CHROOT_DIR/$PKG_COPY && cp $PKG_FULL_PATH $CHROOT_DIR/$PKG_COPY
  165.     mkdir -p $CHROOT_DIR/$PKG_COPY &&
  166.     tar -C ${PKG_FULL_PATH%/*} -czf $CHROOT_DIR/$PKG_COPY/$PKG.tar.gz ${PKG_FULL_PATH##*/}
  167.     if type pkg_package > /dev/null; then # pkg_package is optional
  168.         ( cd $CHROOT_DIR; pkg_package; )
  169.     fi &&
  170.     (
  171.         cd $BUILD_DIR
  172.         if [ -z "$PKG_KEEPLA" ]; then
  173.             find . -type f -a -name \*.la |
  174.             sed 's/^\.//; \@/tmp/@d; \@\.wh\..*@d; \@.unionfs-fuse@d;' |
  175.             tr '\n' '\0' |
  176.             enter_chroot xargs -0 rm -f 2> /dev/null           
  177.         fi     
  178.         if [ -z "$PKG_NOSTRIP" ]; then
  179.             find . -type f -a ! -name '*.dbg' |
  180.             sed 's/^\.//; \@/tmp/@d; \@\.wh\..*@d; \@.unionfs-fuse@d;' |
  181.             tr '\n' '\0' |
  182.             enter_chroot xargs -0 strip --strip-unneeded 2> /dev/null
  183.             #sh
  184.         fi
  185.         if [ $UNIONFS_FUSE ]; then
  186.             rm -rf .unionfs-fuse
  187.         else
  188.             rm -rf .wh..wh.orph .wh..wh.plnk tmp # for aufs    
  189.             find . -name ".wh.*" -delete 2>/dev/null # for aufs
  190.         fi
  191.         rm -rf tmp root/.cache root/.ccache usr/share/info/dir root/.distcc
  192.         [ -z "$PKG_NOMAKEPKG" ] && ! [ -e no-makepkg ] &&
  193.         $MAKEPKG -c n -l n $OUTPUT_DIR/$PKG.$PKG_TYPE > /dev/null 2>&1 || return 0
  194.     )
  195.     [ $? -ne 0 ] && echo "Failed packaging phase." && return 1
  196.     return 0
  197. }
  198.  
  199. ### main
  200. init
  201. prepare && build && package; RESULT=$?
  202. cleanup
  203.  
Add Comment
Please, Sign In to add comment