1#    This file is part of snapcast
2#   Copyright (C) 2014-2021  Johannes Pohl
3#
4#   This program is free software: you can redistribute it and/or modify
5#   it under the terms of the GNU General Public License as published by
6#   the Free Software Foundation, either version 3 of the License, or
7#   (at your option) any later version.
8#
9#   This program is distributed in the hope that it will be useful,
10#   but WITHOUT ANY WARRANTY; without even the implied warranty of
11#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12#   GNU General Public License for more details.
13#
14#   You should have received a copy of the GNU General Public License
15#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17VERSION = 0.25.0
18BIN = snapserver
19
20ifeq ($(TARGET), FREEBSD)
21SHELL   = /usr/local/bin/bash
22else
23SHELL   = /bin/bash
24endif
25
26ifdef DESTDIR
27# dh_auto_install (Debian) sets this variable
28  TARGET_DIR = $(DESTDIR)/usr
29else
30  TARGET_DIR ?= /usr
31endif
32
33DEBUG ?= 0
34ifeq ($(DEBUG), 1)
35    CXXFLAGS += -g
36else
37    CXXFLAGS += -O2
38endif
39
40ifneq ($(SANITIZE), )
41	CXXFLAGS += -fsanitize=$(SANITIZE) -g
42	LDFLAGS += -fsanitize=$(SANITIZE)
43endif
44
45CXXFLAGS += $(ADD_CFLAGS) -std=c++14 -Wall -Wextra -Wpedantic -Wno-unused-function -DBOOST_ERROR_CODE_HEADER_ONLY -DHAS_FLAC -DHAS_OGG -DHAS_VORBIS -DHAS_VORBIS_ENC -DHAS_OPUS -DHAS_SOXR -DVERSION=\"$(VERSION)\" -I. -I.. -I../common
46LDFLAGS  += $(ADD_LDFLAGS) -lvorbis -lvorbisenc -logg -lFLAC -lopus -lsoxr
47OBJ       = snapserver.o server.o config.o control_server.o control_session_tcp.o control_session_http.o control_session_ws.o stream_server.o stream_session.o stream_session_tcp.o stream_session_ws.o streamreader/stream_uri.o streamreader/base64.o streamreader/stream_manager.o streamreader/pcm_stream.o streamreader/posix_stream.o streamreader/pipe_stream.o streamreader/file_stream.o streamreader/tcp_stream.o streamreader/process_stream.o streamreader/airplay_stream.o streamreader/meta_stream.o streamreader/librespot_stream.o streamreader/watchdog.o encoder/encoder_factory.o encoder/flac_encoder.o encoder/opus_encoder.o encoder/pcm_encoder.o encoder/null_encoder.o encoder/ogg_encoder.o ../common/sample_format.o ../common/resampler.o
48
49ifneq (,$(TARGET))
50CXXFLAGS += -D$(TARGET)
51endif
52
53ifeq ($(ENDIAN), BIG)
54CXXFLAGS += -DIS_BIG_ENDIAN
55endif
56
57ifeq ($(TARGET), ANDROID)
58
59CXX       = $(NDK_DIR)/bin/arm-linux-androideabi-g++
60CXXFLAGS += -pthread -DNO_CPP11_STRING -fPIC -I$(NDK_DIR)/include
61LDFLAGS  += -L$(NDK_DIR)/lib -pie -llog -latomic
62
63else ifeq ($(TARGET), OPENWRT)
64
65CXXFLAGS += -DNO_CPP11_STRING -DHAS_AVAHI -DHAS_DAEMON -DHAS_ALSA -pthread
66LDFLAGS  += -lavahi-client -lavahi-common -latomic
67OBJ      += ../common/daemon.o publishZeroConf/publish_avahi.o streamreader/alsa_stream.o
68
69else ifeq ($(TARGET), BUILDROOT)
70
71CXXFLAGS += -DHAS_AVAHI -DHAS_DAEMON -DHAS_ALSA -pthread
72LDFLAGS  += -lrt -lavahi-client -lavahi-common
73OBJ      += publishZeroConf/publish_avahi.o streamreader/alsa_stream.o
74
75else ifeq ($(TARGET), FREEBSD)
76
77CXX       = g++
78CXXFLAGS += -DFREEBSD -DNO_CPP11_STRING -DHAS_AVAHI -DHAS_DAEMON -pthread
79LDFLAGS  += -lrt -lavahi-client -lavahi-common -latomic
80OBJ      += ../common/daemon.o publishZeroConf/publish_avahi.o
81
82else ifeq ($(TARGET), MACOS)
83
84CXX       = g++
85CXXFLAGS += -DFREEBSD -DMACOS -DHAS_BONJOUR -DHAS_DAEMON -Wno-deprecated -I/usr/local/include
86LDFLAGS  += -L/usr/local/lib -framework CoreFoundation -framework IOKit
87OBJ      += ../common/daemon.o publishZeroConf/publish_bonjour.o
88
89else
90
91CXX       = g++
92CXXFLAGS += -DHAS_AVAHI -DHAS_DAEMON -DHAS_ALSA -pthread
93LDFLAGS  += -lrt -lasound -lavahi-client -lavahi-common -latomic
94OBJ      += ../common/daemon.o publishZeroConf/publish_avahi.o streamreader/alsa_stream.o
95
96endif
97
98ifdef HAS_EXPAT
99CXXFLAGS += -DHAS_EXPAT
100LDFLAGS  += -lexpat
101endif
102
103
104all:	$(BIN)
105
106$(BIN): $(OBJ)
107	$(CXX) $(CXXFLAGS) -o $(BIN) $(OBJ) $(LDFLAGS)
108
109%.o: %.cpp
110	$(CXX) $(CXXFLAGS) -c $< -o $@
111
112clean:
113	rm -rf $(BIN) $(OBJ) *~
114
115ifdef DESTDIR
116
117install:
118	$(MAKE) installfiles
119
120else ifeq ($(TARGET), FREEBSD)
121
122install:
123	echo BSD
124	install -s -g wheel -o root -m 555 $(BIN) $(TARGET_DIR)/local/bin/$(BIN)
125	install -g wheel -o root -m 555 $(BIN).1 $(TARGET_DIR)/local/man/man1/$(BIN).1
126	install -g wheel -o root -m 555 etc/$(BIN).bsd $(TARGET_DIR)/local/etc/rc.d/$(BIN)
127	install -g wheel -o root etc/$(BIN).conf /etc/$(BIN).conf
128	install -g wheel -o root -m 644 etc/index.html -Dt /usr/share/snapserver/
129	for file in etc/snapweb/*\.*; do install -g wheel -o root -m 644 $${file} -Dt "/usr/share/snapserver/snapweb/"; done
130	for file in etc/snapweb/3rd-party/*\.*; do install -g wheel -o root -m 644 $${file} -Dt "/usr/share/snapserver/snapweb/3rd-party/"; done
131
132else ifeq ($(TARGET), MACOS)
133
134install:
135	echo macOS
136	install -s -g wheel -o root $(BIN) $(TARGET_DIR)/local/bin/$(BIN)
137	install -g wheel -o root $(BIN).1 $(TARGET_DIR)/local/share/man/man1/$(BIN).1
138	install -g wheel -o root etc/$(BIN).plist /Library/LaunchAgents/de.badaix.snapcast.$(BIN).plist
139	install -g wheel -o root etc/$(BIN).conf /etc/$(BIN).conf
140	install -g wheel -o root -m 644 etc/index.html -Dt /usr/share/snapserver/
141	for file in etc/snapweb/*\.*; do install -g wheel -o root -m 644 $${file} -Dt "/usr/share/snapserver/snapweb/"; done
142	for file in etc/snapweb/3rd-party/*\.*; do install -g wheel -o root -m 644 $${file} -Dt "/usr/share/snapserver/snapweb/3rd-party/"; done
143	launchctl load /Library/LaunchAgents/de.badaix.snapcast.$(BIN).plist
144
145else
146
147install:
148	$(MAKE) adduser
149	$(MAKE) installfiles
150	install -g snapserver -o snapserver -d /var/run/$(BIN)
151
152	@if [[ `systemctl` =~ -\.mount ]]; then \
153		$(MAKE) installsystemd; \
154	elif [[ `/sbin/init --version` =~ upstart ]]; then \
155		$(MAKE) installsysv; \
156	elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]]; then \
157		$(MAKE) installsysv; \
158	else \
159		echo cannot tell; \
160	fi; \
161
162endif
163
164installfiles:
165	install -s -D -g root -o root $(BIN) $(TARGET_DIR)/bin/$(BIN)
166	install -D -g root -o root $(BIN).1 $(TARGET_DIR)/share/man/man1/$(BIN).1
167	install -g root -o root etc/$(BIN).conf /etc/$(BIN).conf;
168	install -g root -o root -m 644 etc/index.html -Dt /usr/share/snapserver/
169	for file in etc/snapweb/*\.*; do install -g root -o root -m 644 $${file} -Dt "/usr/share/snapserver/snapweb/"; done
170	for file in etc/snapweb/3rd-party/*\.*; do install -g root -o root -m 644 $${file} -Dt "/usr/share/snapserver/snapweb/3rd-party/"; done
171
172installsystemd:
173	@echo using systemd; \
174	cp ../debian/$(BIN).service /lib/systemd/system/$(BIN).service; \
175	cp -n ../debian/$(BIN).default /etc/default/$(BIN); \
176	systemctl daemon-reload; \
177	systemctl enable $(BIN); \
178	systemctl start $(BIN); \
179
180installsysv:
181	@echo using sysv; \
182	cp ../debian/$(BIN).init /etc/init.d/$(BIN); \
183	cp -n ../debian/$(BIN).default /etc/default/$(BIN); \
184	update-rc.d $(BIN) defaults; \
185	/etc/init.d/$(BIN) start; \
186
187adduser:
188	sh ../debian/snapserver.postinst configure
189
190ifeq ($(TARGET), FREEBSD)
191
192uninstall:
193	@service $(BIN) stop; \
194	killall -9 $(BIN); \
195	rm -f $(TARGET_DIR)/local/bin/$(BIN); \
196	rm -f $(TARGET_DIR)/local/man/man1/$(BIN).1; \
197	rm -f $(TARGET_DIR)/local/etc/rc.d/$(BIN); \
198 	rm -f /etc/$(BIN).conf; \
199 	rm -rf /var/lib/snapserver; \
200	rm -rf /usr/share/snapserver; \
201
202else ifeq ($(TARGET), MACOS)
203
204uninstall:
205	@launchctl unload /Library/LaunchAgents/de.badaix.snapcast.$(BIN).plist; \
206	killall -9 $(BIN); \
207	rm -f $(TARGET_DIR)/local/bin/$(BIN); \
208	rm -f $(TARGET_DIR)/local/share/man/man1/$(BIN).1; \
209	rm -f /Library/LaunchAgents/de.badaix.snapcast.$(BIN).plist; \
210 	rm -f /etc/$(BIN).conf; \
211	rm -rf /var/lib/snapserver; \
212	rm -rf /usr/share/snapserver; \
213
214else
215
216uninstall:
217	rm -f $(TARGET_DIR)/share/man/man1/$(BIN).1
218	@if [[ `systemctl` =~ -\.mount ]]; then \
219		$(MAKE) uninstallsystemd; \
220	elif [[ `/sbin/init --version` =~ upstart ]]; then \
221		$(MAKE) uninstallsysv; \
222	elif [[ -f /etc/init.d/cron && ! -h /etc/init.d/cron ]]; then \
223		$(MAKE) uninstallsysv; \
224	else \
225		echo cannot tell; \
226	fi; \
227 	rm -f /etc/$(BIN).conf; \
228	rm -rf /var/run/$(BIN); \
229	rm -rf /var/lib/snapserver; \
230	rm -rf /usr/share/snapserver; \
231	$(MAKE) deluser
232
233endif
234
235uninstallsysv:
236	@/etc/init.d/$(BIN) stop; \
237	killall -9 $(BIN); \
238	rm -f /usr/bin/$(BIN); \
239	rm -f /etc/init.d/$(BIN); \
240	rm -f /etc/default/$(BIN); \
241	update-rc.d -f $(BIN) remove; \
242
243uninstallsystemd:
244	@systemctl stop $(BIN); \
245	systemctl disable $(BIN); \
246	killall -9 $(BIN); \
247	rm -f /usr/bin/$(BIN); \
248	rm -f /lib/systemd/system/$(BIN).service; \
249	rm -f /etc/default/$(BIN); \
250	systemctl daemon-reload; \
251
252deluser:
253	sh ../debian/snapserver.postrm purge
254
255