Skip to content

Commit

Permalink
Merge pull request #1231 from smallstep/carl/goreleaser-make
Browse files Browse the repository at this point in the history
Allow GoReleaser Pro for `make` builds
  • Loading branch information
tashian authored Jul 23, 2024
2 parents 90703e4 + b92ea5c commit 6187feb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 44 deletions.
8 changes: 4 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ project_name: step

before:
hooks:
# You may remove this if you don't use go modules.
- go mod download
# - go generate ./...

builds:
- &COMMON
Expand Down Expand Up @@ -36,8 +34,10 @@ builds:
- windows_amd64
- windows_arm64
binary: bin/step
ldflags:
- -w -X main.Version={{.Version}} -X main.BuildTime={{.Date}}
-
<< : *COMMON
id: debug
gcflags: all=-N -l
-
# This build is for S3 binaries that follow our naming convention there.
<< : *COMMON
Expand Down
110 changes: 70 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# Run `make bootstrap` to set up your local environment.
# To build using go, use `make build`
# For a binary that's in parity with how our CI system builds,
# run `make goreleaser` to build using GoReleaser Pro.

# Variables:
# V=1 for verbose output.

# the name of the executable
BINNAME?=step

# the build output path
PREFIX?=bin

# the install path
DESTDIR?=/usr/local/bin

# GOOS_OVERRIDE="GOOS=linux GOARCH=arm GOARM=6" to change OS and arch
GOOS_OVERRIDE?=

# CGO_OVERRIDE="CGO_ENABLED=1" to enable CGO
CGO_OVERRIDE?=CGO_ENABLED=0

# which build id in .goreleaser.yml to build
GORELEASER_BUILD_ID?=default
ifdef DEBUG
GORELEASER_BUILD_ID=debug
endif

all: lint test build

ci: test build
Expand Down Expand Up @@ -32,15 +61,17 @@ $(info VERSION is $(VERSION))
$(info PUSHTYPE is $(PUSHTYPE))
endif

PKG?=github.com/smallstep/cli/cmd/step
BINNAME?=step
DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
ifdef DEBUG
LDFLAGS := -ldflags='-X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
GCFLAGS := -gcflags "all=-N -l"
else
LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
GCFLAGS :=
endif

# Set V to 1 for verbose output from the Makefile
Q=$(if $V,,@)
PREFIX?=
SRC=$(shell find . -type f -name '*.go')
GOOS_OVERRIDE ?=
CGO_OVERRIDE ?= CGO_ENABLED=0
OUTPUT_ROOT=output/

ifeq ($(OS),Windows_NT)
Expand Down Expand Up @@ -76,26 +107,28 @@ bootstra%:
# Build
#########################################

DATE := $(shell date -u '+%Y-%m-%d %H:%M UTC')
ifdef DEBUG
LDFLAGS := -ldflags='-X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
GCFLAGS := -gcflags "all=-N -l"
else
LDFLAGS := -ldflags='-w -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)"'
GCFLAGS :=
endif
build: $(PREFIX)/$(BINNAME)
@echo "Build Complete!"

download:
$Q go mod download
$(PREFIX)/$(BINNAME):
$Q mkdir -p $(PREFIX)
$Q $(GOOS_OVERRIDE) $(CGO_OVERRIDE) go build \
-v \
-o $(PREFIX)/$(BINNAME) \
$(GCFLAGS) $(LDFLAGS) \
github.com/smallstep/cli/cmd/step

build: $(PREFIX)bin/$(BINNAME)
@echo "Build Complete!"
goreleaser:
$Q mkdir -p $(PREFIX)
$Q $(GOOS_OVERRIDE) $(CGO_OVERRIDE) goreleaser build \
--id $(GORELEASER_BUILD_ID) \
--snapshot \
--single-target \
--clean \
--output $(PREFIX)/$(BINNAME)

$(PREFIX)bin/$(BINNAME): download $(call rwildcard,*.go)
$Q mkdir -p $(@D)
$Q $(GOOS_OVERRIDE) $(CGO_OVERRIDE) go build -v -o $@ $(GCFLAGS) $(LDFLAGS) $(PKG)
.PHONY: build goreleaser

