| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # THIRD_PARTY_LICENSES: the license and notice files of every module and of Go |
| 4 | # itself that the release binaries are built from. |
| 5 | # |
| 6 | # scripts/licenses rewrite THIRD_PARTY_LICENSES |
| 7 | # scripts/licenses --check fail when it is out of date |
| 8 | # |
| 9 | # The modules are those of `go list -deps` for the binary on Linux, macOS and |
| 10 | # Windows and for the guest agent embedded in it, with the Makefile's tags. |
| 11 | # Needs go and a module cache (go mod download). Run it again after a change |
| 12 | # of go.mod or third_party/. |
| 13 | |
| 14 | set -euo pipefail |
| 15 | |
| 16 | ROOT="$(CDPATH= cd -P -- "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)" |
| 17 | OUT="$ROOT/THIRD_PARTY_LICENSES" |
| 18 | cd "$ROOT" |
| 19 | |
| 20 | check="" |
| 21 | case "${1:-}" in |
| 22 | "") ;; |
| 23 | --check) check=1 ;; |
| 24 | *) sed -n '5,6p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2 ;; |
| 25 | esac |
| 26 | |
| 27 | tags="$(awk '$1 == "TAGS" && $2 == ":=" { print $3 }' Makefile)" |
| 28 | [ -n "$tags" ] || { echo "licenses: no TAGS in the Makefile" >&2; exit 1; } |
| 29 | |
| 30 | # <path> <version> <dir> per module, for one target. |
| 31 | modules() { # <goos> <goarch> <cgo> <package> |
| 32 | GOOS="$1" GOARCH="$2" CGO_ENABLED="$3" GOWORK=off go list -deps -tags "$tags" \ |
| 33 | -f '{{with .Module}}{{if not .Main}}{{.Path}}{{"\t"}}{{with .Replace}}{{.Path}}{{else}}{{.Version}}{{end}}{{"\t"}}{{.Dir}}{{end}}{{end}}' "$4" |
| 34 | } |
| 35 | |
| 36 | # Modules only the macOS or Windows builds use are not in a cache filled by a |
| 37 | # Linux build. |
| 38 | GOWORK=off go mod download |
| 39 | |
| 40 | list="$(mktemp)" |
| 41 | trap 'rm -f "$list"' EXIT |
| 42 | { |
| 43 | modules linux amd64 0 ./cmd/agent-vm |
| 44 | modules linux arm64 0 ./cmd/agent-vm |
| 45 | modules linux amd64 0 ./cmd/cage |
| 46 | modules linux arm64 0 ./cmd/cage |
| 47 | modules darwin arm64 1 ./cmd/agent-vm |
| 48 | modules darwin arm64 1 ./cmd/cage |
| 49 | modules darwin amd64 1 ./cmd/agent-vm |
| 50 | modules windows amd64 0 ./cmd/agent-vm |
| 51 | modules windows amd64 0 ./cmd/cage |
| 52 | modules windows arm64 0 ./cmd/agent-vm |
| 53 | modules linux arm64 0 github.com/lima-vm/lima/v2/cmd/lima-guestagent |
| 54 | modules linux amd64 0 github.com/lima-vm/lima/v2/cmd/lima-guestagent |
| 55 | modules darwin arm64 0 github.com/lima-vm/lima/v2/cmd/lima-guestagent |
| 56 | } | awk -F '\t' 'NF == 3 && !seen[$1]++' | LC_ALL=C sort > "$list" |
| 57 | |
| 58 | [ -s "$list" ] || { echo "licenses: go list found no module" >&2; exit 1; } |
| 59 | |
| 60 | render() { |
| 61 | cat <<'EOF' |
| 62 | Third-party licenses |
| 63 | |
| 64 | agent-vm is MIT licensed (LICENSE). The binary is built from the modules |
| 65 | below and from Go's standard library. Each section gives a module, its |
| 66 | version and the license and notice files at the root of its source, verbatim. |
| 67 | third_party/ holds the bundled Lima, sshocker, pkg/sftp and gvisor-tap-vsock |
| 68 | source compiled into Cage (see NOTICE). |
| 69 | |
| 70 | Generated by scripts/licenses. |
| 71 | |
| 72 | EOF |
| 73 | printf '================================================================================\n' |
| 74 | printf 'gaoyifan/china-operator-ip: mainland exit address list (MIT)\n' |
| 75 | printf '================================================================================\n\n' |
| 76 | cat internal/exitpolicy/data/LICENSE |
| 77 | printf '\n' |
| 78 | printf '================================================================================\n' |
| 79 | printf 'Go standard library (BSD-3-Clause)\n' |
| 80 | printf '================================================================================\n\n' |
| 81 | go_license="$(go env GOROOT)/LICENSE" |
| 82 | # Homebrew keeps the Go license beside libexec rather than inside GOROOT. |
| 83 | if [ ! -f "$go_license" ]; then go_license="$(go env GOROOT)/../LICENSE"; fi |
| 84 | cat "$go_license" |
| 85 | printf '\n' |
| 86 | while IFS=$'\t' read -r path version dir; do |
| 87 | found=0 |
| 88 | # LICENSE, LICENCE, COPYING, NOTICE, UNLICENSE, PATENTS, with any suffix. |
| 89 | while IFS= read -r f; do |
| 90 | found=1 |
| 91 | printf '================================================================================\n' |
| 92 | printf '%s %s: %s\n' "$path" "${version:-}" "$(basename "$f")" |
| 93 | printf '================================================================================\n\n' |
| 94 | cat "$f" |
| 95 | printf '\n' |
| 96 | done < <(find "$dir" -maxdepth 1 -type f \( -iname 'licen[sc]e*' -o -iname 'copying*' -o -iname 'notice*' -o -iname 'unlicense*' -o -iname 'patents*' \) | LC_ALL=C sort) |
| 97 | if [ "$found" = 0 ]; then |
| 98 | echo "licenses: no license file in $path $version ($dir)" >&2 |
| 99 | return 1 |
| 100 | fi |
| 101 | done < "$list" |
| 102 | tailnet_version="$(awk '$1 == "tailscale.com" { sub(/^v/, "", $2); print $2 }' tools/tailnet/go.mod)" |
| 103 | cat <<EOF |
| 104 | |
| 105 | Cage guest Tailscale component |
| 106 | ----------------------------- |
| 107 | Tailscale $tailnet_version (https://github.com/tailscale/tailscale), BSD-3-Clause. |
| 108 | The guest-only executable also uses gliderlabs/ssh (BSD-3-Clause), pkg/sftp |
| 109 | (BSD-2-Clause), creack/pty (MIT), UserExistsError/conpty (MIT), and the |
| 110 | transitive modules pinned in tools/tailnet/go.mod and tools/tailnet/go.sum. |
| 111 | Full license notices for the modules linked on Linux, macOS and Windows |
| 112 | are distributed in the application licenses/tailnet-go directory. |
| 113 | EOF |
| 114 | } |
| 115 | |
| 116 | tmp="$(mktemp)" |
| 117 | trap 'rm -f "$list" "$tmp"' EXIT |
| 118 | render > "$tmp" |
| 119 | |
| 120 | if [ -n "$check" ]; then |
| 121 | if cmp -s "$tmp" "$OUT"; then |
| 122 | echo "THIRD_PARTY_LICENSES is up to date" |
| 123 | else |
| 124 | echo "THIRD_PARTY_LICENSES is out of date: run scripts/licenses" >&2 |
| 125 | diff "$OUT" "$tmp" | head -20 >&2 || true |
| 126 | exit 1 |
| 127 | fi |
| 128 | else |
| 129 | cat "$tmp" > "$OUT" |
| 130 | echo "THIRD_PARTY_LICENSES: $(wc -l < "$list" | tr -d ' ') modules" |
| 131 | fi |