2011年6月1日 星期三

[Embedded] How to Porting Qt to Target Board

本篇算是工作紀錄,說明如何移植Qt-4.6.2到某目標板,並能啟動examples下的fancybrowser,能支援中文字型,支援JavaScript,不喜勿入,也不說明是哪個板子,實際移植時還需視當時環境做調整

environment:
* host: ubuntu 10.04
* client: target board(?)

1. Get Source
host$ cd ~
host$ wget http://get.qt.nokia.com/qt/source/qt-everywhere-opensource-src-4.6.2.tar.gz
host$ tar zxvf qt-everywhere-opensource-src-4.6.2.tar.gz


2. Settup Toolchain
2-1) add toolchain to environment variable
host$ vim ~/.bashrc
export MY_TOOLCHAIN=/path/to/toolchain    # add this line
export PATH=$PATH:$MY_TOOLCHAIN           # add this line
host$ source ~/.bashrc


3. Qmake
3-1) http://en.wikipedia.org/wiki/Qmake

3-2) modify [/path/to/qt]/mkspecs/qws/linux-arm-g++/qmake.conf
include(../../common/g++.conf)
include(../../common/linux.conf)
include(../../common/qws.conf)
QMAKE_CC            = /path/to/toolchain/gcc
QMAKE_CXX           = /path/to/toolchain/g++
QMAKE_LINK          = /path/to/toolchain/g++
QMAKE_LINK_SHLIB    = /path/to/toolchain/g++
QMAKE_AR            = /path/to/toolchain/ar cqs
QMAKE_OBJCOPY       = /path/to/toolchain/objcopy
QMAKE_STRIP         = /path/to/toolchain/strip
load(qt_config)

3-3) modify fancybrowser
host$ cd ~/qt-everywhere-opensource-src-4.6.2/example/webkit/fancybrowser
host$ vim main.cpp
#include 
#include "mainwindow.h"

int main(int argc, char * argv[])
{
    QApplication app(argc, argv, QApplication::GuiServer);
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");  // add this line for supporting UTF-8 encodeing
    QTextCodec::setCodecForLocale(codec);                   // add this line
    QTextCodec::setCodecForCStrings(codec);                 // add this line
    QTextCodec::setCodecForTr(codec);                       // add this line
    QFont font("wenquanyi",6);                              // add this line for setting font name
    app.setFont(font);                                      // add this line
    MainWindow browser;
    browser.show();
    return app.exec();
}

host$ vim mainwindow.cpp
//! [2]
    view = new QWebView(this);
    view->load(QUrl("http://www.google.com.tw/"));
    view->settings()->setAttribute(QWebSettings::PluginsEnabled, true);            // add this line
    view->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);         // add this line
    view->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);  // add this line
    connect(view, SIGNAL(loadFinished(bool)), SLOT(adjustLocation()));
    connect(view, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
    connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
    connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));


4. Config Parameters & Install
4-1) important parameter
-embedded arm                  // cross-compile for arm platform
-xplatform qws/linux-arm-g++   // cross-compile for arm platform with linux-arm-g++ qmake
-plugin-gfx-directfb           // display with directfb through plugin

4-2) config with your library
host$ ./configure -embedded arm -xplatform qws/linux-arm-g++ -debug -fast -little-endian -no-largefile -accessibility -no-qt3support -webkit -javascript-jit -script -scripttools -qt-zlib -qt-gif -qt-libtiff -qt-libpng -qt-libmng -qt-libjpeg -no-dbus -no-mmx -no-3dnow -no-sse -no-sse2 -openssl -no-gtkstyle -no-nas-sound -no-opengl -no-openvg -no-sm -no-xshape -no-xsync -no-xinerama -xcursor -no-xfixes -no-xrandr -no-xrender -no-mitshm -no-fontconfig -no-xinput -no-xkb -freetype -no-neon -no-glib -plugin-gfx-directfb -I/path/to/directfb/header -ldirectfb -I/path/to/library/header -L/path/to/shared/library -lg -lssl -lcrypto -lz -lm -lpng -ljpeg -lfreetype -lpthread -v -confirm-license
host$ make
host$ sudo make install

