A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
12 KiB

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