I’ve worked thru packaging a static library using the TGZ generator. If I were to host this file somewhere. How would I consume it in another project. I know FetchContent can handle remote archives, but they need to contain a CMakeLists.txt.
If I understand the situation correctly, isn’t it just a matter of using find_package to locate the library once it’s installed locally?
I only want to use it in the local project. I do not want to affect the global system state. Ideally I wouldn’t have to do any extra steps before calling CMake and it would work like FetchContent.
This exported cmake config package is not right!
See Is beman.exemplar/beman.exemplar-version.cmake a vallid package name? - #2 by ClausKlein
what?
If you install it, cmake will not find it.
Let me reframe the question. Given a URL to a tarball created with CPack with the TGZ generator. How would I use it in a CMake project? Is there any way to directly reference the URL in the CMakeLists.txt? If not, how can I reference it after downloading it and extracting it?
With this cmake file names
in the tar file, you can’t use it!
I’ve removed the example from my question. I’m looking for help figuring out the workflow for consuming packaged CMake projects.
you may use find_package() after installing your package.
Can you explain what “installing” means in this context?
extracting the tar file to the right place.
or cmake --install ...
Assuming that you have a config file suitable for find_package, you can download the tgz, unpack it, add the path to CMAKE_ MODULE_PATH and call find_ package.
Without the config file, you can create the imported library target in your own.
By downloading it with file(DOWNLOAD …):
set(SOME_FILE "some.tgz")
message(STATUS "Downloading the file...")
file(DOWNLOAD "https://your.domain/data/${SOME_FILE}"
"${CMAKE_CURRENT_SOURCE_DIR}/external/${SOME_FILE}"
#SHOW_PROGRESS
EXPECTED_HASH SHA1=b28c4cdfd737c41d1252945224aaddf97e45dc27 # should be the actual hash
STATUS DOWNLOAD_DATASETS_RESULTS
)
list(GET DOWNLOAD_DATASETS_RESULTS 0 DOWNLOAD_STATUS_CODE)
list(GET DOWNLOAD_DATASETS_RESULTS 1 DOWNLOAD_ERROR_MESSAGE)
if(${DOWNLOAD_STATUS_CODE} EQUAL 0)
message(STATUS "...successfully downloaded the file")
else()
file(REMOVE "${CMAKE_CURRENT_SOURCE_DIR}/external/${SOME_FILE}")
message(WARNING "...failed to download the file. ${DOWNLOAD_ERROR_MESSAGE}") # might want to use FATAL_ERROR instead of WARNING
endif()
But since it’s a .tgz
archive, you’ll also need to unpack it, perhaps with a cmake -E. And then, as many have already mentioned, ideally you’d need to have a CMake config for your package (which is auto-generated if you have a proper installation procedure), otherwise you’d need to manually add its headers and binaries to your project.
I’d recommend you to use a package manager (such as vcpkg) for all this. Higher entry level, for sure, but then such tasks will get much more simpler for you going forward.