Make the cgo tool do more linking work #159
Loading…
Reference in New Issue
No description provided.
Delete Branch "cgo-directives"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The cgo directives let us do a lot more than I previously thought, so we
can use this to make the building process of git2go go through the go
tool directly rather than via the script.
libgit2 still needs to be built manually, so we do still require make,
but only for building libgit2. Once that's build, any modifications to
git2go can be built with
Leaving out the static tag will make git2go try to link against a
system-provided version of libgit2 via the normal pkg-config channels.
It's a shame that didn't work out. Is there any workaround?
I only had time to dig in a little bit here. What the problem actually was wasn't clear to me from the discussion that took place. From what I see (with this change) any
package main
program fails to properly link againstlibgit2
.This is because when building the binary the go tool pulls the linker flags from the package's (git2go's) source code. The primary problem here is that you can't link against a library using a relative filepath, which is what this PR does.
When building source in the
git2go
directory (go build
), it links fine because the working directory makes the relative path a valid one, but, when building a main binary the working directory is not git2go's source directory.Ultimately, there isn't anything (easy) you can do to work around this right now. However Go 1.5 (just barely missed 1.4) does feature a change (
131758183f
) allowing for CGO lines like this:Which when building the main binary (outside the package's directory) would have
${SRCDIR}
expanded to the package's source directory (e.g./home/stephen/godev/src/github.com/libgit2/git2go
).I think that would fix this issue.
@shurcooL the same issue doesn't apply with go-gl/glfw3 because we include the C sources directly and are not linking to a static library (
.a
file).Thanks for looking into this thoroughly @slimsag.
I don't know that the addition of
${SRCDIR}
is going to fix it. git2go already includes a statically-buit version of libgit2 in it. If a program which uses git2go is then also made to include the statically-built version of libgit2, it's not clear that it won't be included twice, which would still fail to compile.git2go is the only thing that depends on libgit2 being included in it, and it's the only one which should. This is why we now have to have the script with the env variables. Anything depending on libgit2 should not be getting any of the flags we used to include libgit2 in git2go.
The only clear way that this is going to work is dynamic linking, but that won't work for following the master branch. When v0.22 releases, I'll create a branch for git2go so you can use it with a libgit2 on the system via gopkg.in or something similar.
Just to be clear, the issue we are talking about is when running
go test -c
ingithub.com/carlosmn/go.gitfs
, correct?If you build git2go using
go build -x
in the package directory, you'll get a log of all commands that go build executes while running. You will find that Go's build process does not repacklibgit2.a
into the final static library that is your packagegit2go.a
, seen by the finalpack
command in the log:Go package's aren't built as libraries in that sense.
libgit2.a
is not combined with yourgit2go
package, but ratherlibgit2.a
is linked when apackage main
program is built.That is not what the
${SRCDIR}
variable expansion would do: it would simply allow the Go linker (which runs when a main program binary -- not package/library -- is built) to find thelibgit2.a
static library and link against it properly.It also helps to examine the output of
go test -c -ldflags="-v"
which shows the final host linker command used to build the resulting binary:When the linker runs inside
cd $WORK/github.com/carlosmn/go.gitfs/_test
(see-v
flag) -- those relative paths are no longer valid.If you replace your
#cgo LDFLAGS
line with, for example on my machine what is an absolute path to your lib directory:go test -c
builds without error and the resulting binary is statically linked.Obviously, using an absolute path isn't viable because it's specific to my system, this is the part that
${SRCDIR}
fixes.Hope I've been of some help here,
Cheers
We can have absolute paths right now by passing an absolute path to CMake and letting it do the install phase. But that's just a part of it.
When git2go gets installed, we know the libgit2.a version inside git2go agrees with the usage. This then lands in
pkg/.../git2go.a
which you can then use. During the build phase of whatever program uses git2go, there is no guarantee that the libgit2.a in the source portion of git2go has anything to do with what git2go was built with, and it doesn't have to because that's not the archive in the compiled portion of the tree.