https://gitee.com/liangzili/harmony-raspberry
编译鸿蒙的源码建议在 Ubuntu20.04 下进行,Windows 下会出现各种奇怪的问题,不建议使用。
①VirtualBox 虚拟机安装
VirtualBox官网下载地址:https://www.virtualbox.org/
Ubuntu20.04官网下载地址:https://ubuntu.com/download/desktop
关于 VirtualBox 和 Ubuntu 的安装和使用就不展开了,百度一下资料很多。
②改 sh 为 bash
ls -l /bin/sh #如果显示为“/bin/sh -> bash”则为正常,否则请按以下方式修改:
sudo dpkg-reconfigure dash #然后选择no
③安装打包工具 mkfs.vfat mtools
sudo apt-get install dosfstools mtools #官方要求安装的2个工具
sudo apt-get install zip #官方文档没写,但是rootfs过程需要
sudo apt-get install python3-distutils #官方文档没写,但是build过程需要
④下载安装编译工具 gn ninja llvm hc-gen
hc-gen:鸿蒙驱动 hdf 框架之类的,用于生成鸿蒙驱动配置对应的文件。
#下载gn/ninja/LLVM/hc-gen包:
URL_PREFIX=https://repo.huaweicloud.com/harmonyos/compiler
wget $URL_PREFIX/gn/1523/linux/gn.1523.tar
wget $URL_PREFIX/ninja/1.9.0/linux/ninja.1.9.0.tar
wget $URL_PREFIX/clang/9.0.0-34042/linux/llvm-linux-9.0.0-34042.tar
wget $URL_PREFIX/hc-gen/0.65/linux/hc-gen-0.65-linux.tar
wget $URL_PREFIX/gcc_riscv32/7.3.0/linux/gcc_riscv32-linux-7.3.0.tar.gz #【可选】编译riscv需要,如wifiiot
#解压gn/ninja/LLM/hc-gen包:
tar -C ~/ -xvf gn.1523.tar
tar -C ~/ -xvf ninja.1.9.0.tar
tar -C ~/ -xvf llvm-linux-9.0.0-34042.tar
tar -C ~/ -xvf hc-gen-0.65-linux.tar
tar -C ~/ -xvf gcc_riscv32-linux-7.3.0.tar.gz #【可选】编译riscv需要,如wifiiot
#向~/.bashrc中追加gn/ninja/LLVM/hc-gen路径配置:
cat <> ~/.bashrc
export PATH=~/build_tools/gn:\$PATH
export PATH=~/build_tools/ninja:\$PATH
export PATH=~/build_tools/llvm/bin:\$PATH
export PATH=~/build_tools/hc-gen:\$PATH
#export PATH=~/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin:\$PATH
export PATH=~/gcc_riscv32/bin:\$PATH #【可选】编译riscv需要,如wifiiot
#export PATH=~/.local/bin:\$PATH
EOF
#生效环境变量
source ~/.bashrc
⑤安装 python3.8 和 pip
编译鸿蒙源码 hi3861 目标平台需要使用 scons,默认安装的 scons 版本需要 python 版本>=3.7:
a、
如果是 ubuntu20.04,因为默认 python 是 3.8,正好符合要求,只需要安装 pip 即可,可以进行第 6 步了。
sudo apt-get install python3-pip
注意:如果 pip 下载慢,配置 pip 包下载源,加速国内安装 pip 包。
mkdir~/.pip/
cat < ~/.pip/pip.conf
[global]
index-url = https://mirrors.huaweicloud.com/repository/pypi/simple
trusted-host = mirrors.huaweicloud.com
timeout = 120
EOF
b、
如果是其他版本,比如 ubuntu18.04,默认 Python3 版本是 3.6,得先安装 python3.8。
sudo apt-get install python3.8 python3-pip
sudo apt-get install python3-pip
安装完成后,需要将默认 python 切换为 3.8,比较麻烦的是多个 python 版本的管理,通常有两种方式:
方案一:
使用 virtualenv 管理,单独 source 相应的 active 脚本切换当前 shell 会话的默认 python 版本。
virtualenv 管理多个 python 运行环境,不适用 virtualenv 的情况下,pip install 和 sudo pip install 的包会分别放到用户 home 目录和系统目录。
安装 virtualenv:
pip3 install virtualenv
sudo apt install python3-virtualenv
#创建使用python3.8为默认python解释器的virtualenv
mkdir ~/Harmony/venv && virtualenv -p python3.8 ~/Harmony/venv
#激活virtualenv,激活后的pip3 install会将包文件缓存到相应的子目录中
source ~/harmonyos/venv/bin/activate
source ~/Harmony/.venv/bin/activate
方案二:
使用 update-alternatives 管理,并切换全局 Python 脚本配置(不推荐,用完需要切换回去,否则会影响 apt 等依赖 python3 的软件包使用)。
update-alternatives,它是 Debian 系管理多版本软件的工具,不仅适用 Python ,而且还使用 Java
等各种有多版本共存需求的软件。
它跟 virtualenv 这种虚拟环境不同的是,update-alternatives
管理的是系统级的软件版本,virtualenv 管理的是当前用户下的一个虚拟环境。
⑥确定 Python 的环境变量
方案一:
使用【ln -s】,确定 Python 安装好后,运行如下命令,将 Python 路径链接到"/usr/bin/python"。
which python3.8 #查看python3.8的安装位置
输出:/usr/bin/python3.8 #比如我的安装目录
cd /usr/bin #进入python的目录
sudo rm python #删除python
sudo ln -s /usr/local/bin/python3.8 python #将python链接到python3.8
python --version
方案二:
sudo apt-get install python3-venv #安装 venv 虚拟环境。
python3 -m venv .venv #推荐进入项目的根目录再执行此命令
source .venv/bin/activate #进入虚拟环境