build_openwrt_apk.sh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. set -e -o pipefail
  3. ARCHITECTURE="$1"
  4. VERSION="$2"
  5. BINARY_PATH="$3"
  6. OUTPUT_PATH="$4"
  7. if [ -z "$ARCHITECTURE" ] || [ -z "$VERSION" ] || [ -z "$BINARY_PATH" ] || [ -z "$OUTPUT_PATH" ]; then
  8. echo "Usage: $0 <architecture> <version> <binary_path> <output_path>"
  9. exit 1
  10. fi
  11. PROJECT=$(cd "$(dirname "$0")/.."; pwd)
  12. # Convert version to APK format:
  13. # 1.13.0-beta.8 -> 1.13.0_beta8-r0
  14. # 1.13.0-rc.3 -> 1.13.0_rc3-r0
  15. # 1.13.0 -> 1.13.0-r0
  16. APK_VERSION=$(echo "$VERSION" | sed -E 's/-([a-z]+)\.([0-9]+)/_\1\2/')
  17. APK_VERSION="${APK_VERSION}-r0"
  18. ROOT_DIR=$(mktemp -d)
  19. trap 'rm -rf "$ROOT_DIR"' EXIT
  20. # Binary
  21. install -Dm755 "$BINARY_PATH" "$ROOT_DIR/usr/bin/sing-box"
  22. # Config files
  23. install -Dm644 "$PROJECT/release/config/config.json" "$ROOT_DIR/etc/sing-box/config.json"
  24. install -Dm644 "$PROJECT/release/config/openwrt.conf" "$ROOT_DIR/etc/config/sing-box"
  25. install -Dm755 "$PROJECT/release/config/openwrt.init" "$ROOT_DIR/etc/init.d/sing-box"
  26. install -Dm644 "$PROJECT/release/config/openwrt.keep" "$ROOT_DIR/lib/upgrade/keep.d/sing-box"
  27. # Completions
  28. install -Dm644 "$PROJECT/release/completions/sing-box.bash" "$ROOT_DIR/usr/share/bash-completion/completions/sing-box.bash"
  29. install -Dm644 "$PROJECT/release/completions/sing-box.fish" "$ROOT_DIR/usr/share/fish/vendor_completions.d/sing-box.fish"
  30. install -Dm644 "$PROJECT/release/completions/sing-box.zsh" "$ROOT_DIR/usr/share/zsh/site-functions/_sing-box"
  31. # License
  32. install -Dm644 "$PROJECT/LICENSE" "$ROOT_DIR/usr/share/licenses/sing-box/LICENSE"
  33. # APK metadata
  34. PACKAGES_DIR="$ROOT_DIR/lib/apk/packages"
  35. mkdir -p "$PACKAGES_DIR"
  36. # .conffiles
  37. cat > "$PACKAGES_DIR/.conffiles" <<'EOF'
  38. /etc/config/sing-box
  39. /etc/sing-box/config.json
  40. EOF
  41. # .conffiles_static (sha256 checksums)
  42. while IFS= read -r conffile; do
  43. sha256=$(sha256sum "$ROOT_DIR$conffile" | cut -d' ' -f1)
  44. echo "$conffile $sha256"
  45. done < "$PACKAGES_DIR/.conffiles" > "$PACKAGES_DIR/.conffiles_static"
  46. # .list (all files, excluding lib/apk/packages/ metadata)
  47. (cd "$ROOT_DIR" && find . -type f -o -type l) \
  48. | sed 's|^\./|/|' \
  49. | grep -v '^/lib/apk/packages/' \
  50. | sort > "$PACKAGES_DIR/.list"
  51. # Build APK
  52. apk mkpkg \
  53. --info "name:sing-box" \
  54. --info "version:${APK_VERSION}" \
  55. --info "description:The universal proxy platform." \
  56. --info "arch:${ARCHITECTURE}" \
  57. --info "license:GPL-3.0-or-later" \
  58. --info "origin:sing-box" \
  59. --info "url:https://sing-box.sagernet.org/" \
  60. --info "maintainer:nekohasekai <[email protected]>" \
  61. --info "depends:ca-bundle kmod-inet-diag kmod-tun firewall4 kmod-nft-queue" \
  62. --info "provider-priority:100" \
  63. --script "pre-deinstall:${PROJECT}/release/config/openwrt.prerm" \
  64. --files "$ROOT_DIR" \
  65. --output "$OUTPUT_PATH"