Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross-compiling from amd64 to arm64 #609

Open
anibaldeboni opened this issue Sep 11, 2024 · 3 comments
Open

Cross-compiling from amd64 to arm64 #609

anibaldeboni opened this issue Sep 11, 2024 · 3 comments

Comments

@anibaldeboni
Copy link

Hello,

I'm running a docker image inside WSL on windows 11 trying to compile an app to arm64 but I'm getting this error:

go build \
-tags static \
-buildvcs=false \
-ldflags "-s -w -X github.com/anibaldeboni/screech/screenscraper.DevID= -X github.com/anibaldeboni/screech/screenscraper.DevPassword=" \
-o bin/app ./
go: downloading github.com/veandco/go-sdl2 v0.4.40
go: downloading gopkg.in/yaml.v3 v3.0.1
# github.com/veandco/go-sdl2/sdl
In file included from /usr/include/SDL2/SDL_stdinc.h:31:0,
                 from /usr/include/SDL2/SDL_main.h:25,
                 from /usr/include/SDL2/SDL.h:32,
                 from ./sdl_wrapper.h:5,
                 from /root/go/pkg/mod/github.com/veandco/[email protected]/sdl/audio.go:4:
/usr/include/SDL2/SDL_config.h:4:10: fatal error: SDL2/_real_SDL_config.h: No such file or directory
 #include <SDL2/_real_SDL_config.h>
          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:15: build] Error 1

an here is my Dockerfile:

FROM debian:bullseye-slim

ENV DEBIAN_FRONTEND=noninteractive

# Set timezone
ENV TZ=America/Sao_Paulo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install dependencies and update CA certificates
RUN dpkg --add-architecture arm64 && \
    apt-get update && apt-get install -y --no-install-recommends \
        wget \
        build-essential \
        ca-certificates \
        cmake \
        linux-libc-dev-arm64-cross \
        libc6-arm64-cross \
        libc6-dev-arm64-cross \
        binutils-aarch64-linux-gnu \
        gcc-aarch64-linux-gnu \
        libsdl2-dev:arm64 \
        libsdl2-gfx-dev:arm64 \
        libsdl2-image-dev:arm64 \
        libsdl2-mixer-dev:arm64 \
        libsdl2-net-dev:arm64 \
        libsdl2-ttf-dev:arm64 \
        libegl1-mesa-dev \
        libgles2-mesa-dev \
        && rm -rf /var/lib/apt/lists/*

# Install Go 1.23
RUN wget https://go.dev/dl/go1.23.1.linux-amd64.tar.gz && \
    tar -C /usr/local -xzf go1.23.1.linux-amd64.tar.gz && \
    rm go1.23.1.linux-amd64.tar.gz

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

# Download and install Linaro toolchain
RUN wget https://github.com/trimui/toolchain_sdk_smartpro/releases/download/20231018/aarch64-linux-gnu-7.5.0-linaro.tgz && \
    tar -C /usr/local -xzf aarch64-linux-gnu-7.5.0-linaro.tgz && \
    rm aarch64-linux-gnu-7.5.0-linaro.tgz

# Download and install additional libc files
RUN wget https://github.com/trimui/toolchain_sdk_smartpro/releases/download/20231018/SDK_usr_tg5040_a133p.tgz && \
    tar -xzf SDK_usr_tg5040_a133p.tgz -C /tmp && \
    mkdir -p /usr/local/aarch64-linux-gnu-7.5.0-linaro/sysroot/usr && \
    cp -r /usr/local/aarch64-linux-gnu-7.5.0-linaro/aarch64-linux-gnu/libc/* /usr/local/aarch64-linux-gnu-7.5.0-linaro/sysroot && \
    cp -r /tmp/usr/* /usr/local/aarch64-linux-gnu-7.5.0-linaro/sysroot/usr/ && \
    rm -rf SDK_usr_tg5040_a133p.tgz /tmp/usr

ENV PATH="/usr/local/aarch64-linux-gnu-7.5.0-linaro/bin:${PATH}"
ENV SYSROOT="/usr/local/aarch64-linux-gnu-7.5.0-linaro/sysroot/usr"

# Set PKG_CONFIG_PATH to include SDL2 directories
ENV PKG_CONFIG_PATH="/usr/share/pkgconfig:/usr/lib/aarch64-linux-gnu/pkgconfig:${SYSROOT}/lib/pkgconfig"

# Set environment variables for cross-compilation
ENV CC="aarch64-linux-gnu-gcc"
ENV CXX="aarch64-linux-gnu-g++"
ENV LD="aarch64-linux-gnu-ld"
ENV AR="aarch64-linux-gnu-ar"
ENV AS="aarch64-linux-gnu-as"
ENV RANLIB="aarch64-linux-gnu-ranlib"
ENV STRIP="aarch64-linux-gnu-strip"
ENV GOOS="linux"
ENV GOARCH="arm64"
ENV CGO_ENABLED="1"
ENV CGO_LDFLAGS="-L/usr/lib/aarch64-linux-gnu -lSDL2_image -lSDL2_ttf -lSDL2 -ldl -lpthread -lm"
ENV CGO_CFLAGS="-D_REENTRANT -I/usr/include/SDL2"

RUN mkdir -p /root/workspace
WORKDIR /root

VOLUME /root/workspace
WORKDIR /root/workspace

A similar setup works fine when compiling on Mac M1, but I want to setup a CI to build my app so I need to cross-compile

@veeableful
Copy link
Contributor

Hi @anibaldeboni, we don't have support for linux arm64 at the moment but I will see if I can add it this weekend.

@veeableful
Copy link
Contributor

Hi @anibaldeboni, I have pushed the static libraries for linux_arm64 into the test-v0.4.x branch. As static builds in go-sdl2 use their own pre-built static libraries, it might be needed to remove the Linaro configuration and the installed SDL2 packages so they won't conflict. Could you try it out?

@anibaldeboni
Copy link
Author

Hi @veeableful thanks for your support. I'll have a look it will certainly simplify the build process. But it's important to mention that I managed to compile using linaro, the prefix I was using to install the SDL2 lib was wrong, moving it to /usr made the compiling works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants