Saturday, July 8, 2017

Building SQLite

SQLite is one of the mostly used database.
It is a self-contained and reliable full-featured SQL implementation.

Solaris 11.3 (GA / Release) packages a rather old version: 3.8.8.1.
Furthermore, this old version lacks or disables some demanded options.
For coping with more modern software, it may be necessary to upgrade it.

Download the source-code tarball in order to build a newer version.
(a.k.a. raw-source-code-tree from source-code repositories)

NOTE
Also consider according to SQLite Source Repository README that it may be better to download what they call the amalgamation where all source files are combined into a monolithic file which they claim the compiler can do a better optimization job of up to 5%. Interestingly though is that the amalgamation they provide don't necessarily match the latest version available from the SQL Source Repository.
NOTE
SQLite web page says it requires a "recent version" of Tcl. As to SQLite version 3.19.3, the Solaris 11.3 packaged Tcl version 8.5.12 seems good enough. But be aware that in the future it may be necessary to upgrade Tcl first! At the time of this writing, up-to-date Tcl versions are 8.5.19 or 8.6.6.
NOTE
I have enhanced the build procedure in general and reapplied for SQLite 3.24.0. It may be worthy taking a look at a few changes I've taken.
The task uses the GNU build automation and the GNU - Build preparation is recommended with a minor change due to the recommendation of having a separated, newly created, empty, build-directory. From the ZFS perspective, the "sane" environment is:

$ DS=...
$ zfs list -o name -t all -r $DS/software/sqlite |sed "s,$DS,...,"
NAME
.../software/
sqlite
.../software/sqlite/sqlite-3.19.3
.../software/
sqlite/sqlite-3.19.3@source
.../software/
sqlite/sqlite-3.19.3-gnu32
.../software/
sqlite/sqlite-3.19.3-gnu32@config
.../software/
sqlite/sqlite-3.19.3-gnu32@build
.../software/
sqlite/sqlite-3.19.3-gnu64
.../software/
sqlite/sqlite-3.19.3-gnu64@config
.../software/
sqlite/sqlite-3.19.3-gnu64@build

But note that the the -gnu32 and -gnu64 datasets aren't clones of @source.
They are nothing more than newly created (empty) datasets.
Hence, there will no @start snapshots either.

I won't repeat all the work-flow details, but, of course, they apply.
I'll limit myself to discussing the interesting differences.
Refer to the descriptions provided on other posts, such as:
 

