2020-12-30 15:26:25 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# AdGuard Home Build Script
|
|
|
|
#
|
2021-05-19 17:31:20 +00:00
|
|
|
# The commentary in this file is written with the assumption that the reader
|
|
|
|
# only has superficial knowledge of the POSIX shell language and alike.
|
|
|
|
# Experienced readers may find it overly verbose.
|
|
|
|
|
|
|
|
# The default verbosity level is 0. Show every command that is run and every
|
|
|
|
# package that is processed if the caller requested verbosity level greater than
|
|
|
|
# 0. Also show subcommands if the requested verbosity level is greater than 1.
|
|
|
|
# Otherwise, do nothing.
|
|
|
|
verbose="${VERBOSE:-0}"
|
|
|
|
readonly verbose
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
if [ "$verbose" -gt '1' ]
|
|
|
|
then
|
|
|
|
env
|
|
|
|
set -x
|
2021-05-19 17:31:20 +00:00
|
|
|
v_flags='-v'
|
|
|
|
x_flags='-x'
|
2020-12-30 15:26:25 +00:00
|
|
|
elif [ "$verbose" -gt '0' ]
|
|
|
|
then
|
|
|
|
set -x
|
2021-05-19 17:31:20 +00:00
|
|
|
v_flags='-v'
|
|
|
|
x_flags=''
|
2020-12-30 15:26:25 +00:00
|
|
|
else
|
|
|
|
set +x
|
2021-05-19 17:31:20 +00:00
|
|
|
v_flags=''
|
|
|
|
x_flags=''
|
2020-12-30 15:26:25 +00:00
|
|
|
fi
|
2021-05-19 17:31:20 +00:00
|
|
|
readonly x_flags v_flags
|
2020-12-30 15:26:25 +00:00
|
|
|
|
|
|
|
# Exit the script if a pipeline fails (-e), prevent accidental filename
|
|
|
|
# expansion (-f), and consider undefined variables as errors (-u).
|
|
|
|
set -e -f -u
|
|
|
|
|
|
|
|
# Allow users to set the Go version.
|
2021-05-19 17:31:20 +00:00
|
|
|
go="${GO:-go}"
|
|
|
|
readonly go
|
2020-12-30 15:26:25 +00:00
|
|
|
|
|
|
|
# Require the channel to be set and validate the value.
|
2021-05-19 17:31:20 +00:00
|
|
|
channel="$CHANNEL"
|
|
|
|
readonly channel
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
case "$channel"
|
|
|
|
in
|
|
|
|
('development'|'edge'|'beta'|'release')
|
|
|
|
# All is well, go on.
|
|
|
|
;;
|
|
|
|
(*)
|
|
|
|
echo "invalid channel '$channel', supported values are\
|
|
|
|
'development', 'edge', 'beta', and 'release'" 1>&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Require the version to be set.
|
|
|
|
#
|
|
|
|
# TODO(a.garipov): Additional validation?
|
2021-05-19 17:31:20 +00:00
|
|
|
version="$VERSION"
|
|
|
|
readonly version
|
2020-12-30 15:26:25 +00:00
|
|
|
|
2021-04-30 16:51:23 +00:00
|
|
|
# Set date and time of the current build unless already set.
|
2021-05-19 17:31:20 +00:00
|
|
|
buildtime="${BUILD_TIME:-$( date -u +%FT%TZ%z )}"
|
|
|
|
readonly buildtime
|
|
|
|
|
|
|
|
# Set the linker flags accordingly: set the release channel and the current
|
|
|
|
# version as well as goarm and gomips variable values, if the variables are set
|
|
|
|
# and are not empty.
|
|
|
|
version_pkg='github.com/AdguardTeam/AdGuardHome/internal/version'
|
|
|
|
readonly version_pkg
|
2021-03-31 09:55:21 +00:00
|
|
|
|
|
|
|
ldflags="-s -w"
|
|
|
|
ldflags="${ldflags} -X ${version_pkg}.version=${version}"
|
2021-01-13 13:18:51 +00:00
|
|
|
ldflags="${ldflags} -X ${version_pkg}.channel=${channel}"
|
2021-03-31 09:55:21 +00:00
|
|
|
ldflags="${ldflags} -X ${version_pkg}.buildtime=${buildtime}"
|
2020-12-30 15:26:25 +00:00
|
|
|
if [ "${GOARM:-}" != '' ]
|
|
|
|
then
|
2021-01-13 13:18:51 +00:00
|
|
|
ldflags="${ldflags} -X ${version_pkg}.goarm=${GOARM}"
|
2020-12-30 15:26:25 +00:00
|
|
|
elif [ "${GOMIPS:-}" != '' ]
|
|
|
|
then
|
2021-01-13 13:18:51 +00:00
|
|
|
ldflags="${ldflags} -X ${version_pkg}.gomips=${GOMIPS}"
|
2020-12-30 15:26:25 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Allow users to limit the build's parallelism.
|
2021-05-19 17:31:20 +00:00
|
|
|
parallelism="${PARALLELISM:-}"
|
|
|
|
readonly parallelism
|
|
|
|
|
|
|
|
if [ "${parallelism}" != '' ]
|
2020-12-30 15:26:25 +00:00
|
|
|
then
|
2021-05-19 17:31:20 +00:00
|
|
|
par_flags="-p ${parallelism}"
|
2020-12-30 15:26:25 +00:00
|
|
|
else
|
2021-05-19 17:31:20 +00:00
|
|
|
par_flags=''
|
2020-12-30 15:26:25 +00:00
|
|
|
fi
|
2021-05-19 17:31:20 +00:00
|
|
|
readonly par_flags
|
2020-12-30 15:26:25 +00:00
|
|
|
|
|
|
|
# Allow users to specify a different output name.
|
2021-05-19 17:31:20 +00:00
|
|
|
out="${OUT:-}"
|
|
|
|
readonly out
|
|
|
|
|
2020-12-30 15:26:25 +00:00
|
|
|
if [ "$out" != '' ]
|
|
|
|
then
|
2021-05-19 17:31:20 +00:00
|
|
|
out_flags="-o ${out}"
|
2020-12-30 15:26:25 +00:00
|
|
|
else
|
2021-05-19 17:31:20 +00:00
|
|
|
out_flags=''
|
2020-12-30 15:26:25 +00:00
|
|
|
fi
|
2021-05-19 17:31:20 +00:00
|
|
|
readonly out_flags
|
2020-12-30 15:26:25 +00:00
|
|
|
|
2021-05-19 17:31:20 +00:00
|
|
|
# Allow users to enable the race detector. Unfortunately, that means that cgo
|
|
|
|
# must be enabled.
|
|
|
|
if [ "${RACE:-0}" -eq '0' ]
|
2021-04-02 10:44:55 +00:00
|
|
|
then
|
2021-05-19 17:31:20 +00:00
|
|
|
cgo_enabled='0'
|
|
|
|
race_flags=''
|
2021-04-02 10:44:55 +00:00
|
|
|
else
|
2021-05-19 17:31:20 +00:00
|
|
|
cgo_enabled='1'
|
|
|
|
race_flags='--race'
|
2021-04-02 10:44:55 +00:00
|
|
|
fi
|
2021-05-19 17:31:20 +00:00
|
|
|
readonly cgo_enabled race_flags
|
2021-04-02 10:44:55 +00:00
|
|
|
|
2021-05-19 17:31:20 +00:00
|
|
|
CGO_ENABLED="$cgo_enabled"
|
|
|
|
GO111MODULE='on'
|
|
|
|
export CGO_ENABLED GO111MODULE
|
2020-12-30 15:26:25 +00:00
|
|
|
|
2021-05-19 17:31:20 +00:00
|
|
|
build_flags="${BUILD_FLAGS:-$race_flags --trimpath $out_flags $par_flags $v_flags $x_flags}"
|
|
|
|
readonly build_flags
|
2020-12-30 15:26:25 +00:00
|
|
|
|
|
|
|
# Don't use quotes with flag variables to get word splitting.
|
2021-03-23 09:32:07 +00:00
|
|
|
"$go" generate $v_flags $x_flags ./main.go
|
2020-12-30 15:26:25 +00:00
|
|
|
|
|
|
|
# Don't use quotes with flag variables to get word splitting.
|
|
|
|
"$go" build --ldflags "$ldflags" $build_flags
|