Skip to content

Commit d3a765c

Browse files
committed
Moved Arch classes to separate module
1 parent 3b631b1 commit d3a765c

File tree

2 files changed

+137
-136
lines changed

2 files changed

+137
-136
lines changed

pythonforandroid/archs.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
from os.path import (join)
2+
3+
class Arch(object):
4+
5+
toolchain_prefix = None
6+
'''The prefix for the toolchain dir in the NDK.'''
7+
8+
command_prefix = None
9+
'''The prefix for NDK commands such as gcc.'''
10+
11+
def __init__(self, ctx):
12+
super(Arch, self).__init__()
13+
self.ctx = ctx
14+
15+
def __str__(self):
16+
return self.arch
17+
18+
@property
19+
def include_dirs(self):
20+
return [
21+
"{}/{}".format(
22+
self.ctx.include_dir,
23+
d.format(arch=self))
24+
for d in self.ctx.include_dirs]
25+
26+
def get_env(self):
27+
include_dirs = [
28+
"-I{}/{}".format(
29+
self.ctx.include_dir,
30+
d.format(arch=self))
31+
for d in self.ctx.include_dirs]
32+
33+
env = {}
34+
35+
env["CFLAGS"] = " ".join([
36+
"-DANDROID", "-mandroid", "-fomit-frame-pointer",
37+
"--sysroot", self.ctx.ndk_platform])
38+
39+
env["CXXFLAGS"] = env["CFLAGS"]
40+
41+
env["LDFLAGS"] = " ".join(['-lm'])
42+
43+
py_platform = sys.platform
44+
if py_platform in ['linux2', 'linux3']:
45+
py_platform = 'linux'
46+
47+
toolchain_prefix = self.ctx.toolchain_prefix
48+
toolchain_version = self.ctx.toolchain_version
49+
50+
env['TOOLCHAIN_PREFIX'] = toolchain_prefix
51+
env['TOOLCHAIN_VERSION'] = toolchain_version
52+
53+
if toolchain_prefix == 'x86':
54+
toolchain_prefix = 'i686-linux-android'
55+
print('path is', environ['PATH'])
56+
cc = find_executable('{toolchain_prefix}-gcc'.format(
57+
toolchain_prefix=toolchain_prefix), path=environ['PATH'])
58+
if cc is None:
59+
warning('Couldn\'t find executable for CC. This indicates a '
60+
'problem locating the {} executable in the Android '
61+
'NDK, not that you don\'t have a normal compiler '
62+
'installed. Exiting.')
63+
exit(1)
64+
65+
env['CC'] = '{toolchain_prefix}-gcc {cflags}'.format(
66+
toolchain_prefix=toolchain_prefix,
67+
cflags=env['CFLAGS'])
68+
env['CXX'] = '{toolchain_prefix}-g++ {cxxflags}'.format(
69+
toolchain_prefix=toolchain_prefix,
70+
cxxflags=env['CXXFLAGS'])
71+
72+
env['AR'] = '{}-ar'.format(toolchain_prefix)
73+
env['RANLIB'] = '{}-ranlib'.format(toolchain_prefix)
74+
env['LD'] = '{}-ld'.format(toolchain_prefix)
75+
env['STRIP'] = '{}-strip --strip-unneeded'.format(toolchain_prefix)
76+
env['MAKE'] = 'make -j5'
77+
env['READELF'] = '{}-readelf'.format(toolchain_prefix)
78+
79+
hostpython_recipe = Recipe.get_recipe('hostpython2', self.ctx)
80+
81+
# AND: This hardcodes python version 2.7, needs fixing
82+
env['BUILDLIB_PATH'] = join(
83+
hostpython_recipe.get_build_dir(self.arch),
84+
'build', 'lib.linux-{}-2.7'.format(uname()[-1]))
85+
86+
env['PATH'] = environ['PATH']
87+
88+
env['ARCH'] = self.arch
89+
90+
return env
91+
92+
93+
class ArchARM(Arch):
94+
arch = "armeabi"
95+
toolchain_prefix = 'arm-linux-androideabi'
96+
command_prefix = 'arm-linux-androideabi'
97+
platform_dir = 'arch-arm'
98+
99+
100+
class ArchARMv7_a(ArchARM):
101+
arch = 'armeabi-v7a'
102+
103+
def get_env(self):
104+
env = super(ArchARMv7_a, self).get_env()
105+
env['CFLAGS'] = (env['CFLAGS'] +
106+
' -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -mthumb')
107+
env['CXXFLAGS'] = env['CFLAGS']
108+
return env
109+
110+
111+
class Archx86(Arch):
112+
arch = 'x86'
113+
toolchain_prefix = 'x86'
114+
command_prefix = 'i686-linux-android'
115+
platform_dir = 'arch-x86'
116+
117+
def get_env(self):
118+
env = super(Archx86, self).get_env()
119+
env['CFLAGS'] = (env['CFLAGS'] +
120+
' -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32')
121+
env['CXXFLAGS'] = env['CFLAGS']
122+
return env
123+
124+
125+
class Archx86_64(Arch):
126+
arch = 'x86_64'
127+
toolchain_prefix = 'x86'
128+
command_prefix = 'x86_64-linux-android'
129+
platform_dir = 'arch-x86'
130+
131+
def get_env(self):
132+
env = super(Archx86_64, self).get_env()
133+
env['CFLAGS'] = (env['CFLAGS'] +
134+
' -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel')
135+
env['CXXFLAGS'] = env['CFLAGS']
136+
return env

pythonforandroid/toolchain.py

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from colorama import Style as Colo_Style, Fore as Colo_Fore
4949
from collections import defaultdict
5050

51+
from archs import ArchARM, ArchARMv7_a, Archx86, Archx86_64
5152

