|
| 1 | +# The MIT License (MIT) |
| 2 | + |
| 3 | +# Copyright (c) 2021-2023 Krux contributors |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +# syntax=docker/dockerfile:1 |
| 24 | + |
| 25 | +############ |
| 26 | +# build-base |
| 27 | +# install kendryte (k210), cmake and python dependencies |
| 28 | +############ |
| 29 | +FROM gcc:9.5.0-bullseye AS build-base |
| 30 | + |
| 31 | +RUN apt-get update -y && \ |
| 32 | + apt-get install --no-install-recommends -y -q \ |
| 33 | + wget \ |
| 34 | + tar \ |
| 35 | + zip \ |
| 36 | + unzip \ |
| 37 | + build-essential \ |
| 38 | + libtool \ |
| 39 | + autoconf \ |
| 40 | + automake \ |
| 41 | + autotools-dev \ |
| 42 | + curl \ |
| 43 | + libmpc-dev \ |
| 44 | + libmpfr-dev \ |
| 45 | + libgmp-dev \ |
| 46 | + gawk \ |
| 47 | + bison \ |
| 48 | + flex \ |
| 49 | + texinfo \ |
| 50 | + gperf \ |
| 51 | + libtool \ |
| 52 | + patchutils \ |
| 53 | + bc \ |
| 54 | + zlib1g-dev \ |
| 55 | + libexpat-dev \ |
| 56 | + libisl-dev \ |
| 57 | + python3 \ |
| 58 | + python3-pip \ |
| 59 | + python3-setuptools |
| 60 | + |
| 61 | +RUN mkdir -p /opt && \ |
| 62 | + GIT_TERMINAL_PROMPT=0 \ |
| 63 | + git clone --depth 1 --branch v8.2.0-20190409 https://github.com/kendryte/kendryte-gnu-toolchain |
| 64 | + |
| 65 | +RUN cd kendryte-gnu-toolchain && \ |
| 66 | + sed -i 's|https://github.com/bminor/binutils-gdb.git|https://github.com/riscvarchive/riscv-binutils-gdb.git|' .gitmodules && \ |
| 67 | + git submodule sync && \ |
| 68 | + git submodule update \ |
| 69 | + --init \ |
| 70 | + --recursive \ |
| 71 | + --depth 1 |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +RUN cd kendryte-gnu-toolchain && \ |
| 76 | + export PATH=$PATH:/opt/kendryte-toolchain/bin && \ |
| 77 | + ./configure --prefix=/opt/kendryte-toolchain --with-cmodel=medany --with-arch=rv64imafc --with-abi=lp64f --enable-threads=posix --enable-libatomic && \ |
| 78 | + make -j8 |
| 79 | + |
| 80 | +RUN wget https://github.com/Kitware/CMake/releases/download/v3.21.0/cmake-3.21.0.tar.gz && \ |
| 81 | + echo "4a42d56449a51f4d3809ab4d3b61fd4a96a469e56266e896ce1009b5768bd2ab cmake-3.21.0.tar.gz" | sha256sum -c && \ |
| 82 | + tar -xzvf cmake-3.21.0.tar.gz && \ |
| 83 | + cd cmake-3.21.0 && ./bootstrap && make && make install |
| 84 | + |
| 85 | +RUN apt-get update && apt-get install python3-venv -y |
| 86 | +RUN python3 -m venv /kruxenv |
| 87 | +RUN /kruxenv/bin/pip install astor |
| 88 | +RUN /kruxenv/bin/pip install pyserial==3.4 |
| 89 | + |
| 90 | +# Provide memset_s for host tools (mpy-cross) that expect it during link time |
| 91 | +RUN set -eux; \ |
| 92 | + cat > /tmp/memset_s.c <<'EOF' |
| 93 | +#include <errno.h> |
| 94 | +#include <stddef.h> |
| 95 | + |
| 96 | +typedef size_t rsize_t; |
| 97 | +typedef int errno_t; |
| 98 | + |
| 99 | +errno_t memset_s(void *dest, rsize_t destsz, int ch, rsize_t count) { |
| 100 | + if (!dest) return EINVAL; |
| 101 | + if (count > destsz) return EINVAL; |
| 102 | + volatile unsigned char *p = (volatile unsigned char *)dest; |
| 103 | + while (count--) *p++ = (unsigned char)ch; |
| 104 | + __asm__ __volatile__("" : : : "memory"); /* compiler barrier */ |
| 105 | + return 0; |
| 106 | +} |
| 107 | +EOF |
| 108 | +RUN set -eux; \ |
| 109 | + gcc -O2 -c /tmp/memset_s.c -o /tmp/memset_s.o; \ |
| 110 | + ar rcs /usr/local/lib/libmemset_s.a /tmp/memset_s.o; \ |
| 111 | + nm -g /usr/local/lib/libmemset_s.a | grep -w memset_s |
| 112 | + |
| 113 | +############### |
| 114 | +# build-vendor |
| 115 | +# everything except COPY ./src |
| 116 | +############### |
| 117 | +FROM build-base AS build-vendor |
| 118 | +ARG DEVICE="maixpy_m5stickv" |
| 119 | +ENV DEVICE_BUILTIN="firmware/MaixPy/projects/${DEVICE}/builtin_py" |
| 120 | +RUN mkdir /src |
| 121 | +WORKDIR /src |
| 122 | + |
| 123 | +COPY ./vendor vendor |
| 124 | +RUN find vendor/urtypes -type d -name '__pycache__' -exec rm -rv {} + -depth |
| 125 | +RUN find vendor/foundation-ur-py -type d -name '__pycache__' -exec rm -rv {} + -depth |
| 126 | +RUN /kruxenv/bin/pip install vendor/embit |
| 127 | +RUN rm -rf vendor/embit/src/embit/util/prebuilt && \ |
| 128 | + rm -rf vendor/embit/src/embit/liquid && \ |
| 129 | + rm -f vendor/embit/src/embit/psbtview.py && \ |
| 130 | + rm -f vendor/embit/src/embit/slip39.py && \ |
| 131 | + rm -f vendor/embit/src/embit/wordlists/slip39.py && \ |
| 132 | + rm -f vendor/embit/src/embit/util/ctypes_secp256k1.py && \ |
| 133 | + rm -f vendor/embit/src/embit/util/py_secp256k1.py && \ |
| 134 | + rm -f vendor/embit/src/embit/util/py_ripemd160.py && \ |
| 135 | + find vendor/embit -type d -name '__pycache__' -exec rm -rv {} + -depth |
| 136 | + |
| 137 | +COPY ./firmware firmware |
| 138 | +RUN find firmware -type d -name '__pycache__' -exec rm -rv {} + -depth |
| 139 | +RUN cp -r vendor/urtypes/src/urtypes "${DEVICE_BUILTIN}" |
| 140 | +RUN cp -r vendor/foundation-ur-py/src/ur "${DEVICE_BUILTIN}" |
| 141 | +RUN cp -r vendor/embit/src/embit "${DEVICE_BUILTIN}" |
| 142 | + |
| 143 | +################ |
| 144 | +# build-safeclib |
| 145 | +################ |
| 146 | +FROM build-vendor AS build-safeclib |
| 147 | +RUN apt-get update -y && apt-get install --no-install-recommends -y \ |
| 148 | + git make autoconf automake libtool pkg-config ca-certificates \ |
| 149 | + && rm -rf /var/lib/apt/lists/* |
| 150 | +WORKDIR /src |
| 151 | +RUN git clone --depth 1 https://github.com/rurban/safeclib.git |
| 152 | +WORKDIR /src/safeclib |
| 153 | +RUN ./build-aux/autogen.sh \ |
| 154 | + && ./configure --prefix=/opt/safeclib \ |
| 155 | + && make -j"$(nproc)" \ |
| 156 | + && make install |
| 157 | + |
| 158 | + |
| 159 | +########### |
| 160 | +# build-mpy |
| 161 | +########### |
| 162 | +FROM build-safeclib AS build-mpy |
| 163 | +RUN apt-get update -y && apt-get install --no-install-recommends -y \ |
| 164 | + git make python3 ca-certificates \ |
| 165 | + && rm -rf /var/lib/apt/lists/* |
| 166 | +COPY --from=build-safeclib /opt/safeclib /opt/safeclib |
| 167 | +ENV LD_LIBRARY_PATH=/opt/safeclib/lib |
| 168 | + |
| 169 | +WORKDIR /src |
| 170 | +COPY firmware/MaixPy/components/micropython/core/mpy-cross/ \ |
| 171 | + firmware/MaixPy/components/micropython/core/mpy-cross/ |
| 172 | + |
| 173 | +# Provide memset_s for host tool only (mpy-cross). No upstream repo edits. |
| 174 | +RUN set -eux; \ |
| 175 | + cat > /tmp/memset_s.c <<'EOF' |
| 176 | +#include <errno.h> |
| 177 | +#include <stddef.h> |
| 178 | + |
| 179 | +typedef size_t rsize_t; |
| 180 | +typedef int errno_t; |
| 181 | + |
| 182 | +errno_t memset_s(void *dest, rsize_t destsz, int ch, rsize_t count) { |
| 183 | + if (!dest) return EINVAL; |
| 184 | + if (count > destsz) return EINVAL; |
| 185 | + volatile unsigned char *p = (volatile unsigned char *)dest; |
| 186 | + while (count--) *p++ = (unsigned char)ch; |
| 187 | + __asm__ __volatile__("" : : : "memory"); /* compiler barrier */ |
| 188 | + return 0; |
| 189 | +} |
| 190 | +EOF |
| 191 | +RUN set -eux; \ |
| 192 | + gcc -O2 -c /tmp/memset_s.c -o /tmp/memset_s.o; \ |
| 193 | + ar rcs /usr/local/lib/libmemset_s.a /tmp/memset_s.o; \ |
| 194 | + nm -g /usr/local/lib/libmemset_s.a | grep -w memset_s |
| 195 | + |
| 196 | +WORKDIR /src/firmware/MaixPy/components/micropython/core/mpy-cross |
| 197 | +RUN set -eux; \ |
| 198 | + make V=1 LIB="-lm /usr/local/lib/libmemset_s.a"; \ |
| 199 | + chmod +x ./mpy-cross |
| 200 | + |
| 201 | +############## |
| 202 | +# build kapps |
| 203 | +# compilation of kapps inside kapps/ folders |
| 204 | +############# |
| 205 | +FROM build-mpy AS build-kapp |
| 206 | +ARG KAPP="nostr" |
| 207 | +WORKDIR /work |
| 208 | +COPY src/${KAPP}.py /work/${KAPP}.py |
| 209 | +RUN mkdir -p /out \ |
| 210 | + && /src/firmware/MaixPy/components/micropython/core/mpy-cross/mpy-cross \ |
| 211 | + -X heapsize=4194304 -O2 -o "/out/${KAPP}.mpy" "/work/${KAPP}.py" |
0 commit comments