summaryrefslogtreecommitdiff
path: root/Makefile
blob: e15c2a18673d22ee7d2161c050ac5e317daaf2b5 (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
115
116
117
118
119
120
121
122
# Copyright 2015-2018, 2021 Luke Shumaker <lukeshu@parabola.nu>.
#
# 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/>.

include config.mk
include $(topsrcdir)/build-aux/Makefile.head.mk

bins_c      = nshd-setuid nshd-tester
bins_go     = nshd
bins_lib    = common.rb
bins_script = $(filter-out %.c %.o %.in $(bins_c) $(bins_go) $(bins_lib),$(notdir $(wildcard $(srcdir)/bin/*)))
bins = $(bins_c) $(bins_go) $(bins_lib) $(bins_script)

.PHONY: FORCE
.SECONDARY:
.DELETE_ON_ERROR:

#
# Generate (pre-tarball)

files.src.gen += LICENSE.lgpl-2.1.txt LICENSE.gpl-2.txt LICENSE.apache-2.0.txt gofiles.mk

$(srcdir)/LICENSE.lgpl-2.1.txt: $(NET)
	curl https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt > $@
$(srcdir)/LICENSE.gpl-2.txt: $(NET)
	curl https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt > $@
$(srcdir)/LICENSE.apache-2.0.txt: $(NET)
	curl https://www.apache.org/licenses/LICENSE-2.0 > $@
$(srcdir)/LICENSE.wtfpl-2.txt: $(NET)
	curl http://www.wtfpl.net/txt/copying/ > $@
$(srcdir)/vendor: $(srcdir)/go.mod $(srcdir)/go.sum
	cd $(@D) && go mod vendor
	touch --no-create $@

$(srcdir)/gofiles.mk: $(srcdir)/vendor $(WRITE_IFCHANGED)
	find $< -type f | LC_COLLATE=C sort | sed 's,^,_gofiles.all+=,' | $(WRITE_IFCHANGED) $@
_gofiles.all =
-include $(srcdir)/gofiles.mk
files.src.gen += $(_gofiles.all)

#
# Build (post-tarball)

files.out.all += $(addprefix bin/,$(filter-out $(bins_script),$(bins))) nshd.service nshd.sysusers

# Go
$(outdir)/bin/%: $(srcdir)/go/src/% $(srcdir)/vendor FORCE
	cd $(srcdir) && go build -o $(abspath $@) ./go/src/$*
# C
$(outdir)/bin/%: $(srcdir)/bin/%.c $(var.)CC $(var.)CPPFLAGS $(var.)CFLAGS
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $(filter-out $(var.)%,$^)
# Basic variable substitution
$(outdir)/%: $(srcdir)/%.in
	< $< sed $(foreach v,$(patsubst $(var.)%,%,$(filter $(var.)%,$^)), -e 's|@$v@|$($v)|g' ) > $@
$(outdir)/bin/%: $(srcdir)/bin/%.in
	< $< sed $(foreach v,$(patsubst $(var.)%,%,$(filter $(var.)%,$^)), -e 's|@$v@|$($v)|g' ) > $@

# Dependencies
$(outdir)/bin/nshd-setuid: -ldl
$(outdir)/nshd.service: $(var.)user $(var.)bindir
$(outdir)/nshd.sysusers: $(var.)user
$(outdir)/bin/common.rb: $(var.)conf_file

$(outdir)/go/src/nshd/nshd_files/paths.go: $(var.)bindir $(var.)conf_file $(var.)shadow_file
$(outdir)/bin/nshd: $(srcdir)/go/src/nshd/nshd_files/paths.go
files.out.all += $(outdir)/go/src/nshd/nshd_files/paths.go

# We want to use the `-race` flag, but it isn't supported on all CPUs
# (notably: at the time of this writing we can't use it on x86-32bit
# or arm-32bit), so do some dumb sniffing of `go` command output to
# tell if it is supported on this platform.
_go_race_supported = $(filter $(word 4,$(shell go version)),$(shell go help build | sed -n '/^\s*-race/,/^\s*-/p'| grep -oE '[a-z0-9]+/[a-z0-9]+'))
$(outdir)/check: $(srcdir)/vendor
	cd $(srcdir) && go test $(if $(_go_race_supported),-race) ./...
.PHONY: $(outdir)/check
at.targets += $(outdir)/check


# Install

files.sys.all += $(addprefix $(bindir)/,$(bins)) $(systemunitdir)/nshd.socket $(systemunitdir)/nshd.service $(sysusersdir)/nshd.conf $(conf_file) $(shadow_file)

$(DESTDIR)$(bindir)/%: $(outdir)/bin/%
	$(NORMAL_INSTALL)
	install -TDm755 $< $@
$(DESTDIR)$(systemunitdir)/%.socket: $(outdir)/%.socket
	$(NORMAL_INSTALL)
	install -TDm644 $< $@
$(DESTDIR)$(systemunitdir)/%.service: $(outdir)/%.service
	$(NORMAL_INSTALL)
	install -TDm644 $< $@
$(DESTDIR)$(sysusersdir)/%.conf: $(outdir)/%.sysusers
	$(NORMAL_INSTALL)
	install -TDm644 $< $@

# Specific files
$(DESTDIR)$(bindir)/common.rb: $(srcdir)/bin/common.rb
	$(NORMAL_INSTALL)
	install -TDm644 $< $@
$(DESTDIR)$(conf_file): $(srcdir)/parabola-hackers.yml
	$(NORMAL_INSTALL)
	install -TDm644 $< $@
$(DESTDIR)$(shadow_file): $(var.)user $(DESTDIR)$(sysusersdir)/nshd.conf
	$(NORMAL_INSTALL)
	install -d $(@D)
	touch $@
	$(POST_INSTALL)
	-systemd-sysusers
	-chown $(user):$(user) $(@D) $@

include $(topsrcdir)/build-aux/Makefile.tail.mk