Skip to content

Commit

Permalink
improves mac gnu variant detection (#2535)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxesn authored Oct 11, 2023
1 parent 60a80ab commit e24cd14
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 53 deletions.
5 changes: 2 additions & 3 deletions Common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 ######################
Expand Down Expand Up @@ -427,7 +426,7 @@ DEPENDENCY_TOOLS=buildctl helm jq skopeo tuftool yq
####################################################

#################### 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"
Expand Down
39 changes: 30 additions & 9 deletions build/lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,39 @@ 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
return
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)."
Expand Down Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions build/lib/create_release_checksums.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions build/lib/generate_help_body.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions build/lib/helm_pull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
8 changes: 4 additions & 4 deletions build/lib/helm_push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
8 changes: 4 additions & 4 deletions build/lib/helm_replace.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
8 changes: 4 additions & 4 deletions build/lib/readme_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion build/lib/update_checksums.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand All @@ -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

Expand Down
5 changes: 1 addition & 4 deletions build/lib/validate_artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
10 changes: 4 additions & 6 deletions build/lib/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e24cd14

Please sign in to comment.