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