github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/debian/helpers/gen-build-depends.sh (about)

     1  #!/bin/bash
     2  set -eu
     3  set -o pipefail
     4  
     5  goBuildTags='apparmor cgo daemon pkcs11 selinux'
     6  
     7  debDir="$PWD/debian"
     8  
     9  debVer="$(dpkg-parsechangelog -SVersion)"
    10  origVer="${debVer%-*}" # strip everything from the last dash
    11  origVer="$(echo "$origVer" | sed -r 's/^[0-9]+://')" # strip epoch
    12  upstreamVer="${origVer%%[+~]ds*}"
    13  upstreamVer="${upstreamVer//[~]/-}"
    14  
    15  goImportPath="$(awk -F ': ' '$1 == "XS-Go-Import-Path" { print $2; exit }' debian/control)"
    16  [ "$goImportPath" ]
    17  
    18  upstreamArchiveUri="https://$goImportPath/archive/v${upstreamVer}.tar.gz"
    19  
    20  tempDir="$(mktemp -d -t debian-docker-gen-build-depends-XXXXXXXXXX)"
    21  trap "rm -rf '$tempDir'" EXIT
    22  cd "$tempDir"
    23  
    24  mkdir -p "gopath/src/$goImportPath"
    25  wget -qO archive.tar.gz "$upstreamArchiveUri"
    26  tar \
    27  	--extract \
    28  	--file archive.tar.gz \
    29  	--directory "gopath/src/$goImportPath" \
    30  	--strip-components 1
    31  export GOPATH="$PWD/gopath:$PWD/gopath/src/$goImportPath/vendor"
    32  cd "gopath/src/$goImportPath"
    33  
    34  IFS=$'\n'
    35  # get the full list of "docker/docker" Go packages
    36  goPkgs=( $(go list "$goImportPath/..." | grep -vE "^$goImportPath/vendor/") )
    37  # get the list of their dependencies, normalized:
    38  #   - skip stdlib, docker/docker
    39  #   - adjust known hosting locations for their top-level repos
    40  goDeps=( $(
    41  	go list \
    42  		-e \
    43  		-tags "$goBuildTags" \
    44  		-f '{{ join .Deps "\n" }}{{ "\n" }}{{ join .TestImports "\n" }}' \
    45  		"${goPkgs[@]}" \
    46  	| grep -vE '^$' \
    47  	| grep -vE '^[^/]+$' \
    48  	| grep -vE "^$goImportPath/" \
    49  	| sort -u \
    50  	| xargs \
    51  		go list \
    52  			-e \
    53  			-f '{{ if not .Standard }}{{ .ImportPath }}{{ end }}' \
    54  	| grep -vE '^$' \
    55  	| sed -r \
    56  		-e 's!^(github.com/[^/]+/[^/]+)/.*$!\1!' \
    57  		-e 's!^(golang.org/x/[^/]+)/.*$!\1!' \
    58  		-e 's!^(google.golang.org/[^/]+)/.*$!\1!' \
    59  		-e 's!^(gopkg.in/[^/]+)/.*$!\1!' \
    60  	| sort -u
    61  ) )
    62  unset IFS
    63  
    64  # converts a given "goPkg" into the relevant Debian "-dev" package name
    65  debian_pkg() {
    66  	local goPkg="$1"
    67  	local domain="${goPkg%%/*}"
    68  	domain="${domain%%.*}"
    69  	local goPkgPath="${goPkg#*/}"
    70  	local package="golang-$domain-${goPkgPath//\//-}-dev"
    71  	package="${package,,}"
    72  	echo "$package"
    73  }
    74  
    75  # converts "gitRepo" and "gitRef" into a concrete version number
    76  git_version() {
    77  	local goPkg="$1"; shift
    78  	local gitRepo="$1"; shift
    79  	local gitRef="$1"; shift
    80  
    81  	[ "$gitRef" ] || return
    82  
    83  	local gitSnapshotPrefix='0.0~git'
    84  
    85  	# normalize a few "special" cases
    86  	case "$goPkg=$gitRef" in
    87  		github.com/docker/go=*-*-*-*)
    88  			# turn "v1.5.1-1-1-gbaf439e" into "v1.5.1-1" so we can "ls-remote" and generate via commit instead of version
    89  			local remoteCommit="$(git ls-remote "$gitRepo" "refs/tags/${gitRef%-*-*}" | cut -d$'\t' -f1)"
    90  			if [ "$remoteCommit" ]; then
    91  				gitRef="$remoteCommit"
    92  			fi
    93  			;;
    94  
    95  		github.com/docker/libnetwork=v0.7.2-rc.1)
    96  			# TODO get newer version in the archive
    97  			gitRef='v0.7.0~rc.6'
    98  			;;
    99  
   100  		github.com/docker/distribution=467fc068d88aa6610691b7f1a677271a3fac4aac)
   101  			# TODO get newer version in the archive (467fc068d88aa6610691b7f1a677271a3fac4aac really corresponds to v2.5.0-rc.1+)
   102  			gitRef='v2.4.1'
   103  			;;
   104  
   105  		github.com/agl/ed25519=*)
   106  			gitSnapshotPrefix='0~'
   107  			;;
   108  
   109  		github.com/docker/containerd=*|github.com/opencontainers/runc=*)
   110  			# attempt to resolve commit to tag
   111  			local remoteTag="$(git ls-remote --tags "$gitRepo" | awk -F '[\t/]' '$1 == "'"$gitRef"'" { print $4; exit }')"
   112  			if [ "$remoteTag" ]; then
   113  				gitRef="$remoteTag"
   114  			fi
   115  			# TODO get newer (compatible) versions of each of these into the archive
   116  			case "$goPkg" in
   117  				github.com/docker/containerd)
   118  					gitRef='v0.2.1'
   119  					;;
   120  				github.com/opencontainers/runc)
   121  					gitRef='v0.1.0'
   122  					;;
   123  			esac
   124  			;;
   125  	esac
   126  
   127  	case "$gitRef" in
   128  		v[0-9]*|[0-9].*)
   129  			echo "${gitRef#v}"
   130  			return
   131  			;;
   132  	esac
   133  
   134  	local commitDate
   135  	case "$goPkg" in
   136  		github.com/*)
   137  			# for GitHub repos, we can shortcut the date calculation (saves a _lot_ of time)
   138  			local githubPatchUri="https://$goPkg/commit/$gitRef.patch"
   139  			commitDate="$(wget -qO- "$githubPatchUri" | awk -F ': ' '$1 == "Date" { print $2 }' | tail -1)"
   140  			# ".patch" returns potentially multiple commits, so we want the final "Date:" value, hence the "tail -1"
   141  			;;
   142  
   143  		*)
   144  			mkdir -p "$tempDir/git/$goPkg"
   145  			git clone --quiet "$gitRepo" "$tempDir/git/$goPkg"
   146  			local commitUnix="$(git -C "$tempDir/git/$goPkg" log -1 --format='%at' "$gitRef" --)"
   147  			commitDate="@$commitUnix"
   148  			;;
   149  	esac
   150  	[ "$commitDate" ]
   151  	commitDate="$(TZ=UTC date --date="$commitDate" +'%Y%m%d')"
   152  	echo "$gitSnapshotPrefix$commitDate"
   153  }
   154  
   155  declare -A transitionals=(
   156  	[golang-github-agl-ed25519-dev]='golang-ed25519-dev'
   157  	[golang-github-coreos-etcd-dev]='golang-etcd-server-dev'
   158  	[golang-github-go-check-check-dev]='golang-gopkg-check.v1-dev'
   159  	[golang-github-godbus-dbus-dev]='golang-dbus-dev'
   160  	[golang-github-golang-protobuf-dev]='golang-goprotobuf-dev'
   161  	[golang-github-miekg-dns-dev]='golang-dns-dev'
   162  	[golang-github-mistifyio-go-zfs-dev]='golang-go-zfs-dev'
   163  	[golang-github-syndtr-gocapability-dev]='golang-gocapability-dev'
   164  	[golang-github-ugorji-go-dev]='golang-github-ugorji-go-codec-dev'
   165  	[golang-gopkg-fsnotify.v1-dev]='golang-github-fsnotify-fsnotify-dev'
   166  
   167  	# golang-golang-x-oauth2 version 0.0~git20161103.0.36bc617-2 introduced a separate google/ package
   168  	[golang-golang-x-oauth2-dev]='golang-golang-x-oauth2-google-dev'
   169  )
   170  
   171  for goDep in "${goDeps[@]}"; do
   172  	if grep -q "^vendor/src/$goDep\$" "$debDir"/repack/keep/* 2>/dev/null; then
   173  		# skip vendored deps we don't remove
   174  		continue
   175  	fi
   176  
   177  	debPkg="$(debian_pkg "$goDep")"
   178  
   179  	gitRepoRef="$(awk '$1 == "clone" && $2 == "git" && $3 == "'"$goDep"'" { print ($5 && $5 != "#" ? $5 : "") "=" $4; exit }' hack/vendor.sh)"
   180  	gitRepo="${gitRepoRef%=*}"
   181  	gitRef="${gitRepoRef##$gitRepo=}"
   182  	: "${gitRepo:=https://$goDep}"
   183  
   184  	debVer="$(git_version "$goDep" "$gitRepo" "$gitRef")"
   185  
   186  	# deal with "golang-dns-dev" and friends of that nature
   187  	transitional="${transitionals[$debPkg]:-}"
   188  	if [ "$transitional" ]; then
   189  		echo -n "$transitional${debVer:+ (>= ${debVer}~)} | "
   190  	fi
   191  
   192  	echo "$debPkg${debVer:+ (>= ${debVer}~)},"
   193  done | sort