chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

build.sh (5138B)


      1 #!/bin/bash
      2 # build one of our own packages into the repo: tools/pkg/build.sh python3-parted
      3 # a recipe sets name version rel desc url license source sha256 makedeps depends and build(),
      4 # which runs in a clean root and installs into $pkgdir
      5 # BASE=DIR starts from a copy of a root that has the makedeps already, for when the repo is slow
      6 set -euo pipefail
      7 HERE=$(cd "$(dirname "$0")" && pwd)
      8 TOP=$HERE/../..
      9 APK=${APK:-apk}
     10 REPO=${REPO:-$TOP/work/repo}
     11 SRC=${SRC_CACHE:-$HOME/.cache/chamesle-src}
     12 APKCACHE=${APK_CACHE:-$HOME/.cache/chamesle-apk}
     13 KEYDIR=${KEYDIR:-$HOME/.config/chamesle}
     14 KEY=$(ls "$KEYDIR"/chamesle-*.rsa | sort | tail -n1)
     15 ARCH=x86_64
     16 # what it builds with comes from the repo here, or the public one when there's no full copy here
     17 PULL=${PULL:-$( [ -f "$REPO/$ARCH/Packages.adb" ] && echo "$REPO" || echo https://master.dl.sourceforge.net/project/chameslerepos)}
     18 
     19 mkdir -p "$APKCACHE"
     20 [ $# = 1 ] && [ -f "$HERE/$1/recipe" ] || { echo "usage: $0 NAME   (one of: $(cd "$HERE" && ls */recipe | sed 's|/recipe||' | tr '\n' ' '))" >&2; exit 1; }
     21 . "$HERE/$1/recipe"
     22 
     23 # no source means the package is made of files that live here, in NAME/files
     24 if [ -n "${source:-}" ]; then
     25     mkdir -p "$SRC"
     26     file=$SRC/${source##*/}
     27     [ -f "$file" ] || curl -fL -o "$file" "$source"
     28     echo "$sha256  $file" | sha256sum -c --quiet
     29 fi
     30 
     31 W=$TOP/work/pkg/$name
     32 unshare --map-root-user --map-auto rm -rf "$W"
     33 ROOT=$W/root
     34 mkdir -p "$ROOT"
     35 [ -z "${BASE:-}" ] || unshare --map-root-user --map-auto cp -a "$BASE/." "$ROOT/"
     36 unshare --map-root-user --map-auto rm -rf "$ROOT/build"
     37 mkdir -p "$ROOT/etc/apk/keys" "$ROOT/build/src" "$ROOT/build/pkg"
     38 cp "$TOP"/keys/apk/*.pub "$ROOT/etc/apk/keys/"
     39 echo "v3 $PULL" > "$ROOT/etc/apk/repositories"
     40 if [ -n "${source:-}" ]; then
     41     # gnu tar doesn't open zips, some themes only come as one
     42     case $file in *.zip) unzip -q "$file" -d "$ROOT/build/src";; *) tar -xf "$file" -C "$ROOT/build/src";; esac
     43 else
     44     cp -r "$HERE/$1/files" "$ROOT/build/src/files"
     45 fi
     46 cp "$HERE/$1/recipe" "$ROOT/build/"
     47 [ -d "$HERE/$1/patches" ] && cp -r "$HERE/$1/patches" "$ROOT/build/"
     48 
     49 unshare --map-root-user --map-auto --mount --pid --fork bash -euo pipefail -c '
     50 APK=$1 ROOT=$2 IN=$3 makedeps=$4 base=$5 CACHE=$6
     51 # downloads are kept in $CACHE, so a dropped connection only costs the file it dropped,
     52 # and each step gets three tries
     53 try() { "$@" || { sleep 5; "$@"; } || { sleep 20; "$@"; }; }
     54 if [ -z "$base" ]; then
     55 try "$APK" --root "$ROOT" --arch x86_64 add --initdb --cache-dir "$CACHE" --quiet --no-scripts base-files bash coreutils sed grep findutils
     56 try "$APK" --root "$ROOT" --arch x86_64 fix --cache-dir "$CACHE" --quiet --reinstall $("$APK" --root "$ROOT" info --quiet)
     57 [ -z "$makedeps" ] || try "$APK" --root "$ROOT" --arch x86_64 add --cache-dir "$CACHE" --quiet $makedeps
     58 fi
     59 cat > "$ROOT/build/run" <<"EOF"
     60 set -euo pipefail
     61 . /build/recipe
     62 pkgdir=/build/pkg
     63 cd /build/src/*/
     64 for p in /build/patches/*.patch; do [ -e "$p" ] && patch -p1 < "$p"; done
     65 export MAKEFLAGS=-j$(nproc) CFLAGS="-O2 -pipe" CXXFLAGS="-O2 -pipe"
     66 build
     67 EOF
     68 "$IN" "$ROOT" /bin/bash /build/run
     69  ' - "$APK" "$ROOT" "$TOP/tools/in-image.sh" "$makedeps" "${BASE:-}" "$APKCACHE"
     70 
     71 # shared libraries it links to, minus the ones it brings itself
     72 PKG=$ROOT/build/pkg
     73 own=$(find "$PKG" -type f -name '*.so*' -exec basename {} \; | sort -u)
     74 sodeps=$(find "$PKG" -type f \( -perm -u+x -o -name '*.so*' \) -exec readelf -d {} + 2>/dev/null |
     75     sed -n 's/.*(NEEDED).*\[\(.*\)\]/\1/p' | sort -u | grep -vxF "$own" | sed 's/^/so:/' | tr '\n' ' ' || true)
     76 provides=$(for f in $own; do case $f in *.so.[0-9]*) echo "so:$f=${f#*.so.}" | sed 's/=\([0-9]*\).*/=\1/';; esac; done | tr '\n' ' ')
     77 
     78 mkdir -p "$REPO/$ARCH"
     79 out=$REPO/$ARCH/$name-$version-r$rel.apk
     80 info=(--info "name:$name" --info "version:$version-r$rel" --info "arch:$ARCH" --info "description:$desc"
     81       --info "url:$url" --info "license:$license" --info "maintainer:Chamesle Linux <packages@chamesle.org>")
     82 d=$(echo $depends $sodeps)
     83 [ -z "$d" ] || info+=(--info "depends:$d")
     84 [ -z "${provides// }" ] || info+=(--info "provides:$(echo $provides)")
     85 [ -z "${replaces:-}" ] || info+=(--info "replaces:$replaces")
     86 # install scripts, if the package has any (NAME/post-install and so on); @VERSION@ becomes the package's version
     87 for k in pre-install post-install pre-upgrade post-upgrade pre-deinstall post-deinstall; do
     88     [ -f "$HERE/$1/$k" ] || continue
     89     sed "s/@VERSION@/$version/g" "$HERE/$1/$k" > "$W/$k"
     90     info+=(--script "$k:$W/$k")
     91 done
     92 # packed as namespace root so the files are root's
     93 unshare --map-root-user --map-auto "$APK" mkpkg --files "$PKG" --output "$out" --sign-key "$KEY" "${info[@]}"
     94 if [ "$PULL" != "$REPO" ]; then
     95     # an index of just what's here would hide the rest of the repo
     96     echo "built $out (put it in the public index with tools/repo/merge-index.sh)"; exit 0
     97 fi
     98 "$APK" mkndx --keys-dir "$TOP/keys/apk" --sign-key "$KEY" --description "chamesle $(date +%Y-%m-%d)" \
     99     --index "$REPO/$ARCH/Packages.adb" --output "$REPO/$ARCH/Packages.adb.new" "$REPO/$ARCH"/*.apk
    100 mv "$REPO/$ARCH/Packages.adb.new" "$REPO/$ARCH/Packages.adb"
    101 echo "built $out"