hacks: build main target without go in the PATH

`make` is able to build project binaries, but fails with
error `hack/lib/golang.sh: line 455: go: command not found`
trying to place them if go binary is not in the PATH.
This happens because kube::golang::place_bins uses different
environment than kube::golang::build_binaries.

Setting up an one environment for both `kube::golang::place_bins`
and `kube::golang::build_binaries` should solve this issue and allow
default make target to fully work without go binary in the PATH.
This commit is contained in:
Ed Bartosh 2024-04-05 17:36:57 +03:00
parent d9c54f69d4
commit 91099aca72
2 changed files with 85 additions and 89 deletions

View File

@ -873,106 +873,101 @@ kube::golang::get_physmem() {
# KUBE_BUILD_PLATFORMS - Incoming variable of targets to build for. If unset # KUBE_BUILD_PLATFORMS - Incoming variable of targets to build for. If unset
# then just the host architecture is built. # then just the host architecture is built.
kube::golang::build_binaries() { kube::golang::build_binaries() {
# Create a sub-shell so that we don't pollute the outer environment V=2 kube::log::info "Go version: $(GOFLAGS='' go version)"
(
# Check for `go` binary and set ${GOPATH}.
kube::golang::setup_env
V=2 kube::log::info "Go version: $(GOFLAGS='' go version)"
local host_platform local host_platform
host_platform=$(kube::golang::host_platform) host_platform=$(kube::golang::host_platform)
# These are "local" but are visible to and relied on by functions this # These are "local" but are visible to and relied on by functions this
# function calls. They are effectively part of the calling API to # function calls. They are effectively part of the calling API to
# build_binaries_for_platform. # build_binaries_for_platform.
local goflags goldflags gogcflags gotags local goflags goldflags gogcflags gotags
goflags=() goflags=()
gogcflags="${GOGCFLAGS:-}" gogcflags="${GOGCFLAGS:-}"
goldflags="all=$(kube::version::ldflags) ${GOLDFLAGS:-}" goldflags="all=$(kube::version::ldflags) ${GOLDFLAGS:-}"
if [[ "${DBG:-}" == 1 ]]; then if [[ "${DBG:-}" == 1 ]]; then
# Debugging - disable optimizations and inlining and trimPath # Debugging - disable optimizations and inlining and trimPath
gogcflags="${gogcflags} all=-N -l" gogcflags="${gogcflags} all=-N -l"
else
# Not debugging - disable symbols and DWARF, trim embedded paths
goldflags="${goldflags} -s -w"
goflags+=("-trimpath")
fi
# Extract tags if any specified in GOFLAGS
gotags="selinux,notest,$(echo "${GOFLAGS:-}" | sed -ne 's|.*-tags=\([^-]*\).*|\1|p')"
local -a targets=()
local arg
for arg; do
if [[ "${arg}" == -* ]]; then
# Assume arguments starting with a dash are flags to pass to go.
goflags+=("${arg}")
else else
# Not debugging - disable symbols and DWARF, trim embedded paths targets+=("${arg}")
goldflags="${goldflags} -s -w"
goflags+=("-trimpath")
fi fi
done
# Extract tags if any specified in GOFLAGS local -a platforms
gotags="selinux,notest,$(echo "${GOFLAGS:-}" | sed -ne 's|.*-tags=\([^-]*\).*|\1|p')" IFS=" " read -ra platforms <<< "${KUBE_BUILD_PLATFORMS:-}"
if [[ ${#platforms[@]} -eq 0 ]]; then
platforms=("${host_platform}")
fi
local -a targets=() if [[ ${#targets[@]} -eq 0 ]]; then
local arg targets=("${KUBE_ALL_TARGETS[@]}")
fi
kube::util::read-array targets < <(kube::golang::dedup "${targets[@]}")
for arg; do local -a binaries
if [[ "${arg}" == -* ]]; then kube::util::read-array binaries < <(kube::golang::normalize_go_targets "${targets[@]}")
# Assume arguments starting with a dash are flags to pass to go. kube::util::read-array binaries < <(kube::golang::dedup "${binaries[@]}")
goflags+=("${arg}")
else local parallel=false
targets+=("${arg}") if [[ ${#platforms[@]} -gt 1 ]]; then
fi local gigs
gigs=$(kube::golang::get_physmem)
if [[ ${gigs} -ge ${KUBE_PARALLEL_BUILD_MEMORY} ]]; then
kube::log::status "Multiple platforms requested and available ${gigs}G >= threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in parallel"
parallel=true
else
kube::log::status "Multiple platforms requested, but available ${gigs}G < threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in serial"
parallel=false
fi
fi
if [[ "${parallel}" == "true" ]]; then
kube::log::status "Building go targets for {${platforms[*]}} in parallel (output will appear in a burst when complete):" "${targets[@]}"
local platform
for platform in "${platforms[@]}"; do (
kube::golang::set_platform_envs "${platform}"
kube::log::status "${platform}: build started"
kube::golang::build_binaries_for_platform "${platform}"
kube::log::status "${platform}: build finished"
) &> "/tmp//${platform//\//_}.build" &
done done
local -a platforms local fails=0
IFS=" " read -ra platforms <<< "${KUBE_BUILD_PLATFORMS:-}" for job in $(jobs -p); do
if [[ ${#platforms[@]} -eq 0 ]]; then wait "${job}" || (( fails+=1 ))
platforms=("${host_platform}") done
fi
if [[ ${#targets[@]} -eq 0 ]]; then for platform in "${platforms[@]}"; do
targets=("${KUBE_ALL_TARGETS[@]}") cat "/tmp//${platform//\//_}.build"
fi done
kube::util::read-array targets < <(kube::golang::dedup "${targets[@]}")
local -a binaries exit "${fails}"
kube::util::read-array binaries < <(kube::golang::normalize_go_targets "${targets[@]}") else
kube::util::read-array binaries < <(kube::golang::dedup "${binaries[@]}") for platform in "${platforms[@]}"; do
kube::log::status "Building go targets for ${platform}"
local parallel=false (
if [[ ${#platforms[@]} -gt 1 ]]; then kube::golang::set_platform_envs "${platform}"
local gigs kube::golang::build_binaries_for_platform "${platform}"
gigs=$(kube::golang::get_physmem) )
done
if [[ ${gigs} -ge ${KUBE_PARALLEL_BUILD_MEMORY} ]]; then fi
kube::log::status "Multiple platforms requested and available ${gigs}G >= threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in parallel"
parallel=true
else
kube::log::status "Multiple platforms requested, but available ${gigs}G < threshold ${KUBE_PARALLEL_BUILD_MEMORY}G, building platforms in serial"
parallel=false
fi
fi
if [[ "${parallel}" == "true" ]]; then
kube::log::status "Building go targets for {${platforms[*]}} in parallel (output will appear in a burst when complete):" "${targets[@]}"
local platform
for platform in "${platforms[@]}"; do (
kube::golang::set_platform_envs "${platform}"
kube::log::status "${platform}: build started"
kube::golang::build_binaries_for_platform "${platform}"
kube::log::status "${platform}: build finished"
) &> "/tmp//${platform//\//_}.build" &
done
local fails=0
for job in $(jobs -p); do
wait "${job}" || (( fails+=1 ))
done
for platform in "${platforms[@]}"; do
cat "/tmp//${platform//\//_}.build"
done
exit "${fails}"
else
for platform in "${platforms[@]}"; do
kube::log::status "Building go targets for ${platform}"
(
kube::golang::set_platform_envs "${platform}"
kube::golang::build_binaries_for_platform "${platform}"
)
done
fi
)
} }

View File

@ -24,5 +24,6 @@ KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../..
KUBE_VERBOSE="${KUBE_VERBOSE:-1}" KUBE_VERBOSE="${KUBE_VERBOSE:-1}"
source "${KUBE_ROOT}/hack/lib/init.sh" source "${KUBE_ROOT}/hack/lib/init.sh"
kube::golang::setup_env
kube::golang::build_binaries "$@" kube::golang::build_binaries "$@"
kube::golang::place_bins kube::golang::place_bins