Skip to content

Instantly share code, notes, and snippets.

@kadler
Last active February 18, 2019 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadler/ce6d6e3ce2b1d837636f9866410a0840 to your computer and use it in GitHub Desktop.
Save kadler/ce6d6e3ce2b1d837636f9866410a0840 to your computer and use it in GitHub Desktop.
Building ibm_db2 on IBM i 7.2 with Zend PHP

Building ibm_db2 on IBM i

Setup

We need to install various packages in order to build ibm_db2. Most of these can be installed with yum:

yum group install 'Development tools'
yum install git

Next, since Zend PHP is 32-bit, we need a compiler that can build 32-bit binaries. The version of GCC shipped by yum can't do this. If you already have an AIX gcc compiler installed, you can skip to the next section, otherwise read on.

Installing AIX gcc

We'll need to get some more tools to install these AIX rpms, since the IBM i rpm cannot install them directly:

yum install wget cpio-gnu
mkdir ~/gcc-rpms
cd ~/gcc-rpms

wget http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.1/gcc/gcc-6.3.0-1.aix7.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.1/gcc/gcc-cplusplus-6.3.0-1.aix7.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.1/gcc/gcc-cpp-6.3.0-1.aix7.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.1/gcc/libstdcplusplus-6.3.0-1.aix7.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.1/gcc/libstdcplusplus-devel-6.3.0-1.aix7.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc-7.1/gcc/libgcc-6.3.0-1.aix7.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc/mpfr/mpfr-3.1.2-3.aix6.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc/libmpc/libmpc-1.0.3-1.aix6.1.ppc.rpm \
     http://public.dhe.ibm.com/aix/freeSoftware/aixtoolbox/RPMS/ppc/gmp/gmp-5.1.3-2.aix6.1.ppc.rpm

for f in *.rpm
do
    rpm2cpio $f | /QOpenSys/pkgs/bin/cpio -id
done

mkdir /QOpenSys/opt
ln -s /QOpenSys/opt /opt

cp -r opt/freeware /opt

You should now have gcc installed under /opt/freeware/bin.

Setting up CLI headers

The build process needs the CLI headers to build against libdb400. These are the same headers that are used to build ILE CLI applications (linking to QSQCLI service program), but unfortunately they are not shipped in a format that can be used by the PASE compilers.

The following code snippet will copy the necessary CLI headers to /QOpenSys/usr/include and in the process: convert from EBCDIC to UTF-8, split at 80 columns, and replace any trailing whitespace.

for f in sql.h sqlcli.h sqlsystm.h sqludf.h
do
    /QOpenSys/usr/bin/iconv -f IBM-037 -t UTF-8 /QIBM/include/$f | fold -b -w 80 | sed 's| *$||g' > /QOpenSys/usr/include/$f
done

The ibm_db2 uses sqlcli1.h as its main header file, since that's what Db2 LUW uses, but IBM i does not have such a header. We can easily fix this by making our own which just imports sqlcli.h:

cat <<EOF > /QOpenSys/usr/include/sqlcli1.h
#include <sqlcli.h>
EOF

Fixing PHP build macro

PHP has a bug in how it generates PHP modules on AIX so before we can build our PHP module, we have to patch it. This only has to be done once.

yum install wget

pushd /usr/local/zendphp7/lib/php/build

wget --no-check-certifiates https://gist.github.com/kadler/ce6d6e3ce2b1d837636f9866410a0840/raw/74be487c181071468c58d0e8a7e993b9f0cff852/php-acinclude.m4.patch

patch -p0 < php-aclocal.m4.patch

popd

Getting the ibm_db2 source

Normally, you could download the source from https://pecl.php.net/package/ibm_db2, but as of version 2.0.6, it will not build on IBM i due to bugs. Instead, we'll download the code from my GitHub repo.

cd ~ # or wherever you want the code to live
git clone https://github.com/kadler/pecl-database-ibm_db2.git ibm_db2

Building

Now that we have the source code, we're ready to build. The basics of building a PECL are:

phpize
./configure
make

Here's what we actually need to do to get things configured. This usually only needs to be done once:

PATH=/usr/local/zendphp7/bin:/opt/freeware/bin:$PATH

export LDFLAGS='-Wl,-brtl -Wl,-blibpath:/QOpenSys/usr/lib:/opt/freeware/lib'
export OBJECT_MODE=32
export CC=gcc
export CXX=g++

phpize

./configure --with-IBM_DB2=/QOpenSys/usr --host=powerpc-ibm-aix6 --build=powerpc-ibm-aix6

Once configure has finished successfully, we can run make:

make

If all goes, well you should see:

Build complete.
Don't forget to run 'make test'.

If you make changes to ibm_db2.c or php_ibm_db2.h, you'll need to re-run make (but not configure or other previous build steps).

Running tests

** TODO **

--- acinclude.m4.orig 2019-02-18 11:19:21 +0000
+++ acinclude.m4 2019-02-18 11:19:39 +0000
@@ -839,8 +839,8 @@
case $host_alias in
*aix*[)]
- suffix=so
- link_cmd='$(LIBTOOL) --mode=link ifelse($4,,[$(CC)],[$(CXX)]) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) -Wl,-G -o '$3'/$1.la -export-dynamic -avoid-version -prefer-pic -module -rpath $(phplibdir) $(EXTRA_LDFLAGS) $($2) $(translit($1,a-z_-,A-Z__)_SHARED_LIBADD) && mv -f '$3'/.libs/$1.so '$3'/$1.so'
+ suffix=la
+ link_cmd='$(LIBTOOL) --mode=link ifelse($4,,[$(CC)],[$(CXX)]) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) -Wl,-G -o '$3'/$1.la -export-dynamic -avoid-version -prefer-pic -module -rpath $(phplibdir) $(EXTRA_LDFLAGS) $($2) $(translit($1,a-z_-,A-Z__)_SHARED_LIBADD)'
;;
*netware*[)]
suffix=nlm
--- acinclude.m4.orig 2019-02-18 12:09:59 +0000
+++ acinclude.m4 2019-02-18 12:10:31 +0000
@@ -839,8 +839,8 @@
case $host_alias in
*aix*[)]
- suffix=so
- link_cmd='$(LIBTOOL) --mode=link ifelse($4,,[$(CC)],[$(CXX)]) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) -Wl,-G -Wl,-G -o '$3'/$1.la -export-dynamic -avoid-version -prefer-pic -module -rpath $(phplibdir) $(EXTRA_LDFLAGS) $($2) $(translit($1,a-z_-,A-Z__)_SHARED_LIBADD) && mv -f '$3'/.libs/$1.so '$3'/$1.so'
+ suffix=la
+ link_cmd='$(LIBTOOL) --mode=link ifelse($4,,[$(CC)],[$(CXX)]) $(COMMON_FLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) $(LDFLAGS) -Wl,-G -Wl,-G -o '$3'/$1.la -export-dynamic -avoid-version -prefer-pic -module -rpath $(phplibdir) $(EXTRA_LDFLAGS) $($2) $(translit($1,a-z_-,A-Z__)_SHARED_LIBADD)'
;;
*netware*[)]
suffix=nlm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment