vendor-syncthing.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/sh
  2. #
  3. # Copies the Syncthing packages ArozOS builds into src/third_party/syncthing
  4. # =========================================================================
  5. #
  6. # ArozOS embeds Syncthing (mod/fileservers/servers/syncthingserv). Syncthing's
  7. # lib/api needs a generated file with its web GUI, which the published module
  8. # does not carry, so the packages are kept in the tree with a stub for it and
  9. # src/go.mod points the module at them with a replace line.
  10. #
  11. # Usage, from anywhere in the repository:
  12. # sh scripts/vendor-syncthing.sh v2.1.7
  13. #
  14. # Then set the require line of github.com/syncthing/syncthing in src/go.mod to
  15. # the pseudo version this prints, update syncthingVersion in
  16. # syncthingserv/engine_syncthing.go, and run `go build` and the tests in src/.
  17. # Only the non-test files of the packages ArozOS uses (on every platform it
  18. # builds for) are copied; the MPL-2.0 licence and the authors list come along.
  19. set -eu
  20. ref=${1:-}
  21. if [ -z "$ref" ]; then
  22. echo "usage: $0 <syncthing tag or commit>" >&2
  23. exit 1
  24. fi
  25. root=$(cd "$(dirname "$0")/.." && pwd)
  26. dest="$root/src/third_party/syncthing"
  27. # The module path has no /v2, so Go only takes a v2 release by its commit
  28. commit=$(git ls-remote https://github.com/syncthing/syncthing "refs/tags/$ref^{}" | cut -f1)
  29. [ -n "$commit" ] || commit=$ref
  30. work=$(mktemp -d)
  31. trap 'rm -rf "$work"' EXIT
  32. cd "$work"
  33. cat >go.mod <<'EOF'
  34. module stvendor
  35. go 1.26
  36. EOF
  37. cat >main.go <<'EOF'
  38. package main
  39. import (
  40. _ "github.com/syncthing/syncthing/lib/build"
  41. _ "github.com/syncthing/syncthing/lib/config"
  42. _ "github.com/syncthing/syncthing/lib/events"
  43. _ "github.com/syncthing/syncthing/lib/fs"
  44. _ "github.com/syncthing/syncthing/lib/locations"
  45. _ "github.com/syncthing/syncthing/lib/protocol"
  46. _ "github.com/syncthing/syncthing/lib/svcutil"
  47. _ "github.com/syncthing/syncthing/lib/syncthing"
  48. )
  49. func main() {}
  50. EOF
  51. go get "github.com/syncthing/syncthing@$commit" >/dev/null 2>&1
  52. version=$(go list -m -f '{{.Version}}' github.com/syncthing/syncthing)
  53. src=$(go list -m -f '{{.Dir}}' github.com/syncthing/syncthing)
  54. # Every package any build target needs
  55. targets="darwin/amd64 darwin/arm64 freebsd/amd64 freebsd/386 linux/amd64 linux/386 linux/arm linux/arm64 linux/riscv64 linux/loong64 windows/amd64 windows/arm64"
  56. for t in $targets; do
  57. GOOS=${t%/*} GOARCH=${t#*/} CGO_ENABLED=0 go list -tags noassets -deps . 2>/dev/null
  58. done | grep '^github.com/syncthing/syncthing/' | sort -u >"$work/packages"
  59. rm -rf "$dest"
  60. mkdir -p "$dest"
  61. while IFS= read -r pkg; do
  62. rel=${pkg#github.com/syncthing/syncthing/}
  63. mkdir -p "$dest/$rel"
  64. find "$src/$rel" -maxdepth 1 -type f ! -name '*_test.go' -exec cp {} "$dest/$rel/" \;
  65. # Folders the package embeds, like the SQL schema of the index
  66. for dir in $(grep -h '^//go:embed' "$src/$rel"/*.go 2>/dev/null | sed 's#^//go:embed##' | tr ' ' '\n' | cut -d/ -f1 | sort -u); do
  67. if [ -d "$src/$rel/$dir" ]; then
  68. cp -R "$src/$rel/$dir" "$dest/$rel/"
  69. fi
  70. done
  71. done <"$work/packages"
  72. cp "$src/go.mod" "$src/LICENSE" "$src/AUTHORS" "$dest/"
  73. # The web GUI is never served by ArozOS: the stub Syncthing itself uses for
  74. # builds without it, without the noassets build tag
  75. stub="$dest/lib/api/auto/gui_stub.go"
  76. sed 's#^//go:build noassets$#//go:build !noassets#' "$src/lib/api/auto/noassets.go" >"$stub"
  77. # The module cache is read only
  78. chmod -R u+w "$dest"
  79. # With cgo on, Syncthing's index uses the C sqlite of mattn/go-sqlite3, which
  80. # turns ArozOS into a binary linked by gcc (a broken one with TDM-GCC on
  81. # Windows). The pure Go modernc.org/sqlite is used on every build instead.
  82. rm -f "$dest/internal/db/sqlite/db_open_cgo.go"
  83. sed -i 's#^//go:build !cgo && !wazero$#//go:build !wazero#' "$dest/internal/db/sqlite/db_open_nocgo.go"
  84. grep -q '^//go:build !wazero$' "$dest/internal/db/sqlite/db_open_nocgo.go" || {
  85. echo "db_open_nocgo.go changed upstream, check the sqlite driver choice" >&2
  86. exit 1
  87. }
  88. cat >"$dest/README.md" <<EOF
  89. # Syncthing (third party)
  90. The Syncthing packages ArozOS embeds, from
  91. [syncthing/syncthing](https://github.com/syncthing/syncthing) $ref
  92. (commit $commit), licensed under the Mozilla Public License 2.0 (LICENSE).
  93. Copied by \`scripts/vendor-syncthing.sh\`. Do not edit these files: change
  94. the script, or report changes upstream. Two things differ from Syncthing's
  95. own copy: \`lib/api/auto/gui_stub.go\` is \`lib/api/auto/noassets.go\`
  96. without its build tag, because ArozOS never serves the Syncthing web GUI;
  97. and \`internal/db/sqlite\` always uses the pure Go modernc.org/sqlite
  98. (\`db_open_cgo.go\` is left out), so ArozOS never needs cgo.
  99. EOF
  100. echo "Copied $(wc -l <"$work/packages") packages of $ref to $dest"
  101. echo "src/go.mod require line: github.com/syncthing/syncthing $version"