RaspberryPiでQtWebEngineを使いたい

久しぶりにQtに触れる機会があったのですが、RaspberryPi用にクロスコンパイル環境を整えようとしたら意外と手間取ってしまったので覚書として残します。
手探りでやったので参考程度にお願いします。

#1. やりたいこと
ホスト環境で作成したQtWebengineを使ったブラウザアプリをクロスコンパイルしてRaspberryPiで動かしたい。

#2. 準備とか
##2-1. ホスト環境
ビルドに必要なパッケージをメモするの忘れてしまったので参考ページを見たり適宜追加してください。
全部 apt install で入ります。

``` 
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
```
##2-2.  ディレクリ構成
<pre>
~/working ┓
          ┣ build    #Qtのビルド用
          ┣ tool     #クロスコンパイラ置き場
          ┣ syroot   #Raspbianのrootfsマウント場所
          ┗ download #DLしたもの置き場
</pre>
##2-3. 必要なもののDLとか
記事作成中に最新版のRaspbianをDL。
ひとまずデスクトップ環境でアプリを動かしたいのでfullで。
あとはQtのソースとRaspbianのPATH関係を直してくれるスクリプト、クロスコンパイラもDLします。

```
$ cd ~/working/download
$ wget http://ftp.jaist.ac.jp/pub/raspberrypi/raspbian_full/images/raspbian_full-2019-07-12/2019-07-10-raspbian-buster-full.zip
$ wget https://raw.githubusercontent.com/Kukkimonsuta/rpi-buildqt/master/scripts/utils/sysroot-relativelinks.py
$ git clone git://code.qt.io/qt/qt5/ --branch 5.12.5
$ cd ../tool
$ wget https://releases.linaro.org/components/toolchain/binaries/7.4-2019.02/arm-linux-gnueabihf/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz
$ xz -dc gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf.tar.xz | tar xfv -
```
次に ~/working/download/qt5 に移動して実際のソースを取得します。
本来なら init-repository を使って使いたいモジュールのみを引っ張ってくるのですが、現時点だとうまくいかないので下記コマンドですべて引っ張ります。

```
$ cd ~/working/download/qt5
$ git submodule update --init --recursive
```

##2-4. RaspberryPi の準備
DLしてきたイメージをSDカードに焼きます。
無事にRaspberryPiが起動したらupdate後にRaspberryPiに必要なパッケージをインストールします。
参考ページに書いてある必要そうなのを片っ端から入れてます。
自分は以下をインストールしました。

```
$ sudo apt update
$ sudo apt upgrade -y
$ sudo apt install libfontconfig1-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libdbus-1-dev libssl-dev libx11-dev libxcb1-dev  libxkbcommon-x11-dev libx11-xcb-dev libxext-dev libxcb-xinerama0-dev libglu1-mesa-dev libxrender-dev libxi-dev flex bison gperf libicu-dev libxslt-dev ruby libssl-dev libxcursor-dev libxcomposite-dev libxdamage-dev libxrandr-dev libdbus-1-dev libfontconfig1-dev libcap-dev libxtst-dev libpulse-dev libudev-dev libpci-dev libnss3-dev libasound2-dev libxss-dev libegl1-mesa-dev gperf 
```

インストール後はホストPCにSDカードをマウントしてrootfs以下を working/sysroot にコピー、sysroot のPATHを修正します。
参考ページだとDLしたイメージをマウントしたり、rsync使ったりもしてます。
自分はrsync使ったら失敗するファイルがあったりしてとりあえず先に進めたかったのですぐにSDマウント->コピーに切り替えました。

```
$ sudo cp -r /media/user/rootfs/* ~/working/sysroot/
$ cd ~/working
$ chmod +x download/sysroot-relativelinks.py 
$ download/sysroot-relativelinks.py sysroot/
```
#3. Qtのビルド
##3-1. ビルドの前に… 
いくつかソースコードの修正とPATHを通します。

- ~/working/download/qt5/qtbase/mkspecs/devices/linux-rasp-pi2-g++/qmake.conf

```
-QMAKE_LIBS_EGL = -lEGL -lGLESv2
-QMAKE_LIBS_OPENVG = -lEGL -lOpenVG -lGLESv2
+QMAKE_LIBS_EGL = -lbrcmEGL -lbrcmGLESv2
+QMAKE_LIBS_OPENGL_ES2 = -lbrcmEGL -lbrcmGLESv2
+QMAKE_LIBS_OPENVG = -lbrcmEGL -lbrcmOpenVG -lbrcmGLESv2
```
この qmake.comf ですがデバイスごとに用意されていて、RaspberryPiも無印、2、3用と用意されています。
ただし、RaspberryPi 3 mobile B なんですが、OSが32bitなんで、lscpu なんかでみると armV7 なんですよね。
Qtのいろんなマニュアル見る限りだと、armV7 は2のファイルを使うみたな書き方でどっちを使うのが正しいのかわからないです。
今回はなんとなく2のファイルでビルドしてみます。

