Sunday, December 21, 2008

Compiling SQLite 2 on Mac OS X 10.5 Leopard (for x86_64)

Mac OS X 10.5 Leopard comes with SQLite v3 bundled. But many times having SQLite v2 can be handy, given that version 3 is not backwards compatible. The hardest step here is to find the download link for version 2 in the official SQLite site. Don't ask me how did I achieve that... I don't remember ;) Fortunately I wrote down the link so you don't have to bother digging the web again :)

So we start with:
$ wget http://www.sqlite.org/sqlite-2.8.17.tar.gz
$ tar -zxf sqlite-2.8.17.tar.gz


The next big problem comes with CFLAGS. The package we are about to install comes with a pre-made configure script that doesn't respects the CFLAGS environment variable when creating the dynamic library. It took me a while but I have the patch for you, too.

--- configure.old 2008-12-21 18:50:24.000000000 -0200
+++ configure 2008-12-21 18:57:11.000000000 -0200
@@ -8253,7 +8253,7 @@
;;
esac
output_verbose_link_cmd='echo'
- archive_cmds='$CC -dynamiclib $allow_undefined_flag -o $lib $libobjs $deplibs$compiler_flags -install_name $rpath/$soname $verstring'
+ archive_cmds='$CC '$CFLAGS' -dynamiclib $allow_undefined_flag -o $lib $libobjs $deplibs$compiler_flags -install_name $rpath/$soname $verstring'
module_cmds='$CC $allow_undefined_flag -o $lib -bundle $libobjs $deplibs$compiler_flags'
# Don't fix this by using the ld -exported_symbols_list flag, it doesn't exist in older darwin ld's
archive_expsym_cmds='sed -e "s,#.*,," -e "s,^[ ]*,," -e "s,^\(..*\),_&," < $export_symbols > $output_objdir/${libname}-symbols.expsym~$CC -dynamiclib $allow_undefined_flag -o $lib $libobjs $deplibs$compiler_flags -install_name $rpath/$soname $verstring~nmedit -s $output_objdir/${libname}-symbols.expsym ${lib}'


After applying the patch to the configure file we can proceed as usual:

$ ./configure --prefix=/usr/local LDFLAGS="-arch i686 -arch x86_64" CFLAGS="-arch i686 -arch x86_64"
$ make


I like to take full advantage of my system so I like to have things working at 64 bits. Just for the case I ever need a tool that won't go 64, I keep a fat binary with both archs 32 and 64. At this point we should check our work:

$ file .libs/libsqlite.0.8.6.dylib
.libs/libsqlite.0.8.6.dylib: Mach-O universal binary with 2 architectures
.libs/libsqlite.0.8.6.dylib (for architecture i386): Mach-O dynamically linked shared library i386
.libs/libsqlite.0.8.6.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64


Everything seems fine. So we finally install it:

$ sudo make install

4 comments :

Carlos said...

Thanks so much, you have no idea how much pain and grief you saved me :)

Jouni said...

Thanks so much for the download link! I couldn't have found it without your help!

hayatu-123 said...

Not so far I have found new cool tool to work with SQLite on mac os x - Valentina Studio. Its free edition can do things more than many commercial tools!!
I very recommend check it. http://www.valentina-db.com/en/valentina-studio-overview
You can install Valentina Studio (FREE) directly from Mac App Store: https://itunes.apple.com/us/app/valentina-studio/id604825918?ls=1&mt=12

Unknown said...

I didn't understand how to apply the patch to the script...I try manually to change the lines using BBEdit, but then on Mavericks I do not get compiled.