-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: make-rules build script (#66)
* build: make-rules build script * fix: add build args only when env is not empty
- Loading branch information
Showing
5 changed files
with
243 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2024 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
# Prints the value that needs to be passed to the -ldflags parameter of go build | ||
# in order to set the Kubernetes based on the git tree status. | ||
# IMPORTANT: if you update any of these, also update the lists in | ||
# pkg/version/def.bzl and hack/print-workspace-status.sh. | ||
kube::version::ldflags() { | ||
local -a ldflags | ||
function add_ldflag() { | ||
local key=${1} | ||
local val=${2} | ||
# If you update these, also update the list component-base/version/def.bzl. | ||
ldflags+=( | ||
"-X 'k8s.io/client-go/pkg/version.${key}=${val}'" | ||
"-X 'k8s.io/component-base/version.${key}=${val}'" | ||
) | ||
} | ||
|
||
add_ldflag "buildDate" "$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')" | ||
if [[ -n ${GIT_COMMIT-} ]]; then | ||
add_ldflag "gitCommit" "${GIT_COMMIT}" | ||
add_ldflag "gitTreeState" "${GIT_TREE_STATE:-}" | ||
fi | ||
|
||
if [[ -n ${GIT_VERSION-} ]]; then | ||
add_ldflag "gitVersion" "${GIT_VERSION}" | ||
fi | ||
|
||
if [[ -n ${GIT_MAJOR-} && -n ${GIT_MINOR-} ]]; then | ||
add_ldflag "gitMajor" "${GIT_MAJOR:-}" | ||
add_ldflag "gitMinor" "${GIT_MINOR:-}" | ||
fi | ||
|
||
# The -ldflags parameter takes a single string, so join the output. | ||
echo "${ldflags[*]-}" | ||
} | ||
|
||
# Build binaries targets specified | ||
# | ||
# Input: | ||
# $@ - targets and go flags. If no targets are set then all binaries targets | ||
# are built. | ||
# MAKE_RULES_GO_BUILD_PLATFORMS - Incoming variable of targets to build for. If unset | ||
# then just the host architecture is built. | ||
kube::golang::build_binaries() { | ||
# Create a sub-shell so that we don't pollute the outer environment | ||
( | ||
# Check for `go` binary and set ${GOPATH}. | ||
# kube::golang::setup_env | ||
V=2 kube::log::info "Go version: $(go version)" | ||
|
||
local host_platform | ||
host_platform="$(go env GOHOSTOS)/$(go env GOHOSTARCH)" | ||
|
||
export GOLDFLAGS="${GOLDFLAGS:-} $(kube::version::ldflags)" | ||
|
||
local -a targets=() | ||
local arg | ||
|
||
for arg; do | ||
targets+=("${arg}") | ||
done | ||
|
||
local -a platforms | ||
IFS=" " read -ra platforms <<< "${MAKE_RULES_GO_BUILD_PLATFORMS:-}" | ||
if [[ ${#platforms[@]} -eq 0 ]]; then | ||
platforms=("${host_platform}") | ||
fi | ||
|
||
for platform in "${platforms[@]}"; do | ||
kube::log::status "Building go targets for ${platform}:" "${targets[@]}" | ||
( | ||
export GOOS=${platform%/*} | ||
export GOARCH=${platform##*/} | ||
|
||
dir="bin/${platform//\//_}" | ||
mkdir -p "${dir}" | ||
|
||
# shellcheck disable=SC2164 | ||
cd "${MAKE_RULES_WORKSPACE}" | ||
|
||
for target in "${targets[@]}"; do | ||
|
||
|
||
# pre-hook | ||
if [[ -f "${MAKE_RULES_WORKSPACE}/${target}/pre-build" ]]; then | ||
kube::log::status "Pre hook for ${target}:" "${target}/pre-build" | ||
bash "${MAKE_RULES_WORKSPACE}/${target}/pre-build" | ||
fi | ||
|
||
# build | ||
build_args=() | ||
|
||
if [ -n "${GOFLAGS:-}" ]; then | ||
build_args+=("${GOFLAGS[@]}") | ||
fi | ||
|
||
if [ -n "${GOGCFLAGS:-}" ]; then | ||
build_args+=(-gcflags "${GOGCFLAGS}") | ||
fi | ||
|
||
if [ -n "${GOASMFLAGS:-}" ]; then | ||
build_args+=(-asmflags "${GOASMFLAGS}") | ||
fi | ||
|
||
if [ -n "${GOLDFLAGS:-}" ]; then | ||
build_args+=(-ldflags "${GOLDFLAGS}") | ||
fi | ||
|
||
if [ -n "${GOTAGS:-}" ]; then | ||
build_args+=(-tags "${GOTAGS}") | ||
fi | ||
|
||
go_build_args="${build_args[@]}" | ||
|
||
bin=${target##*/} | ||
kube::log::status "Building go target for ${platform}:" "target: ${target}" "output: ${dir}/${bin}" "args: ${go_build_args}" | ||
go build "${build_args[@]}" -o "${dir}/${bin}" "./${target}" | ||
|
||
# post-hook | ||
if [[ -f "${MAKE_RULES_WORKSPACE}/${target}/post-build" ]]; then | ||
kube::log::status "Post hook for ${target}:" "${target}/post-build" | ||
bash "${MAKE_RULES_WORKSPACE}/${target}/post-build" | ||
fi | ||
done | ||
) | ||
done | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2014 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Controls verbosity of the script output and logging. | ||
VERBOSE="${VERBOSE:-5}" | ||
|
||
# Log an error but keep going. Don't dump the stack or exit. | ||
kube::log::error() { | ||
timestamp=$(date +"[%m%d %H:%M:%S]") | ||
echo "!!! ${timestamp} ${1-}" >&2 | ||
shift | ||
for message; do | ||
echo " ${message}" >&2 | ||
done | ||
} | ||
|
||
# Print out some info that isn't a top level status line | ||
kube::log::info() { | ||
local V="${V:-0}" | ||
if [[ ${VERBOSE} < ${V} ]]; then | ||
return | ||
fi | ||
|
||
for message; do | ||
echo "${message}" | ||
done | ||
} | ||
|
||
# Print a status line. Formatted to show up in a stream of output. | ||
kube::log::status() { | ||
local V="${V:-0}" | ||
if [[ ${VERBOSE} < ${V} ]]; then | ||
return | ||
fi | ||
|
||
timestamp=$(date +"[%m%d %H:%M:%S]") | ||
echo "+++ ${timestamp} ${1}" | ||
shift | ||
for message; do | ||
echo " ${message}" | ||
done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Copyright 2024 ByteDance and its affiliates. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
set -o errexit | ||
set -o nounset | ||
set -o pipefail | ||
|
||
export MAKE_RULES_WORKSPACE=$(dirname "${BASH_SOURCE[0]}")/../.. | ||
export MAKE_RULES_GO_BUILD_PLATFORMS=${GO_BUILD_PLATFORMS-""} | ||
|
||
VERBOSE="${VERBOSE:-1}" | ||
if [[ -n ${VERSION-} ]]; then | ||
GIT_VERSION="${VERSION}" | ||
fi | ||
|
||
|
||
source "${MAKE_RULES_WORKSPACE}/hack/lib/golang.sh" | ||
source "${MAKE_RULES_WORKSPACE}/hack/lib/logging.sh" | ||
|
||
bash "${MAKE_RULES_WORKSPACE}/hack/hooks/pre-build" | ||
|
||
kube::golang::build_binaries "$@" | ||
|
||
bash "${MAKE_RULES_WORKSPACE}/hack/hooks/post-build" |