Skip to content

Commit 9dca486

Browse files
QuadTrianglemarceloneil
authored andcommitted
add library bootstrap
1 parent acf89a4 commit 9dca486

File tree

7 files changed

+630
-2
lines changed

7 files changed

+630
-2
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from pythonforandroid.toolchain import (
2+
Bootstrap, shprint, current_directory, info, info_main)
3+
from pythonforandroid.util import ensure_dir
4+
from os.path import join, exists, curdir, abspath
5+
from os import walk
6+
import glob
7+
import sh
8+
9+
10+
EXCLUDE_EXTS = (".py", ".pyc", ".so.o", ".so.a", ".so.libs", ".pyx")
11+
12+
13+
class EmptyBootstrap(Bootstrap):
14+
name = 'empty'
15+
16+
recipe_depends = []
17+
18+
can_be_chosen_automatically = False
19+
20+
def run_distribute(self):
21+
info_main("# Creating Android project ({})".format(self.name))
22+
23+
arch = self.ctx.archs[0]
24+
python_install_dir = self.ctx.get_python_install_dir()
25+
from_crystax = self.ctx.python_recipe.from_crystax
26+
crystax_python_dir = join("crystax_python", "crystax_python")
27+
28+
if len(self.ctx.archs) > 1:
29+
raise ValueError("SDL2/gradle support only one arch")
30+
31+
info("Copying SDL2/gradle build for {}".format(arch))
32+
shprint(sh.rm, "-rf", self.dist_dir)
33+
shprint(sh.cp, "-r", self.build_dir, self.dist_dir)
34+
35+
# either the build use environemnt variable (ANDROID_HOME)
36+
# or the local.properties if exists
37+
with current_directory(self.dist_dir):
38+
with open('local.properties', 'w') as fileh:
39+
fileh.write('sdk.dir={}'.format(self.ctx.sdk_dir))
40+
41+
with current_directory(self.dist_dir):
42+
info("Copying Python distribution")
43+
44+
if not exists("private") and not from_crystax:
45+
ensure_dir("private")
46+
if not exists("crystax_python") and from_crystax:
47+
ensure_dir(crystax_python_dir)
48+
49+
hostpython = sh.Command(self.ctx.hostpython)
50+
if not from_crystax:
51+
try:
52+
shprint(hostpython, '-OO', '-m', 'compileall',
53+
python_install_dir,
54+
_tail=10, _filterout="^Listing")
55+
except sh.ErrorReturnCode:
56+
pass
57+
if not exists('python-install'):
58+
shprint(
59+
sh.cp, '-a', python_install_dir, './python-install')
60+
61+
self.distribute_libs(arch, [self.ctx.get_libs_dir(arch.arch)])
62+
self.distribute_javaclasses(self.ctx.javaclass_dir,
63+
dest_dir=join("src", "main", "java"))
64+
65+
if not from_crystax:
66+
info("Filling private directory")
67+
if not exists(join("private", "lib")):
68+
info("private/lib does not exist, making")
69+
shprint(sh.cp, "-a",
70+
join("python-install", "lib"), "private")
71+
shprint(sh.mkdir, "-p",
72+
join("private", "include", "python2.7"))
73+
74+
libpymodules_fn = join("libs", arch.arch, "libpymodules.so")
75+
if exists(libpymodules_fn):
76+
shprint(sh.mv, libpymodules_fn, 'private/')
77+
shprint(sh.cp,
78+
join('python-install', 'include',
79+
'python2.7', 'pyconfig.h'),
80+
join('private', 'include', 'python2.7/'))
81+
82+
info('Removing some unwanted files')
83+
shprint(sh.rm, '-f', join('private', 'lib', 'libpython2.7.so'))
84+
shprint(sh.rm, '-rf', join('private', 'lib', 'pkgconfig'))
85+
86+
libdir = join(self.dist_dir, 'private', 'lib', 'python2.7')
87+
site_packages_dir = join(libdir, 'site-packages')
88+
with current_directory(libdir):
89+
removes = []
90+
for dirname, root, filenames in walk("."):
91+
for filename in filenames:
92+
for suffix in EXCLUDE_EXTS:
93+
if filename.endswith(suffix):
94+
removes.append(filename)
95+
shprint(sh.rm, '-f', *removes)
96+
97+
info('Deleting some other stuff not used on android')
98+
# To quote the original distribute.sh, 'well...'
99+
shprint(sh.rm, '-rf', 'lib2to3')
100+
shprint(sh.rm, '-rf', 'idlelib')
101+
for filename in glob.glob('config/libpython*.a'):
102+
shprint(sh.rm, '-f', filename)
103+
shprint(sh.rm, '-rf', 'config/python.o')
104+
105+
else: # Python *is* loaded from crystax
106+
ndk_dir = self.ctx.ndk_dir
107+
py_recipe = self.ctx.python_recipe
108+
python_dir = join(ndk_dir, 'sources', 'python',
109+
py_recipe.version, 'libs', arch.arch)
110+
shprint(sh.cp, '-r', join(python_dir,
111+
'stdlib.zip'), crystax_python_dir)
112+
shprint(sh.cp, '-r', join(python_dir,
113+
'modules'), crystax_python_dir)
114+
shprint(sh.cp, '-r', self.ctx.get_python_install_dir(),
115+
join(crystax_python_dir, 'site-packages'))
116+
117+
info('Renaming .so files to reflect cross-compile')
118+
site_packages_dir = join(crystax_python_dir, "site-packages")
119+
find_ret = shprint(
120+
sh.find, site_packages_dir, '-iname', '*.so')
121+
filenames = find_ret.stdout.decode('utf-8').split('\n')[:-1]
122+
for filename in filenames:
123+
parts = filename.split('.')
124+
if len(parts) <= 2:
125+
continue
126+
shprint(sh.mv, filename, filename.split('.')[0] + '.so')
127+
site_packages_dir = join(abspath(curdir),
128+
site_packages_dir)
129+
if 'sqlite3' not in self.ctx.recipe_build_order:
130+
with open('blacklist.txt', 'a') as fileh:
131+
fileh.write('\nsqlite3/*\nlib-dynload/_sqlite3.so\n')
132+
133+
self.strip_libraries(arch)
134+
self.fry_eggs(site_packages_dir)
135+
super(EmptyBootstrap, self).run_distribute()
136+
137+
bootstrap = EmptyBootstrap()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# prevent user to include invalid extensions
2+
*.apk
3+
*.pxd
4+
5+
# eggs
6+
*.egg-info
7+
8+
# unit test
9+
unittest/*
10+
11+
# python config
12+
config/makesetup
13+
14+
# unused kivy files (platform specific)
15+
kivy/input/providers/wm_*
16+
kivy/input/providers/mactouch*
17+
kivy/input/providers/probesysfs*
18+
kivy/input/providers/mtdev*
19+
kivy/input/providers/hidinput*
20+
kivy/core/camera/camera_videocapture*
21+
kivy/core/spelling/*osx*
22+
kivy/core/video/video_pyglet*
23+
kivy/tools
24+
kivy/tests/*
25+
kivy/*/*.h
26+
kivy/*/*.pxi
27+
28+
# unused encodings
29+
lib-dynload/*codec*
30+
encodings/cp*.pyo
31+
encodings/tis*
32+
encodings/shift*
33+
encodings/bz2*
34+
encodings/iso*
35+
encodings/undefined*
36+
encodings/johab*
37+
encodings/p*
38+
encodings/m*
39+
encodings/euc*
40+
encodings/k*
41+
encodings/unicode_internal*
42+
encodings/quo*
43+
encodings/gb*
44+
encodings/big5*
45+
encodings/hp*
46+
encodings/hz*
47+
48+
# unused python modules
49+
bsddb/*
50+
wsgiref/*
51+
hotshot/*
52+
pydoc_data/*
53+
tty.pyo
54+
anydbm.pyo
55+
nturl2path.pyo
56+
LICENCE.txt
57+
macurl2path.pyo
58+
dummy_threading.pyo
59+
audiodev.pyo
60+
antigravity.pyo
61+
dumbdbm.pyo
62+
sndhdr.pyo
63+
__phello__.foo.pyo
64+
sunaudio.pyo
65+
os2emxpath.pyo
66+
multiprocessing/dummy*
67+
68+
# unused binaries python modules
69+
lib-dynload/termios.so
70+
lib-dynload/_lsprof.so
71+
lib-dynload/*audioop.so
72+
lib-dynload/mmap.so
73+
lib-dynload/_hotshot.so
74+
lib-dynload/_heapq.so
75+
lib-dynload/_json.so
76+
lib-dynload/grp.so
77+
lib-dynload/resource.so
78+
lib-dynload/pyexpat.so
79+
lib-dynload/_ctypes_test.so
80+
lib-dynload/_testcapi.so
81+
82+
# odd files
83+
plat-linux3/regen

0 commit comments

Comments
 (0)