oto
oto copied to clipboard
How can we compile for Raspberry pi?
With go options to compile for ARM (i.e. GOARCH=arm etc.), it won't compile without having CGO_ENABLED=1. When I enabled CGO, I get following error.
gcc: error: unrecognized command line option '-marm'; did you mean '-mabm'?
Any help is appreciated.
Due to Cgo, it might be hard or impossible to do cross-compiling. How about compiling Oto on the same host machine (raspberry pi)?
@codevin if you want to cross-compile you must use RPi toolchain/compiler, i.e.
thinkpad ~/golang/src/github.com/hajimehoshi/oto/example # CGO_ENABLED=1 CC=armv7a-hardfloat-linux-gnueabi-gcc GOOS=linux GOARCH=arm go build main.go
thinkpad ~/golang/src/github.com/hajimehoshi/oto/example # file main
main: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, Go BuildID=N1iDJ48-PrZ1-wVzV-Bq/Dof3Qzk74_s5Hr15sN-x/eljJe2AdpYuwzKZUxtpz/LkEDQmrOWrR40gGFM7L6, not stripped
In my distro, it is easy to install such a toolchain for whatever arch/platform, but you can start with https://github.com/raspberrypi/tools/tree/master/arm-bcm2708.
gcc: error: unrecognized command line option '-marm'; did you mean '-mabm'?
This error occurs while trying to use gcc instead of arm compiler. If you want to compile for arm, you should install corresponding compiler and specify it in CC directive. I use arm-linux-gnueabi-gcc, but now have another not solved problem.
I'm using the following Dockerfile to cross-compile my app for Raspberry:
FROM karalabe/xgo-base
ENV GO_VERSION 11603
ENV ROOT_DIST https://storage.googleapis.com/golang/go1.16.3.linux-amd64.tar.gz
ENV ROOT_DIST_SHA 951a3c7c6ce4e56ad883f97d9db74d3d6d80d5fec77455c6ada6c1f7ac4776d2
ADD bootstrap_pure.sh /bootstrap_pure.sh
RUN chmod +x /bootstrap_pure.sh
RUN /bootstrap_pure.sh
ENV ALSA_LIB_VERSION 1.2.4
RUN apt-get update && apt-get install -y \
curl
RUN mkdir /alsa && \
curl "ftp://ftp.alsa-project.org/pub/lib/alsa-lib-${ALSA_LIB_VERSION}.tar.bz2" -o /alsa/alsa-lib-${ALSA_LIB_VERSION}.tar.bz2
# https://www.programering.com/a/MTN0UDMwATk.html
# https://stackoverflow.com/questions/36195926/alsa-util-1-1-0-arm-cross-compile-issue
RUN cd /alsa && \
tar -xvf alsa-lib-${ALSA_LIB_VERSION}.tar.bz2 && \
cd alsa-lib-${ALSA_LIB_VERSION} && \
CC=arm-linux-gnueabihf-gcc-5 ./configure --host=arm-linux && \
make && \
make install
bootstrap_pure.sh:
#!/bin/bash
#
# Contains the Go tool-chain pure-Go bootstrapper, that as of Go 1.5, initiates
# not only a few pre-built Go cross compilers, but rather bootstraps all of the
# supported platforms from the origin Linux amd64 distribution.
#
# Usage: bootstrap_pure.sh
#
# Environment variables for remote bootstrapping:
# FETCH - Remote file fetcher and checksum verifier (injected by image)
# ROOT_DIST - 64 bit Linux Go binary distribution package
# ROOT_DIST_SHA - 64 bit Linux Go distribution package checksum
#
# Environment variables for local bootstrapping:
# GOROOT - Path to the lready installed Go runtime
set -e
# Download, verify and install the root distribution if pulled remotely
if [ "$GOROOT" == "" ]; then
$FETCH $ROOT_DIST $ROOT_DIST_SHA
tar -C /usr/local -xzf `basename $ROOT_DIST`
rm -f `basename $ROOT_DIST`
export GOROOT=/usr/local/go
fi
export GOROOT_BOOTSTRAP=$GOROOT
# Pre-build all guest distributions based on the root distribution
echo "Bootstrapping linux/386..."
GOOS=linux GOARCH=386 CGO_ENABLED=1 go install std
echo "Bootstrapping linux/arm64..."
GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC=aarch64-linux-gnu-gcc-5 go install std
if [ $GO_VERSION -ge 170 ]; then
echo "Bootstrapping linux/mips64..."
GOOS=linux GOARCH=mips64 CGO_ENABLED=1 CC=mips64-linux-gnuabi64-gcc-5 go install std
echo "Bootstrapping linux/mips64le..."
GOOS=linux GOARCH=mips64le CGO_ENABLED=1 CC=mips64el-linux-gnuabi64-gcc-5 go install std
fi
if [ $GO_VERSION -ge 180 ]; then
echo "Bootstrapping linux/mips..."
GOOS=linux GOARCH=mips CGO_ENABLED=1 CC=mips-linux-gnu-gcc-5 go install std
echo "Bootstrapping linux/mipsle..."
GOOS=linux GOARCH=mipsle CGO_ENABLED=1 CC=mipsel-linux-gnu-gcc-5 go install std
fi
echo "Bootstrapping windows/amd64..."
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go install std
echo "Bootstrapping windows/386..."
GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc go install std
echo "Bootstrapping darwin/amd64..."
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 CC=o64-clang go install std
#echo "Bootstrapping darwin/386..."
#GOOS=darwin GOARCH=386 CGO_ENABLED=1 CC=o32-clang go install std
# Install xgo within the container to enable internal cross compilation
echo "Installing xgo-in-xgo..."
go get -u github.com/karalabe/xgo
ln -s /go/bin/xgo /usr/bin/xgo
Docker build and compile commands defined in Makefile:
SHELL := /bin/sh
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
CURRENT_DIR := $(patsubst %/,%,$(dir $(MAKEFILE_PATH)))
DOCKER_MOUNT_DIR := /path-to-your-app
DOCKER_IMAGE := your-app-name
DOCKER_RUN := docker run -it --rm --entrypoint="" -v ${CURRENT_DIR}:${DOCKER_MOUNT_DIR} -v ~/go/pkg/mod:/go/pkg/mod -w ${DOCKER_MOUNT_DIR} ${DOCKER_IMAGE}
.PHONY: build
build:
@docker build -f docker/Dockerfile -t ${DOCKER_IMAGE} ./docker
.PHONY: compile
compile:
@echo "Compiling..."
@${DOCKER_RUN} bash -c ' \
CC=arm-linux-gnueabihf-gcc-5 \
GOOS=linux \
GOARCH=arm \
GOARM=7 \
CGO_ENABLED=1 \
go build \
'
Hope it helps.