以下內容沒有經過語言的整理,但是是我真實的移植過程,僅供參考
源碼下載地址:https://www.python.org/
參考:
https://blog.csdn.net/yyw794/article/details/78059102
https://www.cnblogs.com/pengdonglin137/p/6604471.html
本次移植的是Python-3.6.9 後來我又移植了3.4.6,大部分一直,只有sh有差別。
我的目錄是:\nfs_root\app\Python\Python-3.6.9
Python2.7.13的交叉編譯,在編譯前需要給Python源碼打上用於交叉編譯的patch,從Python-3.3.1開始,就不需要再打什麼patch了,只需要在configure的時候指定--build和--host參數即可。
很重要先編譯PC的
因爲在交叉編譯的時候需要在PC上面運行對應版本的Python程序,所以我們需要先編譯和安裝PC上面用的Python,下面是命令:
./configure
make
sudo make install
測試下是否可以了
新建一個文件:helloword.py
# 該實例輸出 Hello World!
print('Hello World!')
運行:python3 ./helloword.py
輸出
PC環境搭建完成,下面開始編譯arm
這是Python-3.6.4 在3.6.9應該也可以使用
#prepare
echo "prepare stage"
arm_install=`pwd`/arm_python
arm_build=`pwd`/arm_build
mkdir $arm_build
mkdir $arm_install
cd `pwd`
#arm comfigure
echo "arm confiure stage"
cd $arm_build
echo ac_cv_file__dev_ptmx=yes > config.site
echo ac_cv_file__dev_ptc=yes >> config.site
export CONFIG_SITE=config.site
../configure \
--host=arm-fsl-linux-gnueabi \
--build=i686-linux-gnu \
--target=arm-fsl-linux-gnueabi \
--disable-ipv6 \
--prefix=$arm_install \
--enable-shared \
--silent
#pc pgen
echo "pc pgen stage"
cd -
./configure --silent
for args in $@
do
if [ $args = "all" ];then
make --silent && make install --silent
break
fi
done
make Parser/pgen --silent
cd -
cp ../Parser/pgen Parser
#change the pgen time,
# or else the cross compile will replace this pc version pgen. important!!
touch -t 12312359 Parser/pgen
#make
echo "make stage"
make python --silent && make -i install
#make it smaller
#arm-fsl-linux-gnueabi-strip -s $arm_install/bin/python3.3
exit 0
這是Python-3.6.9
#!/bin/bash
#prepare
echo "make clean"
make clean
echo "prepare stage"
arm_install=`pwd`/arm_python
arm_build=`pwd`/arm_build
mkdir $arm_build
mkdir $arm_install
cd `pwd`
#arm comfigure
echo "arm confiure stage"
cd $arm_build
#配置交叉編譯:
#CC爲指定C交叉編譯器,我的是arm-fsl-linux-gnueabi-gcc
#CXX爲指定C++交叉編譯器,我的是arm-fsl-linux-gnueabi-g++
#AR爲ar工具,我的是arm-fsl-linux-gnueabi-ar
#RANLIB爲ranlib工具,我的是arm-fsl-linux-gnueabi-ranlib
#Host爲目標主機,我這裏設置的是arm-fsl-linux-gnueabi
#Build爲編譯環境主機,我的是i386
#Prefix爲安裝位置
#配置命令:
../configure \
CC=arm-fsl-linux-gnueabi-gcc \
CXX=arm-fsl-linux-gnueabi-g++ \
AR=arm-fsl-linux-gnueabi-ar \
RANLIB=arm-fsl-linux-gnueabi-ranlib \
--host=arm-fsl-linux-gnueabi \
--build=i386 \
--disable-ipv6 \
--prefix=$arm_install \
ac_cv_file__dev_ptmx=yes \
ac_cv_file__dev_ptc=yes
echo "make stage"
make HOSTPYTHON=./python \
HOSTPGEN=./Parser/pgen \
BLDSHARED="arm-fsl-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-fsl-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes \
HOSTARCH=arm-fsl-linux-gnueabi \
BUILDARCH=i386
#************************************************
#如果報錯了,則需要刪除下面的內容,先使用
#find / -name lsb_release
#查找路徑是否正確別刪錯了
#rm -rf /usr/bin/lsb_release
#************************************************
#echo "rm -rf /usr/bin/lsb_release"
#rm -rf /usr/bin/lsb_release
echo "make install stage"
sudo make install HOSTPYTHON=./python \
BLDSHARED="arm-fsl-linux-gnueabi-gcc -shared" \
CROSS_COMPILE=arm-fsl-linux-gnueabi- \
CROSS_COMPILE_TARGET=yes
用上面我修改的腳本不會報告下圖中的錯誤,遇到的兄弟可以使用後面的語句解決,下面的錯誤是在make install時候出現的
subprocess.CalledProcessError: Command ‘(‘lsb_release’, ‘-a’)’ returned non-zero exit status 1.
解決方法
find / -name lsb_release
rm -rf /usr/bin/lsb_release
編譯完成後會在下圖中顯示你需要的文件
其中arm_python是需要跑在硬件上的
在開發板上,運行
vi /etc/profile
添加環境變量
export PATH=/usr/python/bin:$PATH
export LD_LIBRARY_PATH=/usr/python/lib:$LD_LIBRARY_PATH
保存退出。將arm_python下的內容拷貝到開發板上/usr/python下
運行:source /etc/profile 或者重啓你的開發板
可以使用
echo $LD_LIBRARY_PATH 驗證lib
echo $PATH 驗證bin
測試python是否移植成功
任意目錄執行python3 ./helloworld.py
看到 Hello World! 說明成功了