15-android.conf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #### Android...
  2. #
  3. # See NOTES.ANDROID 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 => "-pie",
  154. enable => [ ],
  155. },
  156. "android-arm" => {
  157. ################################################################
  158. # Contemporary Android applications can provide multiple JNI
  159. # providers in .apk, targeting multiple architectures. Among
  160. # them there is "place" for two ARM flavours: generic eabi and
  161. # armv7-a/hard-float. However, it should be noted that OpenSSL's
  162. # ability to engage NEON is not constrained by ABI choice, nor
  163. # is your ability to call OpenSSL from your application code
  164. # compiled with floating-point ABI other than default 'soft'.
  165. # (Latter thanks to __attribute__((pcs("aapcs"))) declaration.)
  166. # This means that choice of ARM libraries you provide in .apk
  167. # is driven by application needs. For example if application
  168. # itself benefits from NEON or is floating-point intensive, then
  169. # it might be appropriate to provide both libraries. Otherwise
  170. # just generic eabi would do. But in latter case it would be
  171. # appropriate to
  172. #
  173. # ./Configure android-arm -D__ARM_MAX_ARCH__=8
  174. #
  175. # in order to build "universal" binary and allow OpenSSL take
  176. # advantage of NEON when it's available.
  177. #
  178. # Keep in mind that (just like with linux-armv4) we rely on
  179. # compiler defaults, which is not necessarily what you had
  180. # in mind, in which case you would have to pass additional
  181. # -march and/or -mfloat-abi flags. NDK defaults to armv5te.
  182. # Newer NDK versions reportedly require additional -latomic.
  183. #
  184. inherit_from => [ "android" ],
  185. bn_ops => add("RC4_CHAR"),
  186. asm_arch => 'armv4',
  187. perlasm_scheme => "void",
  188. },
  189. "android-arm64" => {
  190. inherit_from => [ "android" ],
  191. bn_ops => add("RC4_CHAR"),
  192. asm_arch => 'aarch64',
  193. perlasm_scheme => "linux64",
  194. },
  195. "android-mips" => {
  196. inherit_from => [ "android" ],
  197. bn_ops => add("RC4_CHAR"),
  198. asm_arch => 'mips32',
  199. perlasm_scheme => "o32",
  200. },
  201. "android-mips64" => {
  202. ################################################################
  203. # You are more than likely have to specify target processor
  204. # on ./Configure command line. Trouble is that toolchain's
  205. # default is MIPS64r6 (at least in r10d), but there are no
  206. # such processors around (or they are too rare to spot one).
  207. # Actual problem is that MIPS64r6 is binary incompatible
  208. # with previous MIPS ISA versions, in sense that unlike
  209. # prior versions original MIPS binary code will fail.
  210. #
  211. inherit_from => [ "android" ],
  212. bn_ops => add("RC4_CHAR"),
  213. asm_arch => 'mips64',
  214. perlasm_scheme => "64",
  215. },
  216. "android-x86" => {
  217. inherit_from => [ "android" ],
  218. CFLAGS => add(picker(release => "-fomit-frame-pointer")),
  219. bn_ops => add("RC4_INT"),
  220. asm_arch => 'x86',
  221. perlasm_scheme => "android",
  222. },
  223. "android-x86_64" => {
  224. inherit_from => [ "android" ],
  225. bn_ops => add("RC4_INT"),
  226. asm_arch => 'x86_64',
  227. perlasm_scheme => "elf",
  228. },
  229. ####################################################################
  230. # Backward compatible targets, (might) require $CROSS_SYSROOT
  231. #
  232. "android-armeabi" => {
  233. inherit_from => [ "android-arm" ],
  234. },
  235. "android64" => {
  236. inherit_from => [ "android" ],
  237. },
  238. "android64-aarch64" => {
  239. inherit_from => [ "android-arm64" ],
  240. },
  241. "android64-x86_64" => {
  242. inherit_from => [ "android-x86_64" ],
  243. },
  244. "android64-mips64" => {
  245. inherit_from => [ "android-mips64" ],
  246. },
  247. );