WireGuard / wgctrl-go

Package wgctrl enables control of WireGuard interfaces on multiple platforms.
https://godoc.org/golang.zx2c4.com/wireguard/wgctrl
MIT License
727 stars 85 forks source link

Cross compile for freebsd fails #130

Closed mattkasun closed 1 year ago

mattkasun commented 1 year ago
package main

import (
    "log"

    "golang.zx2c4.com/wireguard/wgctrl"
)

func main() {
    port, err := GetLocalListenPort("wg0")
    if err != nil {
        log.Fatal(err)
    }
    log.Println("port:", port)
}

func GetLocalListenPort(ifacename string) (int, error) {
    client, err := wgctrl.New()
    if err != nil {
        return 0, err
    }
    defer client.Close()
    device, err := client.Device(ifacename)
    if err != nil {
        return 0, err
    }
    return device.ListenPort, nil
}

compiles and works on freebsd but compiling on linux

mkasun@endeavour ~/tmp/wgctrl-freebsd (master)> GOOS=freebsd go build
# golang.zx2c4.com/wireguard/wgctrl
../../go/pkg/mod/golang.zx2c4.com/wireguard/wgctrl@v0.0.0-20221104135756-97bc4ad4a1cb/os_freebsd.go:18:27: undefined: wgfreebsd.New
mdlayher commented 1 year ago

I'm not sure there is anything to do here: cross-compiling with cgo is nontrivial.

✘-INT ~/src/wgctrl-go [master|✔] 
21:40 $ GOOS=freebsd go build
# golang.zx2c4.com/wireguard/wgctrl
./os_freebsd.go:18:27: undefined: wgfreebsd.New
✘-2 ~/src/wgctrl-go [master|✔] 
21:40 $ CGO_ENABLED=1 GOOS=freebsd go build
# runtime/cgo
gcc_freebsd_amd64.c:7:10: fatal error: sys/signalvar.h: No such file or directory
    7 | #include <sys/signalvar.h>
      |          ^~~~~~~~~~~~~~~~~
compilation terminated.

You can try setting up a C cross compiling toolchain but there is no pure Go implementation for the FreeBSD kernel as of now.