Build Qt 5 MySQL Plugin for Android

From Qt Wiki
Revision as of 15:06, 25 November 2016 by EdwardWelbourne (talk | contribs) (Gather items into a category:Build.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

Building the Qt5 mysql plugin for Android turned out to not be as straightforward as you may hope. I'll cover the necessary steps to accomplish this below.

Using mariadbclient (Option 1)

Setting up the general environment

Set the following environment variables:

  1. SR=/path/to/android/ndk/platforms/android-9/arch-arm
  2. BR=/path/to/android/ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-

NOTE: I have been unable to get openssl to compile on the x86 version of android, hence the ARMv7 variables above. If you are successful at getting openssl to compile with the x86 android, by all means update this wiki :).

Prerequisites

The mysql module requires the following to compile:

  1. The mysql client library. For mariadb (recommended mysql implementation), you also require:
    1. openssl
    2. libiconv

Compiling OpenSSL

I've tested this with openssl version 1.0.1e. This should work with future versions as well, but I can't guarantee that.

First, grab the sources and unpack them. Then, enter the source directory and do the following:

RANLIB="$BR"ranlib CC="$BR"gcc ./Configure android-armv7 --prefix=$SR/usr
ANDROID_DEV=$SR/usr make
make install

Note: There should be a way to get openssl to compile and install only the library, but I haven't found the right make targets for that yet. The above will make and install everything for openssl rather than just the library.

To install only the binary files use

make install_sw

Compiling libiconv

I've tested this with libiconv version 1.14. Again, this should work with future versions, but I can't guarantee as much.

First, grab the sources and unpack them. Then, enter the source directory and do the following:

STRIP="$BR"strip RANLIB="$BR"ranlib OBJDUMP="$BR"objdump AR="$BR"ar CC="$BR"gcc CFLAGS=--sysroot=$SR CPP="$BR"cpp CPPFLAGS=$CFLAGS ./configure --build=x86_64 --host=arm --prefix=$SR/usr --with-sysroot=$SR && make install

Compiling mariadbclient

After testing, I've found that the released sources are not compatible with mysql (as of version 1.0.0). They are missing the mysql_library_init function, which the Qt mysql plugin uses. However, the bazaar sources are compatible with mysql and include this function. Grab the mariadb sources from bazaar, configure, compile, and install the mariadb connector client library as described below:

bzr branch lp:mariadb-native-client
cd maria-native-client

The mariadb unit tests will fail and halt the compilation of maria db due to some missing pthread routines. I recommend commenting out the unit tests so they don't block the install. Modify CMakeLists.txt in the base source directory and comment out the following line:

ADD_SUBDIRECTORY(unittest/libmariadb)

UPDATE: As of connector version 2.0.0, mariadb is fully compatible with the released version. If you download the 2.0.0 (or later) connector, you can start from this point. You will not need the bazaar sources, nor will you need to disable the unit tests above.

Now you can configure via cmake:

mkdir build && cd build
PKG_CONFIG_PATH=$SR/usr/lib/pkgconfig cmake -DCMAKE_AR="$BR"ar -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER="$BR"gcc -DCMAKE_C_FLAGS=--sysroot=$SR -DCMAKE_INSTALL_PREFIX=$SR/usr -DCMAKE_LINKER="$BR"ld -DCMAKE_NM="$BR"nm -DCMAKE_OBJCOPY="$BR"objcopy -DCMAKE_OBJDUMP="$BR"objdump -DCMAKE_RANLIB="$BR"ranlib -DCMAKE_STRIP="$BR"strip -DWITH_EXTERNAL_ZLIB=ON -DICONV_INCLUDE_DIR=$SR/usr/include -DICONV_LIBRARIES=$SR/usr/lib/libiconv.a -DZLIB_INCLUDE_DIR=$SR/usr/include -DZLIB_LIBRARY=$SR/usr/lib/libz.so ../

At this point, you'll want to make some minor changes to the sources to make them compile on android. For one, the android compiler does not know what a 'ushort' is. So, define it in include/my_global.h inside the #define _global_h block:

#ifndef ushort
#define ushort uint16
#endif

Now you're ready to compile and install:

make install

Deployment Notes

You will need to add the mariadb shared object library file the list of additional libraries to add to the APK image. To get the exact name of the file needed after compiling the QT mysql plugin use the following command:

"$BR"objdump -p libqsqlmysql.so | grep NEEDED

To correctly create a mariadb.so file instead of mariadb.so.1 edit libmariadb/CMakeLists.txt and comment out the block:

 SET_TARGET_PROPERTIES(libmariadb PROPERTIES VERSION ${CPACK_PACKAGE_VERSION_MAJOR} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR})

Using mariadbclient (Option 2)

