LicheeRV 86 can be programmed using C and C++. Some codes are difficult to write, so I thought of porting Python to the board. After many days of hard work, Python 3.8 was successfully ported to the development board. Now I will share the porting process.
The main code of Python is also written in C/C++, so Python can be compiled into a version that can be used by the development board through cross compilation. After the compilation is completed, an executable file and the corresponding lib will be generated.
By adding the executable file to the PATH variable and the library path to the LD_LIBRARY_PATH variable, you can implement the python function.
So the point is how to generate executable files and libraries.
1. Download Python source code
The official website of Python provides a way to download the source code. After downloading, unzip it to get the required source code. I chose Python 3.8.13.
2. Configure Python compilation method
2.1 Set the variables corresponding to the compilation tool
When compiling Python, you need to specify the compilation tools required for compilation, which CC, CXX, AR
are introduced through variables, etc.
These tools have been configured when configuring the cross-compilation environment of the development board, so they can be used directly after the environment is configured.
Note that before use, you must enable the variables of the development board compilation tool through the source command.
2.2 Create a folder
Create a folder in the root directory of the source code to store some intermediate files generated by compilation.
mkdir riscv.build
2.3 Configure compilation parameters
Configure the compilation parameters through the following instructions:
../configure --prefix=/Python3.8.13 --build=riscv64-linux-gnu --host=riscv64-linux --target=riscv64-linux-gnu --enable-shared --enable-ipv6 --with-system-ffi ac_cv_file__dev_ptmx=0 ac_cv_file__dev_ptc=0 --enable-optimizations
I don’t quite understand the above parameters. If you are interested, you can refer to this website: https://docs.python.org/zh-cn/3.10/using/configure.html
Note that if you want to configure python 3.8.13, you need to first install python 3.8 on your computer to successfully configure it , otherwise an error will be reported. The error reported at the time was not recorded, so I won't post the picture here.
3. Generate relevant files and import them into the development board
Then call the command
make
sudo make install
The compilation time will be long. After the compilation is completed, a Python3.8.13 folder will be generated in the root directory. This folder contains all the necessary things. Copy the contents of this folder to the development board, and then add the path of the python executable file and the path of the lib to the two variables PATH and LD_LIBRARY_PATH, and you can use it.
The way to add is to modify the /etc/profile file and add
export PATH=$PATH:/usr/python3.8/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/python3.8/lib
Then use the source command to make it effective.
source /etc/profile
Now you can use python3 commands.
4. Effect
Use python3 to open the python command line in the terminal.
[attach ]598160[/attach][attach]598 160[/attach][attach]598160[ /attach][attach]598160[/attach ]
The compiled folder is nearly 300M, and I don’t have permission to upload it. You can try to compile it yourself.