- ~/working/download/qt5/qtbase/src/plugins/platforms/xcb/gl_integrations/xcb_egl/qxcbeglwindow.cpp:96

```
-m_surface = eglCreateWindowSurface(m_glIntegration->eglDisplay(), m_config, m_window, 0);
+m_surface = eglCreateWindowSurface(m_glIntegration->eglDisplay(), m_config, (void*)m_window, 0);
```

- nss.pc がある場所にPATHを通す

```
$ find ~/working/sysroot -name nss.pc
$ export PKG_CONFIG_PATH= [nss.pc が入ってるディレクトリ]
```

##3-2. configure
各種オプションは configure --help 参照。

```
$ cd ~/working/build
$ ../download/qt5/configure -release -opengl es2 -device linux-rasp-pi2-g++ \
-device-option CROSS_COMPILE=~/working/tool/gcc-linaro-7.4.1-2019.02-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf- \
-sysroot ~/working/sysroot -prefix /opt/qt5.12 -opensource -confirm-license \
-skip qtscript -no-use-gold-linker -no-gbm -nomake examples -nomake tests -no-webengine-v8-snapshot \
-make libs -qpa xcb
```

出力

```
Creating qmake...
.Done.

This is the Qt Open Source Edition.

You have already accepted the terms of the Open Source license.

Running configuration tests...
Done running configuration tests.

Configure summary:

Building on: linux-g++ (x86_64, CPU features: mmx sse sse2)
Building for: devices/linux-rasp-pi2-g++ (arm, CPU features: neon)
Target compiler: gcc 7.4.1
Configuration: cross_compile compile_examples enable_new_dtags largefile neon precompile_header shared rpath release c++11 c++14 c++1z concurrent dbus reduce_exports stl
Build options:
  Mode ................................... release
  Optimize release build for size ........ no
  Building shared libraries .............. yes
  Using C standard ....................... C11
  Using C++ standard ..................... C++1z
  Using ccache ........................... no
  Using gold linker ...................... no
  Using new DTAGS ........................ yes
  Using precompiled headers .............. yes
  Using LTCG ............................. no
  Target compiler supports:
    NEON ................................. yes
  Build parts ............................ libs
Qt modules and options:
  Qt Concurrent .......................... yes
  Qt D-Bus ............................... yes
  Qt D-Bus directly linked to libdbus .... yes
  Qt Gui ................................. yes
  Qt Network ............................. yes
  Qt Sql ................................. yes
  Qt Testlib ............................. yes
  Qt Widgets ............................. yes
  Qt Xml ................................. yes
Support enabled for:
  Using pkg-config ....................... yes
  udev ................................... yes
  Using system zlib ...................... yes
Qt Core:
  DoubleConversion ....................... yes
    Using system DoubleConversion ........ no
  GLib ................................... yes
  iconv .................................. no
  ICU .................................... yes
  Tracing backend ........................ <none>
  Logging backends:
    journald ............................. no
    syslog ............................... no
    slog2 ................................ no
  Using system PCRE2 ..................... no
Qt Network:
  getifaddrs() ........................... yes
  IPv6 ifname ............................ yes
  libproxy ............................... no
  Linux AF_NETLINK ....................... yes
  OpenSSL ................................ yes
    Qt directly linked to OpenSSL ........ no
  OpenSSL 1.1 ............................ yes
  DTLS ................................... yes
  SCTP ................................... no
  Use system proxies ..................... yes
Qt Gui:
  Accessibility .......................... yes
  FreeType ............................... yes
    Using system FreeType ................ yes
  HarfBuzz ............................... yes
    Using system HarfBuzz ................ no
  Fontconfig ............................. yes
  Image formats:
    GIF .................................. yes
    ICO .................................. yes
    JPEG ................................. yes
      Using system libjpeg ............... no
    PNG .................................. yes
      Using system libpng ................ yes
  EGL .................................... yes
  OpenVG ................................. yes
  OpenGL:
    Desktop OpenGL ....................... no
    OpenGL ES 2.0 ........................ yes
    OpenGL ES 3.0 ........................ yes
    OpenGL ES 3.1 ........................ yes
    OpenGL ES 3.2 ........................ yes
  Vulkan ................................. no
  Session Management ..................... yes
Features used by QPA backends:
  evdev .................................. yes
  libinput ............................... no
  INTEGRITY HID .......................... no
  mtdev .................................. no
  tslib .................................. no
  xkbcommon .............................. yes
  X11 specific:
    XLib ................................. yes
    XCB Xlib ............................. yes
    EGL on X11 ........................... yes
QPA backends:
  DirectFB ............................... no
  EGLFS .................................. yes
  EGLFS details:
    EGLFS OpenWFD ........................ no
    EGLFS i.Mx6 .......................... no
    EGLFS i.Mx6 Wayland .................. no
    EGLFS RCAR ........................... no
    EGLFS EGLDevice ...................... yes
    EGLFS GBM ............................ no
    EGLFS VSP2 ........................... no
    EGLFS Mali ........................... no
    EGLFS Raspberry Pi ................... yes
    EGLFS X11 ............................ yes
  LinuxFB ................................ yes
  VNC .................................... yes
  Mir client ............................. no
  XCB:
    Using system-provided XCB libraries .. yes
    XCB XKB .............................. yes
    XCB XInput ........................... yes
    Native painting (experimental) ....... yes
    GL integrations:
      GLX Plugin ......................... no
      EGL-X11 Plugin ..................... yes
Qt Sql:
  SQL item models ........................ yes
Qt Widgets:
  GTK+ ................................... no
  Styles ................................. Fusion Windows
Qt PrintSupport:
  CUPS ................................... no
Qt Sql Drivers:
  DB2 (IBM) .............................. no
  InterBase .............................. no
  MySql .................................. no
  OCI (Oracle) ........................... no
  ODBC ................................... no
  PostgreSQL ............................. no
  SQLite2 ................................ no
  SQLite ................................. yes
    Using system provided SQLite ......... no
  TDS (Sybase) ........................... no
Qt Testlib:
  Tester for item models ................. yes
Qt SerialBus:
  Socket CAN ............................. yes
  Socket CAN FD .......................... yes
Further Image Formats:
  JasPer ................................. no
  MNG .................................... no
  TIFF ................................... yes
    Using system libtiff ................. no
  WEBP ................................... yes
    Using system libwebp ................. no
Qt QML:
  QML network support .................... yes
  QML debugging and profiling support .... yes
  QML sequence object .................... yes
  QML list model ......................... yes
  QML XML http request ................... yes
  QML Locale ............................. yes
  QML delegate model ..................... yes
Qt Quick:
  Direct3D 12 ............................ no
  AnimatedImage item ..................... yes
  Canvas item ............................ yes
  Support for Qt Quick Designer .......... yes
  Flipable item .......................... yes
  GridView item .......................... yes
  ListView item .......................... yes
  TableView item ......................... yes
  Path support ........................... yes
  PathView item .......................... yes
  Positioner items ....................... yes
  Repeater item .......................... yes
  ShaderEffect item ...................... yes
  Sprite item ............................ yes
Qt Scxml:
  ECMAScript data model for QtScxml ...... yes
Qt Gamepad:
  SDL2 ................................... no
Qt 3D:
  Assimp ................................. yes
  System Assimp .......................... no
  Output Qt3D Job traces ................. no
  Output Qt3D GL traces .................. no
  Use SSE2 instructions .................. no
  Use AVX2 instructions .................. no
  Aspects:
    Render aspect ........................ yes
    Input aspect ......................... yes
    Logic aspect ......................... yes
    Animation aspect ..................... yes
    Extras aspect ........................ yes
Qt 3D Renderers:
  OpenGL Renderer ........................ yes
Qt 3D GeometryLoaders:
  Autodesk FBX ........................... no
Qt Wayland Client ........................ no
Qt Wayland Compositor .................... no
Qt Bluetooth:
  BlueZ .................................. no
  BlueZ Low Energy ....................... no
  Linux Crypto API ....................... no
  WinRT Bluetooth API (desktop & UWP) .... no
Qt Sensors:
  sensorfw ............................... no
Qt Quick Controls 2:
  Styles ................................. Default Fusion Imagine Material Universal
Qt Quick Templates 2:
  Hover support .......................... yes
  Multi-touch support .................... yes
Qt Positioning:
  Gypsy GPS Daemon ....................... no
  WinRT Geolocation API .................. no
Qt Location:
  Qt.labs.location experimental QML plugin . yes
  Geoservice plugins:
    OpenStreetMap ........................ yes
    HERE ................................. yes
    Esri ................................. yes
    Mapbox ............................... yes
    MapboxGL ............................. yes
    Itemsoverlay ......................... yes
QtXmlPatterns:
  XML schema support ..................... yes
Qt Multimedia:
  ALSA ................................... yes
  GStreamer 1.0 .......................... yes
  GStreamer 0.10 ......................... no
  Video for Linux ........................ yes
  OpenAL ................................. no
  PulseAudio ............................. yes
  Resource Policy (libresourceqt5) ....... no
  Windows Audio Services ................. no
  DirectShow ............................. no
  Windows Media Foundation ............... no
Qt Tools:
  QDoc ................................... no
Qt WebEngine:
  Embedded build ......................... yes
  Full debug information ................. no
  Pepper Plugins ......................... no
  Printing and PDF ....................... no
  Proprietary Codecs ..................... no
  Spellchecker ........................... yes
  Native Spellchecker .................... no
  WebRTC ................................. no
  Use System Ninja ....................... no
  Geolocation ............................ yes
  WebChannel support ..................... yes
  Use v8 snapshot ........................ no
  Kerberos Authentication ................ no
  Support qpa-xcb ........................ yes
  Use ALSA ............................... yes
  Use PulseAudio ......................... yes
  Optional system libraries used:
    re2 .................................. no
    icu .................................. no
    libwebp, libwebpmux and libwebpdemux . no
    opus ................................. no
    ffmpeg ............................... no
    libvpx ............................... no
    snappy ............................... no
    glib ................................. yes
    zlib ................................. yes
    minizip .............................. no
    libevent ............................. no
    jsoncpp .............................. no
    protobuf ............................. no
    libxml2 and libxslt .................. yes
    lcms2 ................................ no
    png .................................. yes
    JPEG ................................. no
    harfbuzz ............................. no
    freetype ............................. yes
  Required system libraries:
    fontconfig ........................... yes
    dbus ................................. yes
    nss .................................. yes
    khr .................................. yes
    glibc ................................ yes
  Required system libraries for qpa-xcb:
    x11 .................................. yes
    libdrm ............................... yes
    xcomposite ........................... yes
    xcursor .............................. yes
    xi ................................... yes
    xtst ................................. yes

Note: Also available for Linux: linux-clang linux-icc

Note: Disabling X11 Accessibility Bridge: D-Bus or AT-SPI is missing.

WARNING: QDoc will not be compiled, probably because libclang could not be located. This means that you cannot build the Qt documentation.

Either ensure that llvm-config is in your PATH environment variable, or set LLVM_INSTALL_DIR to the location of your llvm installation.
On Linux systems, you may be able to install libclang by installing the libclang-dev or libclang-devel package, depending on your distribution.
On macOS, you can use Homebrew's llvm package.
On Windows, you must set LLVM_INSTALL_DIR to the installation path.

Qt is now configured for building. Just run 'make'.
Once everything is built, you must run 'make install'.
Qt will be installed into '/home/sora/working/sysroot/opt/qt5.12'.

Prior to reconfiguration, make sure you remove any leftovers from
the previous build.

```


