15-android.conf 12 KB

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