wgctrl-go icon indicating copy to clipboard operation
wgctrl-go copied to clipboard

Report that cgo is required for building for FreeBSD

Open iron-udjin opened this issue 1 year ago • 8 comments

# golang.zx2c4.com/wireguard/wgctrl
/root/go/pkg/mod/golang.zx2c4.com/wireguard/[email protected]/os_freebsd.go:18:27: undefined: wgfreebsd.New
gmake: *** [Makefile:78: build] Error 1

OS: FreeBSD 14.2-RELEASE Wireguard kernel module if_wg.ko is loaded.

iron-udjin avatar Dec 19 '24 12:12 iron-udjin

I only have a Linux machine, but do you have cgo disabled by chance? What happens with:

CGO_ENABLED=1 GOOS=freebsd go build ./...

mdlayher avatar Dec 19 '24 14:12 mdlayher

I'm trying to build https://github.com/h44z/wg-portal

# gmake build
cp scripts/wg-portal.service dist
CGO_ENABLED=0 go build -o dist/wg-portal \
 -ldflags "-w -s -extldflags \"-static\" -X 'github.com/h44z/wg-portal/internal/server.Version=-'" \
 -tags netgo \
 cmd/wg-portal/main.go
# github.com/h44z/wg-portal/internal/lowlevel
internal/lowlevel/netlink.go:57:50: undefined: netlink.FAMILY_V4
internal/lowlevel/netlink.go:62:50: undefined: netlink.FAMILY_V6
internal/lowlevel/netlink.go:99:17: undefined: netlink.RuleAdd
internal/lowlevel/netlink.go:103:17: undefined: netlink.RuleDel
internal/lowlevel/netlink.go:107:17: undefined: netlink.RuleList
# golang.zx2c4.com/wireguard/wgctrl
/root/go/pkg/mod/golang.zx2c4.com/wireguard/[email protected]/os_freebsd.go:18:27: undefined: wgfreebsd.New
gmake: *** [Makefile:78: build] Error 1

iron-udjin avatar Dec 19 '24 14:12 iron-udjin

cgo being disabled is the problem. The FreeBSD implementation has a hard dependency on cgo at the moment:

package wgfreebsd

// #include <stdlib.h>
// #include <netinet/in.h>
import "C"

// ...

// New creates a new Client and returns whether or not the ioctl interface
// is available.
func New() (*Client, bool, error) {

I can at least try to come up with a shim here that shows a useful compilation error like wgfreebsd.CgoRequiredForFreeBSD not defined.

mdlayher avatar Dec 19 '24 14:12 mdlayher

I changed CGO_ENABLED=1 in Makefile:

# gmake build
cp scripts/wg-portal.service dist
CGO_ENABLED=1 go build -o dist/wg-portal \
 -ldflags "-w -s -extldflags \"-static\" -X 'github.com/h44z/wg-portal/internal/server.Version=-'" \
 -tags netgo \
 cmd/wg-portal/main.go
# github.com/h44z/wg-portal/internal/lowlevel
internal/lowlevel/netlink.go:57:50: undefined: netlink.FAMILY_V4
internal/lowlevel/netlink.go:62:50: undefined: netlink.FAMILY_V6
internal/lowlevel/netlink.go:99:17: undefined: netlink.RuleAdd
internal/lowlevel/netlink.go:103:17: undefined: netlink.RuleDel
internal/lowlevel/netlink.go:107:17: undefined: netlink.RuleList
gmake: *** [Makefile:78: build] Error 1

I can at least try to come up with a shim here that shows a useful compilation error like

It would be helpful. Thank you.

iron-udjin avatar Dec 19 '24 14:12 iron-udjin

Yep, so you can see now that wgctrl-go is building. But ultimately the wg-portal code is not using build tags correctly and assuming Linux/netlink implementation, so your next problem lies there.

mdlayher avatar Dec 19 '24 14:12 mdlayher

Ok, thank you. I'll report the problem to wg-portal project. We can close this issue.

P.S: Thank you for the fast response.

iron-udjin avatar Dec 19 '24 14:12 iron-udjin

No problem, I'll leave it open for now but edit to make the error more clear.

mdlayher avatar Dec 19 '24 15:12 mdlayher

Is it possible to remove CGO requirement in the future version?

tjjh89017 avatar Jul 12 '25 02:07 tjjh89017