libgit2 / git2go

Git to Go; bindings for libgit2. Like McDonald's but tastier.
MIT License
1.93k stars 315 forks source link

Does it work with CGO_ENABLED=0 ? #689

Closed laszlocph closed 3 years ago

laszlocph commented 3 years ago

Hi,

I followed the Versioned branch, static linking guide, and the project builds with go build.

However when I try to package my software with CGO_ENABLED=0 I get a C compile error.

The command:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build --tags static -ldflags '-s -w -X github.com/gimlet-io/gimlet-cli/version.Version='4543d35e0e975c6df509ed03d06a2146275da069 -a -installsuffix cgo -o bin/gimlet-linux-x86_64 github.com/gimlet-io/gimlet-cli/cmd
# github.com/libgit2/git2go/v31
Error: ../git2go/delta_string.go:11:8: undefined: DeltaUnmodified
Error: ../git2go/delta_string.go:12:8: undefined: DeltaAdded
Error: ../git2go/delta_string.go:13:8: undefined: DeltaDeleted
Error: ../git2go/delta_string.go:14:8: undefined: DeltaModified
Error: ../git2go/delta_string.go:15:8: undefined: DeltaRenamed
Error: ../git2go/delta_string.go:16:8: undefined: DeltaCopied
Error: ../git2go/delta_string.go:17:8: undefined: DeltaIgnored
Error: ../git2go/delta_string.go:18:8: undefined: DeltaUntracked
Error: ../git2go/delta_string.go:28:9: undefined: Delta
Error: ../git2go/difflinetype_string.go:36:9: undefined: DiffLineType
Error: ../git2go/delta_string.go:18:8: too many errors
Makefile:30: recipe for target 'dist' failed
make: *** [dist] Error 2
Error: Process completed with exit code 2.

The repro case: https://github.com/gimlet-io/gimlet-cli/runs/1453818829?check_suite_focus=true

My knowledge is shallow around linking and cgo, should this work at all?

Thank you, Laszlo

lhchavez commented 3 years ago

there's currently no way to make this library work with CGO_ENABLED=0 :( per https://golang.org/cmd/cgo/

The cgo tool is enabled by default for native builds on systems where it is expected to work. It is disabled by default when cross-compiling. You can control this by setting the CGO_ENABLED environment variable when running the go tool: set it to 1 to enable the use of cgo, and to 0 to disable it. The go tool will set the build constraint "cgo" if cgo is enabled. The special import "C" implies the "cgo" build constraint, as though the file also said "// +build cgo". Therefore, if cgo is disabled, files that import "C" will not be built by the go tool. (For more about build constraints see https://golang.org/pkg/go/build/#hdr-Build_Constraints).

laszlocph commented 3 years ago

That is fine, and thanks for the super quick answer!

laszlocph commented 3 years ago

Lol, I should have just look up what is CGO. It is what makes the whole project possible. Silly me. Thanks!