15-android.conf 10 KB

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