github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/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  	cross
    40  )
    41  
    42  VERSION=${VERSION:-dev}
    43  ! BUILDTIME=$(date -u -d "@${SOURCE_DATE_EPOCH:-$(date +%s)}" --rfc-3339 ns 2> /dev/null | sed -e 's/ /T/')
    44  if [ "$DOCKER_GITCOMMIT" ]; then
    45  	GITCOMMIT="$DOCKER_GITCOMMIT"
    46  elif command -v git &> /dev/null && [ -e .git ] && git rev-parse &> /dev/null; then
    47  	GITCOMMIT=$(git rev-parse --short HEAD)
    48  	if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
    49  		GITCOMMIT="$GITCOMMIT-unsupported"
    50  		echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    51  		echo "# GITCOMMIT = $GITCOMMIT"
    52  		echo "# The version you are building is listed as unsupported because"
    53  		echo "# there are some files in the git repository that are in an uncommitted state."
    54  		echo "# Commit these changes, or add to .gitignore to remove the -unsupported from the version."
    55  		echo "# Here is the current list:"
    56  		git status --porcelain --untracked-files=no
    57  		echo "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    58  	fi
    59  else
    60  	echo >&2 'error: .git directory missing and DOCKER_GITCOMMIT not specified'
    61  	echo >&2 '  Please either build with the .git directory accessible, or specify the'
    62  	echo >&2 '  exact (--short) commit hash you are building using DOCKER_GITCOMMIT for'
    63  	echo >&2 '  future accountability in diagnosing build issues.  Thanks!'
    64  	exit 1
    65  fi
    66  
    67  if [ "$AUTO_GOPATH" ]; then
    68  	rm -rf .gopath
    69  	mkdir -p .gopath/src/"$(dirname "${DOCKER_PKG}")"
    70  	ln -sf ../../../.. .gopath/src/"${DOCKER_PKG}"
    71  	export GOPATH="${PWD}/.gopath"
    72  fi
    73  
    74  if [ ! "$GOPATH" ]; then
    75  	echo >&2 'error: missing GOPATH; please see https://golang.org/doc/code.html#GOPATH'
    76  	echo >&2 '  alternatively, set AUTO_GOPATH=1'
    77  	exit 1
    78  fi
    79  
    80  # Adds $1_$2 to DOCKER_BUILDTAGS unless it already
    81  # contains a word starting from $1_
    82  add_buildtag() {
    83  	[[ " $DOCKER_BUILDTAGS" == *" $1_"* ]] || DOCKER_BUILDTAGS+=" $1_$2"
    84  }
    85  
    86  if ${PKG_CONFIG} 'libsystemd' 2> /dev/null; then
    87  	DOCKER_BUILDTAGS+=" journald"
    88  fi
    89  
    90  # test whether "libdevmapper.h" is new enough to support deferred remove
    91  # functionality. We favour libdm_dlsym_deferred_remove over
    92  # libdm_no_deferred_remove in dynamic cases because the binary could be shipped
    93  # with a newer libdevmapper than the one it was built with.
    94  if
    95  	command -v gcc &> /dev/null \
    96  		&& ! (echo -e '#include <libdevmapper.h>\nint main() { dm_task_deferred_remove(NULL); }' | gcc -xc - -o /dev/null $(pkg-config --libs devmapper) &> /dev/null) \
    97  		;
    98  then
    99  	add_buildtag libdm dlsym_deferred_remove
   100  fi
   101  
   102  # Use these flags when compiling the tests and final binary
   103  
   104  IAMSTATIC='true'
   105  if [ -z "$DOCKER_DEBUG" ]; then
   106  	LDFLAGS='-w'
   107  fi
   108  
   109  LDFLAGS_STATIC=''
   110  EXTLDFLAGS_STATIC='-static'
   111  # ORIG_BUILDFLAGS is necessary for the cross target which cannot always build
   112  # with options like -race.
   113  ORIG_BUILDFLAGS=(-tags "netgo osusergo static_build $DOCKER_BUILDTAGS" -installsuffix netgo)
   114  # see https://github.com/golang/go/issues/9369#issuecomment-69864440 for why -installsuffix is necessary here
   115  
   116  BUILDFLAGS=(${BUILDFLAGS} "${ORIG_BUILDFLAGS[@]}")
   117  
   118  LDFLAGS_STATIC_DOCKER="
   119  	$LDFLAGS_STATIC
   120  	-extldflags \"$EXTLDFLAGS_STATIC\"
   121  "
   122  
   123  if [ "$(uname -s)" = 'FreeBSD' ]; then
   124  	# Tell cgo the compiler is Clang, not GCC
   125  	# https://code.google.com/p/go/source/browse/src/cmd/cgo/gcc.go?spec=svne77e74371f2340ee08622ce602e9f7b15f29d8d3&r=e6794866ebeba2bf8818b9261b54e2eef1c9e588#752
   126  	export CC=clang
   127  
   128  	# "-extld clang" is a workaround for
   129  	# https://code.google.com/p/go/issues/detail?id=6845
   130  	LDFLAGS="$LDFLAGS -extld clang"
   131  fi
   132  
   133  bundle() {
   134  	local bundle="$1"
   135  	shift
   136  	echo "---> Making bundle: $(basename "$bundle") (in $DEST)"
   137  	source "$SCRIPTDIR/make/$bundle" "$@"
   138  }
   139  
   140  main() {
   141  	bundle_dir="bundles"
   142  	if [ -n "${PREFIX}" ]; then
   143  		bundle_dir="${PREFIX}/${bundle_dir}"
   144  	fi
   145  
   146  	if [ -z "${KEEPBUNDLE-}" ]; then
   147  		echo "Removing ${bundle_dir}/"
   148  		rm -rf "${bundle_dir}"/*
   149  		echo
   150  	fi
   151  	mkdir -p "${bundle_dir}"
   152  
   153  	if [ $# -lt 1 ]; then
   154  		bundles=(${DEFAULT_BUNDLES[@]})
   155  	else
   156  		bundles=($@)
   157  	fi
   158  	for bundle in ${bundles[@]}; do
   159  		export DEST="${bundle_dir}/$(basename "$bundle")"
   160  		# Cygdrive paths don't play well with go build -o.
   161  		if [[ "$(uname -s)" == CYGWIN* ]]; then
   162  			export DEST="$(cygpath -mw "$DEST")"
   163  		fi
   164  		mkdir -p "$DEST"
   165  		ABS_DEST="$(cd "$DEST" && pwd -P)"
   166  		bundle "$bundle"
   167  		echo
   168  	done
   169  }
   170  
   171  main "$@"