build: Produce correct pc file with absolute libdir
The CMAKE_INSTALL_INCLUDEDIR and CMAKE_INSTALL_LIBDIR variables
can be absolute paths so simply appending them to \${prefix}
might not work.
Unfortunately, CMake does not have simple function for joining paths [1]
so I had to write my own suboptimal one.
It somewhat handles the following cases
- prefix absolute + libdir relative
- prefix absolute + libdir absolute but not under prefix
but for
- prefix absolute + libdir absolute & under prefix
we would want to strip the prefix and make the libdir relative
so we can make it relative to the pkg-config variables.
Basically, we want the equivalent of the following Python function:
import os.path
from pathlib import PurePath
if os.path.isabs(libdir) and os.path.commonpath([libdir, prefix]) == str(PurePath(prefix)):
# Convert path to be relative to prefix
skip = len(prefix) + 1
libdir = libdir[skip:]
return os.path.join('${prefix}', libdir)
This would be non-trivial to replicate in CMake scripts as far as I can tell.