This now can run on Windows with MSYS too. Using this shell script you can build the plugin easily after modifying the following variables (using the ,,clean" parameter will delete the generated files: ./build_libsqlmariadb.so.sh clean):

  1. ANDROID_NDK_ROOT (With MSYS on Windows use /d or /c instead of D:, C:)
  2. QT_ROOT (With MSYS on Windows use /d or /c instead of D:, C:)
  3. SR (optional in case you want to build for higher Android version)
  4. pkg (optional in case you want to build for higher OpenSSL/libiconv/mariadbclient/Qt version)
  5. output_dir (optional in case you want to change where the needed .so files will be gathered)
  6. build_dir (optional, where the temp build files will be downloaded, extracted, so we don't spam the script's directory)
  7. BR (If you are building on Windows with MSYS you probably have to replace the: linux-x86_64 with windows-x86_64)
  8. no_skips (If you set it for false, it will build everything on the script start, and won't skip the already built libs re-build process)
#!/bin/bash
# Usage: ./build_libsqlmariadb.so.sh [clean]
# 2016-07-22 T-bond (https://t-bond.hu) Added Windows-MSYS build support, Added clean option, and output to dir. Added temp build dir. Fixed no getpwent error. More cleaner compile output. Skip for already built libs

# Based on https://gist.github.com/RazZziel/fd607459c1f07a43cdf9
output_dir="libmariadb_so_output"
build_dir="build_libmariadb_temp"

export ANDROID_NDK_ROOT="/d/SDKs/Android/ndk-bundle"
export QT_ROOT="/d/SDKs/Qt/5.7"
 
SR="$ANDROID_NDK_ROOT/platforms/android-17/arch-arm/"
BR="$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-"
no_skips=true

#Clean build and outputs
if [ "$1" = "clean" ]; then
	echo "Cleaning..."
	rm -r -f $build_dir $output_dir
	echo "Cleaned."
else
pushd () {
    command pushd "$@" > /dev/null
}

popd () {
    command popd "$@" > /dev/null
}

if [ ! -d $build_dir ]; then
mkdir $build_dir
fi
pushd $build_dir
 
here="$(dirname "$0")"
find "$SR" > "$here/rootfs_files_before.txt" || exit 1
checkChanges() {
        find "$SR" > "$here/rootfs_files_after.txt" || exit 1
        echo "Changes:"
        diff -u "$here/rootfs_files_before.txt" "$here/rootfs_files_after.txt"
}
trap checkChanges EXIT
 
# OpenSSL
pkg=openssl-1.0.2h.tar.gz
dir=$(basename $pkg .tar.gz)
 
if [ ! -d $dir ]; then
        wget -c http://www.openssl.org/source/$pkg
        tar -xf $pkg || exit 1
fi
 
pushd $dir
		if [ ! -f "skip" ] || [ "$no_skips" = true ]; then
	        RANLIB="$BR"ranlib CC="$BR"gcc ./Configure android-armv7 --prefix=$SR/usr
	        ANDROID_DEV=$SR/usr make || exit 1
	        make build_libs || exit 1
        touch skip
    	fi
popd
 
# libiconv
pkg=libiconv-1.14.tar.gz
dir=$(basename $pkg .tar.gz)
 
if [ ! -d $dir ]; then
        wget -c http://ftp.gnu.org/pub/gnu/libiconv/$pkg
        tar -xf $pkg || exit 1
fi
 
pushd $dir
		if [ ! -f "skip" ] || [ "$no_skips" = true ]; then
			#Fix for outdated scripts on Windows T-bond.
			wget "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD" -O new_config.guess
			wget "http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD" -O new_config.sub
			\cp new_config.guess build-aux/config.guess
			\cp new_config.guess libcharset/build-aux/config.guess
			\cp new_config.sub build-aux/config.sub
			\cp new_config.sub libcharset/build-aux/config.sub
			#Fix for outdated scripts on Windows T-bond. END
			STRIP="$BR"strip RANLIB="$BR"ranlib OBJDUMP="$BR"objdump AR="$BR"ar CC="$BR"gcc CFLAGS=--sysroot=$SR CPP="$BR"cpp CPPFLAGS=$CFLAGS ./configure --host=arm --prefix=$SR/usr --with-sysroot=$SR
	        make || exit 1
	        make install || exit 1
	        touch skip
    	fi
popd
 
 
# mariadbclient
 
version=2.0.0
pkg=mariadb_client-$version-src.tar.gz
url=http://archive.mariadb.org/client-native-$version/src/$pkg
 
# FIXME: This one fails to build, linker errors
#version=2.1.0
#pkg=mariadb-connector-c-$version-src.tar.gz
#url=https://downloads.mariadb.org/f/connector-c-$version/source-tgz/$pkg
 
dir=$(basename $pkg .tar.gz)
if [ ! -d $dir ]; then
        wget -c $url
        tar -xf $pkg || exit 1
fi

pushd $dir
		if [ ! -f "skip" ] || [ "$no_skips" = true ]; then
	        sed -i -e "s|ADD_SUBDIRECTORY(unittest/libmariadb)|#ADD_SUBDIRECTORY(unittest/libmariadb)|" CMakeLists.txt
	        sed -i -e "N; s|typedef unsigned short ushort;\n#endif|#endif\ntypedef unsigned short ushort;|" include/my_global.h
	        sed -i -e "N; s|SET_TARGET_PROPERTIES(libmariadb PROPERTIES VERSION.*||" libmariadb/CMakeLists.txt
	        sed -i -e "N; s|SOVERSION \${CPACK_PACKAGE_VERSION_MAJOR})||" libmariadb/CMakeLists.txt # Pig disgusting: the previous multiline thingie should've done it
	        sed -i -e "N; s|\${CPACK_PACKAGE_VERSION_MAJOR}||" libmariadb/CMakeLists.txt # Pig disgusting: what the fuck
	        sed -i '/#ifdef HAVE_GETPWNAM/c#if defined(HAVE_GETPWNAM) && defined(HAVE_GETPWENT)' libmariadb/mf_pack.c #T-bond: Fix the no getpwent error. http://forum.kodi.tv/showthread.php?tid=280134
	        #DCMAKE_CXX_COMPILER DCMAKE_SYSTEM_VERSION DCMAKE_SYSTEM_NAME WINDOWS ONLY!!!
	        if [ ! -d "build" ]; then
	        	mkdir build
	    	fi
	        pushd build
	        PKG_CONFIG_PATH=$SR/usr/lib/pkgconfig cmake \
			-DCMAKE_BUILD_TYPE=Release \
			-DCMAKE_C_FLAGS=--sysroot="$SR" \
			-DCMAKE_INSTALL_PREFIX="$SR/usr" \
			-DCMAKE_C_COMPILER="$BR"gcc \
			-DCMAKE_SYSTEM_NAME="Android" \
			-DCMAKE_SYSTEM_VERSION=1 \
			-DCMAKE_CXX_COMPILER="$BR"g++ \
			-DCMAKE_LINKER="$BR"ld \
			-DCMAKE_AR="$BR"ar \
			-DCMAKE_NM="$BR"nm \
			-DCMAKE_OBJCOPY="$BR"objcopy \
			-DCMAKE_OBJDUMP="$BR"objdump \
			-DCMAKE_RANLIB="$BR"ranlib \
			-DCMAKE_STRIP="$BR"strip \
			-DICONV_INCLUDE_DIR="$SR/usr/include" \
			-DICONV_LIBRARIES="$SR/usr/lib/libiconv.a" \
			-DWITH_EXTERNAL_ZLIB=ON \
			-DZLIB_INCLUDE_DIR="$SR/usr/include" \
			-DZLIB_LIBRARY="$SR/usr/lib/libz.so" ../ || exit 1

	        make install || exit 1
	        popd
	        cp build/libmariadb/*.{a,so} "$SR/usr/lib/mariadb/" || exit 1
	        touch skip
	    fi
popd
 
 
# qt
qmake="$QT_ROOT/android_armv7/bin/qmake"
[ ! -f "$qmake" ] && { echo "Could not find qmake in '$qmake'"; exit 1; }
[ ! -x "$qmake" ] && { echo "Qmake is not executable in '$qmake'"; exit 1; }
qtVersion=5.7.0
pkg=qtbase-opensource-src-$qtVersion.tar.gz
 
dir=$(basename $pkg .tar.gz)
 
if [ ! -d $dir ]; then
        wget -c http://download.qt.io/official_releases/qt/${qtVersion%.*}/$qtVersion/submodules/$pkg
        tar -xf $pkg || exit 1
fi
 
pushd $dir/src/plugins/sqldrivers/mysql/
		$qmake "QMAKE_CXX=$BR"g++ "QMAKE_LINK=$BR"g++ "INCLUDEPATH+=$SR/usr/include/mariadb" "LIBS+=$SR/usr/lib/mariadb/libmariadbclient.a $SR/usr/lib/libssl.a $SR/usr/lib/libcrypto.a $SR/usr/lib/libiconv.a" "LIBPATH+=$SR/usr/lib/mariadb" -o Makefile mysql.pro
        make || exit 1
        make install || exit 1
popd 
popd

if [ ! -d $output_dir ]; then
	mkdir $output_dir
fi
cp $SR/usr/lib/mariadb/libmariadb.so $output_dir/libmariadb.so
cp $QT_ROOT/android_armv7/plugins/sqldrivers/libqsqlmysql.so $output_dir/libqsqlmysql.so
 
echo
echo "BOOYAH!!!"
echo
fi

Deployment Note

You've to add an external library to your .pro file because the generated plugin depends on libmariadb.so

contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
    ANDROID_EXTRA_LIBS = \
        # modify the path
        $$PWD/../../../mysqllibandroid/mariadb_client-2.0.0-src/build/libmariadb/libmariadb.so
}

Using mysqlclient (Option 3)

Stub for someone to experiment with and update this page

Compiling the Qt mysql plugin

You cannot compile the mysql plugin from the configure script for android- that appears to be broken as of this writing. You can, however, compile it manually from the sources. Grab the Qt5 sources from git, configure them for android as normal (without the qt-sql-mysql or plugin-sql-mysql flags), make, and install as normal. Then enter the qtbase/src/plugins/sqldrivers/mysql directory and execute the following: