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

Include "built" header-only libraries with CPM. #354

Closed
hameer-spire opened this issue May 20, 2022 · 4 comments
Closed

Include "built" header-only libraries with CPM. #354

hameer-spire opened this issue May 20, 2022 · 4 comments

Comments

@hameer-spire
Copy link

Hello,

First of all, thanks for the awesome library!

I'm trying to include header-only libraries that only have a valid configuration and all targets after they're built/installed, for example https://github.com/xtensor-stack/xtensor. In this case, the "build" directory is the actual source directory. Is there a way to handle this?

@TheLartians
Copy link
Member

Hey, it depends on the implementation of the library, but a simple way could be to create an interface target that includes the directory with the generated header. I believe you should be able to use add_dependencies to ensure that the header gets built before dependent targets that use them.

If the library doesn't behave well with CMake you should be able to create a target that uses an external script through add_custom_command.

In any case this seems more CMake related than package management, so it could make sense to ask the question at discourse.cmake.org as well.

@oddko
Copy link

oddko commented May 24, 2022

If it's header only, you're pretty much better off creating an interface library target yourself (e.g. add_library(xtensor INTERFACE)), add an INTERFACE include directory with the path to the include/ directory that you got using CPM, and calling it a day. That's how I do it for Eigen and it works very well. This means you'll need to set the flag DOWNLOAD_ONLY on CPM.

@hameer-spire
Copy link
Author

@oddko Would it be possible for you to link me to a code snippet, if it's not proprietary?

@oddko
Copy link

oddko commented May 25, 2022

Sure thing (note that it uses relative repositories that are added in this PR #333)

CPMAddPackage(
        RELATIVE_REPOSITORY ../eigen
        GIT_TAG 3.4.0
        VERSION 3.4.0
        GIT_SHALLOW TRUE
        DOWNLOAD_ONLY TRUE)

if (eigen_ADDED)
    add_library(eigen INTERFACE)
    target_include_directories(eigen INTERFACE "${eigen_SOURCE_DIR}")
    if (MSVC)
        # For compilers below MSVC2019, ask for extended aligned storage
        if (${MSVC_VERSION} VERSION_LESS 1920)
            target_compile_definitions(eigen INTERFACE _ENABLE_EXTENDED_ALIGNED_STORAGE)
        endif()
    endif()
endif()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants