configure_file vs configure_package_config_file

Hi there,

Very quick question, I just wanted to check is it always preferred to use configure_package_config_file as opposed to configure_file when doing anything with package config files (this is for the main package and any other internal components too). If this is the case, is it also recommended to have every <package>-config.cmake.in also start with @PACKAGE_INIT@? I think that’s the case, but I just wanted to double check.

Thanks very much for your help,

Cheers!

Tom

You only need configure_package_config_file() if you need to refer to a path within your installed directory layout, and that path isn’t trivially available using something like ${CMAKE_CURRENT_LIST_DIR}/blah. It is primarily useful when you need a path relative to the base of the install, and where that path is defined by a variable like CMAKE_INSTALL_LIBDIR. Many projects don’t need to do this, and they can get away with plain old configure_file().

If you do use configure_package_config_file(), then you must put @PACKAGE_INIT@ at the top of it. That is the part that defines variables that the rest of your file will need (the @PACKAGE_BLAHBLAH@ variables that correspond to the PATH_VARS arguments).

1 Like

Thank you very much for the swift response @craig.scott, much appreciated!

I see, that’s good to know. I wound up switching to configure_package_config_file() mainly for consistency in the context I’m using it, but I might consider switching back if it’s simpler.

I have a config.in file that looks more or less like this:

include(CMakeFindDependencyMacro)
find_dependency(my-dependency)
include(${CMAKE_CURRENT_LIST_DIR}/my-library-targets.cmake) 

And was calling it from CMakeLists.txt like so:

configure_file(my-library-config.cmake.in my-library-config.cmake @ONLY)

And decided to update it to:

@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
find_dependency(my-dependency)
include(${CMAKE_CURRENT_LIST_DIR}/my-library-targets.cmake)

And then call it like this:

configure_package_config_file(
  my-library-config.cmake.in my-library-config.cmake
  INSTALL_DESTINATION
    ${CMAKE_INSTALL_LIBDIR}/cmake/my-project)

Both work, and maybe the first way was okay, but I guess in theory if the config.in file ever grows/changes and needs to refer to other paths, it’s now ready to do so.

Thanks again for your help and advice!