15-android.conf 12 KB

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