8000 Fixed sdl2 bootstrap to work with both py2 and py3 · kived/python-for-android@1ed06a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ed06a3

Browse files
committed
Fixed sdl2 bootstrap to work with both py2 and py3
1 parent 6ef2fe7 commit 1ed06a3

File tree

11 files changed

+98
-64
lines changed

11 files changed

+98
-64
lines changed

pythonforandroid/archs.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def get_env(self):
8484
env['AR'] = '{}-ar'.format(command_prefix)
8585
env['RANLIB'] = '{}-ranlib'.format(command_prefix)
8686
env['LD'] = '{}-ld'.format(command_prefix)
87-
# env['LDSHARED'] = join(self.ctx.root_dir, 'tools', 'liblink-jb')
88-
env['LDSHARED'] = env['LD']
87+
# env['LDSHARED'] = join(self.ctx.root_dir, 'tools', 'liblink')
88+
# env['LDSHARED'] = env['LD']
8989
env['STRIP'] = '{}-strip --strip-unneeded'.format(command_prefix)
9090
env['MAKE'] = 'make -j5'
9191
env['READELF'] = '{}-readelf'.format(command_prefix)
@@ -102,6 +102,9 @@ def get_env(self):
102102

103103
env['ARCH'] = self.arch
104104

105+
if self.ctx.ndk_is_crystax: # AND: should use the right python version from the python recipe
106+
env['CRYSTAX_PYTHON_VERSION'] = '3.5'
107+
105108
return env
106109

107110

pythonforandroid/bootstraps/sdl2/build/jni/src/Android.mk

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include
1212
LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
1313
start.c
1414

15-
# LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/include/python2.7
15+
LOCAL_CFLAGS += -I$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/include/python2.7
1616

1717
LOCAL_SHARED_LIBRARIES := SDL2 python_shared
1818

19-
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog #-lpython2.7
19+
LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog $(EXTRA_LDLIBS) #-lpython2.7
2020

21-
# LOCAL_LDFLAGS += -L$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/lib $(APPLICATION_ADDITIONAL_LDFLAGS)
21+
LOCAL_LDFLAGS += -L$(LOCAL_PATH)/../../../../other_builds/$(PYTHON2_NAME)/$(ARCH)/python2/python-install/lib $(APPLICATION_ADDITIONAL_LDFLAGS)
2222

2323
include $(BUILD_SHARED_LIBRARY)
2424

25-
$(call import-module,python/3.5)
25+
ifdef CRYSTAX_PYTHON_VERSION
26+
$(call import-module,python/$(CRYSTAX_PYTHON_VERSION))
27+
endif

pythonforandroid/bootstraps/sdl2/build/jni/src/start.c

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,26 @@ static PyMethodDef AndroidEmbedMethods[] = {
4444
{NULL, NULL, 0, NULL}
4545
};
4646

47-
static struct PyModuleDef androidembed =
48-
{
49-
PyModuleDef_HEAD_INIT,
50-
"androidembed",
51-
"",
52-
-1,
53-
AndroidEmbedMethods
54-
};
55-
56-
PyMODINIT_FUNC initandroidembed(void) {
57-
return PyModule_Create(&androidembed);
58-
/* (void) Py_InitModule("androidembed", AndroidEmbedMethods); */
59-
}
47+
48+
#if PY_MAJOR_VERSION >= 3
49+
static struct PyModuleDef androidembed =
50+
{
51+
PyModuleDef_HEAD_INIT,
52+
"androidembed",
53+
"",
54+
-1,
55+
AndroidEmbedMethods
56+
};
57+
58+
PyMODINIT_FUNC initandroidembed(void) {
59+
return PyModule_Create(&androidembed);
60+
/* (void) Py_InitModule("androidembed", AndroidEmbedMethods); */
61+
}
62+
#else
63+
PyMODINIT_FUNC initandroidembed(void) {
64+
(void) Py_InitModule("androidembed", AndroidEmbedMethods);
65+
}
66+
#endif
6067

