forked from kivy/python-for-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_archs.py
More file actions
419 lines (366 loc) · 15.5 KB
/
test_archs.py
File metadata and controls
419 lines (366 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import os
import unittest
from os import environ
from unittest import mock
from pythonforandroid.bootstrap import Bootstrap
from pythonforandroid.distribution import Distribution
from pythonforandroid.recipe import Recipe
from pythonforandroid.build import Context
from pythonforandroid.util import BuildInterruptingException, build_platform
from pythonforandroid.archs import (
Arch,
ArchARM,
ArchARMv7_a,
ArchAarch_64,
Archx86,
Archx86_64,
)
expected_env_gcc_keys = {
"CFLAGS",
"LDFLAGS",
"CXXFLAGS",
"TOOLCHAIN_PREFIX",
"TOOLCHAIN_VERSION",
"CC",
"CXX",
"AR",
"RANLIB",
"LD",
"LDSHARED",
"STRIP",
"MAKE",
"READELF",
"NM",
"BUILDLIB_PATH",
"PATH",
"ARCH",
"NDK_API",
}
class ArchSetUpBaseClass(object):
"""
An class object which is intended to be used as a base class to configure
an inherited class of `unittest.TestCase`. This class will override the
`setUp` method.
"""
ctx = None
expected_compiler = ""
TEST_ARCH = 'armeabi-v7a'
def setUp(self):
self.ctx = Context()
self.ctx.ndk_api = 21
self.ctx.android_api = 27
self.ctx._sdk_dir = "/opt/android/android-sdk"
self.ctx._ndk_dir = "/opt/android/android-ndk"
self.ctx.setup_dirs(os.getcwd())
self.ctx.bootstrap = Bootstrap().get_bootstrap("sdl2", self.ctx)
self.ctx.bootstrap.distribution = Distribution.get_distribution(
self.ctx,
name="sdl2",
recipes=["python3", "kivy"],
arch_name=self.TEST_ARCH,
)
self.ctx.python_recipe = Recipe.get_recipe("python3", self.ctx)
# Here we define the expected compiler, which, as per ndk >= r19,
# should be the same for all the tests (no more gcc compiler)
self.expected_compiler = (
f"/opt/android/android-ndk/toolchains/"
f"llvm/prebuilt/{build_platform}/bin/clang"
)
class TestArch(ArchSetUpBaseClass, unittest.TestCase):
"""
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
will be used to perform tests for the base class
:class:`~pythonforandroid.archs.Arch`.
"""
def test_arch(self):
arch = Arch(self.ctx)
self.assertEqual(arch.__str__(), arch.arch)
self.assertEqual(arch.target, "None21")
self.assertIsNone(arch.toolchain_prefix)
self.assertIsNone(arch.command_prefix)
self.assertIsInstance(arch.include_dirs, list)
class TestArchARM(ArchSetUpBaseClass, unittest.TestCase):
"""
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
will be used to perform tests for :class:`~pythonforandroid.archs.ArchARM`.
"""
@mock.patch("pythonforandroid.archs.glob")
@mock.patch("pythonforandroid.archs.find_executable")
@mock.patch("pythonforandroid.build.ensure_dir")
def test_arch_arm(self, mock_ensure_dir, mock_find_executable, mock_glob):
"""
Test that class :class:`~pythonforandroid.archs.ArchARM` returns some
expected attributes and environment variables.
.. note::
Here we mock two methods:
- `ensure_dir` because we don't want to create any directory
- `find_executable` because otherwise we will
get an error when trying to find the compiler (we are setting
some fake paths for our android sdk and ndk so probably will
not exist)
"""
mock_find_executable.return_value = self.expected_compiler
mock_ensure_dir.return_value = True
mock_glob.return_value = ["llvm"]
arch = ArchARM(self.ctx)
self.assertEqual(arch.arch, "armeabi")
self.assertEqual(arch.__str__(), "armeabi")
self.assertEqual(arch.toolchain_prefix, "arm-linux-androideabi")
self.assertEqual(arch.command_prefix, "arm-linux-androideabi")
self.assertEqual(arch.target, "armv7a-linux-androideabi21")
self.assertEqual(arch.platform_dir, "arch-arm")
arch = ArchARM(self.ctx)
# Check environment flags
env = arch.get_env()
self.assertIsInstance(env, dict)
self.assertEqual(
expected_env_gcc_keys, set(env.keys()) & expected_env_gcc_keys
)
# check glob and find_executable calls
self.assertEqual(mock_glob.call_count, 4)
for glob_call, kw in mock_glob.call_args_list:
self.assertEqual(
glob_call[0],
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir),
)
mock_find_executable.assert_called_once_with(
self.expected_compiler, path=environ["PATH"]
)
# check gcc compilers
self.assertEqual(env["CC"].split()[0], self.expected_compiler)
self.assertEqual(env["CXX"].split()[0], self.expected_compiler + "++")
# check android binaries
self.assertEqual(env["AR"], "arm-linux-androideabi-ar")
self.assertEqual(env["LD"], "arm-linux-androideabi-ld")
self.assertEqual(env["RANLIB"], "arm-linux-androideabi-ranlib")
self.assertEqual(
env["STRIP"].split()[0], "arm-linux-androideabi-strip"
)
self.assertEqual(
env["READELF"].split()[0], "arm-linux-androideabi-readelf"
)
self.assertEqual(env["NM"].split()[0], "arm-linux-androideabi-nm")
# check that cflags are in gcc
self.assertIn(env["CFLAGS"], env["CC"])
# check that flags aren't in gcc and also check ccache
self.ctx.ccache = "/usr/bin/ccache"
env = arch.get_env(with_flags_in_cc=False)
self.assertNotIn(env["CFLAGS"], env["CC"])
self.assertEqual(env["USE_CCACHE"], "1")
self.assertEqual(env["NDK_CCACHE"], "/usr/bin/ccache")
# Check exception in case that CC is not found
mock_find_executable.return_value = None
with self.assertRaises(BuildInterruptingException) as e:
arch.get_env()
self.assertEqual(
e.exception.args[0],
"Couldn't find executable for CC. This indicates a problem "
"locating the {expected_compiler} executable in the Android "
"NDK, not that you don't have a normal compiler installed. "
"Exiting.".format(expected_compiler=self.expected_compiler),
)
class TestArchARMv7a(ArchSetUpBaseClass, unittest.TestCase):
"""
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
will be used to perform tests for
:class:`~pythonforandroid.archs.ArchARMv7_a`.
"""
@mock.patch("pythonforandroid.archs.glob")
@mock.patch("pythonforandroid.archs.find_executable")
@mock.patch("pythonforandroid.build.ensure_dir")
def test_arch_armv7a(
self, mock_ensure_dir, mock_find_executable, mock_glob
):
"""
Test that class :class:`~pythonforandroid.archs.ArchARMv7_a` returns
some expected attributes and environment variables.
.. note::
Here we mock the same functions than
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
the glob result is the expected even if the folder doesn't exist,
which is probably the case. This has to be done because here we
tests the `get_env` with clang
"""
mock_find_executable.return_value = self.expected_compiler
mock_ensure_dir.return_value = True
mock_glob.return_value = ["llvm"]
arch = ArchARMv7_a(self.ctx)
self.assertEqual(arch.arch, "armeabi-v7a")
self.assertEqual(arch.__str__(), "armeabi-v7a")
self.assertEqual(arch.toolchain_prefix, "arm-linux-androideabi")
self.assertEqual(arch.command_prefix, "arm-linux-androideabi")
self.assertEqual(arch.target, "armv7a-linux-androideabi21")
self.assertEqual(arch.platform_dir, "arch-arm")
env = arch.get_env()
# check glob and find_executable calls
self.assertEqual(mock_glob.call_count, 4)
for glob_call, kw in mock_glob.call_args_list:
self.assertEqual(
glob_call[0],
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir),
)
mock_find_executable.assert_called_once_with(
self.expected_compiler, path=environ["PATH"]
)
# check clang
build_platform = "{system}-{machine}".format(
system=os.uname()[0], machine=os.uname()[-1]
).lower()
self.assertEqual(
env["CC"].split()[0],
"{ndk_dir}/toolchains/llvm/prebuilt/"
"{build_platform}/bin/clang".format(
ndk_dir=self.ctx._ndk_dir, build_platform=build_platform
),
)
self.assertEqual(
env["CXX"].split()[0],
"{ndk_dir}/toolchains/llvm/prebuilt/"
"{build_platform}/bin/clang++".format(
ndk_dir=self.ctx._ndk_dir, build_platform=build_platform
),
)
# For armeabi-v7a we expect some extra cflags
self.assertIn(
" -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -mthumb",
env["CFLAGS"],
)
class TestArchX86(ArchSetUpBaseClass, unittest.TestCase):
"""
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
will be used to perform tests for :class:`~pythonforandroid.archs.Archx86`.
"""
@mock.patch("pythonforandroid.archs.glob")
@mock.patch("pythonforandroid.archs.find_executable")
@mock.patch("pythonforandroid.build.ensure_dir")
def test_arch_x86(self, mock_ensure_dir, mock_find_executable, mock_glob):
"""
Test that class :class:`~pythonforandroid.archs.Archx86` returns
some expected attributes and environment variables.
.. note::
Here we mock the same functions than
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
the glob result is the expected even if the folder doesn't exist,
which is probably the case. This has to be done because here we
tests the `get_env` with clang
"""
mock_find_executable.return_value = self.expected_compiler
mock_ensure_dir.return_value = True
mock_glob.return_value = ["llvm"]
arch = Archx86(self.ctx)
self.assertEqual(arch.arch, "x86")
self.assertEqual(arch.__str__(), "x86")
self.assertEqual(arch.toolchain_prefix, "x86")
self.assertEqual(arch.command_prefix, "i686-linux-android")
self.assertEqual(arch.target, "i686-linux-android21")
self.assertEqual(arch.platform_dir, "arch-x86")
env = arch.get_env()
# check glob and find_executable calls
self.assertEqual(mock_glob.call_count, 4)
for glob_call, kw in mock_glob.call_args_list:
self.assertEqual(
glob_call[0],
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir),
)
mock_find_executable.assert_called_once_with(
self.expected_compiler, path=environ["PATH"]
)
# For x86 we expect some extra cflags in our `environment`
self.assertIn(
" -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32",
env["CFLAGS"],
)
class TestArchX86_64(ArchSetUpBaseClass, unittest.TestCase):
"""
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
will be used to perform tests for
:class:`~pythonforandroid.archs.Archx86_64`.
"""
@mock.patch("pythonforandroid.archs.glob")
@mock.patch("pythonforandroid.archs.find_executable")
@mock.patch("pythonforandroid.build.ensure_dir")
def test_arch_x86_64(
self, mock_ensure_dir, mock_find_executable, mock_glob
):
"""
Test that class :class:`~pythonforandroid.archs.Archx86_64` returns
some expected attributes and environment variables.
.. note::
Here we mock the same functions than
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
the glob result is the expected even if the folder doesn't exist,
which is probably the case. This has to be done because here we
tests the `get_env` with clang
"""
mock_find_executable.return_value = self.expected_compiler
mock_ensure_dir.return_value = True
mock_glob.return_value = ["llvm"]
arch = Archx86_64(self.ctx)
self.assertEqual(arch.arch, "x86_64")
self.assertEqual(arch.__str__(), "x86_64")
self.assertEqual(arch.toolchain_prefix, "x86_64")
self.assertEqual(arch.command_prefix, "x86_64-linux-android")
self.assertEqual(arch.target, "x86_64-linux-android21")
self.assertEqual(arch.platform_dir, "arch-x86_64")
env = arch.get_env()
# check glob and find_executable calls
self.assertEqual(mock_glob.call_count, 4)
for glob_call, kw in mock_glob.call_args_list:
self.assertEqual(
glob_call[0],
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir),
)
mock_find_executable.assert_called_once_with(
self.expected_compiler, path=environ["PATH"]
)
# For x86_64 we expect some extra cflags in our `environment`
mock_find_executable.assert_called_once()
self.assertIn(
" -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel", env["CFLAGS"]
)
class TestArchAArch64(ArchSetUpBaseClass, unittest.TestCase):
"""
An inherited class of `ArchSetUpBaseClass` and `unittest.TestCase` which
will be used to perform tests for
:class:`~pythonforandroid.archs.ArchAarch_64`.
"""
@mock.patch("pythonforandroid.archs.glob")
@mock.patch("pythonforandroid.archs.find_executable")
@mock.patch("pythonforandroid.build.ensure_dir")
def test_arch_aarch_64(
self, mock_ensure_dir, mock_find_executable, mock_glob
):
"""
Test that class :class:`~pythonforandroid.archs.ArchAarch_64` returns
some expected attributes and environment variables.
.. note::
Here we mock the same functions than
:meth:`TestArchARM.test_arch_arm` plus `glob`, so we make sure that
the glob result is the expected even if the folder doesn't exist,
which is probably the case. This has to be done because here we
tests the `get_env` with clang
"""
mock_find_executable.return_value = self.expected_compiler
mock_ensure_dir.return_value = True
mock_glob.return_value = ["llvm"]
arch = ArchAarch_64(self.ctx)
self.assertEqual(arch.arch, "arm64-v8a")
self.assertEqual(arch.__str__(), "arm64-v8a")
self.assertEqual(arch.toolchain_prefix, "aarch64-linux-android")
self.assertEqual(arch.command_prefix, "aarch64-linux-android")
self.assertEqual(arch.target, "aarch64-linux-android21")
self.assertEqual(arch.platform_dir, "arch-arm64")
env = arch.get_env()
# check glob and find_executable calls
self.assertEqual(mock_glob.call_count, 4)
for glob_call, kw in mock_glob.call_args_list:
self.assertEqual(
glob_call[0],
"{ndk_dir}/toolchains/llvm*".format(ndk_dir=self.ctx._ndk_dir),
)
mock_find_executable.assert_called_once_with(
self.expected_compiler, path=environ["PATH"]
)
# For x86_64 we expect to find an extra key in`environment`
for flag in {"CFLAGS", "CXXFLAGS", "CC", "CXX"}:
self.assertIn("-march=armv8-a", env[flag])