Skip to content

Commit

Permalink
Make the makefile generic
Browse files Browse the repository at this point in the history
Extract all the project specific info from go.mod
and make the makefile generic.

Signed-off-by: Roy Golan <[email protected]>
  • Loading branch information
rgolangh committed Oct 23, 2024
1 parent 064bbf9 commit fabb5df
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
SHELL := /bin/bash

# I try to make this makefile generic enough so other go project can just use
# it as is. Any project specific info is extracted from the real projct assests,
# like go.mod or git, so when a change is needed, it is changed in one place.
GO_VERSION := $(shell awk '/^go / {print $$2}' go.mod)
GO_MODULE := $(shell awk '/^module / {print $$2}' go.mod)
GIT_VERSION := $(shell git describe --always --tags HEAD)
GIT_COMMIT := $(shell git rev-parse HEAD)
GIT_COMMIT_DATE := $(shell git log --pretty=%ct -1)
GIT_TREE_STATE := $(shell git diff --exit-code --quiet && echo clean || echo dirty)

# Print out only the variables declared in this makefile. Will be used
# by other tools like github workflows or any other build tool.
# note: the ldflags value is long with spaces and isn't a valid bash expression
ldflags := -X $(GO_MODULE)/internal/version.Version=$(GIT_VERSION)
ldflags += -X $(GO_MODULE)/internal/version.Commit=$(GIT_COMMIT)
ldflags += -X $(GO_MODULE)/internal/version.CommitDate=$(GIT_COMMIT_DATE)
ldflags += -X $(GO_MODULE)/internal/version.TreeState=$(GIT_TREE_STATE)

# Print out only the variables declared in this makefile(not any of the builtins).
# Will be used by other tools like github workflows or any other build tool.
# Note - the ldflags value is long with spaces and isn't a valid bash expression
# unless it is quoted. I did't add quotes to the 'echo' section here because it
# creates other problems when using valus in github action. So to source all
# vars in one-shot just ignore ldflags with a grep or specifically surround it
Expand All @@ -28,10 +36,6 @@ fmt:
vet:
go vet ./...

ldflags := -X github.com/rgolangh/pq/internal/version.Version=$(GIT_VERSION)
ldflags += -X github.com/rgolangh/pq/internal/version.Commit=$(GIT_COMMIT)
ldflags += -X github.com/rgolangh/pq/internal/version.CommitDate=$(GIT_COMMIT_DATE)
ldflags += -X github.com/rgolangh/pq/internal/version.TreeState=$(GIT_TREE_STATE)

build: fmt vet
go build -v -o bin/ -ldflags="$(ldflags)" ./...
Expand Down

0 comments on commit fabb5df

Please sign in to comment.