Issue with TARGETS_EXPORT_NAME

Hi,

I’ve this random issue that I don’t really understand why it’s happening. So, while ago, I put some time and improved the mlpack CMake’s script, and I think for the most part I got everything right. But, every now and then, I see this random error where mlpack-targets.cmake is not being generated correctly.

I’m using this template to generate the mlpack-targets

@PACKAGE_INIT@

include(${CMAKE_CURRENT_LIST_DIR}/@TARGETS_EXPORT_NAME@.cmake)

set_and_check(MLPACK_INCLUDE_DIRS "@PACKAGE_MLPACK_INCS_DIR@")
set(MLPACK_LIBRARIES mlpack::mlpack)

but for some reason, the output file, mlpack-targets.cmake can end up not being complete,

include(${CMAKE_CURRENT_LIST_DIR}/.cmake) # ← **NOTICE HERE**

set_and_check(MLPACK_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include")
set(MLPACK_LIBRARIES mlpack::mlpack)

This of course causes CMake to throw and not being able to configure mlpack in my project. I think I did everything correctly, but apparently not. My workaround is to manually change this file, but that’s a horrible solution. I really appreciate if someone could help me track this down.

The parent CMakeLists.txt is here. And, the only thing I can think of is that since CMakeLists.txt is not in a parent directly, CMake gets confused and cannot handle the case correctly! The project has two CMakeLists.txt files, and mlpack-config.cmake.in is not in the same directory as either of them.

mlpack/CMakeLists.txt
mlpack/src/mlpack/CMakeLists.txt
mlpack/CMake/mlpack-config.cmake.in

and this is the error if mlpack-targets.cmake is incomplete:

CMake Error at /usr/local/lib/cmake/mlpack/mlpack-config.cmake:27 (include):
include could not find load file:

    /usr/local/lib/cmake/mlpack/.cmake
Call Stack (most recent call first):
CMakeLists.txt:40 (find_package)

Thanks in advance,
Amir.

Could you post what mlpack-config.cmake ends up being when configured? It looks like TARGETS_EXPORT_NAME is not valid when it is being configured somehow?

I think that’s exactly what happens, but I don’t know why. Here is mlpack-config.cmake:


####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was mlpack-config.cmake.in                            ########

get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)

macro(set_and_check _var _file)
  set(${_var} "${_file}")
  if(NOT EXISTS "${_file}")
    message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
  endif()
endmacro()

macro(check_required_components _NAME)
  foreach(comp ${${_NAME}_FIND_COMPONENTS})
    if(NOT ${_NAME}_${comp}_FOUND)
      if(${_NAME}_FIND_REQUIRED_${comp})
        set(${_NAME}_FOUND FALSE)
      endif()
    endif()
  endforeach()
endmacro()

####################################################################################

include(${CMAKE_CURRENT_LIST_DIR}/.cmake)

set_and_check(MLPACK_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include")
set(MLPACK_LIBRARIES mlpack::mlpack)

Running cmake --trace-expand and looking at the code which gets executed around the configure_file for it should show it. I suspect that variable is a parameter to the command which isn’t being passed in.

Thanks! I didn’t know about the --trace-expand. The result is interesting since I see this:

CMakeLists.txt(174):  set(TARGETS_EXPORT_NAME mlpack-targets )

I think as I was expecting and proposed a fixed for, TARGETS_EXPORT_NAME should be upper case. I fixed that but today I noticed that the Homebrew version was behind, and I’m guessing that this might be the actual issue.

I’ve updated the formula, and waiting to see how it goes! :slight_smile: