From ae3535945c0afdedbdcd28a831a4902020b1c6d0 Mon Sep 17 00:00:00 2001 From: Ron Evans Date: Thu, 8 Nov 2018 16:46:22 +0100 Subject: [PATCH] all: add support to Dockerfile for AVR and ARM targets Signed-off-by: Ron Evans --- Dockerfile | 66 ++++++++++++++++++++++++++++++++++++++++++++----- docs/docker.rst | 18 ++++++++++++-- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 09486d66..03c495af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -FROM golang:latest +# TinyGo base stage just installs LLVM 7 and the TinyGo compiler itself. +FROM golang:latest AS tinygo-base RUN wget -O- https://apt.llvm.org/llvm-snapshot.gpg.key| apt-key add - && \ echo "deb http://apt.llvm.org/stretch/ llvm-toolchain-stretch-7 main" >> /etc/apt/sources.list && \ @@ -13,15 +14,68 @@ RUN cd /go/src/github.com/aykevl/tinygo/ && \ dep ensure --vendor-only && \ go install /go/src/github.com/aykevl/tinygo/ -FROM golang:latest +ENTRYPOINT ["/go/bin/tinygo"] -COPY --from=0 /go/bin/tinygo /go/bin/tinygo -COPY --from=0 /go/src/github.com/aykevl/tinygo/src /go/src/github.com/aykevl/tinygo/src -COPY --from=0 /go/src/github.com/aykevl/tinygo/targets /go/src/github.com/aykevl/tinygo/targets +# tinygo-wasm stage installs the needed dependencies to compile TinyGo programs for WASM. +FROM tinygo-base AS tinygo-wasm + +COPY --from=tinygo-base /go/bin/tinygo /go/bin/tinygo +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/src /go/src/github.com/aykevl/tinygo/src +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/targets /go/src/github.com/aykevl/tinygo/targets RUN wget -O- https://apt.llvm.org/llvm-snapshot.gpg.key| apt-key add - && \ echo "deb http://apt.llvm.org/stretch/ llvm-toolchain-stretch-7 main" >> /etc/apt/sources.list && \ apt-get update && \ apt-get install -y libllvm7 lld-7 -ENTRYPOINT ["/go/bin/tinygo"] +# tinygo-avr stage installs the needed dependencies to compile TinyGo programs for AVR microcontrollers. +FROM tinygo-base AS tinygo-avr + +COPY --from=tinygo-base /go/bin/tinygo /go/bin/tinygo +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/src /go/src/github.com/aykevl/tinygo/src +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/targets /go/src/github.com/aykevl/tinygo/targets +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/Makefile /go/src/github.com/aykevl/tinygo/ +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/tools /go/src/github.com/aykevl/tinygo/tools +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/lib /go/src/github.com/aykevl/tinygo/lib + +RUN cd /go/src/github.com/aykevl/tinygo/ && \ + apt-get update && \ + apt-get install -y apt-utils python3 make binutils-avr gcc-avr avr-libc && \ + make gen-device-avr && \ + apt-get remove -y python3 make && \ + apt-get autoremove -y && \ + apt-get clean + +# tinygo-arm stage installs the needed dependencies to compile TinyGo programs for ARM microcontrollers. +FROM tinygo-base AS tinygo-arm + +COPY --from=tinygo-base /go/bin/tinygo /go/bin/tinygo +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/src /go/src/github.com/aykevl/tinygo/src +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/targets /go/src/github.com/aykevl/tinygo/targets +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/Makefile /go/src/github.com/aykevl/tinygo/ +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/tools /go/src/github.com/aykevl/tinygo/tools +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/lib /go/src/github.com/aykevl/tinygo/lib + +RUN cd /go/src/github.com/aykevl/tinygo/ && \ + apt-get update && \ + apt-get install -y apt-utils python3 make gcc-arm-none-eabi clang-7 && \ + make gen-device-nrf && make gen-device-stm32 && \ + apt-get remove -y python3 make && \ + apt-get autoremove -y && \ + apt-get clean + +# tinygo-all stage installs the needed dependencies to compile TinyGo programs for all platforms. +FROM tinygo-wasm AS tinygo-all + +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/Makefile /go/src/github.com/aykevl/tinygo/ +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/tools /go/src/github.com/aykevl/tinygo/tools +COPY --from=tinygo-base /go/src/github.com/aykevl/tinygo/lib /go/src/github.com/aykevl/tinygo/lib + +RUN cd /go/src/github.com/aykevl/tinygo/ && \ + apt-get update && \ + apt-get install -y apt-utils python3 make gcc-arm-none-eabi clang-7 binutils-avr gcc-avr avr-libc && \ + make gen-device && \ + apt-get remove -y python3 make && \ + apt-get autoremove -y && \ + apt-get clean + diff --git a/docs/docker.rst b/docs/docker.rst index 609f02f8..5cdeb23f 100644 --- a/docs/docker.rst +++ b/docs/docker.rst @@ -11,5 +11,19 @@ repository:: docker run --rm -v $(pwd):/src tinygo/tinygo build -o /src/wasm.wasm -target wasm examples/wasm -Note that you cannot run ``tinygo flash`` from inside the docker container, -so it is less useful for microcontroller development. +To compile ``blinky1.hex`` targeting an ARM microcontroller, such as the PCA10040:: + + docker run --rm -v $(pwd):/src tinygo/tinygo build -o /src/blinky1.hex -size=short -target=pca10040 examples/blinky1 + +To compile ``blinky1.hex`` targeting an AVR microcontroller such as the Arduino:: + + docker run --rm -v $(pwd):/src tinygo/tinygo build -o /src/blinky1.hex -size=short -target=arduino examples/blinky1 + +Note that for microcontroller development you must flash your hardware devices +from your host environment, since you cannot run ``tinygo flash`` from inside +the docker container. + +So your workflow could be: + +- Compile TinyGo code using the Docker container into a HEX file. +- Flash the HEX file from your host environment to the target microcontroller.