## 移植QT5.15
>Host:Linux环境:(阿里云虚拟机)
>
>Linux version 5.4.0-110-generic (buildd@ubuntu) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)) #124-Ubuntu SMP Thu Apr 14 19:46:19 UTC 2022
>
>Target:Linux环境(DockRV)
>
>Linux version 5.4.61 (root@aly) (riscv64-unknown-linux-gnu-gcc (C-SKY RISCV Tools V1.8.4 B20200702) 8.1.0, GNU ld (GNU Binutils) 2.32) #10 PREEMPT Sat May 28 12:08:35 UTC 2022
>
>参考链接:
>
>[基于qemu-riscv从0开始构建嵌入式linux系统ch23. linux FB应用——Qt库移植 - CodeAntenna](https://codeantenna.com/a/WAzk1zNnDM)
>
>[基于qemu-riscv从0开始构建嵌入式linux系统ch23. linux FB应用——Qt库移植_Quard_D的博客-CSDN博客](https://blog.csdn.net/weixin_39871788/article/details/119842179)
>
>[Qt-5.3.2 在友善Smart210开发板的移植记录_NewThinker_wei的博客-CSDN博客](https://blog.csdn.net/newthinker_wei/article/details/39560109)
>
>[移植QT5.9.1 — Lichee zero 文档](https://licheezero.readthedocs.io/zh/latest/应用/QT_doc2.html)
### 下载源码
> 本次移植的是QT5.15.4,需要在Linux上交叉编译移植到RISC-V开发板上
[点击下载QT5.15.4](https://mirrors.tuna.tsinghua.edu.cn/qt/official_releases/qt/5.15/5.15.4/single/qt-everywhere-opensource-src-5.15.4.tar.xz)
tar zxvf qt-everywhere-opensource-src-5.15.4.tar.xz
cd qt-everywhere-opensource-src-5.15.4
```
解压完毕后进入QT源码目录
### 环境配置
配置交叉编译工具链
添加交叉编译配置文件`qt-everywhere-src-5.15.4/qtbase/mkspecs/linux-riscv64-gnu-g++/qmake.conf`,内容如下:
```shell
# qmake configuration for building with riscv64-unknown-linux-gnu-g++no
MAKEFILE_GENERATOR = UNIX
CONFIG += incremental
QMAKE_INCREMENTAL_STYLE = sublib
include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)
# modifications to g++.conf
QMAKE_CC = riscv64-unknown-linux-gnu-gcc # 需要使用的交叉编译工具链的名称
QMAKE_CXX = riscv64-unknown-linux-gnu-g++
QMAKE_LINK = riscv64-unknown-linux-gnu-g++
QMAKE_LINK_SHLIB = riscv64-unknown-linux-gnu-g++
QMAKE_LIBS = -latomic
# modifications to linux.conf
QMAKE_AR = riscv64-unknown-linux-gnu-ar cqs
QMAKE_OBJCOPY = riscv64-unknown-linux-gnu-objcopy
QMAKE_NM = riscv64-unknown-linux-gnu-nm -P
QMAKE_STRIP = riscv64-unknown-linux-gnu-strip
load(qt_config)
```
添加编译配置文件`qt-everywhere-src-5.15.4/qtbase/mkspecs/linux-riscv64-gnu-g++/qplatformdefs.h`,内容如下:
```c
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the qmake spec of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "../linux-g++/qplatformdefs.h"
```
修改源码中一个缺失的头文件包含,位于 `qt-everywhere-src-5.15.4/qtdeclarative/src/qmldebug/qqmlprofilerevent_p.h` :52行,添加如下内容:
```c
#include <limits>
```
**在QT源码根目录创建 `configure` 自动配置文件**
```bash
vi autoConfigure.sh
```
文件内容如下,具体配置意思自己百度。
```bash
export PATH=$PATH:/home/leesum/tina-d1-h/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702/bin/
./configure -prefix ./riscv-qt \
-opensource \
-confirm-license \
-release \
-strip \
-shared \
-xplatform linux-riscv64-gnu-g++ \
-optimized-qmake \
-c++std c++11 \
--rpath=no \
-pch \
-skip qt3d \
-skip qtactiveqt \
-skip qtandroidextras \
-skip qtcanvas3d \
-skip qtconnectivity \
-skip qtdatavis3d \
-skip qtdoc \
-skip qtgamepad \
-skip qtlocation \
-skip qtmacextras \
-skip qtnetworkauth \
-skip qtpurchasing \
-skip qtremoteobjects \
-skip qtscript \
-skip qtscxml \
-skip qtsensors \
-skip qtspeech \
-skip qtsvg \
-skip qttools \
-skip qttranslations \
-skip qtwayland \
-skip qtwebengine \
-skip qtwebview \
-skip qtwinextras \
-skip qtx11extras \
-skip qtxmlpatterns \
-make libs \
-make examples \
-nomake tools -nomake tests \
-gui \
-widgets \
-dbus-runtime \
--glib=no \
--iconv=no \
--pcre=qt \
--zlib=qt \
-no-openssl \
--freetype=qt \
--harfbuzz=qt \
-no-opengl \
-linuxfb \
--xcb=no \
-tslib \
--libpng=qt \
--libjpeg=qt \
--sqlite=qt \
-plugin-sql-sqlite \
-I/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/include/ \
-I/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/include/allwinner/ \
-I/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/include/allwinner/include/ \
-L/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/lib/ \
-recheck-all
```
几个关键的配置选项说明介绍一下
+ `-xplatform linux-riscv64-gnu-g++`:
`linux-riscv64-gnu-g++` 就是刚刚创建的配置文件夹里面有刚刚创建的`qmake.conf` `qplatformdefs.h`,指明了所使用的交叉编译工具链。
+ `-I/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/include/`
+ `-I/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/include/allwinner/`
+ `-I/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/include/allwinner/include/`
指示说明库头文件的位置
+ `-L/home/leesum/tina-d1-h/out/d1-h-nezha/staging_dir/target/usr/lib/`
说明库的位置
+ `export PATH=$PATH:/home/leesum/tina-d1-h/prebuilt/gcc/linux-x86/riscv/toolchain-thead-glibc/riscv64-glibc-gcc-thead_20200702/bin/`
说明交叉编译工具链的位置
**位置参考自己`Tina-SDK`的下载目录**
为`autoConfigure.sh`添加可执行权限
```bash
chmod +x autoConfigure.sh
```
### 交叉编译QT5
进入QT源码根目录,运行刚刚配置的环境。
```bash
source ./autoConfigure.sh
```
若配置成功会显示以下信息
![image-20220529224209489](https://cdn.jsdelivr.net/gh/zilongmix/doc/img/image-20220529224209489.png)
开始编译,具体核心数按照自己的配置指定,预留 **8G** 空间已足够使用。
```bash
make -j2
```
我按照以上配置一次编译成功,没有遇见错误。
编译成功后执行
```bash
make install
```
安装目录在**qt-everywhere-src-5.15.4/qtbase/bin/riscv-qt**
**riscv-qt** 就是刚刚配置文件中`-prefix ./riscv-qt`指定的目录。
![](https://cdn.jsdelivr.net/gh/zilongmix/doc/img/image-20220529231814325.png)
将该目录移动到DockRV开发板上,具体方法可以 `tar` 打包后通过 `adb push` 或者`SFTP`发送。
### 测试examples
进入QT5目录,创建环境配置文件 `qt-path`
```shell
vi qt-path
#内容如下
echo begin
export QT_HOME=/root/bin/riscv-qt
export QT_QPA_PLATFORM=linuxfb:tty=/dev/fb0
export QT_QPA_FB_DRM=1
export QT_QPA_GENERIC_PLUGINS=evdevkeyboard
export QT_QPA_GENERIC_PLUGINS=evdevmouse
export QT_QPA_EVDEV_MOUSE_PARAMETERS=/dev/input/event1
export QT_QPA_EVDEV_KEYBOARD_PARAMETERS=/dev/input/event0
export QT_PLUGIN_PATH=$QT_HOME/plugins
export LD_LIBRARY_PATH=/lib:/usr/lib:/$QT_HOME/lib
```
鼠标和键盘具体的 **/dev/input/eventx** 可以通过`getevent`获得
![image-20220529230516666](https://cdn.jsdelivr.net/gh/zilongmix/doc/img/image-20220529230516666.png)
激活QT运行环境
```bash
source ./qt-path
```
如果显示
```bash
./zoomlinechart: error while loading shared libraries: libQt5Charts.so.5: cannot open shared object file: No such file or directory
```
检查 **qt-path** 的 LIB 配置
进入 example 目录,运行自带例程
```bash
cd examples/gui/analogclock
chmod +x analogclock
./analogclock
```
![image-20220529231446060](https://cdn.jsdelivr.net/gh/zilongmix/doc/img/image-20220529231446060.png)
HDMI接口显示正常,并且鼠标能够正常移动,可自行测试其他example
由于移植的QT5所有的绘制都是基于软件实现的,运行效果很卡,有好的解决方法可以相互交流。
字体问题也有待解决
![image-20220529232317262](https://cdn.jsdelivr.net/gh/zilongmix/doc/img/image-20220529232317262.png)
### 解决QT5字体问题
> 因为编译的源码库中没有自带fonts,所以不能显示文字。
进入开发板QT5目录,在 `lib` 文件夹中创建 `fonts` 文件夹
```shell
mkdir lib/fonts
```
下载 `TTF 字体文件` 将字体放入刚刚创建的 `fonts` 文件夹中。
[adobe-fonts/source-sans: Sans serif font family for user interface environments (github.com)](https://github.com/adobe-fonts/source-sans)
![](https://cdn.jsdelivr.net/gh/zilongmix/doc/img/20220530134521.png)
**测试**
![](https://cdn.jsdelivr.net/gh/zilongmix/doc/img/20220530135034.gif)
离线