Makefile: validate $(FORGEJO_VERSION)

If the VERSION file was missing and it wasn't a git working copy, then the
`git describe` command would fail and the error was ignored:

    $ gmake show-version-full
    fatal: not a git repository (or any of the parent directories): .git
    fatal: not a git repository (or any of the parent directories): .git
    fatal: not a git repository (or any of the parent directories): .git
    fatal: not a git repository (or any of the parent directories): .git
    +gitea-1.22.0

The net result of this was that $(FORGEJO_VERSION) ended up having invalid
value '+gitea-1.22.0' which would cause the resulting binary to crash upon
startup.

After this change:

  • $(GITEA_COMPATIBILITY) is added only if both it and $(FORGEJO_VERSION)
    are not empty,
  • a new rule called verify-version checks $(FORGEJO_VERSION) and reports an
    error when the variable is empty,
  • show-version-full, show-version-major, show-version-minor, and
    show-version-api have that target as prerequisite, while anything that
    includes $(LDFLAGS) have it as order-only prerequisite.

The latter allows commands like `make clean` to execute regardless if the
correct version string was detected.

Ref: forgejo/forgejo#7358
This commit is contained in:
Matt Latusek 2025-04-02 22:08:26 +00:00
parent 2fe193b477
commit aa290758b0

View file

@ -92,19 +92,31 @@ else
FORGEJO_VERSION_API ?= $(GITEA_VERSION)+${GITEA_COMPATIBILITY}
else
# drop the "g" prefix prepended by git describe to the commit hash
FORGEJO_VERSION ?= $(shell git describe --exclude '*-test' --tags --always | sed 's/^v//' | sed 's/\-g/-/')+${GITEA_COMPATIBILITY}
FORGEJO_VERSION ?= $(shell git describe --exclude '*-test' --tags --always 2>/dev/null | sed 's/^v//' | sed 's/\-g/-/')
ifneq ($(FORGEJO_VERSION),)
ifneq ($(GITEA_COMPATIBILITY),)
FORGEJO_VERSION := $(FORGEJO_VERSION)+$(GITEA_COMPATIBILITY)
endif
endif
endif
endif
FORGEJO_VERSION_MAJOR=$(shell echo $(FORGEJO_VERSION) | sed -e 's/\..*//')
FORGEJO_VERSION_MINOR=$(shell echo $(FORGEJO_VERSION) | sed -E -e 's/^([0-9]+\.[0-9]+).*/\1/')
show-version-full:
.PHONY: verify-version
verify-version:
ifeq ($(FORGEJO_VERSION),)
@echo "Error: Could not determine FORGEJO_VERSION; version file $(STORED_VERSION_FILE) not present and no suitable git tag found"
@false
endif
show-version-full: verify-version
@echo ${FORGEJO_VERSION}
show-version-major:
show-version-major: verify-version
@echo ${FORGEJO_VERSION_MAJOR}
show-version-minor:
show-version-minor: verify-version
@echo ${FORGEJO_VERSION_MINOR}
RELEASE_VERSION ?= ${FORGEJO_VERSION}
@ -112,7 +124,7 @@ VERSION ?= ${RELEASE_VERSION}
FORGEJO_VERSION_API ?= ${FORGEJO_VERSION}
show-version-api:
show-version-api: verify-version
@echo ${FORGEJO_VERSION_API}
# Strip binaries by default to reduce size, allow overriding for debugging
@ -816,7 +828,7 @@ check: test
###
.PHONY: install $(TAGS_PREREQ)
install: $(wildcard *.go)
install: $(wildcard *.go) | verify-version
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) install -v -tags '$(TAGS)' -ldflags '$(LDFLAGS)'
.PHONY: build
@ -844,13 +856,13 @@ generate-go: $(TAGS_PREREQ)
merge-locales:
@echo "NOT NEEDED: THIS IS A NOOP AS OF Forgejo 7.0 BUT KEPT FOR BACKWARD COMPATIBILITY"
$(EXECUTABLE): $(GO_SOURCES) $(TAGS_PREREQ)
$(EXECUTABLE): $(GO_SOURCES) $(TAGS_PREREQ) | verify-version
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) build $(GOFLAGS) $(EXTRA_GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o $@
forgejo: $(EXECUTABLE)
ln -f $(EXECUTABLE) forgejo
static-executable: $(GO_SOURCES) $(TAGS_PREREQ)
static-executable: $(GO_SOURCES) $(TAGS_PREREQ) | verify-version
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) build $(GOFLAGS) $(EXTRA_GOFLAGS) -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -o $(EXECUTABLE)
.PHONY: release
@ -863,18 +875,18 @@ $(DIST_DIRS):
mkdir -p $(DIST_DIRS)
.PHONY: release-linux
release-linux: | $(DIST_DIRS)
release-linux: | $(DIST_DIRS) verify-version
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets '$(LINUX_ARCHS)' -out forgejo-$(VERSION) .
ifeq ($(CI),true)
cp /build/* $(DIST)/binaries
endif
.PHONY: release-darwin
release-darwin: | $(DIST_DIRS)
release-darwin: | $(DIST_DIRS) verify-version
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin-10.12/amd64,darwin-10.12/arm64' -out gitea-$(VERSION) .
.PHONY: release-freebsd
release-freebsd: | $(DIST_DIRS)
release-freebsd: | $(DIST_DIRS) verify-version
CGO_CFLAGS="$(CGO_CFLAGS)" $(GO) run $(XGO_PACKAGE) -go $(XGO_VERSION) -dest $(DIST)/binaries -tags 'netgo osusergo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'freebsd/amd64' -out gitea-$(VERSION) .
.PHONY: release-copy