summaryrefslogtreecommitdiff
path: root/Makefile
blob: 8630cd80b24ab91a062f9861ab481c882dae3da2 (plain)
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
# Copyright 2015 Luke Shumaker <lukeshu@sbcglobal.net>.
#
# This is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# The GNU General Public License's references to "object code" and
# "executables" are to be interpreted to also include the output of
# any document formatting or typesetting system, including
# intermediate and printed output.
#
# This software 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this manual; if not, see
# <http://www.gnu.org/licenses/>.

MAKEFLAGS += --no-builtin-rules
prefix = /usr/local
bindir = $(prefix)/bin
libdir = $(prefix)/lib
systemddir = $(libdir)/systemd

user = nshd
group = nshd

NET ?= FORCE

CFLAGS = -std=c99 -Wall -Wextra -Werror -pedantic
CGO_CFLAGS = $(CFLAGS) -Wno-unused-parameter
CGO_ENABLED = 1
export CGO_ENABLED

cgo_variables = CGO_ENABLED CGO_CFLAGS CGO_CPPFLAGS CGO_CXXFLAGS CGO_LDFLAGS CC CXX
goext = go c s S cc cpp cxx h hh hpp hxx
gosrc_cmd = find -L src -name '.*' -prune -o \( -type f \( -false $(foreach e,$(goext),-o -name '*.$e') \) -o -type d \) -print
gosrc = $(shell $(gosrc_cmd))

GOPATH := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))

vp = .Makefile.var.%

deps = gopkg.in/yaml.v2
subdirs = src/nslcd/proto

all: build

include $(addsuffix /Makefile,$(subdirs))

secondary += test/*.o
download += $(addprefix src/,$(deps))
generate += LICENSE.lgpl-2.1.txt LICENSE.gpl-2.txt LICENSE.apache-2.0.txt
build += bin/nshd nshd.service nshd.socket test/runner
install += $(addprefix $(DESTDIR),$(bindir)/nshd $(systemddir)/system/nshd.socket $(systemddir)/system/nshd.service)

download: $(download)
generate: $(generate)
build: $(build)
install: $(install)
.PHONY: download generate build install

clean:
	rm -rf -- pkg bin src/*.*/
	rm -f -- $(build) $(secondary)
.PHONY: clean
maintainer-clean: clean
	rm -f -- $(generate)
.PHONY: maintainer-clean

uninstall:
	rm -f -- $(install)
	rmdir -p -- $(sort $(dir $(install))) 2>/dev/null || true
.PHONY: uninstall

LICENSE.lgpl-2.1.txt: $(NET)
	curl https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt > $@
LICENSE.gpl-2.txt: $(NET)
	curl https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt > $@
LICENSE.apache-2.0.txt: $(NET)
	curl https://www.apache.org/licenses/LICENSE-2.0 > $@

$(foreach d,$(deps),$(eval src/$d: $(NET); GOPATH='$(GOPATH)' go get -d -u $d))

bin/nshd: $(download) $(generate) $(gosrc) $(addprefix .Makefile.var.,$(cgo_variables))
	@true $(foreach f,$(filter $(vp),$^), && test $@ -nt $f ) || rm -rf -- bin pkg
	GOPATH='$(GOPATH)' CGO_CFLAGS='$(CGO_CFLAGS)' go install nshd

%: %.in
	< $< sed $(foreach v,$(patsubst $(vp),%,$(filter $(vp),$^)), -e 's|@$v@|$($v)|g' ) > $@
nshd.service: .Makefile.var.bindir .Makefile.var.user .Makefile.var.group
nshd.socket:                       .Makefile.var.user .Makefile.var.group

$(DESTDIR)$(bindir)/%: bin/%
	install -TDm755 $< $@
$(DESTDIR)$(systemddir)/system/%.socket: %.socket
	install -TDm644 $< $@
$(DESTDIR)$(systemddir)/system/%.service: %.service
	install -TDm644 $< $@

%.o: %.c .Makefile.var.CC .Makefile.var.CPPFLAGS .Makefile.var.CFLAGS
	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $(filter-out $(vp),$^)
%: %.o .Makefile.var.CC .Makefile.var.LDFLAGS .Makefile.var.LOADLIBES .Makefile.var.LDLIBS
	$(CC) $(LDFLAGS) -o $@ $(filter-out $(vp),$^) $(LOADLIBES) $(LDLIBS)

.Makefile.var.%: FORCE
	@printf '%s' '$($*)' > .tmp$@ && { cmp -s .tmp$@ $@ && rm -f -- .tmp$@ || mv -Tf .tmp$@ $@; } || { rm -f -- .tmp$@; false; }
.PHONY: FORCE

.SECONDARY:
.DELETE_ON_ERROR: