Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #
  2. # This Makefile attempts to build OpenConnect and its dependencies for Android
  3. #
  4. # It doesn't do a stunning job of tracking changes in the dependencies and
  5. # automatically rebuilding them, but it's good enough for getting them built
  6. # and installed into its own local sysroot.
  7. #
  8. # As long as you have the Android NDK toolchain on your path, you should then
  9. # be able to edit fairly much anything in place and rebuild it locally.
  10. #
  11. # It should also be fairly simple to extend this to cross-compile for any target
  12. # Last tested with https://dl.google.com/android/repository/android-ndk-r21b-linux-x86_64.zip
  13. NDK := /opt/android-sdk-linux_x86/android-ndk-r21b
  14. ARCH := x86_64
  15. API_LEVEL := 23
  16. EXTRA_CFLAGS :=
  17. # You should be able to just 'make ARCH=x86' and it should DTRT.
  18. ifeq ($(ARCH),arm)
  19. TRIPLET := arm-linux-androideabi
  20. EXTRA_CFLAGS := -march=armv7-a -mthumb
  21. endif
  22. ifeq ($(ARCH),arm64)
  23. TRIPLET := aarch64-linux-android
  24. API_LEVEL := 26
  25. endif
  26. ifeq ($(ARCH),x86)
  27. TRIPLET := i686-linux-android
  28. endif
  29. ifeq ($(ARCH),x86_64)
  30. TRIPLET := x86_64-linux-android
  31. endif
  32. TOPDIR := $(shell pwd)
  33. DESTDIR := $(TOPDIR)/$(TRIPLET)/out
  34. EXTRA_CFLAGS += -D__ANDROID_API__=$(API_LEVEL) -O2
  35. TOOLCHAIN := $(TOPDIR)/$(TRIPLET)/toolchain
  36. TOOLCHAIN_BUILT := $(TOOLCHAIN)/.built
  37. TOOLCHAIN_OPTS := --platform=android-$(API_LEVEL) --arch=$(ARCH) \
  38. --install-dir=$(TOOLCHAIN)
  39. PATH := $(TOOLCHAIN)/bin:$(PATH)
  40. OC_SYSROOT := $(TOOLCHAIN)/sysroot/usr
  41. PKG_CONFIG_LIBDIR := $(OC_SYSROOT)/lib/pkgconfig
  42. export PATH PKG_CONFIG_LIBDIR
  43. # PKG_CONFIG_LIBDIR gets exported to sub-makes, but not to $(shell
  44. PKG_CONFIG := PKG_CONFIG_LIBDIR=$(PKG_CONFIG_LIBDIR) pkg-config
  45. MAKEINSTALL=$(MAKE) INSTALL=$(TOPDIR)/install_symlink.sh
  46. FETCH=$(TOPDIR)/fetch.sh
  47. CONFIGURE_ARGS := --host=$(TRIPLET) --prefix=$(OC_SYSROOT) \
  48. --disable-shared --enable-static --with-pic \
  49. CC=$(TRIPLET)-clang CFLAGS="$(EXTRA_CFLAGS)"
  50. SOURCE_LIST = $(LIBXML2_SRC)/configure $(GMP_SRC)/configure \
  51. $(NETTLE_SRC)/configure $(GNUTLS_SRC)/configure \
  52. $(STOKEN_SRC)/configure $(OATH_SRC)/configure \
  53. $(LZ4_DIR)/Makefile
  54. PKG_LIST := LIBXML2 GMP NETTLE GNUTLS STOKEN OATH LZ4
  55. MIRROR_TEST_TARGETS := $(addprefix mirror-test-,$(PKG_LIST))
  56. all: openconnect run_pie
  57. #####################################################################
  58. #
  59. # Install a local cross toolchain + sysroot
  60. #
  61. # (The fallback logic is because NDK versions <= r8e can fail after trying to
  62. # use 32-bit binaries on a 64-bit NDK installation.)
  63. #
  64. $(TOOLCHAIN_BUILT):
  65. $(NDK)/build/tools/make-standalone-toolchain.sh $(TOOLCHAIN_OPTS) || \
  66. $(NDK)/build/tools/make-standalone-toolchain.sh \
  67. $(TOOLCHAIN_OPTS) --system=linux-x86_64
  68. touch $@
  69. #####################################################################
  70. #
  71. # Build libxml2 with minimal configuration for OpenConnect
  72. #
  73. # http://xmlsoft.org/news.html
  74. LIBXML2_VER := 2.9.11
  75. LIBXML2_TAR := libxml2-$(LIBXML2_VER).tar.gz
  76. LIBXML2_SHA := 886f696d5d5b45d780b2880645edf9e0c62a4fd6841b853e824ada4e02b4d331
  77. LIBXML2_SRC := sources/libxml2-$(LIBXML2_VER)
  78. LIBXML2_BUILD := $(TRIPLET)/libxml2
  79. $(LIBXML2_TAR):
  80. $(FETCH) $@ $(LIBXML2_SHA)
  81. $(LIBXML2_SRC)/configure: $(LIBXML2_TAR)
  82. mkdir -p sources
  83. tar xfz $< -C sources
  84. touch $@
  85. $(LIBXML2_BUILD)/Makefile: $(TOOLCHAIN_BUILT) $(LIBXML2_SRC)/configure
  86. mkdir -p $(LIBXML2_BUILD)
  87. cd $(LIBXML2_BUILD) && ../../$(LIBXML2_SRC)/configure $(CONFIGURE_ARGS) \
  88. --without-c14n -without-catalog --without-debug --without-docbook \
  89. --without-fexceptions --without-ftp --without-history \
  90. --without-http --without-iconv --without-iconv \
  91. --without-iso8859x --without-legacy --without-pattern \
  92. --without-push --without-regexps --without-run-debug \
  93. --without-sax1 --without-schemas --without-schematron \
  94. --without-threads --without-valid --without-xinclude \
  95. --without-xpath --without-xptr --without-zlib --without-lzma \
  96. --without-coverage --without-python
  97. $(LIBXML2_BUILD)/libxml2.la: $(LIBXML2_BUILD)/Makefile
  98. $(MAKE) -C $(LIBXML2_BUILD) libxml2.la
  99. $(LIBXML2_BUILD)/libxml-2.0.pc: $(LIBXML2_BUILD)/Makefile
  100. $(MAKE) -C $(LIBXML2_BUILD) libxml-2.0.pc
  101. $(OC_SYSROOT)/lib/libxml2.la: $(LIBXML2_BUILD)/libxml2.la
  102. $(MAKEINSTALL) -C $(LIBXML2_BUILD) install-libLTLIBRARIES
  103. $(OC_SYSROOT)/lib/pkgconfig/libxml-2.0.pc: $(LIBXML2_BUILD)/libxml-2.0.pc
  104. $(MAKEINSTALL) -C $(LIBXML2_BUILD) install-data
  105. LIBXML_DEPS := $(OC_SYSROOT)/lib/libxml2.la $(OC_SYSROOT)/lib/pkgconfig/libxml-2.0.pc
  106. libxml: $(LIBXML_DEPS)
  107. #####################################################################
  108. #
  109. # Build GNU MP
  110. #
  111. # https://gmplib.org/
  112. GMP_VER := 6.2.1
  113. GMP_TAR := gmp-$(GMP_VER).tar.xz
  114. GMP_SHA := fd4829912cddd12f84181c3451cc752be224643e87fac497b69edddadc49b4f2
  115. GMP_SRC := sources/gmp-$(GMP_VER)
  116. GMP_BUILD := $(TRIPLET)/gmp
  117. $(GMP_TAR):
  118. $(FETCH) $@ $(GMP_SHA)
  119. $(GMP_SRC)/configure: $(GMP_TAR)
  120. mkdir -p sources
  121. tar -xJf $< -C sources
  122. touch $@
  123. $(GMP_BUILD)/Makefile: $(TOOLCHAIN_BUILT) $(GMP_SRC)/configure
  124. mkdir -p $(GMP_BUILD)
  125. cd $(GMP_BUILD) && ../../$(GMP_SRC)/configure $(CONFIGURE_ARGS)
  126. $(GMP_BUILD)/libgmp.la: $(GMP_BUILD)/Makefile
  127. $(MAKE) -C $(GMP_BUILD)
  128. $(OC_SYSROOT)/lib/libgmp.la: $(GMP_BUILD)/libgmp.la
  129. $(MAKEINSTALL) -C $(GMP_BUILD) install
  130. GMP_DEPS := $(OC_SYSROOT)/lib/libgmp.la
  131. gmp: $(GMP_DEPS)
  132. #####################################################################
  133. #
  134. # Build nettle
  135. #
  136. # https://ftp.gnu.org/gnu/nettle/
  137. NETTLE_VER := 3.6
  138. NETTLE_TAR := nettle-$(NETTLE_VER).tar.gz
  139. NETTLE_SHA := d24c0d0f2abffbc8f4f34dcf114b0f131ec3774895f3555922fe2f40f3d5e3f1
  140. NETTLE_SRC := sources/nettle-$(NETTLE_VER)
  141. NETTLE_BUILD := $(TRIPLET)/nettle
  142. $(NETTLE_TAR):
  143. $(FETCH) $@ $(NETTLE_SHA)
  144. $(NETTLE_SRC)/configure: $(NETTLE_TAR)
  145. mkdir -p sources
  146. tar xfz $< -C sources
  147. touch $@
  148. $(NETTLE_BUILD)/Makefile: $(TOOLCHAIN_BUILT) $(NETTLE_SRC)/configure $(GMP_DEPS)
  149. mkdir -p $(NETTLE_BUILD)
  150. cd $(NETTLE_BUILD) && ../../$(NETTLE_SRC)/configure $(CONFIGURE_ARGS)
  151. $(NETTLE_BUILD)/libnettle.a: $(NETTLE_BUILD)/Makefile
  152. $(MAKE) -C $(NETTLE_BUILD) SUBDIRS=
  153. $(OC_SYSROOT)/lib/libnettle.a: $(NETTLE_BUILD)/libnettle.a
  154. $(MAKEINSTALL) -C $(NETTLE_BUILD) SUBDIRS= install
  155. NETTLE_DEPS := $(OC_SYSROOT)/lib/libnettle.a
  156. nettle: $(NETTLE_DEPS)
  157. #####################################################################
  158. #
  159. # Build GnuTLS
  160. #
  161. # https://www.gnutls.org/download.html
  162. GNUTLS_VER := 3.6.16
  163. GNUTLS_TAR := gnutls-$(GNUTLS_VER).tar.xz
  164. GNUTLS_SHA := 1b79b381ac283d8b054368b335c408fedcb9b7144e0c07f531e3537d4328f3b3
  165. GNUTLS_SRC := sources/gnutls-$(GNUTLS_VER)
  166. GNUTLS_BUILD := $(TRIPLET)/gnutls
  167. $(GNUTLS_TAR):
  168. $(FETCH) $@ $(GNUTLS_SHA)
  169. $(GNUTLS_SRC)/configure: $(GNUTLS_TAR)
  170. mkdir -p sources
  171. xz -d < $< | tar xf - -C sources
  172. touch $@
  173. #$(GNUTLS_SRC)/configure.ac:
  174. # mkdir -p sources
  175. # cd sources && git clone git://gitorious.org/gnutls/gnutls.git
  176. #$(GNUTLS_SRC)/configure: $(GNUTLS_SRC)/configure.ac
  177. # touch $(GNUTLS_SRC)/ChangeLog
  178. # cd $(GNUTLS_SRC) && autoreconf -fvi
  179. $(GNUTLS_BUILD)/Makefile: $(TOOLCHAIN_BUILT) $(GNUTLS_SRC)/configure $(NETTLE_DEPS)
  180. mkdir -p $(GNUTLS_BUILD)
  181. cd $(GNUTLS_BUILD) && ../../$(GNUTLS_SRC)/configure $(CONFIGURE_ARGS) \
  182. AUTOGEN=/bin/false \
  183. --disable-threads --disable-tests --disable-nls \
  184. --disable-doc --disable-openssl-compatibility --disable-cxx \
  185. --disable-openssl-compatibility --disable-ocsp --disable-tools \
  186. --disable-anon-authentication --with-included-libtasn1 \
  187. --enable-psk-authentication --disable-srp-authentication \
  188. --disable-dtls-srtp-support --enable-dhe --enable-ecdhe \
  189. --with-included-unistring --without-p11-kit --disable-guile
  190. $(GNUTLS_BUILD)/lib/libgnutls.la: $(GNUTLS_BUILD)/Makefile
  191. $(MAKE) -C $(GNUTLS_BUILD)
  192. $(OC_SYSROOT)/lib/libgnutls.la: $(GNUTLS_BUILD)/lib/libgnutls.la
  193. $(MAKEINSTALL) -C $(GNUTLS_BUILD) install
  194. GNUTLS_DEPS := $(OC_SYSROOT)/lib/libgnutls.la
  195. gnutls: $(GNUTLS_DEPS)
  196. #####################################################################
  197. #
  198. # Build libstoken
  199. #
  200. # https://sourceforge.net/projects/stoken/files/
  201. STOKEN_VER := 0.92
  202. STOKEN_TAR := stoken-$(STOKEN_VER).tar.gz
  203. STOKEN_SHA := aa2b481b058e4caf068f7e747a2dcf5772bcbf278a4f89bc9efcbf82bcc9ef5a
  204. STOKEN_SRC := sources/stoken-$(STOKEN_VER)
  205. STOKEN_BUILD := $(TRIPLET)/stoken
  206. $(STOKEN_TAR):
  207. $(FETCH) $@ $(STOKEN_SHA)
  208. $(STOKEN_SRC)/configure: $(STOKEN_TAR)
  209. mkdir -p sources
  210. tar xfz $< -C sources
  211. touch $@
  212. $(STOKEN_BUILD)/Makefile: $(TOOLCHAIN_BUILT) $(STOKEN_SRC)/configure $(NETTLE_DEPS)
  213. mkdir -p $(STOKEN_BUILD)
  214. cd $(STOKEN_BUILD) && ../../$(STOKEN_SRC)/configure $(CONFIGURE_ARGS) \
  215. --without-gtk
  216. $(STOKEN_BUILD)/libstoken.la: $(STOKEN_BUILD)/Makefile
  217. $(MAKE) -C $(STOKEN_BUILD)
  218. $(OC_SYSROOT)/lib/libstoken.la: $(STOKEN_BUILD)/libstoken.la
  219. $(MAKEINSTALL) -C $(STOKEN_BUILD) install
  220. STOKEN_DEPS := $(OC_SYSROOT)/lib/libstoken.la
  221. stoken: $(STOKEN_DEPS)
  222. #####################################################################
  223. #
  224. # Build liboath
  225. #
  226. # https://download.savannah.nongnu.org/releases/oath-toolkit/
  227. OATH_VER := 2.6.7
  228. OATH_TAR := oath-toolkit-$(OATH_VER).tar.gz
  229. OATH_SHA := 36eddfce8f2f36347fb257dbf878ba0303a2eaafe24eaa071d5cd302261046a9
  230. OATH_SRC := sources/oath-toolkit-$(OATH_VER)
  231. OATH_BUILD := $(TRIPLET)/oath
  232. $(OATH_TAR):
  233. $(FETCH) $@ $(OATH_SHA)
  234. $(OATH_SRC)/configure: $(OATH_TAR)
  235. mkdir -p sources
  236. tar xfz $< -C sources
  237. > $(OATH_SRC)/liboath/gl/freading.c
  238. touch $@
  239. $(OATH_BUILD)/Makefile: $(TOOLCHAIN_BUILT) $(OATH_SRC)/configure
  240. mkdir -p $(OATH_BUILD)
  241. cd $(OATH_BUILD) && ../../$(OATH_SRC)/configure $(CONFIGURE_ARGS) \
  242. --disable-pskc --disable-pam \
  243. gl_cv_func_fflush_stdin=yes \
  244. gl_cv_func_fpurge_works=yes
  245. $(OATH_BUILD)/liboath/liboath.la: $(OATH_BUILD)/Makefile
  246. $(MAKE) -C $(OATH_BUILD)/liboath
  247. $(OC_SYSROOT)/lib/liboath.la: $(OATH_BUILD)/liboath/liboath.la
  248. $(MAKEINSTALL) -C $(OATH_BUILD)/liboath install
  249. OATH_DEPS := $(OC_SYSROOT)/lib/liboath.la
  250. oath: $(OATH_DEPS)
  251. #####################################################################
  252. #
  253. # Build liblz4
  254. #
  255. # https://github.com/lz4/lz4/tags
  256. LZ4_VER := 1.9.3
  257. LZ4_TAR := lz4-v$(LZ4_VER).tar.gz
  258. LZ4_SHA := 030644df4611007ff7dc962d981f390361e6c97a34e5cbc393ddfbe019ffe2c1
  259. LZ4_DIR := $(TRIPLET)/lz4-$(LZ4_VER)
  260. $(LZ4_TAR):
  261. $(FETCH) $@ $(LZ4_SHA)
  262. $(LZ4_DIR)/Makefile: $(LZ4_TAR)
  263. mkdir -p $(TRIPLET)
  264. tar xzf $< -C $(TRIPLET)
  265. touch $@
  266. $(OC_SYSROOT)/lib/liblz4.a: $(TOOLCHAIN_BUILT) $(LZ4_DIR)/Makefile
  267. $(MAKE) -C $(LZ4_DIR)/lib \
  268. CC="$(TRIPLET)-clang $(EXTRA_CFLAGS)" \
  269. AR="$(TRIPLET)-ar" \
  270. LIBDIR=$(OC_SYSROOT)/lib \
  271. INCLUDEDIR=$(OC_SYSROOT)/include \
  272. install
  273. rm -f $(OC_SYSROOT)/lib/liblz4.so*
  274. LZ4_DEPS := $(OC_SYSROOT)/lib/liblz4.a
  275. lz4: $(LZ4_DEPS)
  276. #####################################################################
  277. #
  278. # Build OpenConnect for Android
  279. #
  280. OPENCONNECT_SRC := ..
  281. OPENCONNECT_BUILD := $(TRIPLET)/openconnect
  282. $(OPENCONNECT_SRC)/configure:
  283. cd $(OPENCONNECT_SRC) && ./autogen.sh
  284. $(OPENCONNECT_BUILD)/Makefile: $(TOOLCHAIN_BUILT) $(GNUTLS_DEPS) $(LIBXML_DEPS) \
  285. $(STOKEN_DEPS) $(OATH_DEPS) $(LZ4_DEPS) $(OPENCONNECT_SRC)/configure
  286. mkdir -p $(OPENCONNECT_BUILD)
  287. cd $(OPENCONNECT_BUILD) && ../../../configure \
  288. --host=$(TRIPLET) --prefix=/ \
  289. CFLAGS="$(EXTRA_CFLAGS) -fvisibility=default -fPIE" \
  290. LDFLAGS="$(EXTRA_LDFLAGS) -rdynamic -pie" \
  291. GNUTLS_LIBS="$(shell $(PKG_CONFIG) --static --libs gnutls)" \
  292. LIBSTOKEN_LIBS="$(shell $(PKG_CONFIG) --static --libs stoken)" \
  293. --enable-shared --with-vpnc-script=/etc/vpnc/vpnc-script \
  294. --with-java=$(OC_SYSROOT)/include --enable-jni-standalone \
  295. --disable-symvers
  296. openconnect: $(OPENCONNECT_BUILD)/Makefile
  297. make -C $(OPENCONNECT_BUILD)
  298. make -C $(OPENCONNECT_BUILD) install-strip DESTDIR=$(DESTDIR)
  299. #####################################################################
  300. #
  301. # Build run_pie helper program
  302. #
  303. $(DESTDIR)/sbin/run_pie: run_pie.c $(TOOLCHAIN_BUILT)
  304. mkdir -p $(DESTDIR)/sbin
  305. $(TRIPLET)-clang $< -o $@ -ldl
  306. .PHONY: run_pie
  307. run_pie: $(DESTDIR)/sbin/run_pie
  308. #####################################################################
  309. #
  310. # Special targets for maintainer use
  311. #
  312. # download + extract, but do not build
  313. .PHONY: sources
  314. sources: $(SOURCE_LIST)
  315. .PHONY: $(MIRROR_TEST_TARGETS)
  316. $(MIRROR_TEST_TARGETS) : mirror-test-% :
  317. $(FETCH) --mirror-test $($(*)_TAR) $($(*)_SHA)
  318. # (re)test all mirrors for all packages. safe for use with "make -jN"
  319. .PHONY: mirror-test
  320. mirror-test: $(MIRROR_TEST_TARGETS)