Ads

 

How to generate a pkg-config file with cmake

pkg-config is a great way to get compiler flags for a given library. Since I'm currently implementing the rainbrurpg's meta as a library (see libwsmeta's annoucement and repository) I would like to generate a pkg-config file using cmake.

The input file

We'll start with a template pc.in file :
prefix=@DEST_DIR@
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include

Name: libmylib
Description: Mylib description.
Version: 1.0

Libs: -L${libdir} @PRIVATE_LIBS@
Cflags: -I${includedir}
The @ variables will be replaced with cmake variable content : it's the process of substitution.

The cmake part

From cmake, you have to define @DEST_DIR@ and @PRIVATE_LIBS@.

set(DEST_DIR "${CMAKE_INSTALL_PREFIX}")
This will set the correct installation prefix to CMAKE_INSTALL_PREFIX.

Now, imagine you have a library list in a cmake variable. This is a comma separaetd value and you need a list of compiler flags :

foreach(LIB ${COMMA_SEAPARATED_LIST})
  set(PRIVATE_LIBS "${PRIVATE_LIBS} -l${LIB}")
endforeach()

Generating the file

Now, the last part, the file generation. And here's the trick. If you simply call CONFIGURE_FILE, cmake will allow substitution in both @ and ${} variables. But you need to keep ${} variables in the pkg-config output file.

CONFIGURE_FILE("mylib.pc.in" "mylib.pc" @ONLY)
The @ONLY prevents cmake substitution of ${} variables (see configure_file documentation).

Comments

Ads