2020-12-30 15:26:25 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# AdGuard Home Build Script
|
|
|
|
#
|
2021-05-19 18:31:20 +01: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.
|
|
|
|
|
2023-02-21 14:07:12 +00:00
|
|
|
# This comment is used to simplify checking local copies of the script. Bump
|
|
|
|
# this number every time a significant change is made to this script.
|
|
|
|
#
|
2024-11-08 14:18:16 +00:00
|
|
|
# AdGuard-Project-Version: 2
|
2023-02-21 14:07:12 +00:00
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
# 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
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$verbose" -gt '1' ]; then
|
2020-12-30 15:26:25 +00:00
|
|
|
env
|
|
|
|
set -x
|
2021-06-10 18:09:00 +01:00
|
|
|
v_flags='-v=1'
|
|
|
|
x_flags='-x=1'
|
2024-11-08 14:18:16 +00:00
|
|
|
elif [ "$verbose" -gt '0' ]; then
|
2020-12-30 15:26:25 +00:00
|
|
|
set -x
|
2021-06-10 18:09:00 +01:00
|
|
|
v_flags='-v=1'
|
|
|
|
x_flags='-x=0'
|
2020-12-30 15:26:25 +00:00
|
|
|
else
|
|
|
|
set +x
|
2021-06-10 18:09:00 +01:00
|
|
|
v_flags='-v=0'
|
|
|
|
x_flags='-x=0'
|
2020-12-30 15:26:25 +00:00
|
|
|
fi
|
2021-05-19 18:31:20 +01: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
|
|
|
|
|
2021-05-21 13:40:44 +01:00
|
|
|
# Allow users to override the go command from environment. For example, to
|
|
|
|
# build two releases with two different Go versions and test the difference.
|
2021-05-19 18:31:20 +01: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-06-10 18:09:00 +01:00
|
|
|
channel="${CHANNEL:?please set CHANNEL}"
|
2021-05-19 18:31:20 +01:00
|
|
|
readonly channel
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
case "$channel" in
|
|
|
|
'development' | 'edge' | 'beta' | 'release' | 'candidate')
|
2020-12-30 15:26:25 +00:00
|
|
|
# All is well, go on.
|
|
|
|
;;
|
2024-11-08 14:18:16 +00:00
|
|
|
*)
|
|
|
|
echo "invalid channel '$channel', supported values are \
|
2024-02-16 16:19:00 +00:00
|
|
|
'development', 'edge', 'beta', 'release', and 'candidate'" 1>&2
|
2020-12-30 15:26:25 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-05-24 19:46:33 +01:00
|
|
|
# Check VERSION against the default value from the Makefile. If it is that, use
|
|
|
|
# the version calculation script.
|
|
|
|
version="${VERSION:-}"
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$version" = 'v0.0.0' ] || [ "$version" = '' ]; then
|
|
|
|
version="$(sh ./scripts/make/version.sh)"
|
2021-05-24 19:46:33 +01:00
|
|
|
fi
|
2021-05-19 18:31:20 +01:00
|
|
|
readonly version
|
2020-12-30 15:26:25 +00:00
|
|
|
|
2022-02-01 18:44:01 +00:00
|
|
|
# Set date and time of the latest commit unless already set.
|
2024-11-08 14:18:16 +00:00
|
|
|
committime="${SOURCE_DATE_EPOCH:-$(git log -1 --pretty=%ct)}"
|
2022-02-01 18:44:01 +00:00
|
|
|
readonly committime
|
2021-05-19 18:31:20 +01:00
|
|
|
|
|
|
|
# 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 10:55:21 +01: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}"
|
2022-02-01 18:44:01 +00:00
|
|
|
ldflags="${ldflags} -X ${version_pkg}.committime=${committime}"
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "${GOARM:-}" != '' ]; then
|
2021-01-13 13:18:51 +00:00
|
|
|
ldflags="${ldflags} -X ${version_pkg}.goarm=${GOARM}"
|
2024-11-08 14:18:16 +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
|
2024-08-20 16:38:04 +01:00
|
|
|
readonly ldflags
|
2020-12-30 15:26:25 +00:00
|
|
|
|
|
|
|
# Allow users to limit the build's parallelism.
|
2021-06-10 18:47:58 +01:00
|
|
|
parallelism="${PARALLELISM:-}"
|
2021-05-19 18:31:20 +01:00
|
|
|
readonly parallelism
|
|
|
|
|
2021-06-10 18:47:58 +01:00
|
|
|
# Use GOFLAGS for -p, because -p=0 simply disables the build instead of leaving
|
|
|
|
# the default value.
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "${parallelism}" != '' ]; then
|
|
|
|
GOFLAGS="${GOFLAGS:-} -p=${parallelism}"
|
2021-06-10 18:47:58 +01:00
|
|
|
fi
|
|
|
|
readonly GOFLAGS
|
|
|
|
export GOFLAGS
|
2020-12-30 15:26:25 +00:00
|
|
|
|
|
|
|
# Allow users to specify a different output name.
|
2021-06-10 18:09:00 +01:00
|
|
|
out="${OUT:-AdGuardHome}"
|
2021-05-19 18:31:20 +01:00
|
|
|
readonly out
|
|
|
|
|
2021-06-10 18:09:00 +01:00
|
|
|
o_flags="-o=${out}"
|
|
|
|
readonly o_flags
|
2020-12-30 15:26:25 +00:00
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
# Allow users to enable the race detector. Unfortunately, that means that cgo
|
|
|
|
# must be enabled.
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "${RACE:-0}" -eq '0' ]; then
|
2023-02-21 14:07:12 +00:00
|
|
|
CGO_ENABLED='0'
|
2021-06-10 18:09:00 +01:00
|
|
|
race_flags='--race=0'
|
2021-04-02 11:44:55 +01:00
|
|
|
else
|
2023-02-21 14:07:12 +00:00
|
|
|
CGO_ENABLED='1'
|
2021-06-10 18:09:00 +01:00
|
|
|
race_flags='--race=1'
|
2021-04-02 11:44:55 +01:00
|
|
|
fi
|
2023-02-21 14:07:12 +00:00
|
|
|
readonly CGO_ENABLED race_flags
|
|
|
|
export CGO_ENABLED
|
2021-04-02 11:44:55 +01:00
|
|
|
|
2021-05-19 18:31:20 +01:00
|
|
|
GO111MODULE='on'
|
2023-02-21 14:07:12 +00:00
|
|
|
export GO111MODULE
|
2020-12-30 15:26:25 +00:00
|
|
|
|
2022-04-26 18:50:09 +01:00
|
|
|
# Build the new binary if requested.
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "${NEXTAPI:-0}" -eq '0' ]; then
|
2022-04-26 18:50:09 +01:00
|
|
|
tags_flags='--tags='
|
|
|
|
else
|
2022-10-10 12:05:24 +01:00
|
|
|
tags_flags='--tags=next'
|
2022-04-26 18:50:09 +01:00
|
|
|
fi
|
|
|
|
readonly tags_flags
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$verbose" -gt '0' ]; then
|
2023-02-21 14:07:12 +00:00
|
|
|
"$go" env
|
|
|
|
fi
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
"$go" build \
|
|
|
|
--ldflags="$ldflags" \
|
|
|
|
"$race_flags" \
|
|
|
|
"$tags_flags" \
|
|
|
|
--trimpath \
|
|
|
|
"$o_flags" \
|
|
|
|
"$v_flags" \
|
|
|
|
"$x_flags" \
|
|
|
|
;
|