4-3) add Chinese font support(download wenquanyi_120_50.qpf here)
host$ cd /usr/local/Trolltech/QtEmbedded-4.6.2-arm/lib
host$ sudo rm -f fonts/*
host$ sudo vim fontdir
wqx wenquanyi_120_50.qpf QPF n 50 120
wqx wenquanyi_120_75.qpf QPF n 75 120
wqx wenquanyi_130_50.qpf QPF n 50 130
wqx wenquanyi_130_75.qpf QPF n 75 130
wqx wenquanyi_150_50.qpf QPF n 50 150
wqx wenquanyi_150_75.qpf QPF n 75 150
wqx wenquanyi_160_50.qpf QPF n 50 160
wqx wenquanyi_160_75.qpf QPF n 75 160
host$ sudo cp [/path/to]/wenquanyi_120_50.qpf fonts

4-4) copy shared library instead of symbolic link
host$ cd /usr/local/Trolltech/QtEmbedded-4.6.2-arm/lib
host$ sudo cp libQtNetwork.so.4.6.2 libQtNetwork.so.4
host$ sudo cp libQtNetwork.so.4.6.2 libQtNetwork.so.4.6
host$ sudo cp libQtNetwork.so.4.6.2 libQtNetwork.so
host$ sudo cp libQtGui.so.4.6.2 libQtGui.so.4
host$ sudo cp libQtGui.so.4.6.2 libQtGui.so.4.6
host$ sudo cp libQtGui.so.4.6.2 libQtGui.so
host$ sudo cp libQtCore.so.4.6.2 libQtCore.so.4
host$ sudo cp libQtCore.so.4.6.2 libQtCore.so.4.6
host$ sudo cp libQtCore.so.4.6.2 libQtCore.so
host$ sudo cp libQtXml.so.4.6.2 libQtXml.so.4
host$ sudo cp libQtXml.so.4.6.2 libQtXml.so.4.6
host$ sudo cp libQtXml.so.4.6.2 libQtXml.so
host$ sudo cp libQtMultimedia.so.4.6.2 libQtMultimedia.so.4.6
host$ sudo cp libQtMultimedia.so.4.6.2 libQtMultimedia.so.4
host$ sudo cp libQtMultimedia.so.4.6.2 libQtMultimedia.so
host$ sudo cp libQtScript.so.4.6.2 libQtScript.so.4.6
host$ sudo cp libQtScript.so.4.6.2 libQtScript.so.4
host$ sudo cp libQtScript.so.4.6.2 libQtScript.so
host$ sudo cp libQtScriptTools.so.4.6.2 libQtScriptTools.so.4.6
host$ sudo cp libQtScriptTools.so.4.6.2 libQtScriptTools.so.4
host$ sudo cp libQtScriptTools.so.4.6.2 libQtScriptTools.so
host$ sudo cp libQtSvg.so.4.6.2 libQtSvg.so.4.6
host$ sudo cp libQtSvg.so.4.6.2 libQtSvg.so.4
host$ sudo cp libQtSvg.so.4.6.2 libQtSvg.so
host$ sudo cp libQtWebKit.so.4.6.2 libQtWebKit.so.4.6
host$ sudo cp libQtWebKit.so.4.6.2 libQtWebKit.so.4
host$ sudo cp libQtWebKit.so.4.6.2 libQtWebKit.so
host$ sudo cp libQtSql.so.4.6.2 libQtSql.so.4
host$ sudo cp libQtSql.so.4.6.2 libQtSql.so.4.6
host$ sudo cp libQtSql.so.4.6.2 libQtSql.so

4-5) copy 3rd library (if configure with -openssl)
host$ sudo cp /path/to/libcrypto.so.1.0.0 .
host$ sudo cp /path/to/libssl.so.1.0.0 .
host$ sudo cp /usr/local/Trolltech/QtEmbedded-4.6.2-arm/lib/libcrypto.so.1.0.0 libcrypto.so
host$ sudo cp /usr/local/Trolltech/QtEmbedded-4.6.2-arm/lib/libssl.so.1.0.0 libssl.so


5. Display Qt Application in Target Board
5-1) setting QtEmbedded-4.6.2-arm environment variable
client# QTDIR=/path/to/QtEmbedded-4.6.2-arm
client# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$QTDIR/lib
client# export QT_QWS_FONTDIR=$QTDIR/lib/fonts
client# export QT_PLUGIN_PATH=$QTDIR/plugins
client# export QWS_DISPLAY=directfb

5-2) execute fancybrowser
client# cd /path/to/QtEmbedded-4.6.2-arm
client# cd examples/webkit/fancybrowser
client# ./fancybrowser

reference: Qt for Embedded Linux Environment Variables

[Tips] 查詢Linux版本

以我自己的Ubuntu為例,

check kernel version
$ uname -a
Linux ubuntu 2.6.32-25-generic #45-Ubuntu SMP Sat Oct 16 19:48:22 UTC 2010 i686 GNU/Linux

check Linux distribution
$ cat /proc/version
Linux version 2.6.32-25-generic (buildd@palmer) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ) #45-Ubuntu SMP Sat Oct 16 19:48:22 UTC 2010
$ cat /etc/issue
Ubuntu 10.10 \n \l

Ubuntu需apt-get install lsb, Fedora需yum install redhat-lsb才能看到完整LSB資訊
$ lsb_release -a
LSB Version: core-2.0-ia32:core-2.0-noarch:core-3.0-ia32:core-3.0-noarch:core-3.1-ia32:core-3.1-noarch:core-3.2-ia32:core-3.2-noarch:core-4.0-ia32:core-4.0-noarch:cxx-3.0-ia32:cxx-3.0-noarch:cxx-3.1-ia32:cxx-3.1-noarch:cxx-3.2-ia32:cxx-3.2-noarch:cxx-4.0-ia32:cxx-4.0-noarch:desktop-3.1-ia32:desktop-3.1-noarch:desktop-3.2-ia32:desktop-3.2-noarch:desktop-4.0-ia32:desktop-4.0-noarch:graphics-2.0-ia32:graphics-2.0-noarch:graphics-3.0-ia32:graphics-3.0-noarch:graphics-3.1-ia32:graphics-3.1-noarch:graphics-3.2-ia32:graphics-3.2-noarch:graphics-4.0-ia32:graphics-4.0-noarch:printing-3.2-ia32:printing-3.2-noarch:printing-4.0-ia32:printing-4.0-noarch:qt4-3.1-ia32:qt4-3.1-noarch
Distributor ID: Ubuntu
Description: Ubuntu 10.10
Release: 10.10
Codename: maverick