From 9d9fe4f82104793522c46e8f1b7746f0b694e573 Mon Sep 17 00:00:00 2001 From: Roy Golan Date: Sat, 26 Oct 2024 13:34:04 +0300 Subject: [PATCH] Add tarball make rpm target Build an rpm using `make rpm` Signed-off-by: Roy Golan Add spec and make target Signed-off-by: Roy Golan rpmbuild works --- Makefile | 22 ++++++++++++++++------ pq.spec | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 pq.spec diff --git a/Makefile b/Makefile index e05b50e..c952fe8 100644 --- a/Makefile +++ b/Makefile @@ -4,16 +4,21 @@ SHELL := /bin/bash # 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) +# RPM doesn't support dashes '-' in the version string +RPM_VERSION = $(shell git describe --always --tags HEAD | sed 's/-/~/g' || true) 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) +GO_MODULE_BASE := $(notdir $(GO_MODULE)) +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) 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) +INSTALL_PATH = bin/ + # 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 @@ -27,6 +32,9 @@ print-vars: $(if $(filter-out .%,$(v)), \ echo $v=$($v);))) +tarball: + git archive --format=tar.gz --prefix=$(GO_MODULE_BASE)-$(RPM_VERSION)/ -o $(GO_MODULE_BASE)-$(RPM_VERSION).tar.gz HEAD + install: go install -ldflags="$(ldflags)" . @@ -36,7 +44,9 @@ fmt: vet: go vet ./... - build: fmt vet - go build -v -o bin/ -ldflags="$(ldflags)" ./... + go build -v -o $(INSTALL_PATH) -ldflags="$(ldflags)" ./... +rpm: tarball + mv -v $(GO_MODULE_BASE)-$(RPM_VERSION).tar.gz ~/rpmbuild/SOURCES + rpmbuild -ba pq.spec --verbose diff --git a/pq.spec b/pq.spec new file mode 100644 index 0000000..19c4d27 --- /dev/null +++ b/pq.spec @@ -0,0 +1,37 @@ +%global project_version %(make print-vars | awk -F = '/RPM_VERSION/ {print $2}' | tr -d '\n') +%global debug_package %{nil} + +Name: pq +Version: %{project_version} +Release: 1%{?dist} +Summary: A tool to manage podman quadlets + +License: Apache-2.0 +URL: https://github.com/rgolangh/pq +Source0: %{name}-%{version}.tar.gz + +BuildRequires: golang +BuildRequires: make + +%description +A tool to manage podman quadlets + +%prep +%autosetup + +%build +%make_build build INSTALL_PATH=./ GIT_COMMIT=000 GIT_COMMIT_DATE=000 GIT_TREE_STATE=clean GIT_VERSION=%{version} RPM_VERSION=%{version} + +%install +#%make_install GIT_COMMIT=000 GIT_COMMIT_DATE=000 GIT_TREE_STATE=clean GIT_VERSION=%{version} INSTALL_PATH=%{name} +install -Dpm 0755 %{name} %{buildroot}%{_bindir}/%{name} + + +%files +%license LICENSE +%doc README.md +%{_bindir}/%{name} + +%changelog +* Sat Oct 26 2024 Roy Golan +-