Skip to content

Commit

Permalink
Use goreleaser to build cog
Browse files Browse the repository at this point in the history
We use goreleaser to build cog for releases, so to minimise differences
between dev and release, we should also use it to build cog in
development.

This commit updates the build steps in the Makefile to call `goreleaser
build`.
  • Loading branch information
nickstenning committed Aug 14, 2024
1 parent 6b1ebe8 commit bc36433
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
21 changes: 17 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@ version: 2
before:
hooks:
- go mod tidy
- make wheel
dist: ./dist/go
env:
- CGO_ENABLED=0
builds:
- binary: cog
id: cog
env:
- CGO_ENABLED=0
goos:
- darwin
- linux
goarch:
- amd64
- arm64
main: ./cmd/cog/cog.go
main: ./cmd/cog
ldflags:
- "-s -w -X github.com/replicate/cog/pkg/global.Version={{.Version}} -X github.com/replicate/cog/pkg/global.Commit={{.Commit}} -X github.com/replicate/cog/pkg/global.BuildTime={{.Date}}"
- binary: base-image
id: base-image
goos:
- darwin
- linux
goarch:
- amd64
- arm64
main: ./cmd/base-image
ldflags:
- "-s -w -X github.com/replicate/cog/pkg/global.Version={{.Version}} -X github.com/replicate/cog/pkg/global.Commit={{.Commit}} -X github.com/replicate/cog/pkg/global.BuildTime={{.Date}}"
archives:
Expand All @@ -27,7 +40,7 @@ archives:
checksum:
name_template: "checksums.txt"
snapshot:
name_template: "{{ .Tag }}-next"
name_template: "{{ incpatch .Version }}-dev+g{{ .ShortCommit }}{{ if .IsGitDirty }}.d{{ .Now.Format \"20060102\" }}{{ end }}"
changelog:
sort: asc
filters:
Expand Down
42 changes: 12 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ PREFIX = /usr/local
BINDIR = $(PREFIX)/bin

INSTALL := install -m 0755
INSTALL_PROGRAM := $(INSTALL)

GO ?= go
GOOS := $(shell $(GO) env GOOS)
GOARCH := $(shell $(GO) env GOARCH)
GORELEASER := $(GO) run github.com/goreleaser/goreleaser/v2@latest

PYTHON ?= python
PYTEST := $(PYTHON) -m pytest
Expand All @@ -26,9 +24,12 @@ endif
COG_WHEEL := dist/cog-$(COG_PYTHON_VERSION)-py3-none-any.whl
endif

COG_GO_SOURCE := $(shell find cmd pkg -type f)
COG_PYTHON_SOURCE := $(shell find python/cog -type f -name '*.py')
COG_EMBEDDED_WHEEL := pkg/dockerfile/embed/$(notdir $(COG_WHEEL))

COG_BINARIES := cog base-image

default: all

.PHONY: all
Expand All @@ -45,39 +46,27 @@ $(COG_EMBEDDED_WHEEL): $(COG_WHEEL)
$(COG_WHEEL): $(COG_PYTHON_SOURCE)
$(PYTHON) -m build

cog: $(COG_EMBEDDED_WHEEL)
CGO_ENABLED=0 $(GO) build -o $@ \
-ldflags "-X github.com/replicate/cog/pkg/global.Version=$(COG_VERSION) -X github.com/replicate/cog/pkg/global.BuildTime=$(shell date +%Y-%m-%dT%H:%M:%S%z) -w" \
cmd/cog/cog.go

base-image: $(COG_EMBEDDED_WHEEL)
CGO_ENABLED=0 $(GO) build -o $@ \
-ldflags "-X github.com/replicate/cog/pkg/global.Version=$(COG_VERSION) -X github.com/replicate/cog/pkg/global.BuildTime=$(shell date +%Y-%m-%dT%H:%M:%S%z) -w" \
cmd/base-image/baseimage.go
$(COG_BINARIES): $(COG_GO_SOURCE) $(COG_EMBEDDED_WHEEL)
$(GORELEASER) build --clean --snapshot --single-target --id $@ --output $@

.PHONY: install
install: cog
$(INSTALL_PROGRAM) -d $(DESTDIR)$(BINDIR)
$(INSTALL_PROGRAM) cog $(DESTDIR)$(BINDIR)/cog

.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(BINDIR)/cog
install: $(COG_BINARIES)
$(INSTALL) -d $(DESTDIR)$(BINDIR)
$(INSTALL) $< $(DESTDIR)$(BINDIR)/$<

.PHONY: clean
clean:
$(GO) clean
rm -rf build dist pkg/dockerfile/embed
rm -f cog
rm -f $(COG_BINARIES)

.PHONY: test-go
test-go: $(COG_EMBEDDED_WHEEL) | check-fmt vet lint-go
$(GO) get gotest.tools/gotestsum
$(GO) run gotest.tools/gotestsum -- -timeout 1200s -parallel 5 ./... $(ARGS)

.PHONY: test-integration
test-integration: cog
cd test-integration/ && $(MAKE) PATH="$(PWD):$(PATH)" test
test-integration: $(COG_BINARIES)
$(MAKE) -C test-integration PATH="$(PWD):$(PATH)" test

.PHONY: test-python
test-python:
Expand All @@ -86,7 +75,6 @@ test-python:
.PHONY: test
test: test-go test-python test-integration


.PHONY: fmt
fmt:
$(GO) run golang.org/x/tools/cmd/goimports -w -d .
Expand All @@ -95,12 +83,10 @@ fmt:
generate:
$(GO) generate ./...


.PHONY: vet
vet:
$(GO) vet ./...


.PHONY: check-fmt
check-fmt:
$(GO) run golang.org/x/tools/cmd/goimports -d .
Expand All @@ -120,10 +106,6 @@ lint-python:
.PHONY: lint
lint: lint-go lint-python

.PHONY: mod-tidy
mod-tidy:
$(GO) mod tidy

.PHONY: run-docs-server
run-docs-server:
pip install mkdocs-material
Expand Down

0 comments on commit bc36433

Please sign in to comment.