Search code examples
gitgithubcmakedependencies

How should I link to library dependencies from my git repository?


I have made a project on github that uses several libraries such as glew, glm, sdl2 or sdl2_image. I use them by having them in an a folder named 'external', and building them from source using CMake.

My problem is trying to setup that 'external' folder right after cloning the repo, so that it is easy for the user to compile the project.

  • I could store the external folder as-is on my github repo, but it would get cumbersome to fetch the new updates of the libraries, and I don't want to make it seem like I'm "stealing ownership" of the libraries. And I don't really want to allow the usage of version control for those files.
  • I could use git submodules to point at the right commits I want, but this makes the download super heavy (fetching each repo is about 10-20 times bigger than the release zip folder). And I do not really need the version control information anyway, since I'm not working on those dependencies, and would not be making changes.

The two reasonable options (but not optimal) that I see for me are downloading a zip file of the dependencies that I would have made myself, or fetching the dependencies one by one and unpacking them at the right locations through a script.

But making the script would add dependencies such as wget and its Windows equivalent, and I don't know where I could store the zip file, since github doesn't seem to provide a storage place for additional files (a new branch?).

  • Does github provide any way of linking a repository to external repo releases?
  • Otherwise, what would be a good place to store additional files relevant to a repository that should not be part of the version control?

Solution

  • This sounds like a job for FetchContent: a fairly recent couple of scripts that should be able to do exactly what you want. That is, download either compiled binary zip files from fixed URLs or even pull in and build the latest commit in a given branch.

    FetchContent is especially helpful, since it does all this at configuration time, skipping a couple problems the alternative, ExternalProject, has always had for me, which is only fetching and compiling a project at build time.

    From the examples on FetchContent:

    include(FetchContent)
    FetchContent_Declare(
      googletest
      GIT_REPOSITORY https://github.com/google/googletest.git
      GIT_TAG        release-1.8.0
    )
    
    FetchContent_MakeAvailable(googletest)
    # you now magically have the googletest cmake targets available 
    # at configuration time and can link against them.
    

    An alternative to this is to use the conan package management system, althought that is way more involved.