CMake generated Targerts file and staging areas

When I look at a <Project>Targets.cmake generated by CMake 22.1, I see that it compensates for the fact that the file may not be in its final destination (e.g., it may be in some staging area). Here is the relevant snippet from SWIPLTargets.cmake, normally located in /usr/lib/cmake/swipl/SWIPLTargets.cmake.

# Compute the installation prefix relative to this file.
get_filename_component(_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
get_filename_component(_IMPORT_PREFIX "${_IMPORT_PREFIX}" PATH)
if(_IMPORT_PREFIX STREQUAL "/")
  set(_IMPORT_PREFIX "")
endif()

# Create imported target swipl::swipl
add_executable(swipl::swipl IMPORTED)

# Create imported target swipl::libswipl
add_library(swipl::libswipl SHARED IMPORTED)

set_target_properties(swipl::libswipl PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/lib/swipl/include"
)

This adjust the location of the include directory, but when I use this for building a snap, where system is installed in the staging area /root/stage/usr/, I get the error below. It seems the location of the library is not updated.

CMake Error at /root/stage/usr/lib/cmake/swipl/SWIPLTargets.cmake:87 (message):
  The imported target "swipl::libswipl" references the file

     "/usr/lib/libswipl.so.8.5.20"

  but this file does not exist.  Possible reasons include:

Is this a bug?

edit This is getting a little weird. I see this is intended to work because the build-type specific Targets files use e.g.

 IMPORTED_LOCATION_RELWITHDEBINFO "${_IMPORT_PREFIX}/lib/swipl/lib/x86_64-linux/libswipl.so.8.5.20"

In the version that is build as part of building a snap however, the import for the executable uses IMPORTED_LOCATION_PGO "${_IMPORT_PREFIX}/lib/swipl/bin/x86_64-linux/swipl", but the one for the library uses simply IMPORTED_LOCATION_PGO "/usr/lib/libswipl.so.8.5.20" That seems wrong to me, but this is from the same source, both running in Ubuntu 22.04 using CMake 22.1. Only, the good one on bare metal and the bad one as part of a snap build process in an LXD container.

I’ll keep digging. Hints are welcome :slight_smile:

1 Like