[CMake] Is there a better way to retrieve content from a remote zip file?

Lee Butler iraytrace at gmail.com
Thu Apr 28 11:19:05 EDT 2016


I am looking for a better way to do something than having a sequence of 
"if (NOT EXISTS)" statements.  My package being built depends on some 
external libraries and headers, that are available in a zip file on an 
external website.  So for example:  zlib.h,zlib.dll and zlib.lib are 
(along with other stuff) in 3rdParty.zip available from a remote URL.  I 
don't want to build these things, I want to fetch the zip file, unpack 
it and copy the relevant portions into place only as needed.

At the moment I have the code below, which feels "unclean" on so many 
levels:

if (NOT EXISTS "${CMAKE_INSTALL_PREFIX}/include/zlib.h")
   if (NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/3rdParty_x86_x64")
     if (NOT EXISTS 
"${CMAKE_CURRENT_BINARY_DIR}/3rdParty_VS2013_v120_x86_x64_V9_small.7z")
        MESSAGE(STATUS "Downloading 3rdParty")
        file(DOWNLOAD
http://download.osgvisual.org/3rdParty_VS2013_v120_x86_x64_V9_small.7z
"${CMAKE_CURRENT_BINARY_DIR}/3rdParty_VS2013_v120_x86_x64_V9_small.7z" 
SHOW_PROGRESS)
     endif()

     message(STATUS "expanding 3rdParty Archive")
     execute_process( COMMAND ${CMAKE_COMMAND} -E tar xzf 
"${CMAKE_CURRENT_BINARY_DIR}/3rdParty_VS2013_v120_x86_x64_V9_small.7z" )
   endif()
   foreach(dir bin data include lib ssl)
     file(RENAME 
"${CMAKE_CURRENT_BINARY_DIR}/3rdParty_x86_x64/x64/${dir}" 
"${CMAKE_INSTALL_PREFIX}/${dir}")
   endforeach(dir)
   file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/3rdParty_x86_x64")
endif()

This has the property of fetching the 7z file, extracting the contents 
and leaving the 7z file around for later rebuilds.  I hope there is a 
cleaner way of saying the same thing.  BTW, I would be just as happy 
putting "products" from the zip file in the current build destinations 
instead of the install destinations.


More information about the CMake mailing list