Build Qt 5 PostgreSQL Plugin for iOS

From Qt Wiki
Jump to navigation Jump to search

Hello everyone,

I managed to build the iOS version for the PostgreSQL driver! Here is how:

First of all, there are two cpu architectures that exist in iOS: armv7 & arm64 so follow the next initially with armv7 and then with arm64.

  1. Initially go to folder with the sources of the PostgreSQL project. Then go to src/interfaces/libpq folder.
  2. Set the $ARCH to armv7 like this: ARCH=armv7
  3. Set CC to a path like this: CC=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
  4. Set CFLAGS to this: CFLAGS=-O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -pthread -pthreads -mthreads -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS -DFRONTEND -DUNSAFE_STAT_OK -I. -I../../../src/include -I../../../src/port -I../../../src/port -DSO_MAJOR_VERSION=5 -arch $ARCH -c
  5. SOURCE files are: fe-auth.c fe-connect.c fe-exec.c fe-misc.c fe-print.c fe-lobj.c fe-protocol2.c fe-protocol3.c pqexpbuffer.c fe-secure.c libpq-events.c ../../../src/port/chklocale.c ../../../src/port/inet_net_ntop.c ../../../src/port/noblock.c ../../../src/port/pgstrcasecmp.c ../../../src/port/pqsignal.c ../../../src/port/thread.c ../../../src/backend/libpq/ip.c ../../../src/backend/libpq/md5.c ../../../src/backend/utils/mb/encnames.c ../../../src/backend/utils/mb/wchar.c
  6. We will compile the SOURCE files without linking, so what we need to do for each one of the SOURCE files is: $CC $CFLAGS $SOURCEfile -o $OBJECTfile.o (eg. for fe-auth.c file this is $CC $CFLAGS fe-auth.c -o fe-auth.o, and for the ../../../src/port/thread.c file this is $CC $CFLAGS ../../../src/port/thread.c -o thread.o).
  7. Then run this awk oneliner which is needed for our next step that is the linking step: awk '/^[^#]/ {printf "_%s\n",$1}' exports.txt >exports.list
  8. The following will generate the armv7 architecture file that will be used for linking to your projects: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch $ARCH -mios-version-min=9.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/ -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -pthread -pthreads -mthreads -D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS -dynamiclib -install_name '/usr/local/pgsql/lib/libpq.5.dylib' -compatibility_version 5 -current_version 5.6 -exported_symbols_list exports.list -multiply_defined suppress -o libpq.5.6.dylib.$ARCH fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o fe-protocol2.o fe-protocol3.o pqexpbuffer.o fe-secure.o libpq-events.o chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o thread.o ip.o md5.o encnames.o wchar.o -L../../../src/port -L../../../src/common -headerpad_max_install_names -stdlib=libc++ -single_module -dynamiclib -Wl,-dead_strip_dylibs -lpthread
  9. After that there are two more instructions, one to archive: ar crs libpq.a.$ARCH fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o fe-protocol2.o fe-protocol3.o pqexpbuffer.o fe-secure.o libpq-events.o chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o thread.o ip.o md5.o encnames.o wchar.o
  10. And another to generate index to archive: ranlib libpq.a.$ARCH
  11. This way you have created the libpq for the armv7 ARCH, now we need to perform the same for the arm64 architecture.
  12. When we have done both, we need to combine them to one file, which we do with the following command: lipo libpq.5.6.dylib.armv7 libpq.5.6.dylib.arm64 -create -output libpq.5.6.dylib
  13. Moving to the final steps, we need to link this file to libpq.5.dylib, with the following command: ln -s libpq.5.6.dylib libpq.5.dylib
  14. And to link it to libpq.dylib too, with the following command: ln -s libpq.5.6.dylib libpq.dylib
  15. After that we are ready to install it to our system.

Next step is to build the plugin that will be linked to this library file. This is done by:

  1. Visiting the qtbase source folder that contains the driver under src/plugins/sqldrivers/psql/.
  2. Then we have to run qmake with some parameters for the library we made, for example: qmake "INCLUDEPATH+=/path/to/postgresql/src/interfaces/libpq /path/to/postgresql/src/include" "LIBS+=-L/path/to/postgresql/src/interfaces/libpq/ -lpq"
  3. And in order to compile the driver and install it, we have to run: make install

This way, we have both architectures for the iOS environment and we have the driver to access PostgreSQL database servers via our iOS device.

For this page, I would like to thank the author of the PostgreSQL to Android web page ([[1]])