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 -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
  58. # Right now auto-detect and use of system miniupnpc is disabled since the
  59. # versions that ship with various Linux distributions are pretty much all
  60. # ancient or broken.
  61. #ifeq ($(wildcard /usr/include/miniupnpc/miniupnpc.h),)
  62. 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
  63. #else
  64. # LDLIBS+=-lminiupnpc
  65. #endif
  66. # libnatpmp on the other hand is safe to auto-detect and use -- the two
  67. # libraries are by the same author but are separate.
  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_DEBUG),1)
  84. DEFS+=-DZT_TRACE
  85. CFLAGS+=-Wall -g -pthread $(INCLUDES) $(DEFS)
  86. CXXFLAGS+=-Wall -g -pthread $(INCLUDES) $(DEFS)
  87. LDFLAGS=
  88. STRIP?=echo
  89. # The following line enables optimization for the crypto code, since
  90. # C25519 in particular is almost UNUSABLE in -O0 even on a 3ghz box!
  91. ext/lz4/lz4.o node/Salsa20.o node/SHA512.o node/C25519.o node/Poly1305.o: CFLAGS = -Wall -O2 -g -pthread $(INCLUDES) $(DEFS)
  92. else
  93. CFLAGS?=-O3 -fstack-protector-strong
  94. CFLAGS+=-Wall -fPIE -fvisibility=hidden -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  95. CXXFLAGS?=-O3 -fstack-protector-strong
  96. CXXFLAGS+=-Wall -Wno-unused-result -Wreorder -fPIE -fvisibility=hidden -fno-rtti -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  97. LDFLAGS=-pie -Wl,-z,relro,-z,now
  98. STRIP?=strip
  99. STRIP+=--strip-all
  100. endif
  101. ifeq ($(ZT_TRACE),1)
  102. DEFS+=-DZT_TRACE
  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. realclean: distclean
  126. debug: FORCE
  127. make ZT_DEBUG=1 one
  128. make ZT_DEBUG=1 selftest
  129. # Note: keep the symlinks in /var/lib/zerotier-one to the binaries since these
  130. # provide backward compatibility with old releases where the binaries actually
  131. # lived here. Folks got scripts.
  132. install: FORCE
  133. mkdir -p $(DESTDIR)/usr/sbin
  134. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  135. cp -f zerotier-one $(DESTDIR)/usr/sbin/zerotier-one
  136. mkdir -p $(DESTDIR)/usr/bin
  137. rm -f $(DESTDIR)/usr/bin/zerotier-cli
  138. rm -f $(DESTDIR)/usr/bin/zerotier-idtool
  139. ln -rs $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/usr/bin/zerotier-cli
  140. ln -rs $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/usr/bin/zerotier-idtool
  141. mkdir -p $(DESTDIR)/var/lib/zerotier-one
  142. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  143. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  144. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  145. ln -rs $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  146. ln -rs $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  147. ln -rs $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  148. mkdir -p $(DESTDIR)/usr/share/man/man8
  149. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  150. cat doc/zerotier-one.8 | gzip -9 >$(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  151. mkdir -p $(DESTDIR)/usr/share/man/man1
  152. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  153. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  154. cat doc/zerotier-cli.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  155. cat doc/zerotier-idtool.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  156. # Uninstall preserves identity.public and identity.secret since the user might
  157. # want to save these. These are your ZeroTier address.
  158. uninstall: FORCE
  159. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  160. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  161. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  162. rm -f $(DESTDIR)/usr/bin/zerotier-cli
  163. rm -f $(DESTDIR)/usr/bin/zerotier-idtool
  164. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  165. rm -rf $(DESTDIR)/var/lib/zerotier-one/iddb.d
  166. rm -rf $(DESTDIR)/var/lib/zerotier-one/updates.d
  167. rm -rf $(DESTDIR)/var/lib/zerotier-one/networks.d
  168. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one.port
  169. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  170. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  171. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  172. # These are just for convenience for building Linux packages
  173. debian: distclean
  174. debuild -I -i -us -uc
  175. redhat: distclean
  176. rpmbuild -ba zerotier-one.spec
  177. FORCE: