From b7dff46f132a904100ec9e251c1e7fc6d9600954 Mon Sep 17 00:00:00 2001 From: Jackson West Date: Tue, 10 Oct 2023 22:15:15 -0500 Subject: [PATCH] improves mac gnu variant detection --- Common.mk | 5 ++-- build/lib/common.sh | 39 ++++++++++++++++++++------- build/lib/create_release_checksums.sh | 15 ++++------- build/lib/generate_help_body.sh | 8 +++--- build/lib/helm_pull.sh | 8 +++--- build/lib/helm_push.sh | 8 +++--- build/lib/helm_replace.sh | 8 +++--- build/lib/readme_check.sh | 8 +++--- build/lib/update_checksums.sh | 6 ++++- build/lib/validate_artifacts.sh | 5 +--- build/lib/version.sh | 10 +++---- 11 files changed, 67 insertions(+), 53 deletions(-) diff --git a/Common.mk b/Common.mk index 1bea2c0c44..5a1a7bc6e0 100644 --- a/Common.mk +++ b/Common.mk @@ -302,8 +302,7 @@ ADD_TRAILING_CHAR=$(if $(1),$(1)$(2),) # check if pass variable has length of 1 IS_ONE_WORD=$(if $(filter 1,$(words $(1))),true,false) -SED_CMD=$(shell if [ "$$(uname -s)" = "Darwin" ] && command -v gsed &> /dev/null; then echo gsed; else echo sed; fi) - +SED_CMD=$(shell source $(BUILD_LIB)/common.sh && build::find::gnu_variant_on_mac sed) #################################################### #################### BINARIES ###################### @@ -426,7 +425,7 @@ GITHUB_TOKEN?= #################################################### #################### LOGGING ####################### -DATE_CMD=TZ=utc $(shell if [ "$$(uname -s)" = "Darwin" ] && command -v gdate &> /dev/null; then echo gdate; else echo date; fi) +DATE_CMD=TZ=utc $(shell source $(BUILD_LIB)/common.sh && build::find::gnu_variant_on_mac date) DATE_NANO=$(shell if [ "$$(uname -s)" = "Linux" ] || command -v gdate &> /dev/null; then echo %3N; fi) TARGET_START_LOG?=$(eval _START_TIME:=$(shell $(DATE_CMD) +%s.$(DATE_NANO)))\\n------------------- $(shell $(DATE_CMD) +"%Y-%m-%dT%H:%M:%S.$(DATE_NANO)%z") Starting target=$@ ------------------- TARGET_END_LOG?="------------------- `$(DATE_CMD) +'%Y-%m-%dT%H:%M:%S.$(DATE_NANO)%z'` Finished target=$@ duration=`echo $$($(DATE_CMD) +%s.$(DATE_NANO)) - $(_START_TIME) | bc` seconds -------------------\\n" diff --git a/build/lib/common.sh b/build/lib/common.sh index 0bb3c38935..5a90b14560 100755 --- a/build/lib/common.sh +++ b/build/lib/common.sh @@ -20,6 +20,31 @@ if [ -n "${OUTPUT_DEBUG_LOG:-}" ]; then set -x fi +function build::find::gnu_variant_on_mac() { + local -r cmd="$1" + + if [ "$(uname -s)" = "Linux" ]; then + echo "$cmd" + return + fi + + local final="$cmd" + if command -v "g$final" &> /dev/null; then + final="g$final" + fi + + if [[ "$final" = "$cmd" ]] && command -v "gnu$final" &> /dev/null; then + final="gnu$final" + fi + + if [[ "$final" = "$cmd" ]]; then + >&2 echo " !!! Building on Mac OS X and GNU '$cmd' not found. Using the builtin version" + >&2 echo " *may* work, but in general you should either build on a Linux host or" + >&2 echo " install the gnu version via brew, usually 'brew install gnu-$cmd'" + fi + + echo "$final" +} function build::common::ensure_tar() { if [[ -n "${TAR:-}" ]]; then @@ -27,12 +52,7 @@ function build::common::ensure_tar() { fi # Find gnu tar if it is available, bomb out if not. - TAR=tar - if which gtar &>/dev/null; then - TAR=gtar - elif which gnutar &>/dev/null; then - TAR=gnutar - fi + TAR=$(build::find::gnu_variant_on_mac tar) if ! "${TAR}" --version | grep -q GNU; then echo " !!! Cannot find GNU tar. Build on Linux or install GNU tar" echo " on Mac OS X (brew install gnu-tar)." @@ -418,12 +438,13 @@ function build::common::copy_if_source_destination_different() { local source=$1 local destination=$2 - source_inode=$(stat -c %i $source) + STAT=$(build::find::gnu_variant_on_mac stat) + source_inode=$($STAT -c %i $source) destination_inode="" if [ -d $destination ] && [ -e $destination/$(basename $source) ]; then - destination_inode=$(stat -c %i $destination/$(basename $source)) + destination_inode=$($STAT -c %i $destination/$(basename $source)) elif [ -f $destination ] && [ -e $destination ]; then - destination_inode=$(stat -c %i $destination) + destination_inode=$($STAT -c %i $destination) fi if [ -n "$destination_inode" ] && [ "$source_inode" = "$destination_inode" ]; then diff --git a/build/lib/create_release_checksums.sh b/build/lib/create_release_checksums.sh index 5e506a60e5..609c994c6c 100755 --- a/build/lib/create_release_checksums.sh +++ b/build/lib/create_release_checksums.sh @@ -17,19 +17,14 @@ set -o errexit set -o nounset set -o pipefail +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + ASSET_ROOT="$1" -FIND=find -if which gfind &>/dev/null; then - FIND=gfind -elif which gnudate &>/dev/null; then - FIND=gnufind -fi +FIND=$(build::find::gnu_variant_on_mac find) -REALPATH=realpath -if which grealpath &>/dev/null; then - REALPATH=grealpath -fi +REALPATH=$(build::find::gnu_variant_on_mac realpath) SHA256SUM=$(dirname ${ASSET_ROOT})/SHA256SUM SHA512SUM=$(dirname ${ASSET_ROOT})/SHA512SUM diff --git a/build/lib/generate_help_body.sh b/build/lib/generate_help_body.sh index 81b11fe064..90a613962e 100755 --- a/build/lib/generate_help_body.sh +++ b/build/lib/generate_help_body.sh @@ -41,10 +41,10 @@ FOOTER="########### END GENERATED ###########################" MAKEFILE=$PROJECT_ROOT/Makefile HELPFILE=$PROJECT_ROOT/Help.mk -SED=sed -if [ "$(uname -s)" = "Darwin" ]; then - SED=gsed -fi +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + +SED=$(build::find::gnu_variant_on_mac sed) $SED -i "/$HEADER/,/$FOOTER/d" $MAKEFILE # remove trailing newlines diff --git a/build/lib/helm_pull.sh b/build/lib/helm_pull.sh index 29ba08dacf..83351196ec 100755 --- a/build/lib/helm_pull.sh +++ b/build/lib/helm_pull.sh @@ -18,10 +18,10 @@ set -o errexit set -o nounset set -o pipefail -SED=sed -if [ "$(uname -s)" = "Darwin" ]; then - SED=gsed -fi +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + +SED=$(build::find::gnu_variant_on_mac sed) IMAGE_REGISTRY="${1?First argument is image registry}" HELM_REPO_URL="${2?Second arguement is update repo url}" diff --git a/build/lib/helm_push.sh b/build/lib/helm_push.sh index 3089e1f4aa..b8fd0cb314 100755 --- a/build/lib/helm_push.sh +++ b/build/lib/helm_push.sh @@ -18,10 +18,10 @@ set -o errexit set -o nounset set -o pipefail -SED=sed -if [ "$(uname -s)" = "Darwin" ]; then - SED=gsed -fi +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + +SED=$(build::find::gnu_variant_on_mac sed) IMAGE_REGISTRY="${1?First argument is image registry}" HELM_DESTINATION_REPOSITORY="${2?Second argument is helm repository}" diff --git a/build/lib/helm_replace.sh b/build/lib/helm_replace.sh index 3eb9b04b74..377799db99 100755 --- a/build/lib/helm_replace.sh +++ b/build/lib/helm_replace.sh @@ -18,10 +18,10 @@ set -o errexit set -o nounset set -o pipefail -SED=sed -if [ "$(uname -s)" = "Darwin" ]; then - SED=gsed -fi +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + +SED=$(build::find::gnu_variant_on_mac sed) HELM_DESTINATION_REPOSITORY="${1?First argument is helm destination repository}" OUTPUT_DIR="${2?Second arguement is output directory}" diff --git a/build/lib/readme_check.sh b/build/lib/readme_check.sh index e65fa2d7ce..1d386a4fc4 100755 --- a/build/lib/readme_check.sh +++ b/build/lib/readme_check.sh @@ -18,11 +18,11 @@ set -o errexit set -o nounset set -o pipefail +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + RETURN=0 -SED=sed -if [ "$(uname -s)" = "Darwin" ]; then - SED=gsed -fi +SED=$(build::find::gnu_variant_on_mac sed) for GIT_TAG_FILE in projects/*/*/GIT_TAG do diff --git a/build/lib/update_checksums.sh b/build/lib/update_checksums.sh index cb42b5c186..b42cd45f45 100755 --- a/build/lib/update_checksums.sh +++ b/build/lib/update_checksums.sh @@ -17,10 +17,14 @@ set -o errexit set -o nounset set -o pipefail +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + MAKE_ROOT="$1" PROJECT_ROOT="$2" OUTPUT_BIN_DIR="$3" +REALPATH=$(build::find::gnu_variant_on_mac realpath) if [ ! -d ${OUTPUT_BIN_DIR} ] ; then echo "${OUTPUT_BIN_DIR} not present! Run 'make binaries'" @@ -31,7 +35,7 @@ CHECKSUMS_FILE=$PROJECT_ROOT/CHECKSUMS rm -f $CHECKSUMS_FILE for file in $(find ${OUTPUT_BIN_DIR} -type f | sort); do - filepath=$(realpath --relative-base=$MAKE_ROOT $file) + filepath=$($REALPATH --relative-base=$MAKE_ROOT $file) sha256sum $filepath >> $CHECKSUMS_FILE done diff --git a/build/lib/validate_artifacts.sh b/build/lib/validate_artifacts.sh index 8197b4406d..df7ebd992e 100755 --- a/build/lib/validate_artifacts.sh +++ b/build/lib/validate_artifacts.sh @@ -37,10 +37,7 @@ if [ -n "$IMAGE_FORMAT" ]; then fi fi -REALPATH=realpath -if which grealpath &>/dev/null; then - REALPATH=grealpath -fi +REALPATH=$(build::find::gnu_variant_on_mac realpath) ACTUAL_FILES=$(mktemp) find "$ARTIFACTS_FOLDER" \ diff --git a/build/lib/version.sh b/build/lib/version.sh index fb02a43efd..5bd078f097 100755 --- a/build/lib/version.sh +++ b/build/lib/version.sh @@ -14,6 +14,9 @@ set -o errexit set -o nounset set -o pipefail +SCRIPT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" +source "${SCRIPT_ROOT}/common.sh" + version::get_version_vars() { local -r repo=$1 GIT_RELEASE_TAG=$(git -C $repo describe --match 'v[0-9]*.[0-9]*.[0-9]**' --abbrev=0 --tags) @@ -58,12 +61,7 @@ version::ldflags() { "-X '${package_prefix}.${key}=${val}'" ) } - DATE=date - if which gdate &>/dev/null; then - DATE=gdate - elif which gnudate &>/dev/null; then - DATE=gnudate - fi + DATE=$(build::find::gnu_variant_on_mac date) # buildDate is not actual buildDate to avoid it breaking reproducible checksums # instead it is the date of the last commit, either the upstream TAG commit or the latest patch applied