Fix: env ver name

pull/194/head
zijiren233 2 years ago
parent 00da69a5fc
commit 44054958c9

@ -2,19 +2,19 @@ function parseDepArgs() {
for i in "$@"; do for i in "$@"; do
case ${i,,} in case ${i,,} in
--version=*) --version=*)
version="${i#*=}" VERSION="${i#*=}"
shift shift
;; ;;
--skip-init-web) --skip-init-web)
skip_init_web="true" SKIP_INIT_WEB="true"
shift shift
;; ;;
--web-version=*) --web-version=*)
web_version="${i#*=}" WEB_VERSION="${i#*=}"
shift shift
;; ;;
--web-repo=*) --web-repo=*)
web_repo="${i#*=}" WEB_REPO="${i#*=}"
shift shift
;; ;;
*) *)
@ -27,14 +27,14 @@ function parseDepArgs() {
function printDepHelp() { function printDepHelp() {
echo -e "${COLOR_YELLOW}--version=${COLOR_RESET} set build version (default: dev)" echo -e "${COLOR_YELLOW}--version=${COLOR_RESET} set build version (default: dev)"
echo -e "${COLOR_YELLOW}--web-version=${COLOR_RESET} set web dependency version (default: VERSION)" echo -e "${COLOR_YELLOW}--web-version=${COLOR_RESET} set web dependency version (default: VERSION)"
echo -e "${COLOR_YELLOW}--web-repo=${COLOR_RESET} set web repository (default: $(repoOwner)/synctv-web)" echo -e "${COLOR_YELLOW}--web-repo=${COLOR_RESET} set web repository (default: <owner>/synctv-web)"
echo -e "${COLOR_YELLOW}--skip-init-web${COLOR_RESET}" echo -e "${COLOR_YELLOW}--skip-init-web${COLOR_RESET}"
} }
function printDepEnvHelp() { function printDepEnvHelp() {
echo -e "${COLOR_LIGHT_GREEN}VERSION${COLOR_RESET} (default: dev)" echo -e "${COLOR_LIGHT_GREEN}VERSION${COLOR_RESET} (default: dev)"
echo -e "${COLOR_LIGHT_GREEN}WEB_VERSION${COLOR_RESET} set web dependency version (default: VERSION)" echo -e "${COLOR_LIGHT_GREEN}WEB_VERSION${COLOR_RESET} set web dependency version (default: VERSION)"
echo -e "${COLOR_LIGHT_GREEN}WEB_REPO${COLOR_RESET} set web repository (default: $(repoOwner)/synctv-web)" echo -e "${COLOR_LIGHT_GREEN}WEB_REPO${COLOR_RESET} set web repository (default: <owner>/synctv-web)"
echo -e "${COLOR_LIGHT_GREEN}SKIP_INIT_WEB${COLOR_RESET}" echo -e "${COLOR_LIGHT_GREEN}SKIP_INIT_WEB${COLOR_RESET}"
} }
@ -45,33 +45,32 @@ function initDepPlatforms() {
fi fi
} }
function repoOwner() {
git config user.name 2>/dev/null || echo "synctv-org"
}
function initDep() { function initDep() {
setDefault "version" "dev" setDefault "VERSION" "dev"
version="$(echo "$version" | sed 's/ //g' | sed 's/"//g' | sed 's/\n//g')" VERSION="$(echo "$VERSION" | sed 's/ //g' | sed 's/"//g' | sed 's/\n//g')"
if [[ "${version}" != "dev" ]] && [[ ! "${version}" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-beta.*|-rc.*|-alpha.*)?$ ]]; then if [[ "${VERSION}" != "dev" ]] && [[ ! "${VERSION}" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-beta.*|-rc.*|-alpha.*)?$ ]]; then
echo "version format error: ${version}" echo "version format error: ${VERSION}"
return 1 return 1
fi fi
setDefault "web_version" "${version}" setDefault "WEB_VERSION" "${VERSION}"
setDefault "web_repo" "$(repoOwner)/synctv-web" # 使用 git 命令获取仓库所有者,如果失败则使用默认值 "synctv-org"
setDefault "skip_init_web" "" local repo_owner
repo_owner=$(git config user.name 2>/dev/null || echo "synctv-org")
setDefault "WEB_REPO" "${repo_owner}/synctv-web"
setDefault "SKIP_INIT_WEB" ""
echo -e "${COLOR_BLUE}version:${COLOR_RESET} ${COLOR_CYAN}${version}${COLOR_RESET}" echo -e "${COLOR_BLUE}version:${COLOR_RESET} ${COLOR_CYAN}${VERSION}${COLOR_RESET}"
addLDFLAGS "-X 'github.com/synctv-org/synctv/internal/version.Version=${version}'" addLDFLAGS "-X 'github.com/synctv-org/synctv/internal/version.Version=${VERSION}'"
setDefault "web_version" "${version}" setDefault "WEB_VERSION" "${VERSION}"
addLDFLAGS "-X 'github.com/synctv-org/synctv/internal/version.WebVersion=${web_version}'" addLDFLAGS "-X 'github.com/synctv-org/synctv/internal/version.WebVersion=${WEB_VERSION}'"
local git_commit local git_commit
git_commit="$(git log --pretty=format:"%h" -1)" || git_commit="unknown" git_commit="$(git log --pretty=format:"%h" -1)" || git_commit="unknown"
addLDFLAGS "-X 'github.com/synctv-org/synctv/internal/version.GitCommit=${git_commit}'" addLDFLAGS "-X 'github.com/synctv-org/synctv/internal/version.GitCommit=${git_commit}'"
if [[ -z "${skip_init_web}" ]] && [[ -n "${web_version}" ]]; then if [[ -z "${SKIP_INIT_WEB}" ]] && [[ -n "${WEB_VERSION}" ]]; then
downloadAndUnzip "https://github.com/${web_repo}/releases/download/${web_version}/dist.tar.gz" "${source_dir}/public/dist" downloadAndUnzip "https://github.com/${WEB_REPO}/releases/download/${WEB_VERSION}/dist.tar.gz" "${source_dir}/public/dist"
fi fi
addTags "jsoniter" addTags "jsoniter"

