Compiling lensfun-0.3.95 on Debian Buster

Lensfun provides lens distortion correction for Darktable and other raw processing applications. Version 0.3.95 provides the ability to use the Adobe Camera Model, and hence use Adobe lens profiles (lcp files). However, lensfun 0.3.95 is not packaged for Debian Buster. Also, Darktable won’t compile against the latest git version of Lensfun, so you must compile and install specifically version 0.3.95 to get ACM support.

We begin by downloading and extracting Lensfun 0.3.95. Lensfun 0.3.95 is not tagged in git, so we have to download the release directly from SourceForge. The release is not available from the GitHub repository.

$ wget https://sourceforge.net/projects/lensfun/files/0.3.95/lensfun-0.3.95.tar.gz
$ tar -xvf lensfun-0.3.95.tar.gz
$ cd lensfun-0.3.95/

Lensfun uses CMake for building and also has CPack enabled. We can use it to build a deb package and install it. This allows easier integration and uninstallation in the future.

$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=release -DCPACK_BINARY_DEB=ON ../
$ make -j`nproc` && make package
$ sudo apt install ./liblensfun2_0.3.95.0_amd64.deb

Enabling C++11 (C++0x) in CMake

Going over some CMakeLists.txt files I’ve written, I came across the following snippet:

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
        message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

Various versions of gcc and clang use different flags to specify C++11 support; namely, older ones accept -std=c++0x and newer ones accept -std=c++11. The above snippet detects which one is the right one for the compiler being used and adds the flag to CXX_FLAGS.

Vim: Creating .clang_complete using CMake

The clang_complete plugin for Vim offers superior code completion. If your project is anything but trivial, it will only do so if you provide a .clang_complete file with the right compilation arguments. The easy way to do so is by using the cc_args.py script that comes with it to record the options directly into the .clang_complete file. Usually, one does

make CXX='~/.vim/bin/cc_args.py clang++'

However, the makefile generated by CMake doesn’t support the CXX configuration.

The solution is to call CMake with the CXX environment variable set:

CXX="$HOME/.vim/bin/cc_args.py clang++" cmake ..
make

Note that this will create the .clang_complete file in the build directory (I’ve assumed an out-of-place build), so just copy the file over to Vim’s working directory so it can find it. You’ll need to re-run CMake again (without CXX) to disable re-creating the .clang_complete file each time.

While looking for this solution, I first tried solving it by setting the CMAKE_CXX_COMPILER variable in CMake. However, for some strange reason, it didn’t like it, saying that the compiler wasn’t found (it shuns command-line arguments given in the compiler command).

The more I use clang_complete, the more awesome I find it. It has its quirks, but nonetheless it’s much simpler and better than manually creating tag files for each library.

Updated 6/1/2014: When setting CXX, use $HOME instead of ~ (to fix issues with newer versions of CMake).

Auto-Detect Dependencies when Building .debs Using CMake

CMake (via CPack) has a great feature that allows you to automatically generate Debian/Ubuntu (.deb) packages. One of the annoying things to do when you create a package is list its dependencies. CMake asks you to do it via the CPACK_DEBIAN_PACKAGE_DEPENDS variable. For example:

set (CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>=2.7-18)")

But what happens when you work on a more complex project? Keeping track of all the dependencies by hand is a tedious task. Debian provides a tool named dpkg-shlibdeps, which makes this task easier by updating the debian/control file with dependencies extracted from the dynamic libraries needed by a given executable. Luckily, since CMake 2.8.3, CMake also supports running this tool automatically to figure out the required dependencies. The documentation is sparse, and I had a hard time finding how to do so (I actually found it via a bug report and a commit message, but afterward I’ve seen it in the official documentation too). To enable it, you need to add the following line to your CMakeLists.txt file:

# autogenerate dependency information
set (CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)