.PHONY: build simple

#########################################
# Test
Expand All @@ -111,7 +144,7 @@ race:

integrate: integration

integration: bin/$(BINNAME)
integration: build
$Q $(CGO_OVERRIDE) gotestsum -- -tags=integration ./integration/...

.PHONY: integrate integration
Expand All @@ -138,14 +171,12 @@ govulncheck:
# Install
#########################################

INSTALL_PREFIX?=/usr/local/

install: $(PREFIX)bin/$(BINNAME)
$Q mkdir -p $(INSTALL_PREFIX)bin/
$Q install $(PREFIX)bin/$(BINNAME) $(DESTDIR)$(INSTALL_PREFIX)bin/$(BINNAME)
install: $(PREFIX)/$(BINNAME)
$Q mkdir -p $(DESTDIR)/
$Q install $(PREFIX)/$(BINNAME) $(DESTDIR)/$(BINNAME)

uninstall:
$Q rm -f $(DESTDIR)$(INSTALL_PREFIX)/bin/$(BINNAME)
$Q rm -f $(DESTDIR)/$(BINNAME)

.PHONY: install uninstall

Expand All @@ -154,9 +185,8 @@ uninstall:
#########################################

clean:
ifneq ($(BINNAME),"")
$Q rm -f bin/$(BINNAME)
endif
$Q rm -f $(PREFIX)/$(BINNAME)
$Q rm -rf dist

.PHONY: clean

Expand All @@ -171,28 +201,28 @@ define BUNDLE_MAKE
# $(2) -- Go Architecture (e.g. amd64, arm, arm64, etc.)
# $(3) -- Go ARM architectural family (e.g. 7, 8, etc.)
# $(4) -- Parent directory for executables generated by 'make'.
$(q) GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2) GOARM=$(3)' PREFIX=$(4) make $(4)bin/step
$Q GOOS_OVERRIDE='GOOS=$(1) GOARCH=$(2) GOARM=$(3)' PREFIX=$(4) make $(4)/$(BINNAME)
endef

binary-linux-amd64:
$(call BUNDLE_MAKE,linux,amd64,,$(BINARY_OUTPUT)linux-amd64/)
$(call BUNDLE_MAKE,linux,amd64,,$(BINARY_OUTPUT)linux-amd64)

binary-linux-arm64:
$(call BUNDLE_MAKE,linux,arm64,,$(BINARY_OUTPUT)linux-arm64/)
$(call BUNDLE_MAKE,linux,arm64,,$(BINARY_OUTPUT)linux-arm64)

binary-linux-armv7:
$(call BUNDLE_MAKE,linux,arm,7,$(BINARY_OUTPUT)linux-armv7/)
$(call BUNDLE_MAKE,linux,arm,7,$(BINARY_OUTPUT)linux-armv7)

binary-linux-mips:
$(call BUNDLE_MAKE,linux,mips,,$(BINARY_OUTPUT)linux-mips/)
$(call BUNDLE_MAKE,linux,mips,,$(BINARY_OUTPUT)linux-mips)

binary-darwin-amd64:
$(call BUNDLE_MAKE,darwin,amd64,,$(BINARY_OUTPUT)darwin-amd64/)
$(call BUNDLE_MAKE,darwin,amd64,,$(BINARY_OUTPUT)darwin-amd64)

binary-darwin-arm64:
$(call BUNDLE_MAKE,darwin,amd64,,$(BINARY_OUTPUT)darwin-arm64/)
$(call BUNDLE_MAKE,darwin,amd64,,$(BINARY_OUTPUT)darwin-arm64)

binary-windows-amd64:
$(call BUNDLE_MAKE,windows,amd64,,$(BINARY_OUTPUT)windows-amd64/)
$(call BUNDLE_MAKE,windows,amd64,,$(BINARY_OUTPUT)windows-amd64)

.PHONY: binary-linux-amd64 binary-linux-arm64 binary-linux-armv7 binary-linux-mips binary-darwin-amd64 binary-darwin-arm64 binary-windows-amd64

0 comments on commit 6187feb

Please sign in to comment.