15-android.conf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #### Android...
  2. #
  3. # See NOTES-Android.md for details, and don't miss platform-specific
  4. # comments below...
  5. {
  6. use File::Spec::Functions;
  7. my $android_ndk = {};
  8. my %triplet = (
  9. arm => "arm-linux-androideabi",
  10. arm64 => "aarch64-linux-android",
  11. mips => "mipsel-linux-android",
  12. mips64 => "mips64el-linux-android",
  13. x86 => "i686-linux-android",
  14. x86_64 => "x86_64-linux-android",
  15. );
  16. sub android_ndk {
  17. unless (%$android_ndk) {
  18. if ($now_printing =~ m|^android|) {
  19. return $android_ndk = { bn_ops => "BN_AUTO" };
  20. }
  21. my $ndk_var;
  22. my $ndk;
  23. foreach (qw(ANDROID_NDK_ROOT ANDROID_NDK)) {
  24. $ndk_var = $_;
  25. $ndk = $ENV{$ndk_var};
  26. last if defined $ndk;
  27. }
  28. die "\$ANDROID_NDK_ROOT is not defined" if (!$ndk);
  29. if (!-d "$ndk/platforms" && !-f "$ndk/AndroidVersion.txt") {
  30. # $ndk/platforms is traditional "all-inclusive" NDK, while
  31. # $ndk/AndroidVersion.txt is so-called standalone toolchain
  32. # tailored for specific target down to API level.
  33. die "\$ANDROID_NDK_ROOT=$ndk is invalid";
  34. }
  35. $ndk = canonpath($ndk);
  36. my $ndkver = undef;
  37. if (open my $fh, "<$ndk/source.properties") {
  38. local $_;
  39. while(<$fh>) {
  40. if (m|Pkg\.Revision\s*=\s*([0-9]+)|) {
  41. $ndkver = $1;
  42. last;
  43. }
  44. }
  45. close $fh;
  46. }
  47. my ($sysroot, $api, $arch);
  48. $config{target} =~ m|[^-]+-([^-]+)$|; # split on dash
  49. $arch = $1;
  50. if ($sysroot = $ENV{CROSS_SYSROOT}) {
  51. $sysroot =~ m|/android-([0-9]+)/arch-(\w+)/?$|;
  52. ($api, $arch) = ($1, $2);
  53. } elsif (-f "$ndk/AndroidVersion.txt") {
  54. $sysroot = "$ndk/sysroot";
  55. } else {
  56. $api = "*";
  57. # see if user passed -D__ANDROID_API__=N
  58. foreach (@{$useradd{CPPDEFINES}}, @{$user{CPPFLAGS}}) {
  59. if (m|__ANDROID_API__=([0-9]+)|) {
  60. $api = $1;
  61. last;
  62. }
  63. }
  64. # list available platforms (numerically)
  65. my @platforms = sort { $a =~ m/-([0-9]+)$/; my $aa = $1;
  66. $b =~ m/-([0-9]+)$/; $aa <=> $1;
  67. } glob("$ndk/platforms/android-$api");
  68. die "no $ndk/platforms/android-$api" if ($#platforms < 0);
  69. $sysroot = "@platforms[$#platforms]/arch-$arch";
  70. $sysroot =~ m|/android-([0-9]+)/arch-$arch|;
  71. $api = $1;
  72. }
  73. die "no sysroot=$sysroot" if (!-d $sysroot);
  74. my $triarch = $triplet{$arch};
  75. my $cflags;
  76. my $cppflags;
  77. # see if there is NDK clang on $PATH, "universal" or "standalone"
  78. if (which("clang") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
  79. my $host=$1;
  80. # harmonize with gcc default
  81. my $arm = $ndkver > 16 ? "armv7a" : "armv5te";
  82. (my $tridefault = $triarch) =~ s/^arm-/$arm-/;
  83. (my $tritools = $triarch) =~ s/(?:x|i6)86(_64)?-.*/x86$1/;
  84. $cflags .= " -target $tridefault "
  85. . "-gcc-toolchain \$($ndk_var)/toolchains"
  86. . "/$tritools-4.9/prebuilt/$host";
  87. $user{CC} = "clang" if ($user{CC} !~ m|clang|);
  88. $user{CROSS_COMPILE} = undef;
  89. if (which("llvm-ar") =~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
  90. $user{AR} = "llvm-ar";
  91. $user{ARFLAGS} = [ "rs" ];
  92. $user{RANLIB} = ":";
  93. }
  94. } elsif (-f "$ndk/AndroidVersion.txt") { #"standalone toolchain"
  95. my $cc = $user{CC} // "clang";
  96. # One can probably argue that both clang and gcc should be
  97. # probed, but support for "standalone toolchain" was added
  98. # *after* announcement that gcc is being phased out, so
  99. # favouring clang is considered adequate. Those who insist
  100. # have option to enforce test for gcc with CC=gcc.
  101. if (which("$triarch-$cc") !~ m|^$ndk|) {
  102. die "no NDK $triarch-$cc on \$PATH";
  103. }
  104. $user{CC} = $cc;
  105. $user{CROSS_COMPILE} = "$triarch-";
  106. } elsif ($user{CC} eq "clang") {
  107. die "no NDK clang on \$PATH";
  108. } else {
  109. if (which("$triarch-gcc") !~ m|^$ndk/.*/prebuilt/([^/]+)/|) {
  110. die "no NDK $triarch-gcc on \$PATH";
  111. }
  112. $cflags .= " -mandroid";
  113. $user{CROSS_COMPILE} = "$triarch-";
  114. }
  115. if (!-d "$sysroot/usr/include") {
  116. my $incroot = "$ndk/sysroot/usr/include";
  117. die "no $incroot" if (!-d $incroot);
  118. die "no $incroot/$triarch" if (!-d "$incroot/$triarch");
  119. $incroot =~ s|^$ndk/||;
  120. $cppflags = "-D__ANDROID_API__=$api";
  121. $cppflags .= " -isystem \$($ndk_var)/$incroot/$triarch";
  122. $cppflags .= " -isystem \$($ndk_var)/$incroot";
  123. }
  124. $sysroot =~ s|^$ndk/||;
  125. $android_ndk = {
  126. cflags => "$cflags --sysroot=\$($ndk_var)/$sysroot",
  127. cppflags => $cppflags,
  128. bn_ops => $arch =~ m/64$/ ? "SIXTY_FOUR_BIT_LONG"
  129. : "BN_LLONG",
  130. };
  131. }
  132. return $android_ndk;
  133. }
  134. }
  135. my %targets = (
  136. "android" => {
  137. inherit_from => [ "linux-generic32" ],
  138. template => 1,
  139. ################################################################
  140. # Special note about -pie. The underlying reason is that
  141. # Lollipop refuses to run non-PIE. But what about older systems
  142. # and NDKs? -fPIC was never problem, so the only concern is -pie.
  143. # Older toolchains, e.g. r4, appear to handle it and binaries
  144. # turn out mostly functional. "Mostly" means that oldest
  145. # Androids, such as Froyo, fail to handle executable, but newer
  146. # systems are perfectly capable of executing binaries targeting
  147. # Froyo. Keep in mind that in the nutshell Android builds are
  148. # about JNI, i.e. shared libraries, not applications.
  149. cflags => add(sub { android_ndk()->{cflags} }),
  150. cppflags => add(sub { android_ndk()->{cppflags} }),
  151. cxxflags => add(sub { android_ndk()->{cflags} }),
  152. bn_ops => sub { android_ndk()->{bn_ops} },
  153. bin_cflags => "-fPIE",
  154. bin_lflags => "-pie",
  155. enable => [ ],
  156. },
  157. "android-arm" => {
  158. ################################################################
  159. # Contemporary Android applications can provide multiple JNI
  160. # providers in .apk, targeting multiple architectures. Among
  161. # them there is "place" for two ARM flavours: generic eabi and
  162. # armv7-a/hard-float. However, it should be noted that OpenSSL's
  163. # ability to engage NEON is not constrained by ABI choice, nor
  164. # is your ability to call OpenSSL from your application code
  165. # compiled with floating-point ABI other than default 'soft'.
  166. # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
  167. # This means that choice of ARM libraries you provide in .apk
  168. # is driven by application needs. For example if application
  169. # itself benefits from NEON or is floating-point intensive, then
  170. # it might be appropriate to provide both libraries. Otherwise
  171. # just generic eabi would do. But in latter case it would be
  172. # appropriate to
  173. #
  174. # ./Configure android-arm -D__ARM_MAX_ARCH__=8
  175. #
  176. # in order to build "universal" binary and allow OpenSSL take
  177. # advantage of NEON when it's available.
  178. #
  179. # Keep in mind that (just like with linux-armv4) we rely on
  180. # compiler defaults, which is not necessarily what you had
  181. # in mind, in which case you would have to pass additional
  182. # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
  183. # Newer NDK versions reportedly require additional -latomic.
  184. #
  185. inherit_from => [ "android" ],
  186. bn_ops => add("RC4_CHAR"),
  187. asm_arch => 'armv4',
  188. perlasm_scheme => "void",
  189. },
  190. "android-arm64" => {
  191. inherit_from => [ "android" ],
  192. bn_ops => add("RC4_CHAR"),
  193. asm_arch => 'aarch64',
  194. perlasm_scheme => "linux64",
  195. },
  196. "android-mips" => {
  197. inherit_from => [ "android" ],
  198. bn_ops => add("RC4_CHAR"),
  199. asm_arch => 'mips32',
  200. perlasm_scheme => "o32",
  201. },
  202. "android-mips64" => {
  203. ################################################################
  204. # You are more than likely have to specify target processor
  205. # on ./Configure command line. Trouble is that toolchain's
  206. # default is MIPS64r6 (at least in r10d), but there are no
  207. # such processors around (or they are too rare to spot one).
  208. # Actual problem is that MIPS64r6 is binary incompatible
  209. # with previous MIPS ISA versions, in sense that unlike
  210. # prior versions original MIPS binary code will fail.
  211. #
  212. inherit_from => [ "android" ],
  213. bn_ops => add("RC4_CHAR"),
  214. asm_arch => 'mips64',
  215. perlasm_scheme => "64",
  216. },
  217. "android-x86" => {
  218. inherit_from => [ "android" ],
  219. CFLAGS => add(picker(release => "-fomit-frame-pointer")),
  220. bn_ops => add("RC4_INT"),
  221. asm_arch => 'x86',
  222. perlasm_scheme => "android",
  223. },
  224. "android-x86_64" => {
  225. inherit_from => [ "android" ],
  226. bn_ops => add("RC4_INT"),
  227. asm_arch => 'x86_64',
  228. perlasm_scheme => "elf",
  229. },
  230. ####################################################################
  231. # Backward compatible targets, (might) require $CROSS_SYSROOT
  232. #
  233. "android-armeabi" => {
  234. inherit_from => [ "android-arm" ],
  235. },
  236. "android64" => {
  237. inherit_from => [ "android" ],
  238. },
  239. "android64-aarch64" => {
  240. inherit_from => [ "android-arm64" ],
  241. },
  242. "android64-x86_64" => {
  243. inherit_from => [ "android-x86_64" ],
  244. },
  245. "android64-mips64" => {
  246. inherit_from => [ "android-mips64" ],
  247. },
  248. );