Repository: kivy/python-for-android

Issue:
Python2 Build fails with make: *** [Makefile:426: sharedmods] Error 139
# Python version: 3.6
# OS: Arch Linux 
# python-for-android version: 0.6.0 

The command I use to build is:

`  
    p4a  apk --private ~/Projects/Python/Mobile_Apps/BeerApp/ --package=org.drink.recommendations --name "Drink Recommendations" --version 0.2 --bootstrap=sdl2 --requirements=python2,kivy --ndk_version r9c
`

The error is:

`
    make: *** [Makefile:426: sharedmods] Error 139
`

The build logs are in the following file.
[p4a_errors.txt](https://github.com/kivy/python-for-android/files/2091833/p4a_errors.txt)

Initally I thought that this was a buildozer issue, as I attempted it that way first. So, I opened an issue on their github page and multiple users pointed out that they too were experiencing this issue. I've tried with both python3 and python2, the out come is the same. There is absolutely no unicode in any of my source files, I've also attempted the build with pygame instead of sdl2 (for python 2). There are also multiple simillar SO threads open about this. 

Does anyone have any sugesstions or ideas as to why this is happening and how to go about fixing it?

It's also worth noting that if I use the kivy buildozer vm, I can use buildozer to carry out a successful build. Just not on any other machine using either buildozer or p4a, using the same source and build commands.

The buildozer issue is here: https://github.com/kivy/buildozer/issues/673

The output from the dump file is:

`
    Reading symbols from /home/suroh/.local/share/python-for-android/build/other_builds/hostpython2/desktop/hostpython2/python...done.
    [New LWP 28854]
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/usr/lib/libthread_db.so.1".
    Core was generated by ./python -E ./setup.py -q build.
    Program terminated with signal SIGSEGV, Segmentation fault.
    #0  0x000055731803eb2a in PyInstance_NewRaw (klass=klass@entry=0x7f7cbf1d1c18, dict=0x557319325210, dict@entry=0x0) at Objects/classobject.c:534
    534         inst->in_dict = dict;
    File "/home/suroh/.local/share/python-for-android/build/other_builds/hostpython2/desktop/hostpython2/python-gdb.py", line 55
        Py_TPFLAGS_HEAPTYPE = (1L << 9)
                                ^
    SyntaxError: invalid syntax
`


Discussion:
It's a bit annoying because it's a segfault :disappointed: 
Can you edit your issue and link to the buildozer ticket you created?

I don't have the solution, but I can suggest few things to take a look at.
1. Try p4a master branch `p4a.branch = master` in your `buildozer.spec`
2. Also I think it could be great to give a try in an isolated Docker environment. Rather it crashes or not would give some hint. You need to clone the p4a repository build the [Docker image](https://github.com/kivy/python-for-android/blob/d013a86/Dockerfile) and play with it.
You can take a look at [.travis.yml](https://github.com/kivy/python-for-android/blob/65157a2/.travis.yml) to see how we play with it.

So I think Docker could help, because it may not crash on that image. but then you can try to modify it a bit and makes it look like your own setup, e.g. Arch Linux... But also maybe it already crashes straight away with the Dockerfile provided. All of that could help to "corner" where the issue comes from and eventually fix it.

Good luck :crossed_fingers: 
do you have a `core.dump` file? if so, you can open it with gdb, maybe it would provide some hinsight about what was happening while it crashed, though, without debug symbols, it's a bit like hitting in the dark.
I have the same issue, on Arch, but haven't had time to really look at it yet.
@AndreMiras I'll absolutely give that a try ( Not at home at the moment)! @tshirtman I've looked for a dump file but haven't been able to locate one, when I get home I'll re-run and search more as I cleared everything out.
Good point for the core dump. On Gentoo it's disable by default, but running `ulimit -c unlimited` enables it.
I have the same issue. I'm also on Arch. Maybe it has to do with some 32bit libs.
Well, someone fixed it by using:
BASECFLAGS=-m32 LDFLAGS=-m32 CFLAGS=-m32 ./configure --prefix=/opt/python32
Don't know how to test this with buildozer though.
The build cpython with BASECFLAGS=-m32 LDFLAGS=-m32 CFLAGS=-m32 ./configure --prefix=/opt/python32 is not a fix.
I have just used alot of time building python2 / python3 and no luck same error. (make: *** [Makefile:426: sharedmods] Error 139)

/bin/sh: linje 1:  9857 Segmentfejl             (smed kerne) CC='gcc -pthread' LDSHARED='gcc -pthread -shared ' OPT='-DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py -q build

This was with python-for-android and buildozer build from master.
Same issue on Fedora 28... where can I add those options? I don't know what to do, it simply crashes (always at the same point when building hostpython2)
I'm also running into the issue on Fedora 28, even with -m32 in CFLAGS.
Got it too :[

Hostpython build is straightforward, and a simple ./configure && make should works. It appears no:
On my system, no ccache, PATH reduced to `/usr/local/sbin:/usr/bin:/usr/lib/jvm/default/bin`:
```
$ wget https://python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2
$ tar xf Python-2.7.2.tar.bz2
$ cd Python-2.7.2
$ ./configure
$ make
```
=> ends up with a segfault: 
```
/home/tito/code/connectage/app/.buildozer/android/platform/build/build/other_builds/p/Python-2.7.2/./Modules/posixmodule.c:7317: warning: the use of `tempnam' is dangerous, better use `mkstemp'
/bin/sh: line 1: 18943 Segmentation fault      (core dumped) CC='gcc -pthread' LDSHARED='gcc -pthread -shared ' OPT='-DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py build
```

So it's system wide, not related to cross compilation (as we are not yet there), and the env (as far as i understand.)

Resolving patch:
diff --git a/pythonforandroid/recipes/hostpython2/__init__.py b/pythonforandroid/recipes/hostpython2/__init__.py
--- a/pythonforandroid/recipes/hostpython2/__init__.py
+++ b/pythonforandroid/recipes/hostpython2/__init__.py
@@ -10,6 +10,7 @@ class Hostpython2Recipe(Recipe):
     version = '2.7.2'
     url = 'https://python.org/ftp/python/{version}/Python-{version}.tar.bz2'
     name = 'hostpython2'
+    patches = ['fix-segfault-pygchead.patch']
 
     conflicts = ['hostpython3']
 


Test patch: