CPM.cmake
CPM.cmake copied to clipboard
Add option to allow `CPMFindPackage` to behave as `find_package`
We currently use CPMFindPackage
as a way to try to find a package if it currently exists on the users system, but then fall back to fetching it and building it from source if it doesn't currently exist. This works great for users in practice because generally it makes the builds just work which is the goal.
Unfortunately, one of the downsides to this is that for us as library maintainers and packagers, we can run into issues where in a packaging flow we expect to have all of the packages installed on the system, but if we miss something then it falls back to CPMAddPackage
and can lead to us erroneously bundling that dependency in our package.
It would be great if there were options that had the opposite behavior of CPM_DOWNLOAD_ALL
and CPM_DOWNLOAD_<dependency name>
that would prevent fetching the package from source if it was failed to be found.
Not sure I understand but does CPM_USE_LOCAL_PACKAGES not work for this?
~Indeed, using CPM_LOCAL_PACKAGES_ONLY
seems to have the behavior I'm looking for, albeit with an extra call to cpm_find_package
underneath the CPMAddPackage
call, but that shouldn't be an issue. Apologies for the noise.~
EDIT: Apologies, I forgot a critical detail of my use case, where there's a combination of CPMAddPackage
and CPMFindPackage
calls and the CPMAddPackage
calls wouldn't be expected to function any differently where they would still fetch and build as expected.
@kkraus14 Is you problem solved with setting CPM_USE_LOCAL_PACKAGES to NO?
Unfortunately not. We have a scenario that looks roughly like the following:
...
# Always want to fetch these as we apply some patches on top of them or something else that prevents using a pre-installed package
CPMAddPackage(package_a ...)
CPMAddPackage(package_b ...)
# We want to find these on the system if they exist, if they don't exist then we want to fetch them (idea being to enable an end user to build and use the software as easily as possible)
# In the case of packaging the software, we don't want to bundle these packages with our software, so we'd love to have an option in CPM to not fall back to fetching if the option is set
CPMFindPackage(package_c ...)
CPMFindPackage(package_d ...)
CPMFindPackage(package_e ...)
Currently, CPM_USE_LOCAL_PACKAGES
and CPM_LOCAL_PACKAGES_ONLY
would enable what we want to do with packages c, d, and e in the above example, but would break what we need to do with packages a and b. If there was CPM_LOCAL_PACKAGE_ONLY_<dependency name>
that would solve the problem or something more general that errors if CPMFindPackage
fails to find the package locally instead of falling back to CPMAddPackage
.
The workaround here looks something like wrapping CPMFindPackage
behind another function call where if an option is set forwards it to find_package
instead of CPMFindPackage
.