The difficulty in the configuration step is selecting which features of SQLite will be included (through compilation #defines). There are tons of specific compilation options which may be pose a certain challenge at first. Furthermore, it is possible to have multiple side-by-side builds, tuned for different scenarios, but I won't get that complex and will stick to a single "multi-role" build, even if it be less optimal to multiple scenarios. For such a "multi-role" build I will suggest the following approach in making choices:
 
  • Include the options required by a each dependent software.
    (this is non-negotiable as otherwise the dependent software will fail)
    For instance: SQLITE_ENABLE_FTS3, SQLITE_ENABLE_DBSTAT_VTAB, SQLITE_THREADSAFE=1SQLITE_SECURE_DELETE, SQLITE_ENABLE_RTREE,SQLITE_SOUNDEX, ...
       
  • Consider further options to enable features normally turned off.
    For instance: SQLITE_ENABLE_ICU, SQLITE_ENABLE_JSON1, SQLITE_ENABLE_UNLOCK_NOTIFY, ...
       
  • Consider further options to set default parameters values.
    For instance: SQLITE_DEFAULT_FILE_PERMISSIONS=N, SQLITE_DEFAULT_PAGE_SIZE=, ...
      
  • Consider the recommended options.
    For instance: SQLITE_DEFAULT_WAL_SYNCHRONOUS=1, ...
      
  • Consider the platform options (reasonable to Solaris 11.3).
    For instance: HAVE_USLEEPHAVE_STRCHRNUL, HAVE_LOCALTIME_R , HAVE_ISNAN and HAVE_FDATASYNC, ...

NOTE
By asking for SQLITE_ENABLE_ICU, it will be necessary to have the supporting packages installed right from the start and also tell configure about the necessary libraries via "$(icu-config --ldflags-libsonly)". The packages are: unicode, developer/icu and library/icu.

NOTE
It may be worth noting that recommended HAVE_MALLOC_USABLE_SIZE is not supported by Solaris. But Solaris may offer interesting alternatives as well. One of them is fast libumem(3LIB). Only new code can take advantage of that by implementing an allocator supporting int(*)(void*) on top of libumem(3LIB) and then properly calling sqlite3_config() with SQLITE_CONFIG_MALLOC and sqlite3_mem_methods.

It may be surprising but configure seems too "conservative" in attempting to deduce a few reasonable options, but apparently not always as reasonable as to Solaris 11, so, in this case, it may be better to stay in control where configure leaves it behind. Furthermore, at least for the moment, I gave up trying to figure out how to pass all the options via configure and did it directly at the make step that follows it.

For GNU32:

$ cd ~/software/sqlite/sqlite-3.19.3-gnu32

$ source ../setenv 32
CONFIG_SHELL=/bin/bash
CC=/usr/bin/gcc CFLAGS=-m32 -march=core2 -std=gnu89
CXX=/usr/bin/g++ CXXFLAGS=-m32 -march=core2 -std=gnu++03
PATH=/opt/libtool-2.4.6/gnu32/bin:/opt/m4-1.4.18/gnu32/bin:/opt/autoconf-2.69/gnu32/bin:/opt/automake-1.15/gnu32/bin:/opt/tcl-8.5.19/gnu32/bin:/usr/gnu/bin:/usr/bin:/usr/sbin
PKG_CONFIG_PATH=/opt/tcl-8.5.19/gnu32/lib/pkgconfig



NOTE
I've initially used the Solaris 11 pre-packaged Tcl found in /usr/lib but on later rebuilds I've manually updated to an independently built version of Tcl in /opt/tcl-8.5.19/gnu32/bin.

NOTE
As said, in order to use ICU additional IPS packages have to be installed beforehand. Those packages are library/icu and developer/icu , but one way one could find out the package name of a specific file (icu-config) is with the following IPS query:

$ pkg search -r -H -o pkg.shortfmri *:file:path:*icu-config*
pkg:/developer/icu@0.5.11-0.151.0.1

$ ../sqlite-3.19.3/configure \
    --prefix=/opt/sqlite-3.19.3/gnu32 \
    --with-tcl=/usr/lib \
      TCLLIBDIR=/opt/sqlite-3.19.3/gnu32/lib \
      LIBS="$(icu-config --ldflags-libsonly)"
checking build system type... i386-pc-solaris2.11
checking host system type... i386-pc-solaris2.11
checking for gcc... /usr/bin/gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether /usr/bin/gcc accepts -g... yes
checking for /usr/bin/gcc option to accept ISO C89... none needed
checking for a sed that does not truncate output... /usr/gnu/bin/sed
checking for grep that handles long lines and -e... /usr/gnu/bin/grep
checking for egrep... /usr/gnu/bin/grep -E
checking for fgrep... /usr/gnu/bin/grep -F
checking for ld used by /usr/bin/gcc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... no
checking for BSD- or MS-compatible name lister (nm)... /usr/gnu/bin/nm -B
checking the name lister (/usr/gnu/bin/nm -B) interface... BSD nm
checking whether ln -s works... yes
checking the maximum length of command line arguments... 786240
checking whether the shell understands some XSI constructs... yes
checking whether the shell understands "+="... yes
checking for /usr/bin/ld option to reload object files... -r
checking for objdump... objdump
checking how to recognize dependent libraries... pass_all
checking for ar... ar
checking for strip... strip
checking for ranlib... ranlib
checking command to parse /usr/gnu/bin/nm -B output from /usr/bin/gcc object... ok
checking how to run the C preprocessor... /usr/bin/gcc -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking for objdir... .libs
checking if /usr/bin/gcc supports -fno-rtti -fno-exceptions... no
checking for /usr/bin/gcc option to produce PIC... -fPIC -DPIC
checking if /usr/bin/gcc PIC flag -fPIC -DPIC works... yes
checking if /usr/bin/gcc static flag -static works... no
checking if /usr/bin/gcc supports -c -o file.o... yes
checking if /usr/bin/gcc supports -c -o file.o... (cached) yes
checking whether the /usr/bin/gcc linker (/usr/bin/ld) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... solaris2.11 ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... no
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... yes
checking for a BSD-compatible install... /usr/gnu/bin/install -c
checking for special C compiler options needed for large files... no
checking for _FILE_OFFSET_BITS value needed for large files... 64
checking for int8_t... yes
checking for int16_t... yes
checking for int32_t... yes
checking for int64_t... yes
checking for intptr_t... yes
checking for uint8_t... yes
checking for uint16_t... yes
checking for uint32_t... yes
checking for uint64_t... yes
checking for uintptr_t... yes
checking for sys/types.h... (cached) yes
checking for stdlib.h... (cached) yes
checking for stdint.h... (cached) yes
checking for inttypes.h... (cached) yes
checking malloc.h usability... yes
checking malloc.h presence... yes
checking for malloc.h... yes
checking for fdatasync... yes
checking for gmtime_r... yes
checking for isnan... yes
checking for localtime_r... yes
checking for localtime_s... no
checking for malloc_usable_size... no
checking for strchrnul... yes
checking for usleep... yes
checking for utime... yes
checking for pread... yes
checking for pread64... yes
checking for pwrite... yes
checking for pwrite64... yes
checking for tclsh8.6... no
checking for tclsh8.5... tclsh8.5
configure: Version set to 3.19
configure: Release set to 3.19.3
configure: Version number set to 3019003
checking whether to support threadsafe operation... yes
checking for library containing pthread_create... none required
checking for library containing pthread_mutexattr_init... none required
checking whether to support shared library linked as release mode or not... no
checking whether to use an in-ram database for temporary tables... no
checking if executables have the .exe suffix... unknown
checking for Tcl configuration... found /usr/lib/tclConfig.sh
checking for existence of /usr/lib/tclConfig.sh... loading
checking for library containing readline... -ledit
checking for library containing fdatasync... none required
checking for library containing dlopen... none required
checking whether to support MEMSYS5... no
checking whether to support MEMSYS3... no
configure: creating ./config.status
config.status: creating Makefile
config.status: creating sqlite3.pc
config.status: creating config.h
config.status: executing libtool commands


It seems, not all options can be easily passed to the configure script: I only clearly see alternatives to fts3 and json1, nothing more. By looking at the generated config.h one recognizes many of the previously mentioned #defines. My problem is to believe if this config.h will be honored or not and I say so because I'm not an expert on the GNU build automation and I couldn't quickly grasp sufficient evidence it will work like that. Hence, I opt to subsequently pass explicit compilation #defines as arguments to gmake, but before that I take a snapshot of the configuration results:

$ DS=.../software/sqlite/sqlite-3.19.3-gnu32
$ zfs snapshot $DS/@config

Now, for the make:

$ OPTS=""

$ # Curiosity: these are required by a Firefox build
$ OPTS+="-DSQLITE_ENABLE_DBSTAT_VTAB=1 "
$ OPTS+="-DSQLITE_SECURE_DELETE=1 "
$ OPTS+="-DSQLITE_THREADSAFE=1 "
$ OPTS+="-DSQLITE_ENABLE_FTS3=1 "
$ OPTS+="-DSQLITE_ENABLE_UNLOCK_NOTIFY=1 "

$ # These are other options of my interest
$ OPTS+="-DSQLITE_SOUNDEX " 
$ OPTS+="-DSQLITE_ENABLE_ICU "
$ OPTS+="-DSQLITE_ENABLE_JSON1 "
$ OPTS+="-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 "

$ OPTS+="-DHAVE_USLEEP "
$ OPTS+="-DHAVE_STRCHRNUL "
$ OPTS+="-DHAVE_LOCALTIME_R "
$ OPTS+="-DHAVE_ISNAN "
$ OPTS+="-DHAVE_FDATASYNC "

$ gmake "OPTS=$OPTS"
...

$ zfs snapshot $DS/@build

$ gmake test
... 
0 errors out of 141005 tests on ... SunOS 32-bit little-endian
All memory allocations freed - no leaks
Maximum memory usage: 114176 bytes
Current memory usage: 0 bytes
Number of malloc()  : -1 calls


$ sudo gmake install
...
-----------------------------------------------------------------
Libraries have been installed in:
   /opt/sqlite-3.19.3/gnu32/lib

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - use the `-RLIBDIR' linker flag

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
-----------------------------------------------------------------


$ ls -1 /opt/sqlite-3.19.3/gnu32/*
/opt/sqlite-3.19.3/gnu32/bin:
sqlite3

/opt/sqlite-3.19.3/gnu32/include:
sqlite3.h
sqlite3ext.h

/opt/sqlite-3.19.3/gnu32/lib:
libsqlite3.a
libsqlite3.la
libsqlite3.so
libsqlite3.so.0
libsqlite3.so.0.8.6
libtclsqlite3.so
pkgconfig
pkgIndex.tcl


$ sudo zfs snapshot rpool/VARSHARE/sqlite-3.19.3/gnu32@release

If necessary, package the results in a tarball or some other method of choice.
Now, it rests setting up PATH and PKG_CONFIG_PATH as necessary.

For GNU64:
The steps are completely analogous.
(use --with-tcl=/usr/lib/64 or other 64-bit Tcl)

During gmake test there's a potential memory shortcoming,which I suspect is not a reason to get alarmed as it's not reported as an ordered failure:

./testfixture: couldn't fork child process:
               not enough memory ...
               gmake: *** [tcltest] Error 1


Perhaps it's possible to pass a #define to gmake test in order to avoid the unexpected out-of-memory exception during tests or perhaps try to update the Tcl version in the system.

That's it.