make-linux.mk 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. # doc: builds manpages, requires rst2man somewhere in PATH
  13. # all: builds 'one'
  14. # selftest: zerotier-selftest
  15. # debug: builds 'one' and 'selftest' with tracing and debug flags
  16. # installer: builds installers and packages (RPM/DEB/etc.) if possible
  17. # official: cleans and then builds 'one', 'installer', and 'doc'
  18. # clean: removes all built files, objects, other trash
  19. #
  20. # Automagically pick clang or gcc, with preference for clang
  21. # This is only done if we have not overridden these with an environment or CLI variable
  22. ifeq ($(origin CC),default)
  23. CC=$(shell if [ -e /usr/bin/clang ]; then echo clang; else echo gcc; fi)
  24. endif
  25. ifeq ($(origin CXX),default)
  26. CXX=$(shell if [ -e /usr/bin/clang++ ]; then echo clang++; else echo g++; fi)
  27. endif
  28. #UNAME_M=$(shell $(CC) -dumpmachine | cut -d '-' -f 1)
  29. INCLUDES?=
  30. DEFS?=-D_FORTIFY_SOURCE=2
  31. LDLIBS?=
  32. DESTDIR?=
  33. include objects.mk
  34. # On Linux we auto-detect the presence of some libraries
  35. ifeq ($(wildcard /usr/include/lz4.h),)
  36. OBJS+=ext/lz4/lz4.o
  37. else
  38. LDLIBS+=-llz4
  39. DEFS+=-DZT_USE_SYSTEM_LZ4
  40. endif
  41. ifeq ($(wildcard /usr/include/http_parser.h),)
  42. OBJS+=ext/http-parser/http_parser.o
  43. else
  44. LDLIBS+=-lhttp_parser
  45. DEFS+=-DZT_USE_SYSTEM_HTTP_PARSER
  46. endif
  47. ifeq ($(wildcard /usr/include/json-parser/json.h),)
  48. OBJS+=ext/json-parser/json.o
  49. else
  50. LDLIBS+=-ljsonparser
  51. DEFS+=-DZT_USE_SYSTEM_JSON_PARSER
  52. endif
  53. ifeq ($(ZT_OFFICIAL_RELEASE),1)
  54. DEFS+=-DZT_OFFICIAL_RELEASE
  55. ZT_USE_MINIUPNPC=1
  56. endif
  57. ifeq ($(ZT_USE_MINIUPNPC),1)
  58. 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
  59. OBJS+=osdep/PortMapper.o
  60. # We always use ext/miniupnpc because versions that ship with various Linux distributions are too old
  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. ifeq ($(wildcard /usr/include/natpmp.h),)
  67. OBJS+=ext/libnatpmp/natpmp.o ext/libnatpmp/getgateway.o
  68. else
  69. LDLIBS+=-lnatpmp
  70. DEFS+=-DZT_USE_SYSTEM_NATPMP
  71. endif
  72. endif
  73. ifeq ($(ZT_ENABLE_NETWORK_CONTROLLER),1)
  74. DEFS+=-DZT_ENABLE_NETWORK_CONTROLLER
  75. LDLIBS+=-L/usr/local/lib -lsqlite3
  76. OBJS+=controller/SqliteNetworkController.o
  77. endif
  78. ifeq ($(ZT_ENABLE_CLUSTER),1)
  79. DEFS+=-DZT_ENABLE_CLUSTER
  80. endif
  81. ifeq ($(ZT_DEBUG),1)
  82. DEFS+=-DZT_TRACE
  83. CFLAGS+=-Wall -g -pthread $(INCLUDES) $(DEFS)
  84. CXXFLAGS+=-Wall -g -pthread $(INCLUDES) $(DEFS)
  85. LDFLAGS=
  86. STRIP?=echo
  87. # The following line enables optimization for the crypto code, since
  88. # C25519 in particular is almost UNUSABLE in -O0 even on a 3ghz box!
  89. ext/lz4/lz4.o node/Salsa20.o node/SHA512.o node/C25519.o node/Poly1305.o: CFLAGS = -Wall -O2 -g -pthread $(INCLUDES) $(DEFS)
  90. else
  91. CFLAGS?=-O3 -fstack-protector-strong
  92. CFLAGS+=-Wall -fPIE -fvisibility=hidden -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  93. CXXFLAGS?=-O3 -fstack-protector-strong
  94. CXXFLAGS+=-Wall -Wreorder -fPIE -fvisibility=hidden -fno-rtti -pthread $(INCLUDES) -DNDEBUG $(DEFS)
  95. LDFLAGS=-pie -Wl,-z,relro,-z,now
  96. STRIP?=strip
  97. STRIP+=--strip-all
  98. endif
  99. ifeq ($(ZT_TRACE),1)
  100. DEFS+=-DZT_TRACE
  101. endif
  102. # Uncomment for gprof profile build
  103. #CFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)
  104. #CXXFLAGS=-Wall -g -pg -pthread $(INCLUDES) $(DEFS)
  105. #LDFLAGS=
  106. #STRIP=echo
  107. all: one manpages
  108. one: $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o
  109. $(CXX) $(CXXFLAGS) $(LDFLAGS) -o zerotier-one $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o $(LDLIBS)
  110. $(STRIP) zerotier-one
  111. ln -sf zerotier-one zerotier-idtool
  112. ln -sf zerotier-one zerotier-cli
  113. netcon: $(OBJS)
  114. rm -f *.o
  115. # Need to selectively rebuild one.cpp and OneService.cpp with ZT_SERVICE_NETCON and ZT_ONE_NO_ROOT_CHECK defined, and also NetconEthernetTap
  116. $(CXX) $(CXXFLAGS) $(LDFLAGS) -DZT_SERVICE_NETCON -DZT_ONE_NO_ROOT_CHECK -Iext/lwip/src/include -Iext/lwip/src/include/ipv4 -Iext/lwip/src/include/ipv6 -o zerotier-netcon-service $(OBJS) service/OneService.cpp netcon/NetconEthernetTap.cpp one.cpp -x c netcon/RPC.c $(LDLIBS) -ldl
  117. # Build netcon/liblwip.so which must be placed in ZT home for zerotier-netcon-service to work
  118. cd netcon ; make -f make-liblwip.mk
  119. # Use gcc not clang to build standalone intercept library since gcc is typically used for libc and we want to ensure maximal ABI compatibility
  120. cd netcon ; gcc -g -O2 -Wall -std=c99 -fPIC -DVERBOSE -D_GNU_SOURCE -DNETCON_INTERCEPT -I. -nostdlib -shared -o libzerotierintercept.so Intercept.c RPC.c -ldl
  121. cp netcon/libzerotierintercept.so libzerotierintercept.so
  122. ln -sf zerotier-netcon-service zerotier-cli
  123. ln -sf zerotier-netcon-service zerotier-idtool
  124. selftest: $(OBJS) selftest.o
  125. $(CXX) $(CXXFLAGS) $(LDFLAGS) -o zerotier-selftest selftest.o $(OBJS) $(LDLIBS)
  126. $(STRIP) zerotier-selftest
  127. manpages: FORCE
  128. cd doc ; ./build.sh
  129. clean: FORCE
  130. rm -rf *.so *.o netcon/*.a 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 zerotier-netcon-service build-* ZeroTierOneInstaller-* *.deb *.rpm .depend netcon/.depend doc/*.1 doc/*.2 doc/*.8 debian/files debian/zerotier-one*.debhelper debian/zerotier-one.substvars debian/*.log debian/zerotier-one
  131. find netcon -type f \( -name '*.o' -o -name '*.so' -o -name '*.1.0' -o -name 'zerotier-one' -o -name 'zerotier-cli' -o -name 'zerotier-netcon-service' \) -delete
  132. find netcon/docker-test -name "zerotier-intercept" -type f -delete
  133. distclean: clean
  134. rm -rf doc/node_modules
  135. debug: FORCE
  136. make ZT_DEBUG=1 one
  137. make ZT_DEBUG=1 selftest
  138. install: FORCE
  139. mkdir -p $(DESTDIR)/usr/sbin
  140. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  141. cp -f zerotier-one $(DESTDIR)/usr/sbin/zerotier-one
  142. mkdir -p $(DESTDIR)/usr/bin
  143. rm -f $(DESTDIR)/usr/bin/zerotier-cli
  144. rm -f $(DESTDIR)/usr/bin/zerotier-idtool
  145. ln -s $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/usr/bin/zerotier-cli
  146. ln -s $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/usr/bin/zerotier-idtool
  147. mkdir -p $(DESTDIR)/var/lib/zerotier-one
  148. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  149. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  150. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  151. ln -s $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  152. ln -s $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  153. ln -s $(DESTDIR)/usr/sbin/zerotier-one $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  154. mkdir -p $(DESTDIR)/usr/share/man/man8
  155. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  156. cat doc/zerotier-one.8 | gzip -9 >$(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  157. mkdir -p $(DESTDIR)/usr/share/man/man1
  158. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  159. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  160. cat doc/zerotier-cli.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  161. cat doc/zerotier-idtool.1 | gzip -9 >$(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  162. uninstall: FORCE
  163. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one
  164. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-cli
  165. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-idtool
  166. rm -f $(DESTDIR)/usr/bin/zerotier-cli
  167. rm -f $(DESTDIR)/usr/bin/zerotier-idtool
  168. rm -f $(DESTDIR)/usr/sbin/zerotier-one
  169. rm -rf $(DESTDIR)/var/lib/zerotier-one/iddb.d
  170. rm -rf $(DESTDIR)/var/lib/zerotier-one/updates.d
  171. rm -rf $(DESTDIR)/var/lib/zerotier-one/networks.d
  172. rm -f $(DESTDIR)/var/lib/zerotier-one/zerotier-one.port
  173. rm -f $(DESTDIR)/usr/share/man/man8/zerotier-one.8.gz
  174. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-idtool.1.gz
  175. rm -f $(DESTDIR)/usr/share/man/man1/zerotier-cli.1.gz
  176. debian: distclean
  177. debuild -I -i -us -uc
  178. FORCE: