cpm-cmake / CPM.cmake

📦 CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using CPM_dep_SOURCE appears to work, but includes are not found

Radagan opened this issue · comments

I have a library in that I can load normally with CPMAddPackage and works fine. As I'm actively working on this library and an app that uses it, I would like to use the CPM_dep_SOURCE feature to point the source to my local checkout.

The library's name in this case is common. I've tried both passing this to CMake or setting it like this:

set(CPM_common_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries/common")

When I do, the #include(s) for the header files are not found. I did try an absolute path as well. Looking into the _deps directory, I do see that the usual common-src directory is missing when I set CPM_common_SOURCE.

Any help would be greatly appreciated. I'm probably just setting something incorrectly.

I also tried replacing my CPMAddPackage with this:

CPMAddPackage(
    NAME common
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries/common
)

But exactly the same behavior resulted: header files not found and only common-build and common-subbuild under _deps.

The missing common-src directory is expected. These directories are created by the underlying call to FetchContent. If FetchContent is called with the SOURCE_DIR argument, it won't copy the source into common-src as it just uses whereever the path in SOURCE_DIR points to.

The two examples you provided are equivalent. If CPM_<package>_SOURCE is set, CPMAddPackage will "call itself" with the SOURCE_DIR argument. For example:

set(CPM_common_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries/common")
CPMAddPackage( NAME common )

results in:

CPMAddPackage( NAME common SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries/common" )

Regarding the actual problem:
I did have the same issue up until I deleted the build directory. During testing i've deleted the cmake cache multiple times, but not the build directory. After that everything worked.

I was able to replicate the issue even when just using FetchContent, so it doesn't seem to be related to CPM.

include(FetchContent)
FetchContent_Declare(
	common
	SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../common"
)
FetchContent_MakeAvailable(common)

Try deleting the build directory and see if it works. Also, run the build with verbose output to see if the include flag is passed to the compiler.

Sorry that I can't give you a definitive answer to the problem. Hopefully, I was atleast able to clear up some confusion around the common-* directories. :D

@Avus-c, thank you for your reply. It helped me greatly to understand where this is going wrong, especially the FetchContent example. You are correct that it doesn't look like this is a CPM issue directly. I can confirm that it happens, as you said, with FetchContent as well.

Unfortunately, removing the build directory doesn't fix it for me. No matter what I've tried, it still doesn't see the headers.

I'll experiment further...

I did find a workaround for this in case anyone else runs into the issue. Add:

$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries/common/include>

or better:

$<BUILD_INTERFACE:${commonSOURCE_DIR}/include

to your target_include_directories for the target, adjusting of course for your library.