github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/hack/make.sh (about) 1 #!/usr/bin/env bash 2 set -e 3 4 # This script builds various binary artifacts from a checkout of the docker 5 # source code. 6 # 7 # Requirements: 8 # - The current directory should be a checkout of the docker source code 9 # (https://github.com/docker/docker). Whatever version is checked out 10 # will be built. 11 # - The VERSION file, at the root of the repository, should exist, and 12 # will be used as Docker binary version and package version. 13 # - The hash of the git commit will also be included in the Docker binary, 14 # with the suffix -unsupported if the repository isn't clean. 15 # - The script is intended to be run inside the docker container specified 16 # in the Dockerfile at the root of the source. In other words: 17 # DO NOT CALL THIS SCRIPT DIRECTLY. 18 # - The right way to call this script is to invoke "make" from 19 # your checkout of the Docker repository. 20 # the Makefile will do a "docker build -t docker ." and then 21 # "docker run hack/make.sh" in the resulting image. 22 # 23 24 set -o pipefail 25 26 export DOCKER_PKG='github.com/docker/docker' 27 export SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 28 export MAKEDIR="$SCRIPTDIR/make" 29 export PKG_CONFIG=${PKG_CONFIG:-pkg-config} 30 31 echo 32 33 # List of bundles to create when no argument is passed 34 DEFAULT_BUNDLES=( 35 binary-daemon 36 dynbinary 37 test-integration 38 test-docker-py 39 ) 40 41 VERSION=${VERSION:-dev} 42 case "$VERSION" in 43 refs/tags/v*) VERSION=${VERSION#refs/tags/v} ;; 44 refs/tags/*) VERSION=${VERSION#refs/tags/} ;; 45 refs/heads/*) VERSION=$(echo "${VERSION#refs/heads/}" | sed -r 's#/+#-#g') ;; 46 refs/pull/*) VERSION=pr-$(echo "$VERSION" | grep -o '[0-9]\+') ;; 47 esac 48 49 ! BUILDTIME=$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/') 50 if [ "$DOCKER_GITCOMMIT" ]; then 51 GITCOMMIT="$DOCKER_GITCOMMIT" 52 elif command -v git &> /dev/null && [ -e .git ] && git rev-parse &> /dev/null; then 53 GITCOMMIT=$(git rev-parse HEAD) 54 if [ -n "$(git status --porcelain --untracked-files=no)" ]; then 55 GITCOMMIT="$GITCOMMIT-unsupported" 56 echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 57 echo "# GITCOMMIT = $GITCOMMIT" 58 echo "# The version you are building is listed as unsupported because" 59 echo "# there are some files in the git repository that are in an uncommitted state." 60 echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version." 61 echo "# Here is the current list:" 62 git status --porcelain --untracked-files=no 63 echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 64 fi 65 else 66 echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified' 67 echo >&2 ' Please either build with the .git directory accessible, or specify the' 68 echo >&2 ' exact commit hash you are building using DOCKER_GITCOMMIT for future' 69 echo >&2 ' accountability in diagnosing build issues. Thanks!' 70 exit 1 71 fi 72 73 if [ "$AUTO_GOPATH" ]; then 74 rm -rf .gopath 75 mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")" 76 ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}" 77 export GOPATH="${PWD}/.gopath" 78 fi 79 80 if [ ! "$GOPATH" ]; then 81 echo >&2 'error: missing GOPATH; please see https://pkg.go.dev/cmd/go#hdr-GOPATH_environment_variable' 82 echo >&2 ' alternatively, set AUTO_GOPATH=1' 83 exit 1 84 fi 85 86 if ${PKG_CONFIG} 'libsystemd' 2> /dev/null; then 87 DOCKER_BUILDTAGS+=" journald" 88 fi 89 90 # Use these flags when compiling the tests and final binary 91 92 if [ -z "$DOCKER_DEBUG" ]; then 93 LDFLAGS='-w' 94 fi 95 96 BUILDFLAGS=(${BUILDFLAGS} -tags "netgo osusergo static_build $DOCKER_BUILDTAGS") 97 LDFLAGS_STATIC="-extldflags -static" 98 99 if [ "$(uname -s)" = 'FreeBSD' ]; then 100 # Tell cgo the compiler is Clang, not GCC 101 # https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752 102 export CC=clang 103 104 # "-extld clang" is a workaround for 105 # https://code.google.com/p/go/issues/detail?id=6845 106 LDFLAGS="$LDFLAGS -extld clang" 107 fi 108 109 bundle() { 110 local bundle="$1" 111 shift 112 echo "---> Making bundle: $(basename "$bundle") (in $DEST)" 113 source "$SCRIPTDIR/make/$bundle" "$@" 114 } 115 116 main() { 117 bundle_dir="bundles" 118 if [ -n "${PREFIX}" ]; then 119 bundle_dir="${PREFIX}/${bundle_dir}" 120 fi 121 122 if [ -z "${KEEPBUNDLE-}" ]; then 123 echo "Removing ${bundle_dir}/" 124 rm -rf "${bundle_dir}"/* 125 echo 126 fi 127 mkdir -p "${bundle_dir}" 128 129 if [ $# -lt 1 ]; then 130 bundles=(${DEFAULT_BUNDLES[@]}) 131 else 132 bundles=($@) 133 fi 134 for bundle in ${bundles[@]}; do 135 export DEST="${bundle_dir}/$(basename "$bundle")" 136 # Cygdrive paths don't play well with go build -o. 137 if [[ "$(uname -s)" == CYGWIN* ]]; then 138 export DEST="$(cygpath -mw "$DEST")" 139 fi 140 mkdir -p "$DEST" 141 ABS_DEST="$(cd "$DEST" && pwd -P)" 142 bundle "$bundle" 143 echo 144 done 145 } 146 147 main "$@"