make-linux.mk 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #
  2. # Makefile for ZeroTier One on Linux
  3. #
  4. # This is confirmed to work on distributions newer than CentOS 6 (the
  5. # one used for reference builds) and on 32 and 64 bit x86 and ARM
  6. # machines. It should also work on other 'normal' machines and recent
  7. # distributions. Editing might be required for tiny devices or weird
  8. # distros.
  9. #
  10. # Targets
  11. # one: zerotier-one and symlinks (cli and idtool)
  12. # manpages: builds manpages, requires 'ronn' or nodeJS (will use either)
  13. # all: builds 'one' and 'manpages'
  14. # selftest: zerotier-selftest
  15. # debug: builds 'one' and 'selftest' with tracing and debug flags
  16. # clean: removes all built files, objects, other trash
  17. # distclean: removes a few other things that might be present
  18. # debian: build DEB packages; deb dev tools must be present
  19. # redhat: build RPM packages; rpm dev tools must be present
  20. #
  21. # Automagically pick clang or gcc, with preference for clang
  22. # This is only done if we have not overridden these with an environment or CLI variable
  23. ifeq ($(origin CC),default)
  24. CC=$(shell if [ -e /usr/bin/clang ]; then echo clang; else echo gcc; fi)
  25. endif
  26. ifeq ($(origin CXX),default)
  27. CXX=$(shell if [ -e /usr/bin/clang++ ]; then echo clang++; else echo g++; fi)
  28. endif
  29. #UNAME_M=$(shell $(CC) -dumpmachine | cut -d '-' -f 1)
  30. INCLUDES?=
  31. DEFS?=-D_FORTIFY_SOURCE=2
  32. LDLIBS?=
  33. DESTDIR?=
  34. include objects.mk
  35. # On Linux we auto-detect the presence of some libraries and if present we
  36. # link against the system version. This works with our package build images.
  37. ifeq ($(wildcard /usr/include/lz4.h),)
  38. OBJS+=ext/lz4/lz4.o
  39. else
  40. LDLIBS+=-llz4
  41. DEFS+=-DZT_USE_SYSTEM_LZ4
  42. endif
  43. ifeq ($(wildcard /usr/include/http_parser.h),)
  44. OBJS+=ext/http-parser/http_parser.o
  45. else
  46. LDLIBS+=-lhttp_parser
  47. DEFS+=-DZT_USE_SYSTEM_HTTP_PARSER
  48. endif
  49. ifeq ($(ZT_USE_MINIUPNPC),1)
  50. OBJS+=osdep/PortMapper.o
  51. DEFS+=-DZT_USE_MINIUPNPC
  52. # Auto-detect libminiupnpc at least v2.0
  53. MINIUPNPC_IS_NEW_ENOUGH=$(shell grep -sqr '.*define.*MINIUPNPC_VERSION.*"2.."' /usr/include/miniupnpc/miniupnpc.h && echo 1)
  54. ifeq ($(MINIUPNPC_IS_NEW_ENOUGH),1)
  55. DEFS+=-DZT_USE_SYSTEM_MINIUPNPC
  56. LDLIBS+=-lminiupnpc
  57. else
  58. DEFS+=-DMINIUPNP_STATICLIB -DMINIUPNPC_SET_SOCKET_TIMEOUT -DMINIUPNPC_GET_SRC_ADDR -D_BSD_SOURCE -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -DOS_STRING=\"Linux\" -DMINIUPNPC_VERSION_STRING=\"2.0\" -DUPNP_VERSION_STRING=\"UPnP/1.1\" -DENABLE_STRNATPMPERR
  59. OBJS+=ext/miniupnpc/connecthostport.o ext/miniupnpc/igd_desc_parse.o ext/miniupnpc/minisoap.o ext/miniupnpc/minissdpc.o ext/miniupnpc/miniupnpc.o ext/miniupnpc/miniwget.o ext/miniupnpc/minixml.o ext/miniupnpc/portlistingparse.o ext/miniupnpc/receivedata.o ext/miniupnpc/upnpcommands.o ext/miniupnpc/upnpdev.o ext/miniupnpc/upnperrors.o ext/miniupnpc/upnpreplyparse.o
  60. endif
  61. # Auto-detect libnatpmp
  62. ifeq ($(wildcard /usr/include/natpmp.h),)
  63. OBJS+=ext/libnatpmp/natpmp.o ext/libnatpmp/getgateway.o
  64. else
  65. LDLIBS+=-lnatpmp
  66. DEFS+=-DZT_USE_SYSTEM_NATPMP
  67. endif
  68. endif
  69. ifeq ($(ZT_ENABLE_CLUSTER),1)
  70. DEFS+=-DZT_ENABLE_CLUSTER
  71. endif
  72. ifeq ($(ZT_TRACE),1)
  73. DEFS+=-DZT_TRACE
  74. endif
  75. ifeq ($(ZT_DEBUG),1)
  76. DEFS+=-DZT_TRACE
  77. override CFLAGS+=-Wall -g -O -pthread $(INCLUDES) $(DEFS)
  78. override CXXFLAGS+=-Wall -g -O -std=c++11 -pthread $(INCLUDES) $(DEFS)
  79. LDFLAGS=
  80. STRIP?=echo
  81. # The following line enables optimization for the crypto code, since
  82. # C25519 in particular is almost UNUSABLE in -O0 even on a 3ghz box!
  83. ext/lz4/lz4.o node/Salsa20.o node/SHA512.o node/C25519.o node/Poly1305.o: CFLAGS = -Wall -O2 -g -pthread $(INCLUDES) $(DEFS)
  84. else
  85. CFLAGS?=-O3 -fstack-protector
  86. override CFLAGS+=-Wall -fPIE -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  87. CXXFLAGS?=-O3 -fstack-protector
  88. override CXXFLAGS+=-Wall -Wno-unused-result -Wreorder -fPIE -std=c++11 -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  89. LDFLAGS=-pie -Wl,-z,relro,-z,now
  90. STRIP?=strip
  91. STRIP+=--strip-all
  92. endif
  93. # Uncomment for gprof profile build
  94. #CFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)
  95. #CXXFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)
  96. #LDFLAGS=
  97. #STRIP=echo
  98. all: one manpages
  99. one: $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o
  100. $(CXX) $(CXXFLAGS) $(LDFLAGS) -o zerotier-one $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o $(LDLIBS)
  101. $(STRIP) zerotier-one
  102. ln -sf zerotier-one zerotier-idtool
  103. ln -sf zerotier-one zerotier-cli
  104. selftest: $(OBJS) selftest.o
  105. $(CXX) $(CXXFLAGS) $(LDFLAGS) -o zerotier-selftest selftest.o $(OBJS) $(LDLIBS)
  106. $(STRIP) zerotier-selftest
  107. manpages: FORCE
  108. cd doc ; ./build.sh
  109. doc: manpages
  110. clean: FORCE
  111. rm -rf *.so *.o node/*.o controller/*.o osdep/*.o service/*.o ext/http-parser/*.o ext/lz4/*.o ext/json-parser/*.o ext/miniupnpc/*.o ext/libnatpmp/*.o $(OBJS) zerotier-one zerotier-idtool zerotier-cli zerotier-selftest build-* ZeroTierOneInstaller-* *.deb *.rpm .depend doc/*.1 doc/*.2 doc/*.8 debian/files debian/zerotier-one*.debhelper debian/zerotier-one.substvars debian/*.log debian/zerotier-one
  112. distclean: clean
  113. rm -rf doc/node_modules
  114. find linux-build-farm -type f -name '*.deb' -print0 | xargs -0 rm -fv
  115. find linux-build-farm -type f -name '*.rpm' -print0 | xargs -0 rm -fv
  116. find linux-build-farm -type f -name 'zt1-src.tar.gz' | xargs rm -fv
  117. realclean: distclean
  118. debug: FORCE
  119. make ZT_DEBUG=1 one
  120. make ZT_DEBUG=1 selftest
  121. # Note: keep the symlinks in /var/lib/zerotier-one to the binaries since these
  122. # provide backward compatibility with old releases where the binaries actually
  123. # lived here. Folks got scripts.
  124. install: FORCE
  125. mkdir -p $(DESTDIR)/usr/sbin
  126. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  127. cp -f zerotier-one $(DESTDIR)/usr/sbin/zerotier-one
  128. rm -f $(DESTDIR)/usr/sbin/zerotier-cli
  129. rm -f $(DESTDIR)/usr/sbin/zerotier-idtool
  130. ln -s zerotier-one $(DESTDIR)/usr/sbin/zerotier-cli
  131. ln -s zerotier-one $(DESTDIR)/usr/sbin/zerotier-idtool
  132. mkdir -p $(DESTDIR)/var/lib/zerotier-one
  133. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  134. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  135. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  136. ln -s ../../../usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  137. ln -s ../../../usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  138. ln -s ../../../usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  139. mkdir -p $(DESTDIR)/usr/share/man/man8
  140. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  141. cat doc/zerotier-one.8 | gzip -9 >$(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  142. mkdir -p $(DESTDIR)/usr/share/man/man1
  143. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  144. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  145. cat doc/zerotier-cli.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  146. cat doc/zerotier-idtool.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  147. # Uninstall preserves identity.public and identity.secret since the user might
  148. # want to save these. These are your ZeroTier address.
  149. uninstall: FORCE
  150. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  151. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  152. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  153. rm -f $(DESTDIR)/usr/sbin/zerotier-cli
  154. rm -f $(DESTDIR)/usr/sbin/zerotier-idtool
  155. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  156. rm -rf $(DESTDIR)/var/lib/zerotier-one/iddb.d
  157. rm -rf $(DESTDIR)/var/lib/zerotier-one/updates.d
  158. rm -rf $(DESTDIR)/var/lib/zerotier-one/networks.d
  159. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one.port
  160. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  161. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  162. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  163. # These are just for convenience for building Linux packages
  164. debian: distclean
  165. debuild -I -i -us -uc
  166. redhat: distclean
  167. rpmbuild -ba zerotier-one.spec
  168. FORCE: