Skip to content

Commit

Permalink
Fix build for Raspberry Pi 1 B+ and Zero
Browse files Browse the repository at this point in the history
  • Loading branch information
darkk committed Feb 2, 2020
1 parent 93f101a commit 7d64d29
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 16 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/ubxtool.*
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ testrunner
tlecatch
ubxtool
ubxtool.nodeps
ubxtool.nodeps.*
ubxtool.static
# - created by build-ubxtool
/bin/ubxtool.*
# - created by update-tles
active.txt
beidou.txt
Expand Down
1 change: 1 addition & 0 deletions Dockerfile.ubxtool → Dockerfile.ubxtool.glibc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ RUN set -ex && \
apt-get install -y \
build-essential \
g++ \
git \
libprotobuf-dev \
make \
protobuf-compiler \
Expand Down
33 changes: 33 additions & 0 deletions Dockerfile.ubxtool.static
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# See `build-ubxtool` script for cross-compilation options.

# 1. GCC has issues with exception unwinding with static build of libmusl.
# The bug is fixed for gcc-10, but alpine:3.11 has 9.2.0-r3 and alpine:3.10 has 8.3.0-r0.
# Both are affected per test and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91737
#
# 2. clang-9.0.0-r1 from alpine:3.11 generates wrong FPU instructions for Raspberry Pi 1 B+ and Zero.
# It uses d{16..31} registers for a VFPv2 FPU and that leads to SIGILL.
# The bug is described at https://reviews.llvm.org/D67375
# The bug is fixed in clang-10: https://reviews.llvm.org/rGddf5e86c222c6b5226be53e1250421fe608bb4d0
#
# 3. clang-8.0.0-r0 from alpine:3.10 is not affected by the bug, so we use it.
FROM alpine:3.10 AS build-env

# g++ is still installed to bring C++ headers like <string>, <map>, etc.
# See https://pkgs.alpinelinux.org/contents?file=iostream&branch=v3.10&arch=armhf
RUN set -ex && \
apk add --no-cache \
clang \
g++ \
git \
make \
protobuf-dev \
&& \
:

ADD . /galmon/
WORKDIR /galmon/
RUN CXX=clang make ubxtool.static

FROM scratch
COPY --from=build-env /galmon/ubxtool.static /opt/ubxtool
ENTRYPOINT ["/opt/ubxtool"]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ UBXTOOL_DEPS = navmon.pb.o ubxtool.o ubx.o bits.o ext/fmt-5.2.1/src/format.o gal

ubxtool: $(UBXTOOL_DEPS)
$(CXX) -std=gnu++17 $^ -o $@ -L/usr/local/lib -lprotobuf -pthread
# Static build with musl on alpine and clang
ubxtool.static: $(UBXTOOL_DEPS)
$(CXX) -std=gnu++17 $^ -o $@ -L/usr/local/lib -lprotobuf -pthread -lstdc++ -static
# Static linking of `glibc` is non-trivial, so glibc is kept dynamically linked
ubxtool.nodeps: $(UBXTOOL_DEPS)
$(CXX) -std=gnu++17 $^ -o $@ -L/usr/local/lib /usr/lib/*-linux-*/libprotobuf.a -pthread -static-libgcc -static-libstdc++
Expand Down
Empty file added bin/.keep
Empty file.
86 changes: 71 additions & 15 deletions build-ubxtool
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,87 @@
# The script relies on the following project:
# - https://github.com/multiarch/qemu-user-static
# - https://github.com/multiarch/ubuntu-core
# - https://github.com/multiarch/alpine
#
# Another easy cross-compilation option is to use https://github.com/docker/binfmt
# It's also probably possible to use more official {i386,amd64,arm32v6,arm32v7,arm64v8}/alpine
# images adding qemu binary from multiarch/qemu-user-static:{arm,arm64} to them.
#
# Register qemu-user-static before building
# $ docker run --rm --privileged multiarch/qemu-user-static:register --reset

set -e

arch=${1:-native}

case "$arch" in
native)
tag=latest
from=ubuntu:bionic
;;
x86|x86_64|arm64|armhf)
tag=${arch}
from=multiarch/ubuntu-core:${arch}-bionic
;;
this_mach=$(uname --machine)

if [ "$this_mach" != "x86_64" ]; then
echo "$0: build was tested on x86_64, not on this <$this_mach>" 1>&2
fi

mach=${1:-${this_mach}}
libc=${2:-static}

case "$mach" in
i686)
alpine=x86 # official: i386/alpine
ubuntu=x86
busybox=i386
;;
x86_64)
alpine=x86_64 # official: amd64/alpine
ubuntu=x86_64
busybox=amd64
;;
armv6l)
# For Raspberry 1 and Zero.
# Tested on Raspberry 1 B+ (BCM2835 rev. 0010).
alpine=armhf # official: arm32v6/alpine
ubuntu=*
busybox=arm32v6
;;
armv7l)
# For Raspberry 2 and 3.
# Tested on Raspberry 3 Model B (BCM2835 revision a02082).
# Tested on Raspberry 3 Model B+ (BCM2835 revision a020d3).
alpine=armv7 # official: arm32v7/alpine
ubuntu=armhf
busybox=arm32v7
;;
aarch64)
# For Raspberry 4?
alpine=arm64 # official: arm64v8/alpine
ubuntu=arm64 # or aarch64? See https://wiki.ubuntu.com/ARM/RaspberryPi#Ubuntu_arm64.2FAArch64
busybox=arm64v8
;;
*)
echo "$0: unknown arch <$arch>, known are: (native|x86|x86_64|arm64|armhf)" 1>&2
echo "$0: unknown machine hardware name <$mach>, known are: {i686,x86_64,armv6l,armv7l,aarch64}" 1>&2
exit 1
esac

sed "s,^FROM ubuntu:bionic AS,FROM ${from} AS," Dockerfile.ubxtool | docker build -f - --tag ubxtool:${tag} .
case "$libc" in
static)
from="alpine:3.10"
to="multiarch/alpine:${alpine}-v3.10"
;;
glibc)
if [ "$ubuntu" = "*" ]; then
echo "$0: unsupported combination of machine <$mach> and libc <$libc>" 1>&2
exit 1
fi
from="ubuntu:bionic"
to="multiarch/ubuntu-core:${ubuntu}-bionic"
;;
*)
echo "$0: unknown libc <$libc>, known are: {static,glibc}" 1>&2
exit 1
esac

tag="${mach}-${libc}"

sed -e "s,^FROM ${from} AS ,FROM ${to} AS ," \
-e "s,^FROM busybox:glibc,FROM ${busybox}/busybox:glibc," \
Dockerfile.ubxtool.${libc} \
| docker build -f - --tag ubxtool:${tag} .

cntr=$(docker create ubxtool:${tag})
docker cp ${cntr}:/opt/ubxtool ./ubxtool.nodeps.${tag}
docker cp ${cntr}:/opt/ubxtool bin/ubxtool.${tag}
docker rm ${cntr}
1 change: 1 addition & 0 deletions ubxtool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <iostream>
#include <fstream>
#include <string.h>
#include <signal.h>
#include "fmt/format.h"
#include "fmt/printf.h"
#include "bits.hh"
Expand Down

0 comments on commit 7d64d29

Please sign in to comment.