void-linux / void-packages

The Void source packages collection
https://voidlinux.org
Other
2.59k stars 2.16k forks source link

[RFC] add support for CGO_ENABLED parameter for Go build style #39315

Closed shanduur closed 2 years ago

shanduur commented 2 years ago

Adding the new parameter into the template (go_cgo_enabled) that can be translated directly to the CGO_ENABLED. This could be helpful when building packages than can be built with both go and cgo. I have encountered this issue when preparing the package for promtail (loki client/exporter). When using CGO_ENABLED=1, it requires systemd specific header files:

# github.com/coreos/go-systemd/sdjournal
vendor/github.com/coreos/go-systemd/sdjournal/journal.go:27:11: fatal error: systemd/sd-journal.h: No such file or directory
   27 | // #include <systemd/sd-journal.h>
      |           ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

Setting CGO_ENABLED=0 is a way to skip cgo compilation.

Proposal:

diff --git a/common/build-style/go.sh b/common/build-style/go.sh
index 9093527860..83006a5adf 100644
--- a/common/build-style/go.sh
+++ b/common/build-style/go.sh
@@ -22,6 +22,10 @@ do_configure() {
 }

 do_build() {
+       if [[ -z ${go_cgo_enabled} ]]; then
+               go_cgo_enabled=1
+       fi
+
        # remove -s and -w from go_ldflags, we should let xbps-src strip binaries itself
        for wd in $go_ldflags; do
                if [ "$wd" == "-s" ] || [ "$wd" == "-w" ]; then
@@ -40,7 +44,7 @@ do_build() {
                        # default behavior.
                        go_mod_mode=
                fi
-               go install -p "$XBPS_MAKEJOBS" -mod="${go_mod_mode}" -x -tags "${go_build_tags}" -ldflags "${go_ldflags}" ${go_package}
+               CGO_ENABLED=${go_cgo_enabled} go install -p "$XBPS_MAKEJOBS" -mod="${go_mod_mode}" -x -tags "${go_build_tags}" -ldflags "${go_ldflags}" ${go_package}
        else
                # Otherwise, build using GOPATH
                go get -p "$XBPS_MAKEJOBS" -x -tags "${go_build_tags}" -ldflags "${go_ldflags}" ${go_package}
paper42 commented 2 years ago

You can export CGO_ENABLED in the template like it's done for example in v2ray.

shanduur commented 2 years ago

@paper42 I don't think so, here is commit:

[void@freepc void-packages]$ cat srcpkgs/promtail/template
# Template file for 'promtail'
pkgname=promtail
version=2.6.1
revision=1
wrksrc="loki-${version}"
build_style=go
go_import_path="github.com/grafana/loki"
go_package="${go_import_path}/clients/cmd/promtail"
short_desc="Like Prometheus, but for logs"
maintainer="Mateusz Urbanek <murbanek@shanduur.com>"
license="Apache-2.0"
homepage="https://grafana.com/oss/loki/"
distfiles="https://github.com/grafana/loki/archive/v${version}.tar.gz"
checksum=4b41175e552dd198bb9cae213df3c0d9ca8cacd0b673f79d26419cea7cfb2df7

export CGO_ENABLED=0

pre_build() {
    msg_warn "CGO_ENABLED: got: $(go env CGO_ENABLED), wanted: $(CGO_ENABLED=0 go env CGO_ENABLED)\n"
}
[void@freepc void-packages]$ ./xbps-src pkg promtail
=> xbps-src: updating repositories for host (x86_64-musl)...
[*] Updating repository `https://repo-default.voidlinux.org/current/musl/x86_64-musl-repodata' ...
[*] Updating repository `https://repo-default.voidlinux.org/current/musl/nonfree/x86_64-musl-repodata' ...
[*] Updating repository `https://repo-default.voidlinux.org/current/musl/debug/x86_64-musl-repodata' ...
=> xbps-src: updating software in / masterdir...
=> xbps-src: cleaning up / masterdir...
=> promtail-2.6.1_1: removing autodeps, please wait...
=> promtail-2.6.1_1: building [go] for x86_64-musl...
   [host] go-1.19_1: found (/host/binpkgs/add-grafana-loki)
=> promtail-2.6.1_1: installing host dependencies: go-1.19_1 ...
=> promtail-2.6.1_1: running pre-build hook: 02-script-wrapper ...
=> promtail-2.6.1_1: running pre_build ...
=> WARNING: CGO_ENABLED: got: 1, wanted: 0
=> promtail-2.6.1_1: running do_build ...
=> Using vendor dir for promtail Go dependencies.
WORK=/tmp/go-build987700878
mkdir -p $WORK/b1062/
cd /builddir/loki-2.6.1/vendor/github.com/coreos/go-systemd/sdjournal
TERM='dumb' /usr/lib/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b1062/ -importpath github.com/coreos/go-systemd/sdjournal -- -I $WORK/b1062/ -mtune=generic -O2 -pipe ./journal.go
# github.com/coreos/go-systemd/sdjournal
vendor/github.com/coreos/go-systemd/sdjournal/journal.go:27:11: fatal error: systemd/sd-journal.h: No such file or directory
   27 | // #include <systemd/sd-journal.h>
      |           ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
=> ERROR: promtail-2.6.1_1: do_build: 'go install -p "$XBPS_MAKEJOBS" -mod="${go_mod_mode}" -x -tags "${go_build_tags}" -ldflags "${go_ldflags}" ${go_package}' exited with 2
=> ERROR:   in do_build() at common/build-style/go.sh:43
[void@freepc void-packages]$
shanduur commented 2 years ago

Full log: xbps-src.log

Duncaen commented 2 years ago

common/environment/build-style/go.sh sets CGO_ENABLED=1, you would have to use export CGO_ENABLED=0 in pre_build().

shanduur commented 2 years ago

This means some packages are doing this wrong, and those could be updated: