|
| 1 | +# Building vacuum for Package Managers |
| 2 | + |
| 3 | +This document provides guidance for package maintainers who need to build vacuum with custom version information. |
| 4 | + |
| 5 | +## Version Information |
| 6 | + |
| 7 | +vacuum supports two methods for embedding version information: |
| 8 | + |
| 9 | +### 1. Automatic Version Detection (Default) |
| 10 | +When built from a git repository or installed via `go install`, vacuum automatically detects version information using Go's `debug.ReadBuildInfo()`. |
| 11 | + |
| 12 | +### 2. Custom Version via ldflags (For Package Maintainers) |
| 13 | +Package maintainers can override version information using ldflags during build time. This is useful when building from source tarballs or when you need to specify your own version scheme. |
| 14 | + |
| 15 | +## Building with Custom Version Information |
| 16 | + |
| 17 | +Use the `-ldflags` option with `go build` to set custom version information: |
| 18 | + |
| 19 | +```bash |
| 20 | +# From within the vacuum source directory: |
| 21 | +go build -ldflags "-X main.version=<version> -X main.commit=<commit> -X 'main.date=<date>'" \ |
| 22 | + -o vacuum |
| 23 | + |
| 24 | +# Or specify the current directory explicitly: |
| 25 | +go build -ldflags "-X main.version=<version> -X main.commit=<commit> -X 'main.date=<date>'" \ |
| 26 | + -o vacuum \ |
| 27 | + . |
| 28 | +``` |
| 29 | + |
| 30 | +### Parameters |
| 31 | + |
| 32 | +- `main.version`: The version string (e.g., `v0.18.6`, `0.18.6-1`, `0.18.6-nixpkg`) |
| 33 | +- `main.commit`: Git commit hash (short or full) |
| 34 | +- `main.date`: Build date (any format, but RFC3339 or human-readable preferred) |
| 35 | + |
| 36 | +### Examples |
| 37 | + |
| 38 | +#### Arch Linux (AUR) PKGBUILD |
| 39 | +```bash |
| 40 | +pkgver="0.18.6" |
| 41 | +_commit="a535d6c" |
| 42 | +_date=$(date '+%a, %d %b %Y %H:%M:%S %Z') |
| 43 | + |
| 44 | +go build -ldflags "-linkmode=external \ |
| 45 | + -X main.version=$pkgver \ |
| 46 | + -X main.commit=$_commit \ |
| 47 | + -X 'main.date=$_date'" \ |
| 48 | + -o vacuum |
| 49 | +``` |
| 50 | + |
| 51 | +#### Nix Package |
| 52 | +```nix |
| 53 | +buildGoModule { |
| 54 | + pname = "vacuum"; |
| 55 | + version = "0.18.6"; |
| 56 | +
|
| 57 | + ldflags = [ |
| 58 | + "-X main.version=${version}" |
| 59 | + "-X main.commit=${src.rev or "unknown"}" |
| 60 | + "-X main.date=1970-01-01T00:00:00Z" |
| 61 | + ]; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +#### Debian Package |
| 66 | +```makefile |
| 67 | +VERSION := $(shell dpkg-parsechangelog -S Version) |
| 68 | +COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown") |
| 69 | +DATE := $(shell date -R) |
| 70 | + |
| 71 | +build: |
| 72 | + go build -ldflags "-X main.version=$(VERSION) \ |
| 73 | + -X main.commit=$(COMMIT) \ |
| 74 | + -X 'main.date=$(DATE)'" \ |
| 75 | + -o vacuum |
| 76 | +``` |
| 77 | + |
| 78 | +#### Homebrew Formula |
| 79 | +```ruby |
| 80 | +def install |
| 81 | + ldflags = %W[ |
| 82 | + -X main.version=#{version} |
| 83 | + -X main.commit=#{Utils.git_short_head} |
| 84 | + -X main.date=#{time.iso8601} |
| 85 | + ] |
| 86 | + |
| 87 | + system "go", "build", *std_go_args(ldflags: ldflags) |
| 88 | +end |
| 89 | +``` |
| 90 | + |
| 91 | +## Testing Your Build |
| 92 | + |
| 93 | +After building with custom ldflags, verify the version information: |
| 94 | + |
| 95 | +```bash |
| 96 | +# Check version |
| 97 | +./vacuum version |
| 98 | +# Expected output: your custom version |
| 99 | + |
| 100 | +# Check full version info in banner |
| 101 | +./vacuum lint --help |
| 102 | +# The banner will show: version: <your-version> | compiled: <your-date> |
| 103 | +``` |
| 104 | + |
| 105 | +## Partial ldflags |
| 106 | + |
| 107 | +You can specify only some of the ldflags: |
| 108 | + |
| 109 | +```bash |
| 110 | +# Only set version (commit will show "unknown", date will default to current time) |
| 111 | +go build -ldflags "-X main.version=v0.18.6" -o vacuum |
| 112 | + |
| 113 | +# Only set version and commit (date will default to current time) |
| 114 | +go build -ldflags "-X main.version=v0.18.6 -X main.commit=a535d6c" -o vacuum |
| 115 | +``` |
| 116 | + |
| 117 | +Note: Unspecified `version` and `commit` values will show as "unknown", while an unspecified `date` will default to the current build time. |
| 118 | + |
| 119 | +## Compatibility Notes |
| 120 | + |
| 121 | +- If ldflags are not provided, vacuum falls back to automatic detection via `debug.ReadBuildInfo()` |
| 122 | +- This dual approach ensures compatibility with both `go install` users and package managers |
| 123 | +- The ldflags approach takes precedence when provided |
| 124 | +- Date strings can be in any format, but RFC3339 format will be automatically reformatted for display |
| 125 | + |
| 126 | +## Integration with CI/CD |
| 127 | + |
| 128 | +For automated builds, you can dynamically generate version information: |
| 129 | + |
| 130 | +```bash |
| 131 | +VERSION=$(git describe --tags --always) |
| 132 | +COMMIT=$(git rev-parse --short HEAD) |
| 133 | +DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 134 | + |
| 135 | +go build -ldflags "-X main.version=${VERSION} \ |
| 136 | + -X main.commit=${COMMIT} \ |
| 137 | + -X main.date=${DATE}" \ |
| 138 | + -o vacuum |
| 139 | +``` |
| 140 | + |
| 141 | +## Need Help? |
| 142 | + |
| 143 | +If you encounter issues with version information in your package, please open an issue at: |
| 144 | +https://github.com/daveshanley/vacuum/issues |
0 commit comments