##3-3. make & make install
この make 時にQtWebEngine 周りのビルドでいろんなヘッダーが見つからないよって言われるので適宜PATHを通します。

```
$ make -j*
$ sudo make install -j*
```

#4. 動作確認
ビルドできたQt一式をRaspberryPiに移して動作確認してきます。

##4-1. qmake
動作確認用の qtwebengine の example をビルド。

```
$ cp -r ~/working/download/qt5/qtwebengine/examples/webengine/quicknanobrowser /どこか適当な場所
$ cd 適当な場所
$ ~/working/sysroot/opt/qt5.12/bin/qmake
$ make
```

##4-2. 転送&実行
~/working/sysroor/opt/qt5.12 下一式を RaspberryPi の/opt/にコピー。
4-1 で作った quicknanobrowser を適当な場所にコピー。
RaspberryPi 上で実行できればOK!
(めっちゃ見切れちゃってます)
![2019-09-14-223950_800x480_scrot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/221989/d1b9e1f8-06c2-7f12-9b7f-e976ed6e91df.png)


#5. 参考ページ
- https://wiki.qt.io/Building_Qt_5_from_Git
- https://wiki.qt.io/RaspberryPi2EGLFS
- https://qiita.com/task_jp/items/428744efe67320cd4d6a
- https://www.tal.org/tutorials/building-qt-512-raspberry-pi
- https://ottycodes.wordpress.com/2019/02/21/cross-compiling-qt5-12-for-raspberry-pi3/
- https://bugreports.qt.io/browse/QTBUG-75328
- https://bugreports.qt.io/browse/QTBUG-62216