Skip to content

Commit d5fa51d

Browse files
committed
Revamp the ways in which the library can be built (#621)
This change allows to link the system version of libgit2 statically. Since `-tags static` is already used for the bundled version of the library and to avoid breaking old workflows, `-tags static,system_libgit2` is now used to select that. This means that the valid combinations are: | Flag | Effect | |-------------------------------|-----------------------------------------------| | _No flags_ | Dynamically-linked against the system libgit2 | | `-tags static,system_libgit2` | Statically-linked against the system libgit2 | | `-tags static` | Statically-linked against the bundled libgit2 | Note that there is no way to express dynamically linking against the bundled libgit2 because that makes very little sense, since the binaries wouldn't be able to be distributed. If that's still desired, the `PKG_CONFIG_PATH` environment variable can set before building the code. [`Makefile`](https://github.com/libgit2/git2go/blob/master/Makefile) has an example of how it is used in the CI. (cherry picked from commit 20a55cd)
1 parent d6c2d12 commit d5fa51d

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@ When linking dynamically against a released version of libgit2, install it via y
4848
import "github.com/libgit2/git2go/v28"
4949
```
5050

51-
### Master branch, or static linking
51+
### Versioned branch, static linking
5252

53-
If using `master` or building a branch statically, we need to build libgit2 first. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be.
53+
Follow the instructions for [Versioned branch, dynamic linking](#versioned-branch-dynamic-linking), but pass the `-tags static,system_libgit2` flag to all `go` commands that build any binaries. For instance:
54+
55+
go build -tags static,system_libgit2 github.com/my/project/...
56+
go test -tags static,system_libgit2 github.com/my/project/...
57+
go install -tags static,system_libgit2 github.com/my/project/...
58+
59+
### Master branch, or vendored static linking
60+
61+
If using `master` or building a branch with the vendored libgit2 statically, we need to build libgit2 first. In order to build it, you need `cmake`, `pkg-config` and a C compiler. You will also need the development packages for OpenSSL (outside of Windows or macOS) and LibSSH2 installed if you want libgit2 to support HTTPS and SSH respectively. Note that even if libgit2 is included in the resulting binary, its dependencies will not be.
5462

5563
Run `go get -d github.com/libgit2/git2go` to download the code and go to your `$GOPATH/src/github.com/libgit2/git2go` directory. From there, we need to build the C code and put it into the resulting go binary.
5664

@@ -83,7 +91,7 @@ For the stable version, `go test` will work as usual. For the `master` branch, s
8391

8492
Alternatively, you can build the library manually first and then run the tests
8593

86-
./script/build-libgit2-static.sh
94+
make install-static
8795
go test -v -tags static ./...
8896

8997
License
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
// +build static
1+
// +build static,!system_libgit2
22

33
package git
44

55
/*
66
#cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/
77
#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp
88
#cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc
9+
#cgo CFLAGS: -DLIBGIT2_STATIC
910
#include <git2.h>
1011
1112
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 28
1213
# error "Invalid libgit2 version; this git2go supports libgit2 v0.28"
1314
#endif
14-
1515
*/
1616
import "C"

git_dynamic.go renamed to git_system_dynamic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
package git
44

55
/*
6-
#include <git2.h>
76
#cgo pkg-config: libgit2
7+
#cgo CFLAGS: -DLIBGIT2_DYNAMIC
8+
#include <git2.h>
89
910
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 28
1011
# error "Invalid libgit2 version; this git2go supports libgit2 v0.28"
1112
#endif
12-
1313
*/
1414
import "C"

git_system_static.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build static,system_libgit2
2+
3+
package git
4+
5+
/*
6+
#cgo pkg-config: libgit2 --static
7+
#cgo CFLAGS: -DLIBGIT2_STATIC
8+
#include <git2.h>
9+
10+
#if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 28
11+
# error "Invalid libgit2 version; this git2go supports libgit2 v0.28"
12+
#endif
13+
*/
14+
import "C"

0 commit comments

Comments
 (0)