15-android.conf 11 KB

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