This document describes how to build Harpoon (hpn) from source code on different platforms.
- Prerequisites
- Quick Start
- Build Scripts
- Build Modes
- Version Information
- Cross-Compilation
- Build Flags
- CI/CD Build Process
- Troubleshooting
- Best Practices
- Go: Version 1.21 or higher
- Git: For version information extraction
- Bash: For running build scripts (Unix-like systems)
# Check Go version
go version
# Output should show: go version go1.21.x or higher
# Check Git
git --version
# Check Bash (macOS/Linux)
bash --versionBefore building, ensure all dependencies are downloaded:
go mod downloadThe simplest way to build hpn for your current platform:
./build.sh currentThis will create a binary named hpn in the dist/ directory.
To build binaries for all supported platforms:
./build.sh allThis will create multiple binaries in the dist/ directory:
dist/hpn-linux-amd64- Linux x86_64dist/hpn-linux-arm64- Linux ARM64dist/hpn-darwin-amd64- macOS Inteldist/hpn-darwin-arm64- macOS Apple Silicondist/hpn-windows-amd64.exe- Windows x86_64
The project provides a build.sh script and a Makefile wrapper that automate the build process.
harpoon/
├── build.sh # Main build script (in root directory)
├── Makefile # Makefile wrapper for build.sh
├── dist/ # Build output directory (created automatically)
└── cmd/hpn/ # Source code
The Makefile provides a convenient interface to the build script:
make build # Build for current platform
make build-all # Build for all platforms
make clean # Clean build artifacts
make test # Run tests
make install # Install to /usr/local/bin (requires sudo)
make help # Show available targetsYou can also use the build script directly:
./build.sh [mode]Available modes:
| Mode | Description |
|---|---|
current (default) |
Build for current platform only |
all |
Build for all supported platforms |
clean |
Remove all build artifacts |
Note: All binaries are output to the dist/ directory.
Builds a binary optimized for your current development machine.
Command:
./build.sh currentProcess:
- Extracts version information from Git
- Sets build flags with version metadata
- Compiles for current OS/architecture
- Outputs:
dist/hpnbinary
Example Output:
Building for current platform...
Version: v1.1, Commit: 6c1201a
✅ Built hpn
Use Cases:
- Local development and testing
- Quick iteration during development
- Testing new features
Builds binaries for all supported platforms using cross-compilation.
Command:
./build.sh allProcess:
- Builds Linux (amd64) - Static linking with
CGO_ENABLED=0 - Builds Linux (arm64) - Static linking with
CGO_ENABLED=0 - Builds macOS (amd64) - Intel Mac
- Builds macOS (arm64) - Apple Silicon Mac
- Builds Windows (amd64)
Example Output:
Building for all platforms...
Version: v1.1, Commit: 6c1201a
Building Linux (amd64) with CGO_ENABLED=0...
Building Linux (arm64) with CGO_ENABLED=0...
Building macOS (amd64)...
Building macOS (arm64)...
Building Windows (amd64)...
✅ Built all platforms
Use Cases:
- Preparing release binaries
- Testing cross-platform compatibility
- Distributing to multiple platforms
Removes all generated binary files.
Command:
./build.sh cleanOutput:
✅ Cleaned
The build script automatically injects version information into the binary.
- Git Tag: If a tag exists (e.g.,
v1.1), it's used as the version - Fallback: If no tag exists, version is set to
dev
The following information is embedded in the binary:
| Field | Source | Example |
|---|---|---|
Version |
Git tag or "dev" | v1.1 or dev |
GitCommit |
Git commit short hash | 6c1201a |
BuildDate |
UTC timestamp | 2024-12-19T10:30:00Z |
# After building
./hpn --version
# Or use the version subcommand
./hpn versionGo natively supports cross-compilation, allowing you to build for different platforms from a single machine.
Linux binaries are built with special flags to ensure compatibility with older systems:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -tags netgo,osusergo -ldflags "${LDFLAGS}" -o dist/hpn-linux-amd64 ./cmd/hpnKey Features:
CGO_ENABLED=0: Disables CGO for static linking-tags netgo,osusergo: Forces pure Go implementations- Result: Statically linked binary compatible with RHEL 8.x+
GOOS=darwin GOARCH=arm64 go build -ldflags "${LDFLAGS}" -o dist/hpn-darwin-arm64 ./cmd/hpnGOOS=darwin GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o dist/hpn-darwin-amd64 ./cmd/hpnGOOS=windows GOARCH=amd64 go build -ldflags "${LDFLAGS}" -o dist/hpn-windows-amd64.exe ./cmd/hpnThe build script uses the following linker flags:
LDFLAGS="-X github.com/harpoon/hpn/internal/version.Version=${VERSION}"
LDFLAGS="${LDFLAGS} -X github.com/harpoon/hpn/internal/version.GitCommit=${COMMIT}"
LDFLAGS="${LDFLAGS} -X github.com/harpoon/hpn/internal/version.BuildDate=${BUILD_DATE}"In Release Builds:
LDFLAGS="-s -w" # Strip symbols and debug infoFor Linux builds:
netgo: Use pure Go DNS resolver (avoids CGO)osusergo: Use pure Go user/group lookup (avoids CGO)
Usage:
go build -tags netgo,osusergo ...| Variable | Value | Purpose |
|---|---|---|
CGO_ENABLED |
0 |
Disable CGO for static linking (Linux only) |
GOOS |
linux, darwin, windows |
Target operating system |
GOARCH |
amd64, arm64 |
Target architecture |
The project uses GitHub Actions for automated builds and releases.
Trigger: Push a tag matching v* pattern
Location: .github/workflows/release.yml
Process:
- Setup: Checkout code and setup Go 1.21
- Test: Run
go test -v ./... - Build: Build binaries for all platforms
- Verify: Check Linux binaries are statically linked
- Checksums: Generate SHA256 checksums
- Release: Create GitHub Release with binaries
The CI process includes verification steps:
# Check if binary is statically linked
file dist/hpn-linux-amd64
# Expected: "statically linked"
# Verify no dynamic library dependencies
ldd dist/hpn-linux-amd64
# Expected: "not a dynamic executable"
# Test binary functionality
./dist/hpn-linux-amd64 --versionError:
go: requires go >= 1.21
Solution:
# Update Go to version 1.21 or higher
brew upgrade go # macOS with Homebrew
# Or download from https://golang.org/dl/Error:
bash: ./build.sh: Permission denied
Solution:
chmod +x build.shError:
git: command not found
Solution:
- Install Git from https://git-scm.com/
- Or use Homebrew:
brew install git
Error:
#cgo CFLAGS: -g -O2
Solution:
This is normal. The build script sets CGO_ENABLED=0 for Linux builds. If you see this error, ensure the build script is using the correct environment variables.
Enable verbose output:
go build -v ./cmd/hpnCheck build flags:
go build -x ./cmd/hpn # Shows all commands executedVerify dependencies:
go mod verify-
Local Development:
# Build for current platform ./build.sh current # Test the binary ./hpn --version
-
Before Committing:
# Run tests go test ./... # Build to ensure it compiles ./build.sh current
-
Before Release:
# Build all platforms ./build.sh all # Verify binaries ls -lh dist/hpn-* # Test on target platform if possible
For Smaller Binaries:
- Use
-ldflags "-s -w"to strip symbols and debug info - Already included in release builds
For Faster Builds:
- Use
-ldflags "-s"(strip symbols only) - Skip debug info stripping during development
Tagging for Release:
# Create a version tag
git tag v1.2.0
# Push the tag (triggers release workflow)
git push origin v1.2.0Semantic Versioning:
v1.0.0- Major version (breaking changes)v1.1.0- Minor version (new features)v1.0.1- Patch version (bug fixes)
Apple Silicon (M1/M2/M3):
- Native builds are ARM64
- Can cross-compile to Intel (amd64)
- Build time: ~5-10 seconds per platform
Intel Mac:
- Native builds are AMD64
- Can cross-compile to ARM64
- Build time: ~5-10 seconds per platform
- Can build for all platforms
- Linux builds use static linking by default
- Compatible with RHEL 8.x+ systems
- Requires Git Bash or WSL for build script
- Can cross-compile to all platforms
- Native builds produce
.exefiles
Last Updated: 2024-12-19
Maintained by: Harpoon Project