valyala / gozstd

go wrapper for zstd
MIT License
432 stars 60 forks source link

build: enable build without cgo #41

Closed giuseppe closed 1 year ago

giuseppe commented 2 years ago

we have moved from github.com/klauspost/compress to github.com/valyala/gozstd in github.com/containers/storage for performance reasons when pulling container images, but unfortunately we need to support build also on targets where cgo is missing: https://github.com/containers/podman/pull/12794

Without this PR, I get:

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
# github.com/valyala/gozstd
./stream.go:14:48: undefined: DefaultCompressionLevel
./stream.go:31:59: undefined: CDict
./stream.go:35:64: undefined: CDict
./stream.go:47:20: undefined: Writer
./stream.go:56:22: undefined: NewWriterLevel
./stream.go:101:61: undefined: DDict
./stream.go:110:6: undefined: Reader
./stream.go:117:8: undefined: NewReader

now the build works fine

Signed-off-by: Giuseppe Scrivano gscrivan@redhat.com

giuseppe commented 2 years ago

@valyala PTAL

rhatdan commented 2 years ago

The Podman team is waiting on this to merge.

rhatdan commented 2 years ago

@valyala PTAL

valyala commented 2 years ago

@giuseppe , sorry for the delay and thanks for the pull request! I'm unsure this is a good way to go, since it allows building github.com/valyala/gozstd without cgo, but the resulting build is unusable - it is just a stub. Probably the approach taken in VictoriaMetrics is better - e.g. to create a wrapper package containing two files:

See https://github.com/VictoriaMetrics/VictoriaMetrics/tree/master/lib/encoding/zstd .

rhatdan commented 2 years ago

We are fine with that. The issue we are seeing when we vendor in your code into a package, it blows up on compile, even if we are not using gozstd in the revendored package.

valyala commented 2 years ago

The issue we are seeing when we vendor in your code into a package, it blows up on compile, even if we are not using gozstd in the revendored package.

This sounds strange, since VictoriaMetrics is successfully built with both CGO_ENABLED=0 and CGO_ENABLED=1 when using the trick described above. Make sure that the github.com/valyala/gozstd is imported in the file, which compiles only for CGO_ENABLED=1. See build instructions and this particular section for details.