From f64f607122606a397a95c68fff7a35200a4a44a9 Mon Sep 17 00:00:00 2001 From: Nick Stenning Date: Thu, 10 Oct 2024 14:41:50 +0200 Subject: [PATCH] Improve the build process We've recently run into issues where Homebrew's build process for cog doesn't match the build process we use for our own binaries. This commit makes changes to our build process that will hopefully allow us to update Homebrew to use our Makefile to run its builds. 1. Use `python -m pip wheel` to build the wheel rather than relying on a manual `pip install -e .[dev]` and `python -m build`. 2. Allow COG_VERSION_OVERRIDE and COG_COMMIT_OVERRIDE to specify the version and commit values used by goreleaser. 3. Set the `-trimpath` flag for `go build` so we don't capture directory names from the Homebrew build process or GitHub Actions. --- .gitignore | 2 +- .goreleaser.yaml | 16 ++++++++++++++-- Makefile | 32 ++++++++++++++------------------ 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 7e85a917b..bd5163ee7 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ __pycache__ build dist *.egg-info -pkg/dockerfile/embed/*.whl +pkg/dockerfile/embed/* # Used by a vim plugin (projectionist) .projections.json .tox/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 061d7acda..a0d5fff1a 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,8 +16,14 @@ builds: - amd64 - arm64 main: ./cmd/cog + flags: + - -trimpath 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}}" + - -s + - -w + - -X github.com/replicate/cog/pkg/global.Version={{ if index .Env "COG_VERSION_OVERRIDE" }}{{ .Env.COG_VERSION_OVERRIDE }}{{ else }}{{ .Version }}{{ end }} + - -X github.com/replicate/cog/pkg/global.Commit={{ if index .Env "COG_COMMIT_OVERRIDE" }}{{ .Env.COG_COMMIT_OVERRIDE }}{{ else }}{{ .Commit }}{{ end }} + - -X github.com/replicate/cog/pkg/global.BuildTime={{ .Date }} - binary: base-image id: base-image goos: @@ -27,8 +33,14 @@ builds: - amd64 - arm64 main: ./cmd/base-image + flags: + - -trimpath 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}}" + - -s + - -w + - -X github.com/replicate/cog/pkg/global.Version={{ if index .Env "COG_VERSION_OVERRIDE" }}{{ .Env.COG_VERSION_OVERRIDE }}{{ else }}{{ .Version }}{{ end }} + - -X github.com/replicate/cog/pkg/global.Commit={{ if index .Env "COG_COMMIT_OVERRIDE" }}{{ .Env.COG_COMMIT_OVERRIDE }}{{ else }}{{ .Commit }}{{ end }} + - -X github.com/replicate/cog/pkg/global.BuildTime={{ .Date }} archives: - format: binary builds: diff --git a/Makefile b/Makefile index 757890408..ad620a4bf 100644 --- a/Makefile +++ b/Makefile @@ -14,19 +14,8 @@ GORELEASER := $(GO) run github.com/goreleaser/goreleaser/v2@v2.2.0 PYTHON ?= python TOX := $(PYTHON) -Im tox -# If cog's wheel has been prebuilt, it can be specified with the COG_WHEEL -# environment variable and we will not attempt to build it. -ifndef COG_WHEEL -COG_PYTHON_VERSION := $(shell $(PYTHON) -m setuptools_scm 2>/dev/null) -ifndef COG_PYTHON_VERSION -$(error Could not determine a version for cog! Did you `pip install -e '.[dev]'` first?) -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 @@ -36,17 +25,24 @@ default: all all: cog .PHONY: wheel -wheel: $(COG_EMBEDDED_WHEEL) +wheel: pkg/dockerfile/embed/.wheel -$(COG_EMBEDDED_WHEEL): $(COG_WHEEL) +ifdef COG_WHEEL +pkg/dockerfile/embed/.wheel: $(COG_WHEEL) @mkdir -p pkg/dockerfile/embed @rm -f pkg/dockerfile/embed/*.whl # there can only be one embedded wheel - cp $< $@ - -$(COG_WHEEL): $(COG_PYTHON_SOURCE) - $(PYTHON) -m build + @echo "Using prebuilt COG_WHEEL $<" + cp $< pkg/dockerfile/embed/ + @touch $@ +else +pkg/dockerfile/embed/.wheel: $(COG_PYTHON_SOURCE) + @mkdir -p pkg/dockerfile/embed + @rm -f pkg/dockerfile/embed/*.whl # there can only be one embedded wheel + $(PYTHON) -m pip wheel --no-deps --no-binary=:all: --wheel-dir=pkg/dockerfile/embed . + @touch $@ +endif -$(COG_BINARIES): $(COG_GO_SOURCE) $(COG_EMBEDDED_WHEEL) +$(COG_BINARIES): $(COG_GO_SOURCE) pkg/dockerfile/embed/.wheel $(GORELEASER) build --clean --snapshot --single-target --id $@ --output $@ .PHONY: install