Add libgit2 dependency to build system

This is pretty much #7339 with all changes to base removed. And I'm changing the way I'm handling flags and compiler names with cmake in the last commit.