Compile and transplant python to arm board (two) transplant zlib and _ctypes

In the previous section, we have compiled and transplanted basic python, today we will talk about how to transplant zlib and _ctypes

Port zlib

zlib download zlib-1.2.11
is used here

tar xzvf zlib-1.2.11.tar.gz # 解压缩
cd zlib-1.2.11
  • Compile the host version of zlib

Before compiling the host zlib, we need to switch the compiler to execute export CC=gcc for the host compiler

export CC=gcc
./configure
make
sudo make install

 

  • Compile the target version of zlib
mkdir /opt/zlib
export CC=arm-himix200-linux-gcc # 更改编译器
./configure --prefix=/opt/zlib # 指定编译目录
make
sudo make install
root@ubuntu:~# ls /opt/zlib
include  lib  share
  • Recompile host python

Modify source/Python-3.7.6/Modules/Setup.dist and uncomment zlib

 

 

export CC=gcc  # 返回默认编译器
./configure
make
sudo make install

Run the host python to view the modules at this time, you will see that there is a zlib module at the end, the host compiles successfully, and then compile the target machine.

  • Recompile the target version of Python

sudo cp -rfp /opt/zlib/* /home/python3.7/install/python # 复制zlib到python安装路径

Recompile python, you can change the execution of make.sh script

#file make.sh
...
make_target () {
    cd ${BASE}/source/Python*
    echo `pwd`
    sudo make clean
    mkdir bulid-${BUILD_HOST} -p
    cd  bulid-${BUILD_HOST}
    mkdir ${BASE}/install/python -p
    ../configure CC=${BUILD_HOST}-gcc \
    CXX=${BUILD_HOST}-g++ \
    --host=${BUILD_HOST} \
    --build=x86_64-linux-gnu \
    --target=${BUILD_HOST} --disable-ipv6 \
    --prefix=${BASE}/install/python \
    --enable-optimizations \
    ac_cv_file__dev_ptmx=yes ac_cv_file__dev_ptc=yes
    #make && make install
}
#make_dirs
#download_package
#tar_package
#make_host #如果没有的话,需要安装
make_target

Keep make_target in the script, other comments, make && make install in make_target also need to be commented out and re-execute./make.sh

Compile the host version python with the same, cd build-${BUILD_HOST} modify and Modules/Setup,uncomment zlib.

Uncomment zlib

Then compile

make && make install

After python is compiled, put it on the board, enter repl to view modules and find that zlib has been added.

Port_ctypes

As mentioned earlier, the version before python3.7 comes with compiled _ctypes, and python3.7 does not. This is because 3.7 needs to rely on a new library to implement this module, the libffi library. According to the test comparison, python3 is found. Version 7 runs faster than the previous version. I don’t know if it’s because of this. So the python3.7 version that most developers currently use, we also directly port python3.7. Next, talk about how to transplant libffi.

  • Compile host_ctypes

The ubuntu host is very simple, just install the ready-made package directly

sudo apt-get install libffi-dev

After installation, recompile the host python. The compilation method has been explained before, so I won't repeat it. After editing and viewing modules, you can find that _ctypes already exists. At this point, you can use the host to perform setup to install third-party modules.

  • Compile target machine _ctypes

It is more troublesome to compile the target machine, after all, the linux on the rudimentary arm board does not have the ready-made package management and installation function, we need to compile and put it in by ourselves.

Download libffi source code: ftp://sourceware.org/pub/libffi/

 

Just choose the latest version to download. I downloaded version 3.3. Put the source code on the host, decompress and enter the source code path, and then compile the arm version of the libffi library.

export CC=arm-himix200-linux-gcc
./configure --host=arm-himix200-linux --build=x86_64-linux-gnu --target=arm-himix200-linux --prefix=/opt/libffi-3.3

After the library is compiled, we need to copy the lib and header files to the cross-compilation toolchain library path. How to find this path? You can see the print information of the previous cross-compiled python similar to this:

arm-himix200-linux-v2-gcc -shared build/temp.linux-arm-3.7/home/amba/21819/Python-3.7.4/Modules/xxlimited.o -L/usr/local/arm-himix200-linux-v2/target/usr/lib -L/usr/local/arm-himix200-linux-v2/target/lib -L/usr/local/arm-himix200-linux-v2/arm-linux-gnueabi/lib -L/usr/local/arm-himix200-linux-v2/lib/gcc -o build/lib.linux-arm-3.7/xxlimited.cpython-37m-arm-linux-gnueabi.so

Take mine as an example:

cp /opt/libffi-3.3/lib/* /opt/hisi-linux/x86-arm/arm-himix200-linux/target/usr/lib/
cp /opt/libffi-3.3/include/* /opt/hisi-linux/x86-arm/arm-himix200-linux/target/usr/include/

Then recompile the target board python

make

After make, you can see the printed information, and there will be an error. Regarding the _uuid module, this will not affect it for the time being. Don’t worry about it. This error does not affect the compilation of python. Below the compilation information is a list of which modules have not been compiled. . _ctypes will be listed before libffi transplantation, _uuid has not been compiled successfully, and now only _uuid should be listed, which means that our _ctypes has been compiled, as shown below:

make install

After installation, put it on the board and check the modules again, and found that _ctypes is already there. So far, the most basic functions of python are already available. We can also install third-party modules on the board through setup. Of course, the setup on the host is also installed to the python installation path Yes.

I will talk about how to install third-party modules later.

Guess you like

Origin blog.csdn.net/qq_34440409/article/details/109287347