-
Notifications
You must be signed in to change notification settings - Fork 18
/
Makefile
185 lines (162 loc) · 5.38 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
.POSIX:
.SUFFIXES:
# Project variables
# Used as a prefix to binary names. Cannot contain spaces.
SHORTNAME := rhc
# Used as file and directory names. Cannot contain spaces.
LONGNAME := rhc
# Used as the tarball file name. Cannot contain spaces.
PKGNAME := rhc
VERSION := 0.2.4
# Used for service name of yggdrasil service (rhcd service). Cannot contain spaces
SERVICENAME := yggdrasil
# Used as the prefix for MQTT topic names
TOPICPREFIX := redhat/insights
# Used to force sending all HTTP traffic to a specific host.
DATAHOST := cert.cloud.redhat.com
# Used to identify the agency providing the connection broker.
PROVIDER := Red Hat
# Installation directories
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
SBINDIR ?= $(PREFIX)/sbin
LIBEXECDIR ?= $(PREFIX)/libexec
SYSCONFDIR ?= $(PREFIX)/etc
DATADIR ?= $(PREFIX)/share
DATAROOTDIR ?= $(PREFIX)/share
MANDIR ?= $(DATADIR)/man
DOCDIR ?= $(DATADIR)/doc
LOCALSTATEDIR ?= $(PREFIX)/var
DESTDIR ?=
# Dependent package directories
SYSTEMD_SYSTEM_UNIT_DIR := $(shell pkg-config --variable systemdsystemunitdir systemd)
SYSTEMD_SYSTEM_PRESET_DIR := $(shell pkg-config --variable systemdsystempresetdir systemd)
# Build flags
LDFLAGS :=
LDFLAGS += -X 'main.Version=$(VERSION)'
LDFLAGS += -X 'main.ShortName=$(SHORTNAME)'
LDFLAGS += -X 'main.LongName=$(LONGNAME)'
LDFLAGS += -X 'main.PrefixDir=$(PREFIX)'
LDFLAGS += -X 'main.BinDir=$(BINDIR)'
LDFLAGS += -X 'main.SbinDir=$(SBINDIR)'
LDFLAGS += -X 'main.LibexecDir=$(LIBEXECDIR)'
LDFLAGS += -X 'main.SysconfDir=$(SYSCONFDIR)'
LDFLAGS += -X 'main.DataDir=$(DATADIR)'
LDFLAGS += -X 'main.DatarootDir=$(DATAROOTDIR)'
LDFLAGS += -X 'main.ManDir=$(MANDIR)'
LDFLAGS += -X 'main.DocDir=$(DOCDIR)'
LDFLAGS += -X 'main.LocalstateDir=$(LOCALSTATEDIR)'
LDFLAGS += -X 'main.TopicPrefix=$(TOPICPREFIX)'
LDFLAGS += -X 'main.Provider=$(PROVIDER)'
LDFLAGS += -X 'main.ServiceName=$(SERVICENAME)'
BUILDFLAGS ?=
BUILDFLAGS += -buildmode=pie
ifeq ($(shell find . -name vendor), ./vendor)
BUILDFLAGS += -mod=vendor
endif
BIN = rhc
DATA = rhc.1.gz \
USAGE.md
GOSRC := $(shell find . -name '*.go')
GOSRC += go.mod go.sum
.PHONY: all
all: $(BIN) $(DATA)
.PHONY: bin
bin: $(BIN)
$(BIN): $(GOSRC)
go build $(BUILDFLAGS) -ldflags "$(LDFLAGS)" -o $@ .
.PHONY: data
data: $(DATA)
%.1: $(GOSRC)
go run $(BUILDFLAGS) -ldflags "$(LDFLAGS)" . --generate-man-page > $@
%.1.gz: %.1
gzip -k -f $^
USAGE.md: $(GOSRC)
go run $(BUILDFLAGS) -ldflags "$(LDFLAGS)" . --generate-markdown > $@
%: %.in Makefile
sed \
-e 's,[@]SHORTNAME[@],$(SHORTNAME),g' \
-e 's,[@]LONGNAME[@],$(LONGNAME),g' \
-e 's,[@]VERSION[@],$(VERSION),g' \
-e 's,[@]PACKAGE[@],$(PACKAGE),g' \
-e 's,[@]TOPICPREFIX[@],$(TOPICPREFIX),g' \
-e 's,[@]DATAHOST[@],$(DATAHOST),g' \
-e 's,[@]PROVIDER[@],$(PROVIDER),g' \
-e 's,[@]PREFIX[@],$(PREFIX),g' \
-e 's,[@]BINDIR[@],$(BINDIR),g' \
-e 's,[@]SBINDIR[@],$(SBINDIR),g' \
-e 's,[@]LIBEXECDIR[@],$(LIBEXECDIR),g' \
-e 's,[@]DATAROOTDIR[@],$(DATAROOTDIR),g' \
-e 's,[@]DATADIR[@],$(DATADIR),g' \
-e 's,[@]SYSCONFDIR[@],$(SYSCONFDIR),g' \
-e 's,[@]LOCALSTATEDIR[@],$(LOCALSTATEDIR),g' \
-e 's,[@]DOCDIR[@],$(DOCDIR),g' \
-e 's,[@]SERVICENAME[@],$(SERVICENAME),g' \
$< > [email protected] && mv [email protected] $@
.PHONY: install
install: $(BIN) $(DATA)
pkg-config --modversion dbus-1 || exit 1
pkg-config --modversion systemd || exit 1
install -D -m755 ./rhc $(DESTDIR)$(BINDIR)/$(SHORTNAME)
install -D -m644 ./rhc.1.gz $(DESTDIR)$(MANDIR)/man1/$(SHORTNAME).1.gz
install -D -m644 ./rhc.bash $(DESTDIR)$(DATADIR)/bash-completion/completions/$(SHORTNAME)
install -D -m644 ./rhc-canonical-facts.service $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)/rhc-canonical-facts.service
install -D -m644 ./rhc-canonical-facts.timer $(DESTDIR)$(SYSTEMD_SYSTEM_UNIT_DIR)/rhc-canonical-facts.timer
install -D -m644 ./80-rhc.preset $(DESTDIR)$(SYSTEMD_SYSTEM_PRESET_DIR)/80-rhc.preset
.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(BINDIR)/$(SHORTNAME)
rm -f $(DESTDIR)$(MANDIR)/man1/$(SHORTNAME).1.gz
rm -f $(DESTDIR)$(DATADIR)/bash-completion/completions/$(SHORTNAME)
.PHONY: dist
dist:
go mod vendor
tar --create \
--gzip \
--file /tmp/$(PKGNAME)-$(VERSION).tar.gz \
--exclude=.git \
--exclude=.vscode \
--exclude=.github \
--exclude=.gitignore \
--exclude=rhc-*.tar.gz \
--exclude=rhc \
--transform s/^\./$(PKGNAME)-$(VERSION)/ \
. && mv /tmp/$(PKGNAME)-$(VERSION).tar.gz .
rm -rf ./vendor
rhc.spec: rhc.spec.in
sed \
-e 's,[@]SHORTNAME[@],$(SHORTNAME),g' \
-e 's,[@]LONGNAME[@],$(LONGNAME),g' \
-e 's,[@]VERSION[@],$(VERSION),g' \
-e 's,[@]PKGNAME[@],$(PKGNAME),g' \
-e 's,[@]TOPICPREFIX[@],$(TOPICPREFIX),g' \
-e 's,[@]DATAHOST[@],$(DATAHOST),g' \
-e 's,[@]PROVIDER[@],$(PROVIDER),g' \
-e 's,[@]PREFIX[@],$(PREFIX),g' \
-e 's,[@]BINDIR[@],$(BINDIR),g' \
-e 's,[@]SBINDIR[@],$(SBINDIR),g' \
-e 's,[@]LIBEXECDIR[@],$(LIBEXECDIR),g' \
-e 's,[@]DATAROOTDIR[@],$(DATAROOTDIR),g' \
-e 's,[@]DATADIR[@],$(DATADIR),g' \
-e 's,[@]SYSCONFDIR[@],$(SYSCONFDIR),g' \
-e 's,[@]LOCALSTATEDIR[@],$(LOCALSTATEDIR),g' \
-e 's,[@]DOCDIR[@],$(DOCDIR),g' \
-e 's,[@]SERVICENAME[@],$(SERVICENAME),g' \
$^ > $@
.PHONY: clean
clean:
go mod tidy
rm -f $(BIN)
rm -f $(DATA)
.PHONY: tests
tests:
go test -v ./...
.PHONY: vet
vet:
go vet -v ./...