6168
/* int dir_exists(char* filename) */
6269
/* /\* Function from http://stackoverflow.com/questions/12510874/how-can-i-check-if-a-directory-exists-on-linux-in-c# *\/ */
@@ -132,53 +139,33 @@ int main(int argc, char *argv[]) {
132139
LOG(env_argument);
133140
chdir(env_argument);
134141

135-
136142
Py_SetProgramName(L"android_python");
137143

144+
#if PY_MAJOR_VERSION >= 3
138145
/* our logging module for android
139146
*/
140147
PyImport_AppendInittab("androidembed", initandroidembed);
148+
#endif
141149

142150
LOG("Preparing to initialize python");
143151

144-
char errstr[256];
145-
snprintf(errstr, 256, "errno before is %d",
146-
errno);
147-
LOG(errstr);
148-
149-
if (dir_exists("crystax_python")) {
150-
LOG("exists without slash");
151-
}
152-
153-
snprintf(errstr, 256, "errno after is %d",
154-
errno);
155-
LOG(errstr);
156-
157-
if (dir_exists("../libs")) {
158-
LOG("libs exists");
159-
} else {
160-
LOG("libs does not exist");
161-
}
162-
163-
if (file_exists("main.py")) {
164-
LOG("The main.py does exist");
165-
}
166-
167-
if (file_exists("main.py") == 1) {
168-
LOG("The main.py does exist2");
169-
}
170-
171152
if (dir_exists("crystax_python/")) {
172-
/* if (1) { */
173153
LOG("crystax_python exists");
174154
char paths[256];
175155
snprintf(paths, 256, "%s/crystax_python/stdlib.zip:%s/crystax_python/modules", env_argument, env_argument);
176156
/* snprintf(paths, 256, "%s/stdlib.zip:%s/modules", env_argument, env_argument); */
177157
LOG("calculated paths to be...");
178158
LOG(paths);
179159

160+
#if PY_MAJOR_VERSION >= 3
180161
wchar_t* wchar_paths = Py_DecodeLocale(paths, NULL);
181162
Py_SetPath(wchar_paths);
163+
#else
164+
char* wchar_paths = paths;
165+
LOG("Can't Py_SetPath in python2, so crystax python2 doesn't work yet");
166+
exit(1);
167+
#endif
168+
182169
LOG("set wchar paths...");
183170
} else { LOG("crystax_python does not exist");}
184171

@@ -196,8 +183,11 @@ int main(int argc, char *argv[]) {
196183
PyEval_InitThreads();
197184

198185

186+
#if PY_MAJOR_VERSION < 3
187+
initandroidembed();
188+
#endif
189+
199190
PyRun_SimpleString("import androidembed\nandroidembed.log('testing python print redirection')");
200-
LOG("tried to run simple androidembed test");
201191

202192
/* inject our bootstrap code to redirect python stdin/stdout
203193
* replace sys.path with our path
@@ -215,7 +205,6 @@ int main(int argc, char *argv[]) {
215205
" private + '/lib/python2.7/site-packages/', \n" \
216206
" argument ]\n");
217207
} else {
218-
219208
char add_site_packages_dir[256];
220209
snprintf(add_site_packages_dir, 256, "sys.path.append('%s/crystax_python/site-packages')",
221210
env_argument);
@@ -275,6 +264,7 @@ int main(int argc, char *argv[]) {
275264
/* "print 'Android kivy bootstrap done. __name__ is', __name__"); */
276265

277266
LOG("AND: Ran string");
267+
278268
/* run it !
279269
*/
280270
LOG("Run user program, change dir and execute main.py");

pythonforandroid/bootstraps/sdl2python3crystax/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def run_distribute(self):
2525
# AND: Hardcoding armeabi - naughty!
2626
arch = ArchARM(self.ctx)
2727

28-
with current_directory(self.dist_dir):
28+
with current_directory(self.dist_dir)crystax_python/:
2929
info('Copying python distribution')
3030

3131
if not exists('private'):

pythonforandroid/build.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ def __init__(self):
446446
self.env.pop("ARCHFLAGS", None)
447447
self.env.pop("CFLAGS", None)
448448

449+
self.python_recipe = None # Set by TargetPythonRecipe
450+
449451
def set_archs(self, arch_names):
450452
all_archs = self.archs
451453
new_archs = set()

pythonforandroid/recipe.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,13 @@ class PythonRecipe(Recipe):
679679
@property
680680
def hostpython_location(self):
681681
if not self.call_hostpython_via_targetpython:
682-
return join(
683-
Recipe.get_recipe('hostpython2', self.ctx).get_build_dir(),
684-
'hostpython')
682+
if 'hostpython2' in self.ctx.build_order:
683+
return join(
684+
Recipe.get_recipe('hostpython2', self.ctx).get_build_dir(),
685+
'hostpython')
686+
else:
687+
python_recipe = self.ctx.python_recipe
688+
return 'python{}'.format(python_recipe.version)
685689
return self.ctx.hostpython
686690

687691
def should_build(self, arch):
@@ -715,7 +719,7 @@ def install_python_package(self, arch, name=None, env=None, is_dir=True):
715719
with current_directory(self.get_build_dir(arch.arch)):
716720
# hostpython = sh.Command(self.ctx.hostpython)
717721
hostpython = sh.Command(self.hostpython_location)
718-
hostpython = sh.Command('python3.5')
722+
# hostpython = sh.Command('python3.5')
719723

720724

721725
if self.ctx.ndk_is_crystax:
@@ -827,8 +831,8 @@ def build_cython_components(self, arch):
827831
env['PYTHONPATH'] = ':'.join(site_packages_dirs)
828832

829833
with current_directory(self.get_build_dir(arch.arch)):
830-
# hostpython = sh.Command(self.ctx.hostpython)
831-
hostpython = sh.Command('python3.5')
834+
hostpython = sh.Command(self.ctx.hostpython)
835+
# hostpython = sh.Command('python3.5')
832836
shprint(hostpython, '-c', 'import sys; print(sys.path)', _env=env)
833837
print('cwd is', realpath(curdir))
834838
info('Trying first build of {} to get cython files: this is '
@@ -892,7 +896,7 @@ def get_recipe_env(self, arch):
892896
if self.ctx.ndk_is_crystax:
893897
env['LDSHARED'] = env['CC'] + ' -shared'
894 10000 898
else:
895-
env['LDSHARED'] = join(self.ctx.root_dir, 'tools', 'liblink-jb')
899+
env['LDSHARED'] = join(self.ctx.root_dir, 'tools', 'liblink')
896900
shprint(sh.whereis, env['LDSHARED'], _env=env)
897901
env['LIBLINK'] = 'NOTNONE'
898902
env['NDKPLATFORM'] = self.ctx.ndk_platform
@@ -908,3 +912,25 @@ def get_recipe_env(self, arch):
908912
env['CFLAGS'] = '-I/home/asandy/android/crystax-ndk-10.3.0/sources/python/3.5/include/python ' + env['CFLAGS']
909913

910914
return env
915+
916+
917+
class TargetPythonRecipe(Recipe):
918+
'''Class for target python recipes. Sets ctx.python_recipe to point to
919+
itself, so as to know later what kind of Python was built or used.'''
920+
921+
from_crystax = False
922+
'''True if the python is used from CrystaX, False otherwise (i.e. if
923+
it is built by p4a).'''
924+
925+
def __init__(self, *args, **kwargs):
926+
self._ctx = None
927+
super(TargetPythonRecipe, self).__init__(*args, **kwargs)
928+
929+
@property
930+
def ctx(self):
931+
return self._ctx
932+
933+
@ctx.setter
934+
def ctx(self, ctx):
935+
self._ctx = ctx
936+
ctx.python_recipe = self

pythonforandroid/recipes/pysdl2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class PySDL2Recipe(PythonRecipe):
88
version = '0.9.3'
99
url = 'https://bitbucket.org/marcusva/py-sdl2/downloads/PySDL2-{version}.tar.gz'
1010

11-
depends = ['sdl2']
11+
depends = [('sdl2', 'sdl2python3crystax')]
1212

1313

1414
recipe = PySDL2Recipe()

pythonforandroid/recipes/python2/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

2-
from pythonforandroid.toolchain import Recipe, shprint, current_directory, info
2+
from pythonforandroid.recipe import TargetPythonRecipe, Recipe
3+
from pythonforandroid.toolchain import shprint, current_directory, info
34
from pythonforandroid.patching import is_linux, is_darwin, is_api_gt
45
from os.path import exists, join, realpath
56
import sh
67

78

8-
class Python2Recipe(Recipe):
9+
class Python2Recipe(TargetPythonRecipe):
910
version = "2.7.2"
1011
url = 'http://python.org/ftp/python/{version}/Python-{version}.tar.bz2'
1112
name = 'python2'
@@ -33,6 +34,8 @@ class Python2Recipe(Recipe):
3334
('patches/fix-distutils-darwin.patch', is_linux),
3435
('patches/fix-ftime-removal.patch', is_api_gt(19))]
3536

37+
from_crystax = False
38+
3639
def build_arch(self, arch):
3740

3841
if not exists(join(self.get_build_dir(arch.arch), 'libpython2.7.so')):

pythonforandroid/recipes/python3crystax/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11

2-
from pythonforandroid.toolchain import Recipe, shprint, current_directory, ArchARM
2+
from pythonforandroid.recipe import TargetPythonRecipe
3+
from pythonforandroid.toolchain import shprint, current_directory, ArchARM
34
from pythonforandroid.logger import info
45
from pythonforandroid.util import ensure_dir
56
from os.path import exists, join
67
from os import uname
78
import glob
89
import sh
910

10-
class Python3Recipe(Recipe):
11+
class Python3Recipe(TargetPythonRecipe):
1112
version = '3.5'
1213
url = ''
1314
name = 'python3crystax'
1415

1516
depends = ['hostpython3crystax']
1617
conflicts = ['python2', 'python3']
1718

19+
from_crystax = True
20+
1821
def get_dir_name(self):
1922
name = super(Python3Recipe, self).get_dir_name()
2023
name += '-version{}'.format(self.version)
@@ -31,6 +34,8 @@ def build_arch(self, arch):
3134

3235
dirn = self.ctx.get_python_install_dir()
3336
ensure_dir(dirn)
37+
38+
self.ctx.hostpython = 'python{}'.format(self.version)
3439
# ensure_dir(join(dirn, 'lib'))
3540
# ensure_dir(join(dirn, 'lib', 'python{}'.format(self.version),
3641
# 'site-packages'))

pythonforandroid/recipes/sdl2/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def get_recipe_env(self, arch=None):
1818
env = super(LibSDL2Recipe, self).get_recipe_env(arch)
1919
py2 = self.get_recipe('python2', arch.ctx)
2020
env['PYTHON2_NAME'] = py2.get_dir_name()
21+
if 'python2' in self.ctx.recipe_build_order:
22+
env['EXTRA_LDLIBS'] = ' -lpython2.7'
2123
return env
2224

2325
def build_arch(self, arch):

0 commit comments

Comments
 (0)
0