Skip to content

Commit

Permalink
build: make-rules build script (#66)
Browse files Browse the repository at this point in the history
* build: make-rules build script

* fix: add build args only when env is not empty
  • Loading branch information
xuqingyun authored Jul 26, 2024
1 parent 1a48c44 commit eab9b9c
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 5 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ else
GOBIN=$(shell go env GOBIN)
endif

MYBUILD="hack/make-rules/build.sh"

define command_exists
@$(shell command -v $(1) >/dev/null 2>&1)
Expand Down Expand Up @@ -81,7 +82,7 @@ all build:
@echo "$$ALL_HELP_INFO"
else
all build: make-rules
@$(MYMAKE) go build --version="$(VERSION)" --v="$(VERBOSE)" $(WHAT)
@$(MYBUILD) $(WHAT)
endif

define GO_BUILD_HELP_INFO
Expand All @@ -106,7 +107,7 @@ $(GO_BUILD_TARGETS):
$(call GO_BUILD_HELP_INFO, $@)
else
$(GO_BUILD_TARGETS): make-rules
@$(MYMAKE) go build --version="$(VERSION)" --v="$(VERBOSE)" $@
@$(MYBUILD) $@
endif

define UNITTEST_HELP_INFO
Expand Down Expand Up @@ -161,7 +162,7 @@ build-local:
@echo "$$BUILD_LOCAL_HELP_INFO"
else
build-local: make-rules
@$(MYMAKE) go build --version="$(VERSION)" --v="$(VERBOSE)" $(WHAT)
@$(MYBUILD) $(WHAT)
endif

define BUILD_IN_CONTAINER_HELP_INFO
Expand Down Expand Up @@ -190,7 +191,7 @@ build-in-container:
@echo "$$BUILD_LINUX_HELP_INFO"
else
build-in-container: make-rules
@$(MYMAKE) go build --version="$(VERSION)" --v="$(VERBOSE)" $(WHAT)
@$(MYBUILD) $(WHAT)
endif

define CONTAINER_HELP_INFO
Expand Down
2 changes: 1 addition & 1 deletion hack/hooks/post-build
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

function resolve_platform() {
local platforms=(${MAKE_RULES_GO_BUILD_PLATFORMS[@]})
local platform="linux/amd64"
local platform="$(go env GOHOSTOS)/$(go env GOHOSTARCH)"
if [[ ${#platforms[@]} -ge 1 ]]; then
platform="${platforms[0]}"
fi
Expand Down
144 changes: 144 additions & 0 deletions hack/lib/golang.sh
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
)
}
55 changes: 55 additions & 0 deletions hack/lib/logging.sh
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
}
38 changes: 38 additions & 0 deletions hack/make-rules/build.sh
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"

0 comments on commit eab9b9c

Please sign in to comment.