Skip to content
Snippets Groups Projects
Commit 50108c0c authored by jkito's avatar jkito :skull:
Browse files

chore: simplify setting of version for the app

this removes the go generate way of generating the
pkg/config/version.go file and uses build time  ld
flags to set the version when building from git or
uses .gitattributes to set it during git archive
parent d52c5286
Branches
Tags
1 merge request!200chore: simplify setting of version for the app
/pkg/config/version/version.go export-subst
......@@ -7,7 +7,6 @@ build_test:
image: registry.0xacab.org/jkito/bitmask-vpn:latest
stage: build
script:
- make generate
- make test
- patch -p1 < docker/*.patch
# TODO missing xcb package in the docker image. investigate what to install to run minimal tests.
......
......@@ -112,7 +112,7 @@ PKGFILES = $(shell find pkg -type f -name '*.go')
endif
lib/%.a: $(PKGFILES)
@XBUILD=no CC=${CC} VENDOR_PATH=${VENDOR_PATH} CXX=${CXX} MAKE=${MAKE} AR=${AR} LD=${LD} ./gui/build.sh --just-golib
@XBUILD=no CC=${CC} VENDOR_PATH=${VENDOR_PATH} CXX=${CXX} MAKE=${MAKE} AR=${AR} LD=${LD} VERSION=${VERSION} ./gui/build.sh --just-golib
# FIXME move platform detection above! no place to uname here, just use $PLATFORM
#
......@@ -350,15 +350,11 @@ vendor: gen_providers_json prepare_templates gen_pkg_snap gen_pkg_deb
gen_providers_json:
@VENDOR_PATH=${VENDOR_PATH} branding/scripts/gen-providers-json gui/providers/providers.json
prepare_templates: generate tgz
prepare_templates: tgz
@mkdir -p build/${PROVIDER}/bin/ deploy
@cp ${TEMPLATES}/makefile/Makefile build/${PROVIDER}/Makefile
@VERSION=${VERSION} VENDOR_PATH=${VENDOR_PATH} ${SCRIPTS}/generate-vendor-make build/${PROVIDER}/vendor.mk
generate:
@go generate gui/backend.go
@go generate pkg/config/version/genver/gen.go
TGZ_NAME = bitmask-vpn_${VERSION}-src
TGZ_PATH = ./build/${TGZ_NAME}
tgz:
......
......@@ -9,6 +9,7 @@ XBUILD="${XBUILD:-no}"
LRELEASE="${LRELEASE:-lrelease}"
VENDOR_PATH="${VENDOR_PATH:-providers/riseup}"
APPNAME="${APPNAME:-BitmaskVPN}"
LDFLAGS_VER="-X 0xacab.org/leap/bitmask-vpn/pkg/config/version.appVersion=${VERSION}"
OSX_TARGET=12
WIN64="win64"
......@@ -64,9 +65,6 @@ function init {
}
function buildGoLib {
echo "[+] Using go in" $GO "[`go version`]"
"$GO" generate ./pkg/config/version/genver/gen.go || echo "[!] Error on go generate"
if [ "$PLATFORM" == "Darwin" ]
then
GOOS=darwin
......@@ -78,13 +76,15 @@ function buildGoLib {
if [ "$XBUILD" == "no" ]
then
echo "[+] Building Go library with standard Go compiler"
CGO_ENABLED=1 GOOS=$GOOS CC=$CC CGO_CFLAGS=$CGO_CFLAGS CGO_LDFLAGS=$CGO_LDFLAGS go build -buildmode=c-archive -ldflags="-extar=$AR -extld=$LD -extldflags=$LDFLAGS" -o $TARGET_GOLIB $SOURCE_GOLIB
CGO_ENABLED=1 GOOS=$GOOS CC=$CC CGO_CFLAGS=$CGO_CFLAGS CGO_LDFLAGS=$CGO_LDFLAGS go build -buildmode=c-archive \
-ldflags="${LDFLAGS_VER} -extar=$AR -extld=$LD -extldflags=$LDFLAGS" -o $TARGET_GOLIB $SOURCE_GOLIB
fi
if [ "$XBUILD" == "$WIN64" ]
then
echo "[+] Building Go library with mxe"
echo "[+] Using cc:" $CC
CC=$CC CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -buildmode=c-archive -ldflags="-extar=$AR -extld=$LD -extldflags=$LDFLAGS" -o $TARGET_GOLIB $SOURCE_GOLIB
CC=$CC CGO_ENABLED=1 GOOS=windows GOARCH=amd64 go build -buildmode=c-archive -ldflags="${LDFLAGS_VER} \
-extar=$AR -extld=$LD -extldflags=$LDFLAGS" -o $TARGET_GOLIB $SOURCE_GOLIB
fi
}
......
......@@ -287,5 +287,5 @@ func EnableWebAPI(port string) {
}
func GetVersion() *C.char {
return C.CString(version.VERSION)
return C.CString(version.Version())
}
......@@ -29,7 +29,7 @@ func initializeContext(opts *InitOpts) {
DonateURL: opts.ProviderOptions.DonateURL,
AskForDonations: opts.ProviderOptions.AskForDonations,
DonateDialog: false,
Version: version.VERSION,
Version: version.Version(),
Status: st,
IsReady: false,
}
......
version.go
Copyright (c) 2015 Vincent Batts, Raleigh, NC, USA
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
......@@ -63,12 +63,12 @@ func CanUpgrade() bool {
Msg("Could not parse version string")
return false
}
canUpgrade := versionOrdinal(r) > versionOrdinal(VERSION)
canUpgrade := versionOrdinal(r) > versionOrdinal(Version())
log.Debug().
Str("version", r).
Msg("Remote version")
log.Debug().
Str("version", VERSION).
Str("version", Version()).
Msg("Installed version")
log.Info().
Bool("updateAvailable", canUpgrade).
......
package main
//go:generate go run . -output=../version.go
package main
import (
"flag"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
)
var (
flPackageName = flag.String("package", "version", "name for the generated golang package")
flVariableName = flag.String("variable", "VERSION", "variable name in the generated golang package")
flOutputFile = flag.String("output", "", "output filename (default stdout)")
)
func main() {
flag.Parse()
dir := "."
if flag.NArg() > 0 {
dir = flag.Args()[0]
}
dir, err := filepath.Abs(dir)
if err != nil {
log.Fatal(err)
}
var output io.Writer
if len(*flOutputFile) > 0 {
fh, err := os.Create(*flOutputFile)
if err != nil {
log.Fatal(err)
}
defer fh.Close()
output = fh
} else {
output = os.Stdout
}
vers, err := GitDescribe(dir)
if err != nil {
vers = ReadVersionFile(dir)
}
vp := VersionPackage{
Name: *flPackageName,
Path: dir,
Date: time.Now(),
Variable: *flVariableName,
Version: vers,
}
packageTemplate.Execute(output, vp)
}
// VersionPackage is the needed information to template a version package
type VersionPackage struct {
Name string
Path string
Date time.Time
Variable string
Version string
}
var packageTemplate = template.Must(template.New("default").Parse(packageLayout))
var packageLayout = `package {{.Name}}
// AUTO-GENERATED. DO NOT EDIT
// {{.Date}}
// {{.Variable}} is generated by git-describe or static version file from gen.go
var {{.Variable}} = "{{.Version}}"
`
// GitDescribe calls `git describe` in the provided path
func GitDescribe(path string) (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
// TODO check if this is a directory
if err := os.Chdir(path); err != nil {
return "", err
}
defer os.Chdir(cwd)
buf, err := exec.Command("git", "describe").CombinedOutput()
if err != nil {
return "", err
}
return strings.TrimSpace(string(buf)), nil
}
func ReadVersionFile(path string) string {
versionFile := filepath.Join(path, "../../../version")
content, err := os.ReadFile(versionFile)
if err != nil {
return "unknown"
}
return strings.Trim(string(content), "\n")
}
package version
import (
"strings"
)
var (
// set during build with '-X 0xacab.org/leap/bitmask-vpn/pkg/config/version.appVersion'
appVersion = "unknown"
// set when `git archive` is used, the "$Fromat:%(describe)" will be replaced
// by the o/p of `git describe` https://git-scm.com/docs/gitattributes#_export_subst
gitArchiveVersion = "$Format:%(describe)$"
)
func Version() string {
switch {
case !strings.HasPrefix(gitArchiveVersion, "$Format:"):
return gitArchiveVersion
case appVersion != "":
return appVersion
default:
// should not reach here
return ""
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment