From 1ab9ba20b482271d27456d6d52c124991278a299 Mon Sep 17 00:00:00 2001 From: Nicolas Paul Date: Tue, 25 Apr 2023 17:39:02 +0200 Subject: Add Makefile Allows inserting build information variables at compile time --- .gitignore | 10 ++++++++++ Makefile | 18 ++++++++++++++++++ crocc.go | 16 ++++++++++++---- 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27e3c4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +# Copyright (c) 2023 Nicolas Paul All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +/.vscode/ +/.idea/ +*.o +*.out +*.exe +crocc \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..206bf7c --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +# Copyright (c) 2023 Nicolas Paul All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +BINARY := crocc + +VERSION := $(shell git describe --tags --always --dirty) +DATE := $(shell date -u '+%FT%T%z') + +LDFLAGS := -ldflags "-w -s -X main.version=$(VERSION) -X main.date=$(DATE)" + +$(BINARY): *.go + go build $(LDFLAGS) -o $(BINARY) + +clean: + rm -f $(BINARY) + +.PHONY: clean diff --git a/crocc.go b/crocc.go index 7be6806..59f1c7d 100644 --- a/crocc.go +++ b/crocc.go @@ -8,6 +8,7 @@ package main /* import "go.nc0.fr/crocc" */ import ( "flag" "log" + "runtime" ) var ( @@ -16,7 +17,7 @@ var ( sitemap = flag.Bool("sitemap", false, "generate sitemap.xml") generateHidden = flag.Bool("hidden", false, "generate hidden pages") verbose = flag.Bool("v", false, "verbose output") - version = flag.Bool("version", false, "print version and exit") + printVersion = flag.Bool("version", false, "print version and exit") ) const usage string = `crocc is a simple Markdown-based static site generator. @@ -31,7 +32,11 @@ Usage: Options:` -const Version string = "1.0.0" +// Set at compilation time +var ( + version string + date string +) func init() { flag.Usage = func() { @@ -43,8 +48,11 @@ func init() { func main() { flag.Parse() - if *version { - log.Printf("crocc v%s\n", Version) + log.SetFlags(0) + + if *printVersion { + log.Printf("crocc version %s %s/%s %s date %s", + version, runtime.GOOS, runtime.GOARCH, runtime.Compiler, date) return } } -- cgit v1.2.3