-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
175 lines (146 loc) · 7.68 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
# IRCdns
# Copyright (C) 2019-2020+ James Shubin and the project contributors
# Written by James Shubin <[email protected]> and the project contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
SHELL = /usr/bin/env bash
.PHONY: all program version channel run race bindata build build-debug clean test gofmt install uninstall client client_install client_uninstall
.SILENT: clean bindata
# a large amount of output from this `find`, can cause `make` to be much slower!
GO_FILES := $(shell find * -name '*.go' -not -path 'client/*' -not -path 'old/*' -not -path 'tmp/*')
PROGRAM := $(shell echo $(notdir $(CURDIR)) | cut -f1 -d"-")
SVERSION := $(or $(SVERSION),$(shell git describe --match '[0-9]*\.[0-9]*\.[0-9]*' --tags --dirty --always))
VERSION := $(or $(VERSION),$(shell git describe --match '[0-9]*\.[0-9]*\.[0-9]*' --tags --abbrev=0))
PKGNAME := $(shell go list .)
CHANNEL_FILE := channel.$(PROGRAM)
# doesn't work: CHANNEL := $(shell cat "channel.$(PROGRAM)")
CHANNEL = `cat "$(CHANNEL_FILE)" 2>/dev/null`
ifneq ($(GOTAGS),)
BUILD_FLAGS = -tags '$(GOTAGS)'
endif
GOOSARCHES ?= linux/amd64 linux/ppc64 linux/ppc64le linux/arm64 darwin/amd64
GOHOSTOS = $(shell go env GOHOSTOS)
GOHOSTARCH = $(shell go env GOHOSTARCH)
default: build
all: docs $(PROGRAM).static
# show the current program name
program: ## show the program name
@echo $(PROGRAM)
# show the current version
version: ## show the current version
@echo $(VERSION)
# show the current channel
channel: ## show the channel name
@echo $(CHANNEL)
run: ## run program
find . -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' | xargs go run -ldflags "-X main.X_program=$(PROGRAM) -X main.X_version=$(SVERSION) -X main.X_channel=$(CHANNEL)"
# include race flag
race:
find . -maxdepth 1 -type f -name '*.go' -not -name '*_test.go' | xargs go run -race -ldflags "-X main.X_program=$(PROGRAM) -X main.X_version=$(SVERSION) -X main.X_channel=$(CHANNEL)"
# generate go files from non-go source
bindata: ## generate go files from non-go sources
$(MAKE) --quiet -C bindata
# build a binary for current host os/arch
$(PROGRAM): build/ircdns-${GOHOSTOS}-${GOHOSTARCH} ## build a binary for current host os/arch
cp -a $< $@
$(PROGRAM).static: $(GO_FILES) $(wildcard $(CHANNEL_FILE))
@echo "Building: $(PROGRAM).static, version: $(SVERSION)..."
go generate
go build -a -installsuffix cgo -tags netgo -ldflags '-extldflags "-static" -X main.X_program=$(PROGRAM) -X main.X_version=$(SVERSION) -X main.X_channel=$(CHANNEL) -s -w' -o $(PROGRAM).static $(BUILD_FLAGS);
build: LDFLAGS=-s -w ## build a fresh binary
build: $(PROGRAM)
build-debug: LDFLAGS=
build-debug: $(PROGRAM)
# pattern rule target for (cross)building, ircdns-OS-ARCH will be expanded to the correct build
# extract os and arch from target pattern
GOOS=$(firstword $(subst -, ,$*))
GOARCH=$(lastword $(subst -, ,$*))
build/ircdns-%: $(GO_FILES) $(wildcard $(CHANNEL_FILE)) | bindata
@echo "Building: $(PROGRAM), os/arch: $*, version: $(SVERSION)..."
@# reassigning GOOS and GOARCH to make build command copy/pastable
@# go 1.10+ requires specifying the package for ldflags
@if go version | grep -qE 'go1.9'; then \
time env GOOS=${GOOS} GOARCH=${GOARCH} go build -i -ldflags "-X main.X_program=$(PROGRAM) -X main.X_version=$(SVERSION) -X main.X_channel=$(CHANNEL) ${LDFLAGS}" -o $@ $(BUILD_FLAGS); \
else \
time env GOOS=${GOOS} GOARCH=${GOARCH} go build -i -ldflags=$(PKGNAME)="-X main.X_program=$(PROGRAM) -X main.X_version=$(SVERSION) -X main.X_channel=$(CHANNEL) ${LDFLAGS}" -o $@ $(BUILD_FLAGS); \
fi
clean: ## clean things up
$(MAKE) --quiet -C bindata clean
[ ! -e $(PROGRAM) ] || rm $(PROGRAM)
rm -f *_stringer.go # generated by `go generate`
rm -f *_mock.go # generated by `go generate`
# crossbuild artifacts
rm -f build/ircdns-*
# client specific
rm -f libnss_ircdns.h
rm -f libnss_ircdns.so
gofmt:
# TODO: remove gofmt once goimports has a -s option
find . -maxdepth 9 -type f -name '*.go' -not -path './old/*' -not -path './tmp/*' -not -path './vendor/*' -not -path './gopath/*' -exec gofmt -s -w {} \;
find . -maxdepth 9 -type f -name '*.go' -not -path './old/*' -not -path './tmp/*' -not -path './vendor/*' -not -path './gopath/*' -exec goimports -w {} \;
install: $(PROGRAM) ## install the service
cp -a $(PROGRAM) /usr/local/bin/$(PROGRAM)
chmod u=rwx,go=rx /usr/local/bin/$(PROGRAM)
chown root:root /usr/local/bin/$(PROGRAM)
restorecon -rv /usr/local/bin/$(PROGRAM) # selinux!!!
cp -a misc/$(PROGRAM).service /etc/systemd/system/
if [ -e /etc/redhat-release ] && [[ `cat /etc/redhat-release` == "CentOS Linux release 7"* ]]; then \
cp -a misc/$(PROGRAM).service.centos-7 /etc/systemd/system/$(PROGRAM).service; \
fi
chmod 644 /etc/systemd/system/$(PROGRAM).service
restorecon -rv /etc/systemd/system/$(PROGRAM).service # selinux!!!
systemctl daemon-reload
systemctl enable $(PROGRAM)
@echo "Use: systemctl start $(PROGRAM).service to start $(PROGRAM)."
uninstall: ## uninstall the service
systemctl stop $(PROGRAM) || true
systemctl disable $(PROGRAM) || true
rm -f /etc/systemd/system/$(PROGRAM).service
systemctl daemon-reload
rm -f /usr/local/bin/$(PROGRAM)
client: libnss_ircdns.so ## build the client .so file
libnss_ircdns.so: client/*.go
@# TODO: the $(PKGNAME) part messes this up, so I've omitted it for now!
@# TODO: both these forms work
@#go build -o libnss_ircdns.so -buildmode=c-shared -ldflags="-extldflags '-Wl,-soname,libnss_ircdns.so.2' -X 'main.X_program=$(PROGRAM)'" client/*.go
@#go build -o libnss_ircdns.so -buildmode=c-shared -ldflags='-extldflags "-Wl,-soname,libnss_ircdns.so.2" -X "main.X_program=$(PROGRAM)"' client/*.go
go build -o libnss_ircdns.so -buildmode=c-shared -ldflags="-extldflags '-Wl,-soname,libnss_ircdns.so.2' -X 'main.X_program=$(PROGRAM)' -X 'main.X_version=$(SVERSION)' -X 'main.X_channel=$(CHANNEL)' ${LDFLAGS}" client/*.go
# This client install target is typically run as root.
# Leo Antunes (@costela) says: This is a dirty hack to avoid needing autoconf,
# and it would be nice to also get SONAME dynamically.
client_install: libnss_ircdns.so ## install the client
# On my machine, $TARGET works out to the /lib64/ dir.
$(eval TARGET := $(shell dirname $(shell ldd libnss_ircdns.so | grep libc.so | awk '{ print $$3 }')))
# DESTDIR: https://www.gnu.org/prep/standards/html_node/DESTDIR.html
install -D libnss_ircdns.so $(DESTDIR)$(TARGET)/libnss_ircdns-$(VERSION).so
# In case selinux matters here.
restorecon -Rv /lib64/libnss_ircdns-$(VERSION).so
# Makes a /lib64/libnss_ircdns.so.2 symlink to the installed file.
ldconfig -v
# XXX: automate the modification of /etc/nsswitch.conf
@echo 'Add the "ircdns" entry to /etc/nsswitch.conf'
client_uninstall: ## uninstall the client
# XXX: automate the modification of /etc/nsswitch.conf
@echo 'Remove the "ircdns" entry from /etc/nsswitch.conf'
rm -f $(DESTDIR)$(TARGET)/libnss_ircdns-$(VERSION).so
ldconfig -v # cleans up the symlinks
help: ## show this help screen
@echo 'Usage: make <OPTIONS> ... <TARGETS>'
@echo ''
@echo 'Available targets are:'
@echo ''
@grep -E '^[ a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
@echo ''
# vim: ts=8