Skip to content

Steps for Android Native Build of Python 3.4.2 #67684

Closed

Description

@chaselton
mannequin
BPO 23496
Nosy @terryjreedy, @vstinner, @bitdancer, @freakboy3742, @ericsnowcurrently, @Fak3, @refi64, @mpaolini, @tritium21, @yan12125
Dependencies
  • bpo-16353: add function to os module for getting path to default shell
  • bpo-20305: Android's incomplete locale.h implementation prevents cross-compilation
  • bpo-21668: The select and time modules uses libm functions without linking against it
  • Files
  • patch.diff: patch for pythonrun.c
  • android_segfault_fix.patch
  • kbox_fix.patch
  • issue_16353.patch
  • issue_20305_tweaked.patch
  • issue_21668.patch
  • unused_var.patch
  • rjmatthews62_fixes_tweaked.patch
  • androidfn.patch
  • issue_20306.patch
  • rjmatthews64_fixes2.patch
  • rjmatthews64_fixes2.patch
  • rjmatthews64_fixes2.patch
  • kbox_fix.patch
  • python.patch: patch for python.c. that prevents android segfault
  • kbox_fix.patch
  • lib_fixes.patch
  • py_test_results.log: ./python -m test log
  • test_gdb.log
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-10-22.23:47:24.109>
    created_at = <Date 2015-02-21.20:46:56.960>
    labels = ['type-feature', 'build']
    title = 'Steps for Android Native Build of Python 3.4.2'
    updated_at = <Date 2019-10-22.23:47:24.108>
    user = 'https://bugs.python.org/chaselton'

    bugs.python.org fields:

    activity = <Date 2019-10-22.23:47:24.108>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-22.23:47:24.109>
    closer = 'vstinner'
    components = ['Build']
    creation = <Date 2015-02-21.20:46:56.960>
    creator = 'chaselton'
    dependencies = ['16353', '20305', '21668']
    files = ['38201', '38575', '38576', '38577', '38578', '38580', '38581', '38582', '38846', '38853', '38880', '38881', '38884', '39134', '39135', '39628', '39629', '40002', '40586']
    hgrepos = []
    issue_num = 23496
    keywords = ['patch']
    message_count = 177.0
    messages = ['236389', '236444', '236445', '236447', '236462', '236469', '236488', '236530', '236566', '236567', '236571', '236578', '236584', '236611', '236667', '236683', '236685', '236722', '236746', '236756', '236761', '236763', '236766', '236773', '236779', '236797', '236800', '236801', '236810', '236819', '236820', '236857', '236858', '236883', '236884', '236886', '236890', '236911', '236926', '236999', '237009', '237073', '237118', '237215', '237216', '237219', '237222', '237260', '237280', '237284', '237285', '237374', '237574', '237639', '237654', '237680', '237681', '237682', '237683', '237684', '237693', '237724', '237725', '237774', '237785', '238592', '238744', '238932', '238934', '239256', '239264', '239466', '240117', '240118', '240119', '240127', '240143', '240144', '240157', '240176', '240178', '240208', '240218', '240329', '240338', '240339', '240342', '240350', '240351', '240352', '240356', '240357', '240363', '240366', '240401', '240403', '240404', '240406', '240423', '240489', '240822', '241154', '241385', '241427', '241432', '241437', '241457', '241574', '241576', '241577', '241578', '241579', '241634', '241642', '241657', '241663', '241805', '241862', '242062', '244366', '244387', '244389', '244845', '244846', '244859', '244868', '245445', '245674', '245894', '245932', '245937', '245938', '245988', '245990', '245993', '246938', '247118', '247232', '247235', '247269', '247310', '247346', '247359', '247364', '247584', '247713', '248452', '248458', '248671', '248672', '248730', '248731', '248733', '248909', '248938', '248954', '248957', '248974', '248975', '248990', '248995', '248996', '248997', '249199', '249208', '250794', '251598', '251648', '253800', '253817', '253847', '253851', '253856', '253862', '254248', '264176', '355183']
    nosy_count = 12.0
    nosy_names = ['terry.reedy', 'vstinner', 'r.david.murray', 'cvrebert', 'freakboy3742', 'eric.snow', 'Roman.Evstifeev', 'refi64', 'mpaolini', 'chaselton', 'tritium', 'yan12125']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue23496'
    versions = ['Python 3.4']

    Activity

    1. chaselton commented on Feb 21, 2015

      chaseltonmannequin
      MannequinAuthor

      This is a (hopefully) complete list of patches and modifications required to natively build Python 3.4.2 on an Android device where
      patches == listed issues in this bug tracker
      modifications (mods) == unsubmitted patches and/or edits made to source

      CAVEATS
      Build was completed on a tablet running Android 4.4.2 and within the KBOX environment; an app that simulates a Linux filesystem by using a ported version of fakechroot. Details of the environment can be found here: http://kevinboone.net/kbox2_how_it_works.html. NOTE: During the Python build it was necessary to patch this environment with a modified version of fakechroot; the KBOX version currently available as of 2.21.2015 may not contain the modified fakechroot.

      CONFIGURE && POST-CONFIGURE && MAKE
      The configure command was run with the following parameters
      --enable-shared
      --without-ensurepip
      --prefix=/usr/python

      After running configure. changes were made to pyconfig.h to account for various platform limitations and to Setup to ensure that a) all modules specified were built as shared and b) modules that used symbols from libpython were explicitly linked to said library. See attached pyconfig.h and Setup.

      Make fails when the newly built python binary tries to execute and run setup.py. This is resolved by copying the shared library from the build directory to /lib in the KBOX environment. Otherwise make && make install runs without errors provided patches/mods are applied. I did not run make test or the equivalent for this build.

      PATCHES/MODS
      The attached patches from the following issues must be applied:
      http://bugs.python.org/issue20305
      http://bugs.python.org/issue16353 (NOTE: use patch with 'defpath' not 'confstr', which is not supported in Android)
      http://bugs.python.org/issue21668

      The following modifications to source must be made:
      The instructions under 'Code Changes' at this link: https://code.google.com/p/python-for-android/wiki/CrossCompilingPython
      The attached patch, contributed by Ryan Gonzales (patch.diff)
      The changes listed below by file (apologies for formatting)
              
      /* patch for strdup segfault issue in readline.c */
       #define RESTORE_LOCALE(sl) { if (sl) { setlocale(LC_CTYPE, sl); free(sl); } }
       
       char *_locale = setlocale(LC_CTYPE, NULL); 
       char *saved_locale; 
       if(!_locale) 
       _locale = ""; 
       saved_locale = strdup(_locale);
       
       
       char *saved_locale = setlocale(LC_CTYPE, NULL);
          if (saved_locale != NULL) {
              saved_locale = strdup(saved_locale);
              if (!saved_locale)
                  Py_FatalError("not enough memory to save locale");
          }
          

      /* fix for Lib/ctypes/__init.py__ */
      30 DEFAULT_MODE == RTLD_GLOBAL

      /* patch for python.c */
      /* here line for oldloc is commented out */
      24 int i, res;
      25 /* char *oldloc; */
      26 #ifdef __FreeBSD__

      /* here oldloc lines commented out */
      46
      47 /* oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
      48 * if (!oldloc) {
      49 * fprintf(stderr, "out of memory\n");
      50 * return 1;
      51 *
      52 * }
      53 */

      /* patch for pythonrun.c 307, --309-310, ++311-313 --1009-1012 ++1013-1014 */

      305     return get_codec_name(codeset);
      306 #else
      307    char* m;
      308    PyErr_SetNone(PyExc_NotImplementedError);
      309    /*  return NULL; \*/
      310    /* char* m; \*/
      311    m = malloc(6);
      312    strcpy(m, "ascii");
      313    return m;
      314 #endif
      315 }
      

      1002 static int
      1003 initfsencoding(PyInterpreterState *interp)
      1004 {
      1005 PyObject *codec;
      1006
      1007 if (Py_FileSystemDefaultEncoding == NULL)
      1008 {
      1009 /* Py_FileSystemDefaultEncoding = get_locale_encoding();
      1010 * if (Py_FileSystemDefaultEncoding == NULL)
      1011 * Py_FatalError("Py_Initialize: Unable to get the locale encoding
      1011 ");
      1012 */
      1013 Py_FileSystemDefaultEncoding = malloc(6);
      1014 strcpy(Py_FileSystemDefaultEncoding, "ascii");
      1015 Py_HasFileSystemDefaultEncoding = 0;
      1016 interp->fscodec_initialized = 1;
      1017 return 0;
      1018 }
      1019

      /* changes to Lib/plat-linux/DLFCN.py */
      76 # Included from bits/dlfcn.h
      77 # Changed for Android
      78 RTLD_LAZY = 1
      79 RTLD_NOW = 0
      80 #RTLD_BINDING_MASK = 0x3
      81 #RTLD_NOLOAD = 0x00004
      82 RTLD_GLOBAL = 2
      83 RTLD_LOCAL = 0
      84 #RTLD_NODELETE = 0x01000
      85
      86 # Also from Android's dlfcn.h
      87 RTLD_DEFAULT ((void*) 0xffffffff)
      88 RTLD_NEXT ((void*) 0xfffffffe)
      89

    2. added
      build
      on Feb 21, 2015
    3. chaselton commented on Feb 23, 2015

      chaseltonmannequin
      MannequinAuthor

      The attached g-zipped file contains the first set of patches required to build Python 3.4.2 from source in the environment specified in the origonal post.

      Will post the second/final set ASAP

    4. vstinner commented on Feb 23, 2015

      @vstinner
      Member

      It's not convinient to have patches in a tarball. Please at least open patches one by one, or better: open one issue per patch. You should group similar changes in a single patch: for example all changes related to wcstombs().

      You should explain each change.

      For example, the following change (os.patch) looks like a bug. You cannot simply rename a function. It completly breaks backward compatibility.

      -.. function :: get_shell()
      +.. function :: get_shell_executable()

      selectmodule.patch:

      •    timeout = (int)ceill(dtimeout * 1000.0);
        

      + timeout = (int)ceil(dtimeout * 1000.0);

      I don't understand where ceill() does come from. I cannot find it in the current source code. Maybe the patch was produced in the wrong direction?

      patch.diff: please generate unified patches. Your format is ugly, I cannot read it. And the bug tracker failed to create a "review" button.

      It's hard to follow instructions to apply patches. Maybe you should use a Mercurial repository, which include all changes?

    5. chaselton commented on Feb 23, 2015

      chaseltonmannequin
      MannequinAuthor

      Apologies for the tarball, but all patches within are related to this "issue"

      Removing tarball and will re-post individual, cleaned-up patches, grouped by issue.

      Ryan, can you re-do patch for pythonrun.c? If not. I'l. work it in.

    6. refi64 commented on Feb 23, 2015

      refi64mannequin
      Mannequin

      Cyd,

      What revision of the Python 3.4 source is this against? The tarball?

      I'm not quite sure what to redo that patch against.

      Also, shouldn't the code be selectively enabled with macros/configure.ac edits? Or did I miss where someone said this should be in its own branch (which, in my opinion, isn't very clean)?

    7. chaselton commented on Feb 23, 2015

      chaseltonmannequin
      MannequinAuthor

      Ryan,
      I manually applied the patch to the Python 3.4.2 source tarball, dowmloaded from the main site to my Android tablet. I'm guessing (re)generating the patch from that source should work.

      I've no knowledge of configure syntax, but most of the patches apply only if __ANDROID__ is defined. This can be enabled with the -mandroid/-mbionic CFLAGS that work with GCC 4.8.x and later.

    8. chaselton commented on Feb 24, 2015

      chaseltonmannequin
      MannequinAuthor

      Additionally, for the Lib/plat-<os> specific changes, I can create Lib/plat-android and move the modified DLFCN.py and others there, provided someone could help with the appropriate configure.ac syntax.

      I can/could do it myself, but it would take a *while* as I'd need to learn a fair amount of configure scripting first.

    9. refi64 commented on Feb 24, 2015

      refi64mannequin
      Mannequin

      So...

      I know a decent amount of configure scripts, although Python's is easily going to be the absolute longest I've ever touched.

      Maybe these should be made against the 3.6 Mercurial source? I'm not sure if the Python devs would prefer that.

      Also, should the configure script be based on the Android target triple?

    10. added
      type-feature
      on Feb 24, 2015
    11. chaselton commented on Feb 25, 2015

      chaseltonmannequin
      MannequinAuthor

      Pulling from Mercurial will require taking a couple of huge steps back, as I've ported git to the environment I am using but not Mercurial. It would also mean getting up to speed *fast* as I know nothing about VCS...including Mercurial.

      I'm willing to do this (what's another port after all) but it may be a while before I can get everything set up correctly.

    12. vstinner commented on Feb 25, 2015

      @vstinner
      Member

      Take a look at https://docs.python.org/devguide/gitdevs.html
      "Mercurial for git developers"

    13. chaselton commented on Feb 25, 2015

      chaseltonmannequin
      MannequinAuthor

      Thanks for the suggestion Victor, however I've only ported git to this environment; the most I;ve used it is to run 'git clone' to pull source from a repo. The recommendation from the mobile-sig mailing list is...given my lack of familiarity with VCS..is to attempt to port and work with Mercurial. If doing so starts to take too much bandwidth I'll fall back to git and the article you linked.

      Side Note: I cannot emphasize enough how little I know about how to use git and most of the other tools I've ported to this environment. My goal was to get as many of the 'standard' Linux utilities running in this environment, then start learning them.

    14. bitdancer commented on Feb 25, 2015

      @bitdancer
      Member

      There is a github mirror of the hg repository (https://github.com/python/cpython). Patches produced from it don't automatically get review links in the tracker, but at least they would then be against the most current source (and someone else could regenerate them). That would at least allow you to skip the "port mercurial" step.

      On the other hand, it sounds like Ryan may be volunteering to update the patches to apply to the head of default for you. (And yes, Ryan, the patches should be against the head of the default branch.)

      Now, whether or not we will accept the patches is a different discussion, but I'd certainly like to see more Android support in our tree myself.

    15. chaselton commented on Feb 25, 2015

      chaseltonmannequin
      MannequinAuthor

      Thanks for that suggestion R. David...that would be ideal given the 2.x requirements of Mercurial would mean i'd need to re-port Python before tackling a port of Mercurial.

      I'll wait until Ryan chimes in before I start serious work with git, just in case.

    16. 151 remaining items

    17. refi64 commented on Aug 26, 2015

      refi64mannequin
      Mannequin

      I don't think so. I believe that switch is just to enable writing gdb extensions in Python.

      On August 26, 2015 9:34:29 AM CDT, Cyd Haselton <report@bugs.python.org> wrote:

      Cyd Haselton added the comment:

      UPDATE:
      Still working on the test_hash issue; not making much progress

      QUESTION
      Does gdb have to be configured with the --with-python switch in order
      to debug the python binary correctly?

      ----------


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue23496\>


    18. chaselton commented on Sep 15, 2015

      chaseltonmannequin
      MannequinAuthor

      Ryan,

      Here's the results after the edit to the ctypes test. I also re-compiled gdb with python support, which is why this took so long

      (gdb) file ./python

      Load new symbol table from "./python"? (y or n) y
      Reading symbols from ./python...done.
      Traceback (most recent call last):
        File "/bld/pyt/cpython-android/python-gdb.py", line 59, in <module>
          _type_char_ptr = gdb.lookup_type('char').pointer() # char*
      AttributeError: 'module' object has no attribute 'lookup_type'

      (gdb) set args -m tests
      (gdb) set sysroot /usr/gcc-4.9-pie/sysroot
      (gdb) run

      Starting program: /bld/pyt/cpython-android/python -m tests
      setpgrp failed in child: No such process
      warning: Unable to find dynamic linker breakpoint function.
      GDB will be unable to debug shared library initializers
      and track explicitly loaded dynamic code.

      Program received signal SIGILL, Illegal instruction.
      0xb6a63cc8 in ?? ()
      (gdb) bt
      Python Exception <class 'ImportError'> No module named 'gdb.frames':
      #0 0xb6a63cc8 in ?? ()
      #1 0xb6a5feb0 in ?? ()
      Backtrace stopped: previous frame identical to this frame (corrupt stack?)

    19. chaselton commented on Sep 25, 2015

      chaseltonmannequin
      MannequinAuthor

      Still battling this bus error issue. Recompiling gdb --with-python did not help; I still cannot get any useful info out of it. I suspect it may be due to this error:

      setpgrp failed in child: No such process

      but I'm not sure.

      At this point I'm going to try downloading the Google-patched version...hopefully it is patched such that the setpgrp bug is resolved.

    20. chaselton commented on Sep 26, 2015

      chaseltonmannequin
      MannequinAuthor

      UPDATE: Before downloading/building Google gdb source I ran test_gdb.py, which failed completely. Details of those tests are attached.

    21. chaselton commented on Oct 31, 2015

      chaseltonmannequin
      MannequinAuthor

      UPDATE: Finslly rebuilding Python from github clone after having to factory reset my tablet for reasons (root access). Hopefully build and tests will benefit from paving and starting over.

    22. chaselton commented on Nov 1, 2015

      chaseltonmannequin
      MannequinAuthor

      Unfortunately, in spite of the other minor issues that resolved,I am still getting a bus error when the hash test runs.

      Before I dive back into trying to get gdb working to debug this, is there any way to skip this test to see of the others run?

    23. chaselton commented on Nov 1, 2015

      chaseltonmannequin
      MannequinAuthor

      GDB debugging isn't going to happen any time soon; all of the gdb tests fail.
      I re-compiled with the --with-pydebug option, thinking that would help, but it won't build; I get the following error when the newly built python starts to build packages:

      Debug memory block at address p=0xb6003070: API 'a'
      795107700 bytes originally requested
      The 3 pad bytes at p-3 are not all FORBIDDENBYTE (0xfb):
      at p-3: 0x2f *** OUCH
      at p-2: 0x00 *** OUCH
      at p-1: 0x00 *** OUCH
      Because memory is corrupted at the start, the count of bytes requested
      may be bogus, and checking the trailing pad bytes may segfault.
      The 4 pad bytes at tail=0xe56491e4 are Segmentation fault
      generate-posix-vars failed

      Additional things I've attemptedto try to resolve the test_hash failure, based on this bug report (https://mail.python.org/pipermail/python-bugs-list/2015-March/267442.html)

      1. compiling with -mno-unaligned-access
      2. compiling with FNV as the default hash algorithm.

      Neither worked. At this point I am at a loss and am beginning to think there may be a problem with my device. Any suggestions are welcome.

    24. refi64 commented on Nov 1, 2015

      refi64mannequin
      Mannequin

      Are there no binaries?

      Pretty sure it isn't your device. I'd think a compiler issue. What are you using, and what version?

      You have insane determination! :D

      On November 1, 2015 8:34:21 AM CST, Cyd Haselton <report@bugs.python.org> wrote:

      Cyd Haselton added the comment:

      GDB debugging isn't going to happen any time soon; all of the gdb tests
      fail.
      I re-compiled with the --with-pydebug option, thinking that would help,
      but it won't build; I get the following error when the newly built
      python starts to build packages:

      Debug memory block at address p=0xb6003070: API 'a'
      795107700 bytes originally requested
      The 3 pad bytes at p-3 are not all FORBIDDENBYTE (0xfb):
      at p-3: 0x2f *** OUCH
      at p-2: 0x00 *** OUCH
      at p-1: 0x00 *** OUCH
      Because memory is corrupted at the start, the count of bytes requested
      may be bogus, and checking the trailing pad bytes may segfault.
      The 4 pad bytes at tail=0xe56491e4 are Segmentation fault
      generate-posix-vars failed

      Additional things I've attemptedto try to resolve the test_hash
      failure, based on this bug report
      (https://mail.python.org/pipermail/python-bugs-list/2015-March/267442.html)

      1. compiling with -mno-unaligned-access
      2. compiling with FNV as the default hash algorithm.

      Neither worked. At this point I am at a loss and am beginning to think
      there may be a problem with my device. Any suggestions are welcome.

      ----------


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue23496\>


    25. chaselton commented on Nov 1, 2015

      chaseltonmannequin
      MannequinAuthor

      Pretty sure there are binaries, but the python binary can't build its own modules.

      I'm using GCC 4.9.2 on Android L...which means it is a PIE and that Python is being built as a PIE

    26. chaselton commented on Nov 1, 2015

      chaseltonmannequin
      MannequinAuthor

      Key note: it looks like the config option --with-hash-algorithm does nothing; python is still built with the siphash hash algorithim even though I specified fnv and, in pyconfig.h:

      /* Define hash algorithm for str, bytes and memoryview. SipHash24: 1, FNV: 2,
         externally defined: 0 */
      #define Py_HASH_ALGORITHM 2

      What's the correct way to build with fnv as the hash algorithm?

    27. terryjreedy commented on Nov 7, 2015

      @terryjreedy
      Member

      Ryan, please sign the PSF Contributor Agreement
      https://www.python.org/psf/contrib/
      https://www.python.org/psf/contrib/contrib-form/

      Also, when replying by email, please delete the previous message, except possibly for quoting a line or two. When listed on the web page, big quotations are useless noise.

    28. ericsnowcurrently commented on Apr 25, 2016

      @ericsnowcurrently
      Member
    29. vstinner commented on Oct 22, 2019

      @vstinner
      Member

      It seems like attached patches are outdated: Python has been fixed to support Android. For example, Python now uses the UTF-8 codec rather than mbstowcs() to decode byte strings. I'm quite sure that there are still corner cases which are not well supported on Android, but I would suggest to open more specific issues in that case.

    30. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      buildtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions