Skip to content

Commit

Permalink
feat(build): add options to the build script and optimize the details…
Browse files Browse the repository at this point in the history
… of the script

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>
  • Loading branch information
cubxxw committed May 6, 2023
1 parent 2f5e6c6 commit 1b23e59
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 45 deletions.
144 changes: 110 additions & 34 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
#!/bin/bash
# Copyright © 2021 Alibaba Group Holding Ltd.
#
# 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.

# -----------------------------------------------------------------------------
# Build management helpers. These functions help to set, save and load the
# following variables:
#
# GIT_TAG - The version for sealer.
# MULTI_PLATFORM_BUILD - Need build all platform.(linux and darwin)
#!/usr/bin/env bash

## Build script

# Build helper functions. These functions help to set up, save, and load the following variables:
#
# GIT_TAG - the version number of sealer
# MULTI_PLATFORM_BUILD - whether to build for all platforms (linux and darwin)

# Set GO111MODULE=on to enable Go Modules when using go mod to manage dependencies
export GO111MODULE=on
# Turn on command tracing so that each command is output when the script is run
set -x
# Get the absolute path of the current script and set the variable SEALER_ROOT to this value
SEALER_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
# Set the variables THIS_PLATFORM_BIN and THIS_PLATFORM_ASSETS for use when building binaries and assets (such as tar.gz files)
export THIS_PLATFORM_BIN="${SEALER_ROOT}/_output/bin"
export THIS_PLATFORM_ASSETS="${SEALER_ROOT}/_output/assets"

# fix containers dependency issue
# https://github.com/containers/image/pull/271/files
# for btrfs, we just use overlay at present, so there is no need to include btrfs, otherwise we need fix some lib problems
# Fixed container dependency issues
# Link -> https://github.com/containers/image/pull/271/files
# For btrfs, we are currently only using overlays, so we don't need to include btrfs, otherwise we need to fix some lib issues
GO_BUILD_FLAGS="containers_image_openpgp exclude_graphdriver_devicemapper exclude_graphdriver_btrfs"

# Output debug information
debug() {
timestamp=$(date +"[%m%d %H:%M:%S]")
echo "[debug] ${timestamp} ${1-}" >&2
Expand All @@ -41,6 +32,7 @@ debug() {
done
}

# Get version information
get_version_vars() {
GIT_VERSION=unknown
if [[ $GIT_TAG ]]; then
Expand All @@ -55,6 +47,8 @@ get_version_vars() {
debug "commit id: $GIT_COMMIT"
}

# Parameters used when building the program
# Used to pass parameters such as compilation time, version number, commit ID, etc.
ldflags() {
local -a ldflags
function add_ldflag() {
Expand All @@ -78,24 +72,27 @@ ldflags() {
echo "${ldflags[*]-}"
}

# Package path for sealer source code
readonly SEALER_GO_PACKAGE=github.com/sealerio/sealer
# The server platform we are building on.
# Platforms supported when building the program
readonly SEALER_SUPPORTED_PLATFORMS=(
linux/amd64
linux/arm64
)

# Check if the program needs to be built
check() {
timestamp=$(date +"[%m%d %H:%M:%S]")
ret=$1
if [[ $ret -eq 0 ]];then
if [[ $ret -eq 0 ]]; then
echo "[info] ${timestamp} ${2-} up to date."
else
echo "[err] ${timestamp} ${2-} is out of date. Please run $0"
exit 1
fi
}

# Function for building the program
build_binaries() {
get_version_vars
goldflags="${GOLDFLAGS=-s -w} $(ldflags)"
Expand Down Expand Up @@ -124,19 +121,98 @@ build_binaries() {
mv *.tar.gz* $THIS_PLATFORM_ASSETS/
debug "output tar.gz: $THIS_PLATFORM_ASSETS/seautil-$tarFile"
debug "output sha256sum: $THIS_PLATFORM_ASSETS/seautil-$tarFile.sha256sum"
debug ""
}

# Display help information
show_help() {
cat << EOF
Usage: $0 [-h] [-p PLATFORMS] [-a] [-b BINARIES]
Build Sealer binaries for one or more platforms.
DOTO: I recommend using a Makefile for a more immersive experience
-h, --help display this help and exit
-p, --platform build binaries for the specified platform(s), e.g. linux/amd64 or linux/arm64.
Multiple platforms should be separated by comma, e.g. linux/amd64,linux/arm64.
-a, --all build binaries for all supported platforms
-b, --binary build the specified binary/binaries, e.g. sealer or seautil.
Multiple binaries should be separated by comma, e.g. sealer,seautil.
(note: currently only supported in Makefile)
EOF
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-p|--platform)
shift
PLATFORMS=$1
;;
-a|--all)
ALL_PLATFORMS=true
;;
-b|--binary)
shift
BINARIES=$1
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
shift
done

debug "root dir: $SEALER_ROOT"
debug "build dir: $THIS_PLATFORM_BIN"

#Multi platform
if [[ $MULTI_PLATFORM_BUILD ]]; then
for platform in "${SEALER_SUPPORTED_PLATFORMS[@]}"; do
OS=${platform%/*}
ARCH=${platform##*/}
build_binaries $OS $ARCH
done;
# Build binaries for the specified platforms
if [[ -n "$PLATFORMS" ]]; then
IFS=',' read -ra PLATFORM_LIST <<< "$PLATFORMS"
for platform in "${PLATFORM_LIST[@]}"; do
OS=${platform%/*}
ARCH=${platform##*/}
build_binaries "$OS" "$ARCH"
done
# Build binaries for all supported platforms
elif [[ "$ALL_PLATFORMS" = true ]]; then
for platform in "${SEALER_SUPPORTED_PLATFORMS[@]}"; do
OS=${platform%/*}
ARCH=${platform##*/}
build_binaries "$OS" "$ARCH"
done
# Build the specified binaries
elif [[ -n "$BINARIES" ]]; then
IFS=',' read -ra BINARY_LIST <<< "$BINARIES"
for binary in "${BINARY_LIST[@]}"; do
case "$binary" in
sealer)
build_binaries `go env GOOS` `go env GOARCH`
;;
seautil)
osarch=`go env GOOS`_`go env GOARCH`
GOOS=`go env GOOS` GOARCH=`go env GOARCH` go build -o $THIS_PLATFORM_BIN/seautil/$osarch/seautil -mod vendor -ldflags "$(ldflags)" $SEALER_ROOT/cmd/seautil/main.go
check $? "build seautil"
debug "output bin: $THIS_PLATFORM_BIN/seautil/$osarch/seautil"
;;
*)
echo "Unknown binary: $binary"
show_help
exit 1
;;
esac
done
# Build all binaries for the current platform by default
else
build_binaries `go env GOOS` `go env GOARCH`
fi
fi
16 changes: 8 additions & 8 deletions scripts/make-rules/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ endef

# Here are some examples of builds
define MAKEFILE_EXAMPLE
# make build BINS=sealer Only a single sealer binary is built
# make -j $(nproc) all Run tidy gen add-copyright format lint cover build concurrently
# make gen Generate all necessary files
# make deepcopy Generate deepcopy code
# make verify-copyright Verify the license headers for all files
# make install-deepcopy-gen Install deepcopy-gen tools if the license is missing
# make build BINS=sealer V=1 DEBUG=1 Build debug binaries for only sealer
# make build.multiarch PLATFORMS="linux_arm64 linux_amd64" V=1 Build binaries for both platforms
# make build BINS=sealer Only a single sealer binary is built.
# make -j $(nproc) all Run tidy gen add-copyright format lint cover build concurrently.
# make gen Generate all necessary files.
# make linux.arm64 sealer is compiled on arm64 platform.
# make verify-copyright Verify the license headers for all files.
# make install-deepcopy-gen Install deepcopy-gen tools if the license is missing.
# make build BINS=sealer V=1 DEBUG=1 Build debug binaries for only sealer.
# make build.multiarch PLATFORMS="linux_arm64 linux_amd64" V=1 Build binaries for both platforms.
endef
export MAKEFILE_EXAMPLE

Expand Down
4 changes: 2 additions & 2 deletions scripts/make-rules/golang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ go.build.%:
@echo "===========> Building binary $(COMMAND) $(VERSION) for $(OS) $(ARCH)"
@mkdir -p $(OUTPUT_DIR)/bin/$(OS)/$(ARCH)

@CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o $(OUTPUT_DIR)/bin/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_PACKAGE)/cmd/$(COMMAND)
# @CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o $(OUTPUT_DIR)/bin/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_PACKAGE)/cmd/$(COMMAND)

## go.build: Build binaries
.PHONY: go.build
Expand All @@ -136,7 +136,7 @@ go.build: go.build.verify $(addprefix go.build., $(addprefix $(PLATFORM)., $(BIN
.PHONY: go.build.multiarch
go.build.multiarch: go.build.verify $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS))))

## go.linux.%: Build linux_amd64 binaries
## go.linux.%: Build linux_amd64 OR linux_arm64 binaries
.PHONY: go.linux.%
go.linux.%:
@echo "Building sealer and seautil binaries for Linux $*"
Expand Down
7 changes: 6 additions & 1 deletion scripts/make-rules/tools.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TEST_TOOLS = ginkgo go-junit-report gotests
# Version control tools
VERSION_CONTROL_TOOLS = addlicense go-gitlint git-chglog github-release gsemver
# Utility tools
UTILITY_TOOLS = go-mod-outdated mockgen gothanks richgo
UTILITY_TOOLS = go-mod-outdated mockgen gothanks richgo kubeconform
# All tools
ALL_TOOLS ?= $(ANALYSIS_TOOLS) $(GENERATION_TOOLS) $(TEST_TOOLS) $(VERSION_CONTROL_TOOLS) $(UTILITY_TOOLS)

Expand Down Expand Up @@ -104,6 +104,11 @@ install.go-junit-report:
install.kube-score:
@$(GO) install github.com/zegl/kube-score/cmd/kube-score@latest

## install.kubeconform: Install kubeconform, used to check kubernetes yaml files
.PHONY: install.kubeconform
install.kubeconform:
@$(GO) install github.com/yannh/kubeconform/cmd/kubeconform@latest

## Install go-gitlint: Install go-gitlint, used to check git commit message
.PHONY: install.go-gitlint
install.go-gitlint:
Expand Down

0 comments on commit 1b23e59

Please sign in to comment.