Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMake package config #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

globberwops
Copy link

Fixes #35

  • Add a project version
  • Add package version and config files

@globberwops globberwops marked this pull request as ready for review January 4, 2023 10:14
@globberwops globberwops force-pushed the add-cmake-package-config branch from c61aaba to d86e492 Compare January 17, 2023 19:25
@globberwops
Copy link
Author

Ping @jpihl @mortenvp @techbech @loglund

@DerThorsten
Copy link

Ping @jpihl @mortenvp @techbech @loglund
It would really be nice to have this PR merged as this would simplify the conda-forge build conda-forge/staged-recipes#22527

@jpihl
Copy link
Member

jpihl commented Apr 13, 2023

Hi @globberwops,

Sorry for the radio silence and thanks for your extensive perseverance 👍

Non of us are very familiar with vcpkg not do we understand the need for your changes in the cmake file. Would you care to elaborate on the motivation for the changes?

All the best

Jeppe

@globberwops
Copy link
Author

Hi @jpihl, no worries! Thanks for your reply.

These changes are only loosely related to vcpkg.
The primary purpose of a CMake package config is for CMake to be able to find the lib with a call to find_package in a downstream project, see cmake-packages(7).

Once installed, CMake will be able to find the file recycle-config.cmake, which in turn includes recycle-targets.cmake.
The latter defines imported targets that can be linked in the downstream project to provide the location of the lib and the corresponding include path.

vcpkg is just one of the package managers that rely on CMake to provide package config files.
However, vcpkg is by no means needed to consume the package configs.

I hope that helps.

Cheers,
Martin

@jpihl
Copy link
Member

jpihl commented Apr 14, 2023

Thanks, that cleared up a few things.
I still need to understand all the changes before I feel comfortable merging this one. I will add a few comments inline, and I hope you'll be able to help me understand everything. As I mentioned, I only have a little experience with CMake Packages.

CMakeLists.txt Outdated

# Define library
add_library(recycle INTERFACE)
target_compile_features(recycle INTERFACE cxx_std_14)
target_include_directories(recycle INTERFACE src/)
target_include_directories(recycle INTERFACE $<BUILD_INTERFACE:src> $<INSTALL_INTERFACE:include>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between this and the original?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The include path in the BUILD_INTERFACE is used during the build of this project, and the include path in the INSTALL_INTERFACE will be used by downstream projects that use an installed version of this project.

During the build of this project, the headers are located in src, but they will be installed in <INSTALL_PREFIX>/include (according to GNU convention).
This is why we have to differentiate and tell CMake, where to find the headers in both cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so if the library is being used as a CMake Package, INSTALL_INTERFACE will be used, but if it's being used with, e.g.,
add_subdirectory or FetchContent_Declare, BUILD_INTERFACE will be used?
Is this correctly understood?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly 👍🏻

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our internal projects, we use add_subdirectory for dependencies. I just tried using these changes, and I got this error:

[cmake] CMake Error in resolve_symlinks/slide/CMakeLists.txt:
[cmake]   Target "recycle" contains relative path in its
[cmake]   INTERFACE_INCLUDE_DIRECTORIES:
[cmake] 
[cmake]     "src"
[cmake] 
[cmake] 
[cmake] -- Generating done
[cmake] CMake Generate step failed.  Build files cannot be regenerated correctly.

I search a little and found this, so it seems the BUILD_INTERFACE needs to have ${CMAKE_CURRENT_SOURCE_DIR} prepended. At least that fixed the error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @globberwops; if you make this change, we should be able to merge this one finally ;-)

Cheers!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpihl Done! Thanks for the reminder :)

install(
EXPORT recycle-targets
DESTINATION ${RECYCLE_CONFIG_DIR}
NAMESPACE steinwurf::)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need help understanding the purpose of lines 24-29.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call will create the file recycle-targets.cmake in the dir ${RECYCLE_CONFIG_DIR}.
The targets defined in this file will be prefixed with steinwurf::.

Downstream projects can then link these targets e.g.

find_package(recycle)
target_link_libraries(<TARGET> PUBLIC steinwurf::recycle)

cmake/recycle-config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/recycle-config.cmake"
INSTALL_DESTINATION ${RECYCLE_CONFIG_DIR}
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the NO_CHECK_REQUIRED_COMPONENTS_MACRO option set?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, configure_package_config_file will put a macro into the package config file that can be used to check, if all components of the package can be found, e.g.

find_package(Boost COMPONENTS regex log)

Since this project does not have any components, we disable the macro.

@globberwops globberwops force-pushed the add-cmake-package-config branch from d86e492 to edd76bc Compare April 22, 2023 07:41
@globberwops globberwops force-pushed the add-cmake-package-config branch from edd76bc to 27ab859 Compare April 22, 2023 08:02
@globberwops globberwops mentioned this pull request Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CMake package config
3 participants