2

Could anyone offer some suggestions (or resources) on how I could package a GO program that uses git2go, libssl and libssh2 such that it doesn't require the end user to install these libraries separately?

I am only targeting Linux distros (if it matters)

Sthe
  • 2,575
  • 2
  • 31
  • 48
  • There's some scripts in the git2go repo to make a static build. Have you tried those? – JimB Dec 18 '15 at 14:11
  • When I need C code statically compiled with my go code (e.g. Sqlite) I usually just do `go install -tags netgo -ldflags '-extldflags "-static"'`. – Ainar-G Dec 18 '15 at 14:45
  • The Makefile is using those scripts and they do generate a static object `libgit2.a` file, but after building my program, I get `error while loading shared libraries: libhttp_parser.so.2.1: cannot open shared object file: No such file or directory`. Here is one of the flags that appear when running the build `-L/usr/local/lib -lgit2 -lhttp_parser -lssh2 -lgcrypt -lrt -lssl -lcrypto -ldl -lz` – Sthe Dec 18 '15 at 16:10
  • That's a way to build libgit2 into git2go statically, the external libs are still loading dynamically by the result – Carlos Martín Nieto Dec 18 '15 at 17:09

1 Answers1

2

One way would be to build those dependencies statically as well and use PKG_CONFIG_PATH point to your own copies so everything gets linked statically. That should make CMake choose the static versions.

But if the goal is to avoid depending on the user-installed libraries rather than making everything a single executable, I would recommend shipping the libraries and working with the load path to make sure they get loaded. With gcc you'd pass -Wl,-R to set the search path in the binary itself, so you can set where to search for the shared libraries you're shipping with your app. With go it looks like you can pass -r to the linker (via -ldflags or manually) to do the same thing.

libgit2 is rather extensible, so there is a third option which is to implement the TLS stream and SSH transport in Go and plug those into a version of libgit2 without support for these. This is however a significant amount of work.

Carlos Martín Nieto
  • 5,207
  • 1
  • 15
  • 16
  • Thanks Carlos. Your suggestion worked like a charm. I forked the git2go repo and added a new `make` rule for achieving this. Please see the link below. Do you think it's worth making a pull request for? https://github.com/sithembiso/git2go/blob/next/Makefile#L16 – Sthe Dec 20 '15 at 11:55
  • I would like to contribute to the third option after my current project. Even though the changes I made have solved my problem for now, they have increased the size of my binary file from `19MB` to `23MB`. Also, building for different platforms requires great care for now. – Sthe Dec 20 '15 at 12:01
  • Embedding multiple crypto libraries into git2go is not something I think the upstream project should have in its code, but TLS and SSH are both things you get with Go itself so we should go use that. It also helpfully bypasses the OpenSSL multithreading nonsense. – Carlos Martín Nieto Dec 23 '15 at 14:40