NOAA-EMC / NCEPLIBS-g2c

This library contains C decoder/encoder routines for GRIB edition 2.
Other
18 stars 11 forks source link

Error in installed cmake package files #520

Closed DusanJovic-NOAA closed 3 days ago

DusanJovic-NOAA commented 1 week ago

In the installed cmake package file (<prefix>/lib64/cmake/g2c/g2c-config.cmake) there is this line:

get_target_property(g2c_BUILD_TYPES g2c::g2c IMPORTED_CONFIGURATIONS)

which assumes that g2c::g2c target exists, which is not the case if library is built with -DBUILD_SHARED_LIBS=OFF. In that case only g2c::g2c_static exists. I get this error when I build my project that uses g2c library:

CMake Error at /home/dusan/noaa/g2c/NCEPLIBS-g2c-develop/install/lib64/cmake/g2c/g2c-config.cmake:32 (get_target_property):
  get_target_property() called with non-existent target "g2c::g2c".
Call Stack (most recent call first):
  CMakeLists.txt:24 (find_package)

File cmake/PackageConfig.cmake.in should be changed like this:

diff --git a/cmake/PackageConfig.cmake.in b/cmake/PackageConfig.cmake.in
index 1db3d57..b881354 100644
--- a/cmake/PackageConfig.cmake.in
+++ b/cmake/PackageConfig.cmake.in
@@ -5,6 +5,11 @@
# Include targets file.  This will create IMPORTED target @PROJECT_NAME@
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake")

+if(@BUILD_SHARED_LIBS@)
get_target_property(@PROJECT_NAME@_BUILD_TYPES @PROJECT_NAME@::@PROJECT_NAME@ IMPORTED_CONFIGURATIONS)
+endif()
+if(@BUILD_STATIC_LIBS@)
+get_target_property(@PROJECT_NAME@_static_BUILD_TYPES @PROJECT_NAME@::@PROJECT_NAME@_static IMPORTED_CONFIGURATIONS)
+endif()

check_required_components("@PROJECT_NAME@")

to conditionally run get_target_property depending on whether shared or static libraries are installed.

DusanJovic-NOAA commented 1 week ago

Another issue with installed package config files is that INTERFACE_INCLUDE_DIRECTORIES is not defined for imported targets, so I get this error when I try to build project that requires g2c

-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/dusan/noaa/g2c/test/b
[ 50%] Building C object CMakeFiles/read_g2.dir/read_g2.c.o
/home/dusan/noaa/g2c/test/read_g2.c:1:10: fatal error: grib2.h: No such file or directory
    1 | #include "grib2.h"
      |          ^~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/read_g2.dir/build.make:76: CMakeFiles/read_g2.dir/read_g2.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/read_g2.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

To fix this, the location of installed include file(s) must be defined for both shared and static targets, now it's only defined for "objlib" target which is not installed at all.

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a1ad0c6..bdebf93 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -157,6 +157,14 @@ CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/grib2.h.in" "${CMAKE_CURRENT_BINARY_

 target_include_directories(${lib_name}_objlib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
                                                      $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
+if(BUILD_SHARED_LIBS)
+  target_include_directories(${lib_name}_shared PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
+                                                       $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
+endif()
+if(BUILD_STATIC_LIBS)
+  target_include_directories(${lib_name}_static PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
+                                                       $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>)
+endif()

 if (NOT WIN32)
   target_link_libraries(${lib_name}_objlib INTERFACE m)