|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 4 | +source "${SCRIPT_DIR}/utils.sh" |
| 5 | + |
| 6 | +set -eo pipefail |
| 7 | + |
| 8 | +main_srcdir="$1" |
| 9 | +[[ -n "${main_srcdir}" ]] || die "Usage: $0 <directory with main file>" |
| 10 | + |
| 11 | +x_defs=() |
| 12 | +x_def_errors=() |
| 13 | + |
| 14 | +while read -r line || [[ -n "$line" ]]; do |
| 15 | + if [[ "$line" =~ ^[[:space:]]*$ ]]; then |
| 16 | + continue |
| 17 | + elif [[ "$line" =~ ^([^[:space:]]+)[[:space:]]+(.*)[[:space:]]*$ ]]; then |
| 18 | + var="${BASH_REMATCH[1]}" |
| 19 | + def="${BASH_REMATCH[2]}" |
| 20 | + eval "stamp_${var}=$(printf '%q' "$def")" |
| 21 | + else |
| 22 | + die "Malformed variable_stamps.sh output line ${line}" |
| 23 | + fi |
| 24 | +done < <("${SCRIPT_DIR}/variable_stamps.sh") |
| 25 | + |
| 26 | +while read -r line || [[ -n "$line" ]]; do |
| 27 | + if [[ "$line" =~ ^[[:space:]]*$ ]]; then |
| 28 | + continue |
| 29 | + elif [[ "$line" =~ ^([^:]+):([[:digit:]]+):[[:space:]]*(var[[:space:]]+)?([^[:space:]]+)[[:space:]].*//XDef:([^[:space:]]+)[[:space:]]*$ ]]; then |
| 30 | + go_file="${BASH_REMATCH[1]}" |
| 31 | + go_line="${BASH_REMATCH[2]}" |
| 32 | + go_var="${BASH_REMATCH[4]}" |
| 33 | + stamp_var="${BASH_REMATCH[5]}" |
| 34 | + |
| 35 | + varname="stamp_${stamp_var}" |
| 36 | + [[ -n "${!varname}" ]] || x_def_errors+=( |
| 37 | + "Variable ${go_var} defined in ${go_file}:${go_line} references status var ${stamp_var} that is not part of the variable_stamps.sh output" |
| 38 | + ) |
| 39 | + go_package="$(cd "${SCRIPT_DIR}/.."; go list -e "./$(dirname "$go_file")")" |
| 40 | + |
| 41 | + x_defs+=(-X "\"${go_package}.${go_var}=${!varname}\"") |
| 42 | + fi |
| 43 | +done < <(git -C "${SCRIPT_DIR}/.." grep -n '//XDef:' -- '*.go') |
| 44 | +if [[ "${#x_def_errors[@]}" -gt 0 ]]; then |
| 45 | + printf >&2 "%s\n" "${x_def_errors[@]}" |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +ldflags=(-s -w "${x_defs[@]}") |
| 50 | + |
| 51 | +[[ -n "${GOOS}" ]] || die "GOOS must be set" |
| 52 | +bin_name="$(basename "$main_srcdir")" |
| 53 | +output_file="bin/${GOOS}/${bin_name}" |
| 54 | +if [[ "$GOOS" == "windows" ]]; then |
| 55 | + output_file="${output_file}.exe" |
| 56 | +fi |
| 57 | +mkdir -p "$(dirname "$output_file")" |
| 58 | +echo >&2 "Compiling Go source in ${main_srcdir} to ${output_file}" |
| 59 | +go build -ldflags="${ldflags[*]}" -o "${output_file}" "${main_srcdir}" |
0 commit comments