FindPkgConfig: translate pkg-config target paths to host when cross-compiling
We have a library (nanomsg in this case) which we build for ourselves if the library is not found. Therefore, we create an imported target and set the target properties. When we cross compile this on a toolchain which already has nanomsg, the package points to "/usr/lib", but it's really located in "<sysroot>/usr/lib".
root@15e3ded24d03:/gateway# cmake --version cmake version 2.8.12.2
Here are the cmake instructions generating this imported target:
pkg_search_module(NANOMSG REQUIRED nanomsg)
set (NANOMSG_LIB_LOCATION "${NANOMSG_LIBDIR}/lib${NANOMSG_LIBRARIES}.so")
message(STATUS "NANOMSG_FOUND ${NANOMSG_FOUND}")
message(STATUS "NANOMSG_LIBRARIES ${NANOMSG_LIBRARIES}")
message(STATUS "NANOMSG_LIBRARY_DIRS ${NANOMSG_LIBRARY_DIRS}")
message(STATUS "NANOMSG_LDFLAGS ${NANOMSG_LDFLAGS}")
message(STATUS "NANOMSG_LDFLAGS_OTHER ${NANOMSG_LDFLAGS_OTHER}")
message(STATUS "NANOMSG_INCLUDE_DIRS ${NANOMSG_INCLUDE_DIRS}")
message(STATUS "NANOMSG_CFLAGS ${NANOMSG_CFLAGS}")
message(STATUS "NANOMSG_CFLAGS_OTHER ${NANOMSG_CFLAGS_OTHER}")
message(STATUS "NANOMSG VERSION: ${NANOMSG_VERSION}")
message(STATUS "NANOMSG PREFIX: ${NANOMSG_PREFIX}")
message(STATUS "NANOMSG INCLUDEDIR: ${NANOMSG_INCLUDEDIR}")
message(STATUS "NANOMSG LIBDIR: ${NANOMSG_LIBDIR}")
add_library(nanomsg SHARED IMPORTED)
set_target_properties(nanomsg PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${NANOMSG_INCLUDEDIR}"
INTERFACE_LINK_LIBRARIES "${NANOMSG_LIBRARIES}"
INTERFACE_COMPILE_OPTIONS "${NANOMSG_LDFLAGS}"
IMPORTED_LOCATION "${NANOMSG_LIB_LOCATION}"
)
The output from this:
-- NANOMSG_FOUND 1
-- NANOMSG_LIBRARIES nanomsg
-- NANOMSG_LIBRARY_DIRS
-- NANOMSG_LDFLAGS -lnanomsg
-- NANOMSG_LDFLAGS_OTHER
-- NANOMSG_INCLUDE_DIRS
-- NANOMSG_CFLAGS
-- NANOMSG_CFLAGS_OTHER
-- NANOMSG VERSION: 1.0.0
-- NANOMSG PREFIX: /usr
-- NANOMSG INCLUDEDIR: /usr/include
-- NANOMSG LIBDIR: /usr/lib
-- NANOMSG LIBRARIES: nanomsg
-- NANOMSG LDFLAGS: -lnanomsg
-- NANOMSG CFLAGS:
-- NANOMSG LOCATION: /usr/lib/libnanomsg.so
pkg-config output
root@15e3ded24d03:/gateway# pkg-config --print-variables nanomsg
exec_prefix
includedir
libdir
pcfiledir
prefix
root@15e3ded24d03:/gateway# pkg-config --variable=exec_prefix nanomsg
/usr
root@15e3ded24d03:/gateway# pkg-config --variable=includedir nanomsg
/usr/include
root@15e3ded24d03:/gateway# pkg-config --variable=libdir nanomsg
/usr/lib
root@15e3ded24d03:/gateway# pkg-config --variable=pcfiledir nanomsg
*<sysroot>*/usr/lib/pkgconfig
root@15e3ded24d03:/gateway# pkg-config --variable=prefix nanomsg
/usr
The make error:
make[2]: *** No rule to make target `/usr/lib/libnanomsg.so', needed by `core/libgateway.so'. Stop.
The generated link.txt
:
<sysroot_native>/arm-poky-linux-gnueabi-gcc -fPIC --std=c99 -fPIC -O -fno-omit-frame-pointer -g -feliminate-unused-debug-types -pipe -march=armv7-a -mfloat-abi=hard -mfpu=neon -m tune=cortex-a9 --sysroot=*<sysroot>* -g -Wl,-O1 -Wl,--hash-style=gn u -Wl,--as-needed --sysroot=*<sysroot>* -Wl,-soname,libgatew ay.so -o libgateway.so CMakeFiles/gateway.dir/src/message.c.o CMakeFiles/gateway.dir/src/internal/event_system.c.o CMake Files/gateway.dir/src/gateway_internal.c.o CMakeFiles/gateway.dir/src/gateway.c.o CMakeFiles/gateway.dir/src/gateway_cre atefromjson.c.o CMakeFiles/gateway.dir/src/broker.c.o CMakeFiles/gateway.dir/src/module_loader.c.o CMakeFiles/gateway.di r/adapters/dynamic_library_linux.c.o CMakeFiles/gateway.dir/adapters/gb_library_linux.c.o CMakeFiles/gateway.dir/src/mod ule_loaders/dynamic_loader.c.o deps/libparson.a /usr/lib/libnanomsg.so ../../install-deps/lib/libaziotsharedutil.so -ldl -lm -luuid -lcurl -lssl -lcrypto -lpthread -lm -luuid -Wl,-rpath,/gateway/install-deps/lib:::::::::::::::::::::
Is there a better solution for this? I worked around this by not creating the imported target when nanomsg is found in a "standard" location, but it doesn't cover all of the possible locations a toolchain might install a library, and I don't want to add edge cases.