CPM.cmake icon indicating copy to clipboard operation
CPM.cmake copied to clipboard

Eigen and package_lock

Open DNKpp opened this issue 2 years ago • 4 comments

I recently tried adding Eigen from the latest gitlab release. That's working fine, but I had a hard time making it work with a package lock and in the end I gave up. The example here https://github.com/cpm-cmake/CPM.cmake/wiki/More-Snippets#eigen shows how to make it work via custom target, but the Eigen_ADDED is not set when using the combination CPMDeclarePackage and CPMGetPackage combination. Can someone enlighten me? :D

DNKpp avatar Jul 26 '22 19:07 DNKpp

Thats strange and sounds like a bug. Unfortunately, I can't recreate it, as the script below seems to work fine for me (created Eigen target is printed on each CMake run). Could you share some minimal example code to help reproduce the issue?

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(Playground LANGUAGES CXX)

# ---- Fetch CPM ----

set(CPM_DOWNLOAD_VERSION 0.35.3) 
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
  message(STATUS "Downloading CPM.cmake v${CPM_DOWNLOAD_VERSION}")
  file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake ${CPM_DOWNLOAD_LOCATION})
endif()
include(${CPM_DOWNLOAD_LOCATION})

# Run `cmake --build build --target cpm-update-package-lock` to update the package lock.
CPMUsePackageLock(package-lock.cmake)

# ---- Add dependencies ----

CPMAddPackage(
  NAME Eigen
  VERSION 3.2.8
  URL https://gitlab.com/libeigen/eigen/-/archive/3.2.8/eigen-3.2.8.tar.gz
  # Eigen's CMakelists are not intended for library use
  DOWNLOAD_ONLY YES 
)

if(Eigen_ADDED)
  add_library(Eigen INTERFACE IMPORTED)
  target_include_directories(Eigen INTERFACE ${Eigen_SOURCE_DIR})
  message("created Eigen target")
endif()

TheLartians avatar Jul 31 '22 20:07 TheLartians

Perhaps I misunderstand the package lock setup. My package-lock.cmake looks like this:

CPMDeclarePackage(
    Eigen
    GIT_TAG 3.4.0
    GITLAB_REPOSITORY libeigen/eigen
    DOWNLOAD_ONLY YES
    EXCLUDE_FROM_ALL YES
)

and in my CMakeLists.txt where I try to get it like this.

CPMGetPackage(Eigen)
if (Eigen_ADDED)
    message(STATUS "Add custom Eigen target.")
    add_library(Eigen INTERFACE IMPORTED)
    target_include_directories(Eigen INTERFACE ${Eigen_SOURCE_DIR})
endif()

The message won't be printed. Am I doing anything wrong here?

DNKpp avatar Jul 31 '22 21:07 DNKpp

Ah thanks for clarifying, I now too have the issue. ~~That's strange and there definitely seem to be some bugs involving the package lock, as the initial package lock file created by the cpm-update-package-lock seems to be wrong. I'll look into it.~~

I got confused by the default package-lock formatting into believing that the package name has been omitted. Actually, it seems to be working as intended.

TheLartians avatar Sep 13 '22 19:09 TheLartians

Actually I believe the issue is that you forgot the package name (it needs to be redundant in CPMDeclarePackage as it current allows overriding all arguments, including the name). This overwrites the CPMAddPackage call to add a package without name, so that _ADDED is defined instead of Eigen_ADDED. To fix, simply update your package lock:

CPMDeclarePackage(
    Eigen
+++ NAME Eigen
    GIT_TAG 3.4.0
    GITLAB_REPOSITORY libeigen/eigen
    DOWNLOAD_ONLY YES
    EXCLUDE_FROM_ALL YES
)

TheLartians avatar Sep 13 '22 20:09 TheLartians

Actually I believe the issue is that you forgot the package name (it needs to be redundant in CPMDeclarePackage as it current allows overriding all arguments, including the name). This overwrites the CPMAddPackage call to add a package without name, so that _ADDED is defined instead of Eigen_ADDED. To fix, simply update your package lock:

CPMDeclarePackage(
    Eigen
+++ NAME Eigen
    GIT_TAG 3.4.0
    GITLAB_REPOSITORY libeigen/eigen
    DOWNLOAD_ONLY YES
    EXCLUDE_FROM_ALL YES
)

Thank you very much. That seems to solve my issue ;)

DNKpp avatar Sep 25 '22 14:09 DNKpp