make-linux.mk 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 ($(wildcard /usr/include/json-parser/json.h),)
  50. OBJS+=ext/json-parser/json.o
  51. else
  52. LDLIBS+=-ljsonparser
  53. DEFS+=-DZT_USE_SYSTEM_JSON_PARSER
  54. endif
  55. ifeq ($(ZT_USE_MINIUPNPC),1)
  56. OBJS+=osdep/PortMapper.o
  57. DEFS+=-DZT_USE_MINIUPNPC
  58. # Auto-detect libminiupnpc at least v2.0
  59. MINIUPNPC_IS_NEW_ENOUGH=$(shell grep -sqr '.*define.*MINIUPNPC_VERSION.*"2.."' /usr/include/miniupnpc/miniupnpc.h && echo 1)
  60. ifeq ($(MINIUPNPC_IS_NEW_ENOUGH),1)
  61. DEFS+=-DZT_USE_SYSTEM_MINIUPNPC
  62. LDLIBS+=-lminiupnpc
  63. else
  64. 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
  65. 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
  66. endif
  67. # Auto-detect libnatpmp
  68. ifeq ($(wildcard /usr/include/natpmp.h),)
  69. OBJS+=ext/libnatpmp/natpmp.o ext/libnatpmp/getgateway.o
  70. else
  71. LDLIBS+=-lnatpmp
  72. DEFS+=-DZT_USE_SYSTEM_NATPMP
  73. endif
  74. endif
  75. ifeq ($(ZT_ENABLE_NETWORK_CONTROLLER),1)
  76. DEFS+=-DZT_ENABLE_NETWORK_CONTROLLER
  77. LDLIBS+=-L/usr/local/lib -lsqlite3
  78. OBJS+=controller/SqliteNetworkController.o
  79. endif
  80. ifeq ($(ZT_ENABLE_CLUSTER),1)
  81. DEFS+=-DZT_ENABLE_CLUSTER
  82. endif
  83. ifeq ($(ZT_TRACE),1)
  84. DEFS+=-DZT_TRACE
  85. endif
  86. ifeq ($(ZT_DEBUG),1)
  87. DEFS+=-DZT_TRACE
  88. override CFLAGS+=-Wall -g -pthread $(INCLUDES) $(DEFS)
  89. override CXXFLAGS+=-Wall -g -pthread $(INCLUDES) $(DEFS)
  90. LDFLAGS=
  91. STRIP?=echo
  92. # The following line enables optimization for the crypto code, since
  93. # C25519 in particular is almost UNUSABLE in -O0 even on a 3ghz box!
  94. ext/lz4/lz4.o node/Salsa20.o node/SHA512.o node/C25519.o node/Poly1305.o: CFLAGS = -Wall -O2 -g -pthread $(INCLUDES) $(DEFS)
  95. else
  96. CFLAGS?=-O3 -fstack-protector-strong
  97. override CFLAGS+=-Wall -fPIE -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  98. CXXFLAGS?=-O3 -fstack-protector-strong
  99. override CXXFLAGS+=-Wall -Wno-unused-result -Wreorder -fPIE -fno-rtti -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  100. LDFLAGS=-pie -Wl,-z,relro,-z,now
  101. STRIP?=strip
  102. STRIP+=--strip-all
  103. endif
  104. # Uncomment for gprof profile build
  105. #CFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)
  106. #CXXFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)
  107. #LDFLAGS=
  108. #STRIP=echo
  109. all: one manpages
  110. one: $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o
  111. $(CXX) $(CXXFLAGS) $(LDFLAGS) -o zerotier-one $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o $(LDLIBS)
  112. $(STRIP) zerotier-one
  113. ln -sf zerotier-one zerotier-idtool
  114. ln -sf zerotier-one zerotier-cli
  115. selftest: $(OBJS) selftest.o
  116. $(CXX) $(CXXFLAGS) $(LDFLAGS) -o zerotier-selftest selftest.o $(OBJS) $(LDLIBS)
  117. $(STRIP) zerotier-selftest
  118. manpages: FORCE
  119. cd doc ; ./build.sh
  120. doc: manpages
  121. clean: FORCE
  122. 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
  123. distclean: clean
  124. rm -rf doc/node_modules
  125. find linux-build-farm -type f -name '*.deb' -print0 | xargs -0 rm -fv
  126. find linux-build-farm -type f -name '*.rpm' -print0 | xargs -0 rm -fv
  127. find linux-build-farm -type f -name 'zt1-src.tar.gz' | xargs rm -fv
  128. realclean: distclean
  129. debug: FORCE
  130. make ZT_DEBUG=1 one
  131. make ZT_DEBUG=1 selftest
  132. # Note: keep the symlinks in /var/lib/zerotier-one to the binaries since these
  133. # provide backward compatibility with old releases where the binaries actually
  134. # lived here. Folks got scripts.
  135. install: FORCE
  136. mkdir -p $(DESTDIR)/usr/sbin
  137. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  138. cp -f zerotier-one $(DESTDIR)/usr/sbin/zerotier-one
  139. rm -f $(DESTDIR)/usr/sbin/zerotier-cli
  140. rm -f $(DESTDIR)/usr/sbin/zerotier-idtool
  141. ln -s zerotier-one $(DESTDIR)/usr/sbin/zerotier-cli
  142. ln -s zerotier-one $(DESTDIR)/usr/sbin/zerotier-idtool
  143. mkdir -p $(DESTDIR)/var/lib/zerotier-one
  144. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  145. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  146. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  147. ln -s ../../../usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  148. ln -s ../../../usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  149. ln -s ../../../usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  150. mkdir -p $(DESTDIR)/usr/share/man/man8
  151. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  152. cat doc/zerotier-one.8 | gzip -9 >$(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  153. mkdir -p $(DESTDIR)/usr/share/man/man1
  154. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  155. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  156. cat doc/zerotier-cli.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  157. cat doc/zerotier-idtool.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  158. # Uninstall preserves identity.public and identity.secret since the user might
  159. # want to save these. These are your ZeroTier address.
  160. uninstall: FORCE
  161. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  162. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  163. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  164. rm -f $(DESTDIR)/usr/sbin/zerotier-cli
  165. rm -f $(DESTDIR)/usr/sbin/zerotier-idtool
  166. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  167. rm -rf $(DESTDIR)/var/lib/zerotier-one/iddb.d
  168. rm -rf $(DESTDIR)/var/lib/zerotier-one/updates.d
  169. rm -rf $(DESTDIR)/var/lib/zerotier-one/networks.d
  170. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one.port
  171. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  172. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  173. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  174. # These are just for convenience for building Linux packages
  175. debian: distclean
  176. debuild -I -i -us -uc
  177. redhat: distclean
  178. rpmbuild -ba zerotier-one.spec
  179. FORCE: