Search code examples
c++windowsdependenciescmake

How to automatically download C++ dependencies in a cross platform way + CMake?


I want to achieve this workflow:

  1. Checkout from repository on windows system (or any platform for that matter).
  2. Run some tool that gets dependencies, both includes and libs and puts them in their proper place (like in "\Program Files\Microsoft Visual Studio 10.0\VC\Lib and \Includes" on windows)
  3. Run CMake (creates MSVS projects on win)
  4. Open up MSVS project and compile it.

And I would like to have this workflow on most platforms. I don't want to have to download dependencies manually.

How to do this without storing dependencies in repository?


Solution

  • In CMake you can use file(DOWNLOAD URL PATH) to download a file, combine this with custom commands to download and unpack:

    set(MY_URL "http://...")
    set(MY_DOWNLOAD_PATH "path/to/download/to")
    set(MY_EXTRACTED_FILE "path/to/extracted/file")
    
    if (NOT EXISTS "${MY_DOWNLOAD_PATH}")
        file(DOWNLOAD "${MY_URL}" "${MY_DOWNLOAD_PATH}")
    endif()
    
    add_custom_command(
        OUTPUT "${MY_EXTRACTED_FILE}"
        COMMAND command to unpack
        DEPENDS "${MY_DOWNLOAD_PATH}")
    

    Your target should depend on the output from the custom command, then when you run CMake the file will be downloaded, and when you build, extracted and used.

    This could all be wrapped up in a macro to make it easier to use.

    You could also look at using the CMake module ExternalProject which may do what you want.