Skip to content

Building & Cross-Compiling

Because go4vl checks in architecture-specific Linux UAPI structure definitions (v4l2/ztypes_linux_{amd64,arm64,arm,riscv64}.go) and constants (v4l2/zconsts_linux.go) and invokes kernel ioctls through golang.org/x/sys/unix, standard builds require no C compiler, no CGO, and no Linux kernel headers.

Prerequisites

  • Go 1.26 or later
  • Target OS: GOOS=linux with GOARCH set to amd64, arm64, arm, or riscv64
  • Optional ABI regeneration dependencies: gcc or clang plus linux-libc-dev are needed only when regenerating UAPI definitions (go generate ./v4l2) or running -tags=cgo_verify parity checks on Linux headers.

Native Linux builds

Run a full native build with CGO disabled to verify every package and example compiles cleanly:

CGO_ENABLED=0 go build ./...

To compile a single binary directly from one of the examples:

CGO_ENABLED=0 go build -o iter_capture ./examples/iter_capture

Cross-compiling for other Linux architectures

Standard Go cross-compilation works from Linux, macOS, or Windows development workstations. Set CGO_ENABLED=0 GOOS=linux alongside your target GOARCH:

# 64-bit ARM (Raspberry Pi 4/5 64-bit OS, Jetson, server ARM64)
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o iter_capture ./examples/iter_capture

# 32-bit ARM (Raspberry Pi Zero/3 32-bit OS, ARMv7 boards)
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build -o iter_capture ./examples/iter_capture

# 64-bit RISC-V Linux
CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 go build -o iter_capture ./examples/iter_capture

# 64-bit x86 Linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o iter_capture ./examples/iter_capture

On 32-bit linux/arm, go4vl's checked-in UAPI generator handles 32-bit struct padding, pointer sizes in v4l2_buffer.m, and timeval widths cleanly—no special build flags or host C sysroot are needed.

Multi-stage container builds

Static CGO_ENABLED=0 binaries deploy into minimal scratch or distroless containers. Remember to pass the required /dev/video* or /dev/media* device node into the container at runtime (docker run --device /dev/video0):

FROM golang:1.26 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o /out/app ./examples/iter_capture

FROM scratch
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]

For a concrete example tailored to web video streaming, see examples/simplecam/Dockerfile in the repository.

Running tests & verification

Unit tests mock the low-level ioctl seam and run on any Linux host without video hardware:

CGO_ENABLED=0 go vet ./v4l2 ./device
CGO_ENABLED=0 go test -v ./v4l2 ./device

To run the integration test suite against real V4L2 hardware or a loaded v4l2loopback device, include the integration build tag:

CGO_ENABLED=0 go test -v -tags=integration ./test/... -args -use-device=/dev/video0

See the Testing & CI Guide for loopback setup and CI flags.

Regenerating UAPI definitions (contributors)

When adding support for new Linux kernel structs, constants, or ioctls, run code generation on a Linux machine with kernel UAPI headers installed (sudo apt-get install build-essential linux-libc-dev):

# Regenerate v4l2/zconsts_linux.go and v4l2/ztypes_linux_<arch>.go
go generate ./v4l2

# Verify Go struct sizes, field offsets, ioctl IDs, and constants against host C headers
CGO_ENABLED=1 go test -v -tags=cgo_verify ./v4l2 -run TestABI

Run CGO_ENABLED=0 go build ./... across all four supported architectures after regenerating so that 32-bit and 64-bit builds remain aligned.