Add build time variables

main
Sean Hickey 2023-01-01 15:29:48 -08:00
parent 460316aaf6
commit 06bd0cfd12
7 changed files with 65 additions and 17 deletions

View File

@ -11,6 +11,7 @@ import (
"git.wisellama.rocks/Project-Ely/project-ely/internal/entity"
"git.wisellama.rocks/Project-Ely/project-ely/internal/player"
"git.wisellama.rocks/Project-Ely/project-ely/internal/sprite"
"git.wisellama.rocks/Project-Ely/project-ely/internal/version"
"git.wisellama.rocks/Wisellama/gosimpleconf"
rl "github.com/gen2brain/raylib-go/raylib"
)
@ -35,9 +36,15 @@ func Run(ctx context.Context, configMap gosimpleconf.ConfigMap) error {
framerate64 := gosimpleconf.Int64(configMap["game.framerate"])
framerate := int32(framerate64)
windowTitle := fmt.Sprintf("%s - %s (%s)",
configMap["game.title"],
version.Version,
version.CommitHash,
)
// Initialize the RayLib window
channels.RL.Do(func() {
rl.InitWindow(800, 600, configMap["game.title"])
rl.InitWindow(800, 600, windowTitle)
rl.SetTargetFPS(framerate)
})
defer func() {

View File

@ -0,0 +1,18 @@
package version
import "fmt"
// Thanks to the following blog post for how to use the linker flags
// to pass in build-time variables.
//
// https://belief-driven-design.com/build-time-variables-in-go-51439b26ef9/
var (
Version = "dev"
CommitHash = "n/a"
BuildTimestamp = "n/a"
)
func BuildVersion() string {
return fmt.Sprintf("%s %s %s", Version, CommitHash, BuildTimestamp)
}

7
scripts/build-unix.sh Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
d=$(cd $(dirname $0) && pwd -P)
. "${d}/common-release-build.sh"
build $(uname) $(uname -m)

View File

@ -1,5 +1,7 @@
#!/usr/bin/env sh
. common-build-windows.sh
d=$(cd $(dirname $0) && pwd -P)
. "${d}/common-release-build.sh"
build windows 386

View File

@ -1,5 +0,0 @@
#!/usr/bin/env sh
. common-release-build.sh
build windows amd64

View File

@ -0,0 +1,7 @@
#!/usr/bin/env sh
d=$(cd $(dirname $0) && pwd -P)
. "${d}/common-release-build.sh"
build windows amd64

View File

@ -5,25 +5,38 @@ build(){
OS=$1
ARCH=$2
if [ -z $NAME ]
then
NAME=output
fi
# Lowercase
OS=$(echo "$OS" | awk '{ print tolower($0) }')
ARCH=$(echo "$ARCH" | awk '{ print tolower($0) }')
if [ -z "${CC}" ]
then
export CC=gcc
fi
LDFLAGS_WIN=""
# Add .exe to Windows output
EXE=""
if [ "${OS}" == "windows" ]
if [ "${OS}" = "windows" ]
then
LDFLAGS_WIN="-H windowsgui"
EXE=".exe"
fi
OUTPUT="${NAME}-${OS}-${ARCH}${EXE}"
if [ "${ARCH}" = "x86_64" ]
then
ARCH="amd64"
fi
PACKAGE="$(grep module go.mod | awk '{print $2}')"
TAG="$(git describe --tags --abbrev=0)"
VERSION="$(echo ${TAG} | awk -F '-' '{print $NF}')"
COMMIT_HASH="$(git rev-parse --short HEAD)"
BUILD_TIMESTAMP="$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
OUTPUT="${TAG}-${OS}-${ARCH}${EXE}"
LDFLAGS="${LDFLAGS} -X '${PACKAGE}/internal/version.Version=${VERSION}'"
LDFLAGS="${LDFLAGS} -X '${PACKAGE}/internal/version.CommitHash=${COMMIT_HASH}'"
LDFLAGS="${LDFLAGS} -X '${PACKAGE}/internal/version.BuildTimestamp=${BUILD_TIMESTAMP}'"
export CGO_ENABLED=1
export CC="${CC}"
@ -31,7 +44,6 @@ build(){
export GOARCH=$ARCH
go build -x -v \
-trimpath \
-tags static \
-ldflags="-s -w ${LDFLAGS_WIN}" \
-ldflags="-s -w ${LDFLAGS}" \
-o "${OUTPUT}"
}