title: How to cross-compile Python?
body: <p>I have an SoC with Cortex A9 ARMv7 architecture. I need to be able to run python scripts on a board with this SoC. So I need to cross-compile Python for this platform.</p>
  <p>The SoC has ARMv7 architecture, so I've installed the <code>arm-linux-gnueabihf</code> cross-compiler for Ubuntu 20.04 running in the VirtualBox.</p>
  <p>I am following <a href="https://www.cnx-software.com/2011/02/04/cross-compiling-python-for-mips-and-arm-platforms/" rel="nofollow noreferrer">this</a> instruction:</p>
  <ol>
  <li><p>First I've downloaded <a href="https://www.python.org/downloads/release/python-271/" rel="nofollow noreferrer">Python 2.7.1</a> sources and extracted it to the <code>/home/user/python/Python-2.7.1</code> directory.</p>
  </li>
  <li><p>Then I've downloaded the <a href="https://www.cnx-software.com/patch/python-2.7.1-cross-compile.patch" rel="nofollow noreferrer">patch</a> from the instruction I am following.</p>
  </li>
  <li><p>Then I've applied the patch:</p>
  <pre><code>patch -p1 &lt; python-2.7.1-cross-compile.patch
  </code></pre>
  </li>
  <li><p>Then I've compiled some tools for host:</p>
  <pre><code>./configure
  make python Parser/pgen
  mv python hostpython
  mv Parser/pgen Parser/hostpgen
  make distclean
  </code></pre>
  </li>
  <li><p>Then I've configured Python for cross-compilation:</p>
  <pre><code>readonly CROSS_COMPILER=arm-linux-gnueabihf
  
  CC=${CROSS_COMPILER}-gcc \
  CXX=${CROSS_COMPILER}-g++ \
  AR=${CROSS_COMPILER}-ar \
  RANLIB=${CROSS_COMPILER}-ranlib \
  ./configure \
  --host=${CROSS_COMPILER} \
  --target=${CROSS_COMPILER} \
  --prefix=/python
  </code></pre>
  </li>
  <li><p>And then I finally cross-compile it:</p>
  <pre><code>make \
  HOSTPYTHON=./hostpython \
  HOSTPGEN=./Parser/hostpgen \
  BLDSHARED=&quot;${CROSS_COMPILER}-gcc -shared&quot; \
  CROSS_COMPILE=${CROSS_COMPILER}- \
  CROSS_COMPILE_TARGET=yes
  </code></pre>
  <p>But eventually I have got an <code>IndentationError</code>:</p>
  <pre><code>/usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tmpnam':
  /home/user/python/Python-2.7.1/./Modules/posixmodule.c:7346: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp'
  /usr/lib/gcc-cross/arm-linux-gnueabihf/9/../../../../arm-linux-gnueabihf/bin/ld: libpython2.7.a(posixmodule.o): in function `posix_tempnam':
  /home/user/python/Python-2.7.1/./Modules/posixmodule.c:7301: warning: the use of `tempnam' is dangerous, better use `mkstemp'
  File &quot;./setup.py&quot;, line 316
    self.announce('*** WARNING: renaming &quot;%s&quot; since importing it'
      ^
  IndentationError: expected an indented block
  make: *** [Makefile:425: sharedmods] Error 1
  </code></pre>
  </li>
  </ol>
  <p>What I do wrong and how to solve this problem?</p>
  <hr />
  <p>It seems that Python itself is successfully compiled and linked because after all this process I get the <code>python</code> file in the build directory:</p>
  <pre><code>$ file python
  python: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=1a757bca3295fe062ffbee5cf2d791eb36861524, for GNU/Linux 3.2.0, with debug_info, not stripped
  </code></pre>