aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2017-08-25 15:57:55 -0600
committerMatthew Holt <[email protected]>2017-08-25 15:59:36 -0600
commit74316fe01bbb78e5383bb79c8f546d8615a36a75 (patch)
treeee4468671c73baf4fb0e2c189be353433550fd69
parentef3d63e3e5566d20952479fb725042468e8335b7 (diff)
downloadcaddy-74316fe01bbb78e5383bb79c8f546d8615a36a75.tar.gz
caddy-74316fe01bbb78e5383bb79c8f546d8615a36a75.zip
Replace build.bash with build.go; limit timestamp inclusion
build.go is (should be) cross-platform compatible. Timestamps are now excluded from all builds on a clean commit, in an effort to be byte-for-byte reproducible.
-rw-r--r--README.md2
-rwxr-xr-xcaddy/build.bash56
-rw-r--r--caddy/build.go73
-rw-r--r--caddy/caddymain/run.go6
4 files changed, 79 insertions, 58 deletions
diff --git a/README.md b/README.md
index 247e3bad0..2b7a0e383 100644
--- a/README.md
+++ b/README.md
@@ -57,7 +57,7 @@ Caddy binaries have no dependencies and are available for every platform. Get Ca
customize your build in the browser
- **[Latest release](https://github.com/mholt/caddy/releases/latest)** for
pre-built, vanilla binaries
-- **go get** to build from source: `go get github.com/mholt/caddy/caddy` (requires Go 1.8 or newer)
+- **go get** to build from source: `go get github.com/mholt/caddy/caddy` (requires Go 1.8 or newer) - to build with proper version information (required when filing issues), `cd` to the `caddy` folder and use `go run build.go`.
Then make sure the `caddy` binary is in your PATH.
diff --git a/caddy/build.bash b/caddy/build.bash
deleted file mode 100755
index f55a98826..000000000
--- a/caddy/build.bash
+++ /dev/null
@@ -1,56 +0,0 @@
-#!/usr/bin/env bash
-#
-# Caddy build script. Automates proper versioning.
-#
-# Usage:
-#
-# $ ./build.bash [output_filename] [git_repo]
-#
-# Outputs compiled program in current directory.
-# Default git repo is current directory.
-# Builds always take place from current directory.
-
-set -euo pipefail
-
-: ${output_filename:="${1:-}"}
-: ${output_filename:=""}
-
-: ${git_repo:="${2:-}"}
-: ${git_repo:="."}
-
-pkg=github.com/mholt/caddy/caddy/caddymain
-ldflags=()
-
-# Timestamp of build
-name="${pkg}.buildDate"
-value=$(date -u +"%a %b %d %H:%M:%S %Z %Y")
-ldflags+=("-X" "\"${name}=${value}\"")
-
-# Current tag, if HEAD is on a tag
-name="${pkg}.gitTag"
-set +e
-value="$(git -C "${git_repo}" describe --exact-match HEAD 2>/dev/null)"
-set -e
-ldflags+=("-X" "\"${name}=${value}\"")
-
-# Nearest tag on branch
-name="${pkg}.gitNearestTag"
-value="$(git -C "${git_repo}" describe --abbrev=0 --tags HEAD)"
-ldflags+=("-X" "\"${name}=${value}\"")
-
-# Commit SHA
-name="${pkg}.gitCommit"
-value="$(git -C "${git_repo}" rev-parse --short HEAD)"
-ldflags+=("-X" "\"${name}=${value}\"")
-
-# Summary of uncommitted changes
-name="${pkg}.gitShortStat"
-value="$(git -C "${git_repo}" diff-index --shortstat HEAD)"
-ldflags+=("-X" "\"${name}=${value}\"")
-
-# List of modified files
-name="${pkg}.gitFilesModified"
-value="$(git -C "${git_repo}" diff-index --name-only HEAD)"
-ldflags+=("-X" "\"${name}=${value}\"")
-
-go build -ldflags "${ldflags[*]}" -o "${output_filename}"
diff --git a/caddy/build.go b/caddy/build.go
new file mode 100644
index 000000000..864996d7f
--- /dev/null
+++ b/caddy/build.go
@@ -0,0 +1,73 @@
+// +build dev
+
+// build.go automates proper versioning of caddy binaries.
+// Use it like: go run build.go
+// You can customize the build with the -goos, -goarch, and
+// -goarm CLI options: go run build.go -goos=windows
+//
+// To get proper version information, this program must be
+// run from the directory of this file, and the source code
+// must be a working git repository, since it needs to know
+// if the source is in a clean state.
+//
+// This program is NOT required to build Caddy from source
+// since it is go-gettable. (You can run plain `go build`
+// in this directory to get a binary.) However, issues filed
+// without version information will likely be closed.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "os/exec"
+ "path/filepath"
+
+ "github.com/caddyserver/buildworker"
+)
+
+var goos, goarch, goarm string
+
+func init() {
+ flag.StringVar(&goos, "goos", "", "GOOS for which to build")
+ flag.StringVar(&goarch, "goarch", "", "GOARCH for which to build")
+ flag.StringVar(&goarm, "goarm", "", "GOARM for which to build")
+}
+
+func main() {
+ flag.Parse()
+
+ gopath := os.Getenv("GOPATH")
+
+ pwd, err := os.Getwd()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ ldflags, err := buildworker.MakeLdFlags(filepath.Join(pwd, ".."))
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ args := []string{"build", "-ldflags", ldflags}
+ args = append(args, "-asmflags", fmt.Sprintf("-trimpath=%s", gopath))
+ args = append(args, "-gcflags", fmt.Sprintf("-trimpath=%s", gopath))
+ cmd := exec.Command("go", args...)
+ cmd.Stderr = os.Stderr
+ cmd.Stdout = os.Stdout
+ cmd.Env = os.Environ()
+ for _, env := range []string{
+ "CGO_ENABLED=0",
+ "GOOS=" + goos,
+ "GOARCH=" + goarch,
+ "GOARM=" + goarm,
+ } {
+ cmd.Env = append(cmd.Env, env)
+ }
+
+ err = cmd.Run()
+ if err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/caddy/caddymain/run.go b/caddy/caddymain/run.go
index b88997192..7bfb58768 100644
--- a/caddy/caddymain/run.go
+++ b/caddy/caddymain/run.go
@@ -186,10 +186,14 @@ func setVersion() {
// A development build is one that's not at a tag or has uncommitted changes
devBuild = gitTag == "" || gitShortStat != ""
+ if buildDate != "" {
+ buildDate = " " + buildDate
+ }
+
// Only set the appVersion if -ldflags was used
if gitNearestTag != "" || gitTag != "" {
if devBuild && gitNearestTag != "" {
- appVersion = fmt.Sprintf("%s (+%s %s)",
+ appVersion = fmt.Sprintf("%s (+%s%s)",
strings.TrimPrefix(gitNearestTag, "v"), gitCommit, buildDate)
} else if gitTag != "" {
appVersion = strings.TrimPrefix(gitTag, "v")