From bbe9884c355ec805d55ec22eb5421402abc9d1fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Fri, 20 Jan 2017 11:58:39 +0000 Subject: [PATCH] Link dynamically or statically depending on build flags This lets us specify which set of rules to link to libgit2 we'll use during build. Together with the changes to the Makefile, this lets you specify make install or make install-static to build either dynamically against the system-provided library or against the version specified in the submodule. The dynamic linking against the system library is still the default in order to allow for the Go toolchain to build it when it's used as a dependency. --- .gitmodules | 3 +++ Makefile | 15 ++++++++++++++- git.go | 1 - git_dynamic.go | 9 +++++++++ git_static.go | 11 +++++++++++ vendor/libgit2 | 1 + 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 git_dynamic.go create mode 100644 git_static.go create mode 160000 vendor/libgit2 diff --git a/.gitmodules b/.gitmodules index e69de29..8eb5872 100644 --- a/.gitmodules +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/libgit2"] + path = vendor/libgit2 + url = https://github.com/libgit2/libgit2 diff --git a/Makefile b/Makefile index fa6964f..acb57b0 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,21 @@ default: test +build-libgit2: + ./script/build-libgit2-static.sh + +test-static: build-libgit2 + go run script/check-MakeGitError-thread-lock.go + go test -tags static ./... + +build-static: build-libgit2 + go build -tags static ./... + +install-static: build-libgit2 + go install -tags static ./... + test: go run script/check-MakeGitError-thread-lock.go go test ./... -install: build-libgit2 +install: go install ./... diff --git a/git.go b/git.go index 411c11b..31d863a 100644 --- a/git.go +++ b/git.go @@ -3,7 +3,6 @@ package git /* #include #include -#cgo pkg-config: libgit2 #if LIBGIT2_VER_MAJOR != 0 || LIBGIT2_VER_MINOR != 25 # error "Invalid libgit2 version; this git2go supports libgit2 v0.25" diff --git a/git_dynamic.go b/git_dynamic.go new file mode 100644 index 0000000..75d9cbc --- /dev/null +++ b/git_dynamic.go @@ -0,0 +1,9 @@ +// +build !static + +package git + +/* +#cgo pkg-config: libgit2 +#include +*/ +import "C" diff --git a/git_static.go b/git_static.go new file mode 100644 index 0000000..062dd95 --- /dev/null +++ b/git_static.go @@ -0,0 +1,11 @@ +// +build static + +package git + +/* +#cgo CFLAGS: -I${SRCDIR}/vendor/libgit2/include +#cgo LDFLAGS: -L${SRCDIR}/vendor/libgit2/build/ -lgit2 +#cgo windows LDFLAGS: -lwinhttp +#cgo !windows pkg-config: --static ${SRCDIR}/vendor/libgit2/build/libgit2.pc +*/ +import "C" diff --git a/vendor/libgit2 b/vendor/libgit2 new file mode 160000 index 0000000..2fcb870 --- /dev/null +++ b/vendor/libgit2 @@ -0,0 +1 @@ +Subproject commit 2fcb8705e584ca61f6c4657525c9d2713f6a39d2