@ -1,7 +1,6 @@
#!/bin/bash #!/bin/bash
set -e set -e
# 使用更丰富的颜色定义
readonly COLOR_RED='\033[0;31m' readonly COLOR_RED='\033[0;31m'
readonly COLOR_GREEN='\033[0;32m' readonly COLOR_GREEN='\033[0;32m'
readonly COLOR_YELLOW='\033[0;33m' readonly COLOR_YELLOW='\033[0;33m'
@ -65,6 +64,7 @@ function printEnvHelp() {
function printHelp() { function printHelp() {
echo -e "${COLOR_BLUE}-h, --help${COLOR_RESET} get help" echo -e "${COLOR_BLUE}-h, --help${COLOR_RESET} get help"
echo -e "${COLOR_BLUE}-eh, --env-help${COLOR_RESET} get environment help"
echo -e "${COLOR_BLUE}--disable-cgo${COLOR_RESET} disable cgo" echo -e "${COLOR_BLUE}--disable-cgo${COLOR_RESET} disable cgo"
echo -e "${COLOR_BLUE}--source-dir=${COLOR_RESET} set source dir (default: ${DEFAULT_SOURCE_DIR})" echo -e "${COLOR_BLUE}--source-dir=${COLOR_RESET} set source dir (default: ${DEFAULT_SOURCE_DIR})"
echo -e "${COLOR_BLUE}--more-go-cmd-args=${COLOR_RESET} more go cmd args" echo -e "${COLOR_BLUE}--more-go-cmd-args=${COLOR_RESET} more go cmd args"
@ -80,18 +80,14 @@ function printHelp() {
echo -e "${COLOR_BLUE}--host-cc=${COLOR_RESET} host cc (default: ${DEFAULT_CC})" echo -e "${COLOR_BLUE}--host-cc=${COLOR_RESET} host cc (default: ${DEFAULT_CC})"
echo -e "${COLOR_BLUE}--host-cxx=${COLOR_RESET} host cxx (default: ${DEFAULT_CXX})" echo -e "${COLOR_BLUE}--host-cxx=${COLOR_RESET} host cxx (default: ${DEFAULT_CXX})"
echo -e "${COLOR_DARK_GRAY}$(getSeparator)${COLOR_RESET}"
printBuildConfigHelp
if declare -f printDepHelp >/dev/null; then if declare -f printDepHelp >/dev/null; then
echo -e "${COLOR_PURPLE}$(getSeparator)${COLOR_RESET}" echo -e "${COLOR_PURPLE}$(getSeparator)${COLOR_RESET}"
echo -e "Dep Help:" echo -e "Dep Help:"
printDepHelp printDepHelp
fi fi
echo -e "${COLOR_LIGHT_GRAY}$(getSeparator)${COLOR_RESET}" echo -e "${COLOR_DARK_GRAY}$(getSeparator)${COLOR_RESET}"
echo -e "Env Help:" printBuildConfigHelp
printEnvHelp
} }
function setDefault() { function setDefault() {
@ -116,34 +112,34 @@ function addBuildArgs() {
} }
function fixArgs() { function fixArgs() {
setDefault "source_dir" "${DEFAULT_SOURCE_DIR}" setDefault "SOURCE_DIR" "${DEFAULT_SOURCE_DIR}"
source_dir="$(cd "${source_dir}" && pwd)" source_dir="$(cd "${SOURCE_DIR}" && pwd)"
setDefault "bin_name" "$(basename "${source_dir}")" setDefault "BIN_NAME" "$(basename "${SOURCE_DIR}")"
setDefault "result_dir" "${DEFAULT_RESULT_DIR}" setDefault "RESULT_DIR" "${DEFAULT_RESULT_DIR}"
mkdir -p "${result_dir}" mkdir -p "${RESULT_DIR}"
result_dir="$(cd "${result_dir}" && pwd)" result_dir="$(cd "${RESULT_DIR}" && pwd)"
echo -e "${COLOR_BLUE}build source dir: ${COLOR_GREEN}${source_dir}${COLOR_RESET}" echo -e "${COLOR_BLUE}build source dir: ${COLOR_GREEN}${source_dir}${COLOR_RESET}"
echo -e "${COLOR_BLUE}build result dir: ${COLOR_GREEN}${result_dir}${COLOR_RESET}" echo -e "${COLOR_BLUE}build result dir: ${COLOR_GREEN}${result_dir}${COLOR_RESET}"
setDefault "cgo_cross_compiler_dir" "$DEFAULT_CGO_CROSS_COMPILER_DIR" setDefault "CGO_CROSS_COMPILER_DIR" "$DEFAULT_CGO_CROSS_COMPILER_DIR"
mkdir -p "${cgo_cross_compiler_dir}" mkdir -p "${CGO_CROSS_COMPILER_DIR}"
cgo_cross_compiler_dir="$(cd "${cgo_cross_compiler_dir}" && pwd)" cgo_cross_compiler_dir="$(cd "${CGO_CROSS_COMPILER_DIR}" && pwd)"
setDefault "platforms" "${GOHOSTPLATFORM}" setDefault "PLATFORMS" "${GOHOSTPLATFORM}"
setDefault "disable_micro" "" setDefault "DISABLE_MICRO" ""
setDefault "host_cc" "${DEFAULT_CC}" setDefault "HOST_CC" "${DEFAULT_CC}"
setDefault "host_cxx" "${DEFAULT_CXX}" setDefault "HOST_CXX" "${DEFAULT_CXX}"
setDefault "force_cc" "" setDefault "FORCE_CC" ""
setDefault "force_cxx" "" setDefault "FORCE_CXX" ""
setDefault "gh_proxy" "" setDefault "GH_PROXY" ""
setDefault "tags" "" setDefault "TAGS" ""
setDefault "ldflags" "${DEFAULT_LDFLAGS}" setDefault "LDFLAGS" "${DEFAULT_LDFLAGS}"
setDefault "build_args" "" setDefault "BUILD_ARGS" ""
setDefault "cgo_deps_version" "${DEFAULT_CGO_DEPS_VERSION}" setDefault "CGO_DEPS_VERSION" "${DEFAULT_CGO_DEPS_VERSION}"
} }
function isCGOEnabled() { function isCGOEnabled() {
[[ "${cgo_enabled}" == "1" ]] [[ "${CGO_ENABLED}" == "1" ]]
} }
function downloadAndUnzip() { function downloadAndUnzip() {
@ -261,7 +257,7 @@ function removeDuplicatePlatforms() {
} }
function initPlatforms() { function initPlatforms() {
setDefault "cgo_enabled" "${DEFAULT_CGO_ENABLED}" setDefault "CGO_ENABLED" "${DEFAULT_CGO_ENABLED}"
unset -v CURRENT_ALLOWED_PLATFORM unset -v CURRENT_ALLOWED_PLATFORM
@ -342,11 +338,11 @@ function initCGODeps() {
local goarch="$2" local goarch="$2"
local micro="$3" local micro="$3"
if [[ -n "${force_cc}" ]] && [[ -n "${force_cxx}" ]]; then if [[ -n "${FORCE_CC}" ]] && [[ -n "${FORCE_CXX}" ]]; then
cgo_deps["CC"]="${force_cc}" cgo_deps["CC"]="${FORCE_CC}"
cgo_deps["CXX"]="${force_cxx}" cgo_deps["CXX"]="${FORCE_CXX}"
return return
elif [[ -n "${force_cc}" ]] || [[ -n "${force_cxx}" ]]; then elif [[ -n "${FORCE_CC}" ]] || [[ -n "${FORCE_CXX}" ]]; then
echo -e "${COLOR_RED}FORCE_CC and FORCE_CXX must be set at the same time${COLOR_RESET}" echo -e "${COLOR_RED}FORCE_CC and FORCE_CXX must be set at the same time${COLOR_RESET}"
return 1 return 1
fi fi
@ -400,8 +396,8 @@ function initCGODeps() {
} }
function initHostCGODeps() { function initHostCGODeps() {
cgo_deps["CC"]="${host_cc}" cgo_deps["CC"]="${HOST_CC}"
cgo_deps["CXX"]="${host_cxx}" cgo_deps["CXX"]="${HOST_CXX}"
} }
function initDefaultCGODeps() { function initDefaultCGODeps() {
@ -411,7 +407,6 @@ function initDefaultCGODeps() {
local unamespacer="${GOHOSTOS}-${GOHOSTARCH}" local unamespacer="${GOHOSTOS}-${GOHOSTARCH}"
[[ "${GOHOSTARCH}" == "arm" ]] && unamespacer="${GOHOSTOS}-arm32v7" [[ "${GOHOSTARCH}" == "arm" ]] && unamespacer="${GOHOSTOS}-arm32v7"
# 根据不同的目标平台和架构初始化 CGO 依赖项
case "${goos}" in case "${goos}" in
"linux") "linux")
case "${micro}" in case "${micro}" in
@ -523,7 +518,7 @@ function initLinuxCGO() {
eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-gcc\"" eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-gcc\""
eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-g++\"" eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-g++\""
else else
downloadAndUnzip "${GH_PROXY}https://github.com/zijiren233/musl-cross-make/releases/download/${cgo_deps_version}/${cross_compiler_name}-${unamespacer}.tgz" \ downloadAndUnzip "${GH_PROXY}https://github.com/zijiren233/musl-cross-make/releases/download/${CGO_DEPS_VERSION}/${cross_compiler_name}-${unamespacer}.tgz" \
"${cgo_cross_compiler_dir}/${cross_compiler_name}" "${cgo_cross_compiler_dir}/${cross_compiler_name}"
eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-gcc\"" eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-gcc\""
eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-g++\"" eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-linux-musl${abi}${micro}-g++\""
@ -553,7 +548,7 @@ function initWindowsCGO() {
eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-gcc\"" eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-gcc\""
eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-g++\"" eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-g++\""
else else
downloadAndUnzip "${GH_PROXY}https://github.com/zijiren233/musl-cross-make/releases/download/${cgo_deps_version}/${cross_compiler_name}-${unamespacer}.tgz" \ downloadAndUnzip "${GH_PROXY}https://github.com/zijiren233/musl-cross-make/releases/download/${CGO_DEPS_VERSION}/${cross_compiler_name}-${unamespacer}.tgz" \
"${cgo_cross_compiler_dir}/${cross_compiler_name}" "${cgo_cross_compiler_dir}/${cross_compiler_name}"
eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-gcc\"" eval "${cc_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-gcc\""
eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-g++\"" eval "${cxx_var}=\"${cgo_cross_compiler_dir}/${cross_compiler_name}/bin/${arch_prefix}-w64-mingw32-g++\""
@ -592,7 +587,6 @@ function getSeparator() {
echo $separator echo $separator
} }
# 构建目标平台
function buildTarget() { function buildTarget() {
local platform="$1" local platform="$1"
local target_name="$2" local target_name="$2"
@ -605,7 +599,7 @@ function buildTarget() {
supportPIE "${platform}" && build_mode="-buildmode=pie" supportPIE "${platform}" && build_mode="-buildmode=pie"
local build_env=( local build_env=(
"CGO_ENABLED=${cgo_enabled}" "CGO_ENABLED=${CGO_ENABLED}"
"GOOS=${goos}" "GOOS=${goos}"
"GOARCH=${goarch}" "GOARCH=${goarch}"
) )
@ -614,7 +608,7 @@ function buildTarget() {
buildTargetWithMicro "" "${build_env[@]}" buildTargetWithMicro "" "${build_env[@]}"
if [ -n "${disable_micro}" ]; then if [ -n "${DISABLE_MICRO}" ]; then
return return
fi fi
@ -664,7 +658,6 @@ function buildTarget() {
esac esac
} }
# 构建特定微架构的目标平台
function buildTargetWithMicro() { function buildTargetWithMicro() {
local micro="$1" local micro="$1"
local build_env=("${@:2}") local build_env=("${@:2}")
@ -719,7 +712,7 @@ function buildTargetWithMicro() {
echo -e "${COLOR_PURPLE}building ${goos}/${goarch}${micro:+/${micro}}${COLOR_RESET}" echo -e "${COLOR_PURPLE}building ${goos}/${goarch}${micro:+/${micro}}${COLOR_RESET}"
echo "${build_env[@]}" echo "${build_env[@]}"
env "${build_env[@]}" go build -tags "${tags}" -ldflags "${ldflags}" -trimpath ${build_args} ${build_mode} -o "${target_file}" "${source_dir}" env "${build_env[@]}" go build -tags "${TAGS}" -ldflags "${LDFLAGS}" -trimpath ${BUILD_ARGS} ${build_mode} -o "${target_file}" "${source_dir}"
echo -e "${COLOR_LIGHT_GREEN}build ${goos}/${goarch}${micro:+ ${micro}} success${COLOR_RESET}" echo -e "${COLOR_LIGHT_GREEN}build ${goos}/${goarch}${micro:+ ${micro}} success${COLOR_RESET}"
} }
@ -754,7 +747,7 @@ function autoBuild() {
fi fi
for platform in ${platforms//,/ }; do for platform in ${platforms//,/ }; do
buildTarget "${platform}" "${bin_name}" buildTarget "${platform}" "${BIN_NAME}"
done done
} }
@ -781,12 +774,16 @@ for i in "$@"; do
printHelp printHelp
exit 0 exit 0
;; ;;
-eh | --env-help)
printEnvHelp
exit 0
;;
--disable-cgo) --disable-cgo)
cgo_enabled="0" CGO_ENABLED="0"
shift shift
;; ;;
--source-dir=*) --source-dir=*)
source_dir="${i#*=}" SOURCE_DIR="${i#*=}"
shift shift
;; ;;
--more-go-cmd-args=*) --more-go-cmd-args=*)
@ -794,7 +791,7 @@ for i in "$@"; do
shift shift
;; ;;
--disable-micro) --disable-micro)
disable_micro="true" DISABLE_MICRO="true"
shift shift
;; ;;
--ldflags=*) --ldflags=*)
@ -802,11 +799,11 @@ for i in "$@"; do
shift shift
;; ;;
--platforms=*) --platforms=*)
platforms="${i#*=}" PLATFORMS="${i#*=}"
shift shift
;; ;;
--result-dir=*) --result-dir=*)
result_dir="${i#*=}" RESULT_DIR="${i#*=}"
shift shift
;; ;;
--tags=*) --tags=*)
@ -819,23 +816,23 @@ for i in "$@"; do
exit 0 exit 0
;; ;;
--github-proxy-mirror=*) --github-proxy-mirror=*)
gh_proxy="${i#*=}" GH_PROXY="${i#*=}"
shift shift
;; ;;
--force-gcc=*) --force-gcc=*)
force_cc="${i#*=}" FORCE_CC="${i#*=}"
shift shift
;; ;;
--force-g++=*) --force-g++=*)
force_cxx="${i#*=}" FORCE_CXX="${i#*=}"
shift shift
;; ;;
--host-cc=*) --host-cc=*)
host_cc="${i#*=}" HOST_CC="${i#*=}"
shift shift
;; ;;
--host-cxx=*) --host-cxx=*)
host_cxx="${i#*=}" HOST_CXX="${i#*=}"
shift shift
;; ;;
*) *)
@ -851,4 +848,4 @@ done
fixArgs fixArgs
initPlatforms initPlatforms
autoBuild "${platforms}" autoBuild "${PLATFORMS}"

Loading…
Cancel
Save