File Makefile of Package xilinx-xdma-kmp

#
# Targets:
#   make snapshot     -> Resolve latest commit date (YYYYMMDD), create tarball, update spec Version.
#   make rpm          -> Build binary RPM (requires snapshot done).
#   make srpm         -> Build source RPM.
#   make force-snapshot -> Recreate tarball & rewrite Version even if tarball exists.
#   make clean        -> Remove generated artifacts.
#   make info         -> Show resolved upstream metadata (without modifying files).
#
# Environment (optional):
#   BRANCH        (default: master; falls back to main if master not found)
#
# Dependencies: curl, awk, sed, jq.
#
# The spec file's Version: line is edited in-place by snapshot target.
#

REGISTRY_AND_PROJECT :=
PROJECT_DIR  := $(CURDIR)

BRANCH       ?= master
SPEC         := xilinx-xdma-kmp.spec
TARBALL_PREF := xilinx-dma_ip_drivers
API_BASE     := https://api.github.com/repos/Xilinx/dma_ip_drivers

ifdef GITHUB_TOKEN
AUTH_HEADER  := -H "Authorization: Bearer $(GITHUB_TOKEN)"
endif

# Lazy singleton eval, see https://make.mad-scientist.net/deferred-simple-variable-expansion/
commit_json     = $(eval commit_json := $$(shell curl -fsSL $(AUTH_HEADER) "$(API_BASE)/branches/$(BRANCH)"))$(commit_json)
current_sha     = $(shell jq -r '.commit.sha' <<< '$(commit_json)')
derived_version = $(shell jq -r '.commit.commit.committer.date | split(".")[0] | strptime("%Y-%m-%dT%H:%M:%SZ") | strftime("%Y%m%d")' <<< '$(commit_json)')
commit_date     = $(shell jq -r '.commit.commit.committer.date' <<< '$(commit_json)')
spec_version    = $(shell awk '/^Version:/ {print $$2}' $(SPEC))
tarball         = $(TARBALL_PREF)-$(derived_version).tar.gz

.PHONY: snapshot force-snapshot rpm srpm clean info version

info:
	@echo "[info] Resolving latest upstream metadata..."
	@echo "Branch attempted: $(BRANCH)"
	@echo "Latest SHA:       $(current_sha)"
	@echo "Commit date:      $(commit_date)"
	@echo "Derived version:  $(derived_version)"
	@echo "Expected tarball: $(TARBALL_PREF)-$(derived_version).tar.gz"
	@echo "Current spec Version: $(spec_version)"

$(TARBALL_PREF)-%.tar.gz:
	# this is under the assumption that the date passed to this corresponds to the current_sha
	curl -fsSL $(AUTH_HEADER) "$(API_BASE)/tarball/$(current_sha)" -o "$@"

snapshot:
	@echo "[snapshot] Determining latest commit..."
	@echo "[snapshot] SHA: $(current_sha)  Date: $(commit_date)  Version: $(derived_version)"
	$(MAKE) $(tarball)

	# Reset release counter if we have an update
	[ "$(spec_version)" = "$(derived_version)" ] || sed -i.bak -E 's|^(Release:[[:space:]]+).*|\10|' $(SPEC)
	sed -i.bak -E \
	  -e 's|^(Version:[[:space:]]+).*|\1$(derived_version)|' \
	  -e 's|^(%define git_sha[[:space:]]+).*|\1$(shell cut -c 1-7 <<< $(current_sha))|' \
	  $(SPEC)

force-snapshot:
	@echo "[force-snapshot] Forcing new tarball and spec version reset."
	@rm -f $(TARBALL_PREF)-*.tar.gz
	@$(MAKE) snapshot

rpm:
	@echo "[rpm] Building binary RPM using current spec Version..."
	@rpmbuild -bb \
	               --define "_sourcedir $(CURDIR)" \
	               --define "_specdir $(CURDIR)" \
	               --define "_srcrpmdir $(CURDIR)" \
	               --define "_rpmdir $(CURDIR)" \
	               $(SPEC)
	@echo "[rpm] Done. Binary RPMs under $(CURDIR)/$$(uname -m)/ or noarch."

srpm:
	@echo "[srpm] Building source RPM..."
	@rpmbuild -bs \
	               --define "_sourcedir $(CURDIR)" \
	               --define "_specdir $(CURDIR)" \
	               --define "_srcrpmdir $(CURDIR)" \
	               --define "_rpmdir $(CURDIR)" \
	               $(SPEC)
	@echo "[srpm] Done. Source RPM in $(CURDIR)"

clean:
	@echo "[clean] Removing generated tarballs and RPMs..."
	@rm -f $(TARBALL_PREF)-*.tar.gz
	@rm -f *.rpm *.src.rpm
	@echo "[clean] Complete."

in-container:
	docker buildx build -f ./Dockerfile -t $(REGISTRY_AND_PROJECT)xilinx-xdma-builder .
	docker run -ti --rm $(DOCKEROPTS) \
                -v "$(PROJECT_DIR):/build" \
                $(REGISTRY_AND_PROJECT)xilinx-xdma-builder:latest \
                make rpm srpm

print-patch-list: SHELL=/bin/zsh
print-patch-list:
	@for p in *.patch ; do \
	  printf "%-15s %s\n" $$(printf "Patch%i:" "$${p/-*}") "$$p" ; \
	done
openSUSE Build Service is sponsored by