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

Don't unconditionally download in get_cpm.cmake

genevieve-me opened this issue · comments

The provided get_cpm.cmake template will always try to download from github:

file(DOWNLOAD

This causes builds to fail when run by a sandboxed builder. For example, I was hoping to package ada-url for nixpkgs, which uses your template verbatim, and had to patch it to not download if CPM_DOWNLOAD_LOCATION was set, eg:

-file(DOWNLOAD
+if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
+  file(DOWNLOAD

Would you be open to making such a change upstream? Thanks!

In my opinion, adding the if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) check contradicts the SHA256 option set in the file(DOWNLOAD) command. If the download somehow failed and corrupted but the file exists, it may result in unexpected behavior.

Thank you for the feedback! I'm not very experienced with CMake, which is why I didn't file this as a PR, but if you have a suggestion for a better way (all that is really necessary here is some way of preventing the download) I'd be happy to test it and create a PR.

Hey, thanks for the issue! For sandboxed environments, I recommend vendoring dependencies (and CPM itself) by setting the CPM_SOURCE_CACHE environment to a populated cache directory on the Sandbox machine. If all dependencies are present this should allow configuring the project even without any connection to the internet.

Thank you for the suggestion! Unfortunately, even with CPM vendored under CPM_SOURCE_CACHE, I was experiencing build failures due to attempted downloads.

@genevieve-me that's unexpected, as one of the goal of this project is to make it simple to work with sandboxed / offline environments. Can you please try if the following works for you?

# while connected to the internet we clone a test project
git clone https://github.com/TheLartians/ModernCppStarter
cd ModernCppStarter
# define the source cache to a local directory or leave as-is if it's already defined
export CPM_SOURCE_CACHE=$(pwd)/cache 
# configure the project (this should populate the cache with CPM and some dependencies of the starter)
cmake -S test -B build
# once finished, delete the build directory to simulate a clean environment
rm -rf build

Now the cache is populated, and we should be able to configure the project without internet connection. With the same CPM_SOURCE_CACHE environment as above, disconnect from the internet (or move the project to a sandbox) and run the following.

# should work offline if the cache is populated
cmake -S test -B build
# we should also be able to build the project
cmake --build build

This works fine on my computer, so I assume that working in sandboxed environments with populated caches should work too. If you encounter issues can you share the full error message together with information about your system, like OS, CMake version etc?