5253
# monkey patch to show full output
5354
sh.ErrorReturnCode.truncate_cap = 999999
@@ -463,142 +464,6 @@ def sync(self):
463464
fd.write(unicode(json.dumps(self.data, ensure_ascii=False)))
464465

465466

466-
class Arch(object):
467-
468-
toolchain_prefix = None
469-
'''The prefix for the toolchain dir in the NDK.'''
470-
471-
command_prefix = None
472-
'''The prefix for NDK commands such as gcc.'''
473-
474-
def __init__(self, ctx):
475-
super(Arch, self).__init__()
476-
self.ctx = ctx
477-
478-
def __str__(self):
479-
return self.arch
480-
481-
@property
482-
def include_dirs(self):
483-
return [
484-
"{}/{}".format(
485-
self.ctx.include_dir,
486-
d.format(arch=self))
487-
for d in self.ctx.include_dirs]
488-
489-
def get_env(self):
490-
include_dirs = [
491-
"-I{}/{}".format(
492-
self.ctx.include_dir,
493-
d.format(arch=self))
494-
for d in self.ctx.include_dirs]
495-
496-
env = {}
497-
498-
env["CFLAGS"] = " ".join([
499-
"-DANDROID", "-mandroid", "-fomit-frame-pointer",
500-
"--sysroot", self.ctx.ndk_platform])
501-
502-
env["CXXFLAGS"] = env["CFLAGS"]
503-
504-
env["LDFLAGS"] = " ".join(['-lm'])
505-
506-
py_platform = sys.platform
507-
if py_platform in ['linux2', 'linux3']:
508-
py_platform = 'linux'
509-
510-
toolchain_prefix = self.ctx.toolchain_prefix
511-
toolchain_version = self.ctx.toolchain_version
512-
513-
env['TOOLCHAIN_PREFIX'] = toolchain_prefix
514-
env['TOOLCHAIN_VERSION'] = toolchain_version
515-
516-
if toolchain_prefix == 'x86':
517-
toolchain_prefix = 'i686-linux-android'
518-
print('path is', environ['PATH'])
519-
cc = find_executable('{toolchain_prefix}-gcc'.format(
520-
toolchain_prefix=toolchain_prefix), path=environ['PATH'])
521-
if cc is None:
522-
warning('Couldn\'t find executable for CC. This indicates a '
523-
'problem locating the {} executable in the Android '
524-
'NDK, not that you don\'t have a normal compiler '
525-
'installed. Exiting.')
526-
exit(1)
527-
528-
env['CC'] = '{toolchain_prefix}-gcc {cflags}'.format(
529-
toolchain_prefix=toolchain_prefix,
530-
cflags=env['CFLAGS'])
531-
env['CXX'] = '{toolchain_prefix}-g++ {cxxflags}'.format(
532-
toolchain_prefix=toolchain_prefix,
533-
cxxflags=env['CXXFLAGS'])
534-
535-
env['AR'] = '{}-ar'.format(toolchain_prefix)
536-
env['RANLIB'] = '{}-ranlib'.format(toolchain_prefix)
537-
env['LD'] = '{}-ld'.format(toolchain_prefix)
538-
env['STRIP'] = '{}-strip --strip-unneeded'.format(toolchain_prefix)
539-
env['MAKE'] = 'make -j5'
540-
env['READELF'] = '{}-readelf'.format(toolchain_prefix)
541-
542-
hostpython_recipe = Recipe.get_recipe('hostpython2', self.ctx)
543-
544-
# AND: This hardcodes python version 2.7, needs fixing
545-
env['BUILDLIB_PATH'] = join(
546-
hostpython_recipe.get_build_dir(self.arch),
547-
'build', 'lib.linux-{}-2.7'.format(uname()[-1]))
548-
549-
env['PATH'] = environ['PATH']
550-
551-
env['ARCH'] = self.arch
552-
553-
return env
554-
555-
556-
class ArchARM(Arch):
557-
arch = "armeabi"
558-
toolchain_prefix = 'arm-linux-androideabi'
559-
command_prefix = 'arm-linux-androideabi'
560-
platform_dir = 'arch-arm'
561-
562-
563-
class ArchARMv7_a(ArchARM):
564-
arch = 'armeabi-v7a'
565-
566-
def get_env(self):
567-
env = super(ArchARMv7_a, self).get_env()
568-
env['CFLAGS'] = (env['CFLAGS'] +
569-
' -march=armv7-a -mfloat-abi=softfp -mfpu=vfp -mthumb')
570-
env['CXXFLAGS'] = env['CFLAGS']
571-
return env
572-
573-
574-
class Archx86(Arch):
575-
arch = 'x86'
576-
toolchain_prefix = 'x86'
577-
command_prefix = 'i686-linux-android'
578-
platform_dir = 'arch-x86'
579-
580-
def get_env(self):
581-
env = super(Archx86, self).get_env()
582-
env['CFLAGS'] = (env['CFLAGS'] +
583-
' -march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32')
584-
env['CXXFLAGS'] = env['CFLAGS']
585-
return env
586-
587-
588-
class Archx86_64(Arch):
589-
arch = 'x86_64'
590-
toolchain_prefix = 'x86'
591-
command_prefix = 'x86_64-linux-android'
592-
platform_dir = 'arch-x86'
593-
594-
def get_env(self):
595-
env = super(Archx86_64, self).get_env()
596-
env['CFLAGS'] = (env['CFLAGS'] +
597-
' -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel')
598-
env['CXXFLAGS'] = env['CFLAGS']
599-
return env
600-
601-
602467
class Graph(object):
603468
# Taken from the old python-for-android/depsort
604469
# Modified to include alternative dependencies

0 commit comments

Comments
 (0)