rpi-ws281x / rpi-ws281x-go

Go library wrapping for the rpi-ws281x library
Apache License 2.0
65 stars 15 forks source link

Undefined WS2811X at cross-compiling with Docker buildx #21

Closed Durus-R closed 1 year ago

Durus-R commented 1 year ago

Hello, I have a probleme with my project and ws2881x. I cant cross-compile it.

It shows me the error:

# github.com/rpi-ws281x/rpi-ws281x-go
/go/pkg/mod/github.com/rpi-ws281x/rpi-ws281x-go@v1.0.10/ws2811.go:135:15: undefined: WS2811
/go/pkg/mod/github.com/rpi-ws281x/rpi-ws281x-go@v1.0.10/ws2811.go:140:15: undefined: WS2811

and exits later without compiling all my packages (error code 1).

My dockerfile is like that:

FROM alpine AS WS281X_BUILDER

RUN apk add --no-cache alpine-sdk cmake git linux-headers

# Add build-base maybe

WORKDIR /foundry

RUN git clone https://github.com/jgarff/rpi_ws281x.git \
  && cd rpi_ws281x \
  && mkdir build \
  && cd build \
  && cmake -D BUILD_SHARED=OFF -D BUILD_TEST=OFF .. \
  && cmake --build . \
  && make install

# Stage 1 : Build a go image with the rpi_ws281x C library

FROM golang:1.20-alpine

RUN apk add --no-cache protobuf bash libprotobuf protobuf-dev

ENV PATH="/go/bin:${PATH}:/usr/local/go/pkg/tool/linux_arm64"

RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.30.0
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.3.0

COPY --from=WS281X_BUILDER /usr/local/lib/libws2811.a /usr/local/lib/
COPY --from=WS281X_BUILDER /usr/local/include/ws2811 /usr/local/include/ws2811

I switched to alpine, but, the building is fine.

I use make to build it, and it is:

# Variable that defines the build command
BUILD_CMD := go build

# Variable that defines the build tag
BUILD_TAG := -tags pi

APP_NAME := Freenove_4WD_GO_Backend

TARGET_ARCH_CROSS := linux/arm64

DOCKER_EXEC_COMMAND := docker run --rm -v "$(shell pwd)":/usr/src/$(APP_NAME) --platform $(TARGET_ARCH_CROSS) \
                             -w /usr/src/$(APP_NAME) go-cross-builder:latest

# Target for the build
all:
    bash protoc.sh
    $(BUILD_CMD)

cross:
    docker buildx build --platform $(TARGET_ARCH_CROSS) --tag go-cross-builder .

    $(DOCKER_EXEC_COMMAND) bash protoc.sh

    $(DOCKER_EXEC_COMMAND) go build $(BUILD_TAG) -o "$(APP_NAME)-arm64" -v

I already tried everything, I just cant figure the error. Hope you guys can help me. Thanks, Durus

Durus-R commented 1 year ago

I think I solved it. I did not install gcc and alpine-sdk, which was required to build my project.

supcik commented 1 year ago

OK. Let me know if you need more help.