Integrate improvements from #137

Also fixed a bug with empty quoted strings
This commit is contained in:
Joe Eaves 2020-12-18 17:58:34 +00:00
parent a7df0a0279
commit 60ef9b54fb
4 changed files with 67 additions and 54 deletions

View File

@ -1,58 +1,73 @@
# Build the project using Docker # Build the project using Docker
There are [Docker images (Dockerfile)](../docker) containing the build environment for AMD64 (x86_64) and ARM64 architectures. These images make the build of the firmware and the generation of the DFU file for OTA quite easy, as well as preventing clashes with any other toolchains or development environments you may have installed.
A [Docker image (Dockerfile)](../docker) containing all the build environment is available for X86_64 and AMD64 architectures. These images make the build of the firmware and the generation of the DFU file for OTA quite easy, as well as preventing clashes with any other toolchains or development environments you may have installed.
Based on Ubuntu 18.04 with the following build dependencies: Based on Ubuntu 18.04 with the following build dependencies:
When this is done, a new image named *infinitime-build* is available.
* ARM GCC Toolchain * ARM GCC Toolchain
* nRF SDK * nRF SDK
* MCUBoot * MCUBoot
* adafruit-nrfutil * adafruit-nrfutil
## Run a container to build the project: ## Run a container to build the project
The `infinitime-build` image contains all the dependencies you need. The default `CMD` will compile sources found in `/sources`, so you need only mount your code. The `infinitime-build` image contains all the dependencies you need. The default `CMD` will compile sources found in `/sources`, so you need only mount your code.
This example will build the firmware, generate the MCUBoot image and generate the DFU file. Outputs will be written to **<project_root>/build/output**: This example will build the firmware, generate the MCUBoot image and generate the DFU file. Outputs will be written to **<project_root>/build/output**:
```bash ```bash
$ cd <project_root> # e.g. cd ./work/Pinetime cd <project_root> # e.g. cd ./work/Pinetime
$ docker run --rm -it -v $(pwd):/sources infinitime-build docker run --rm -it -v $(pwd):/sources infinitime-build
```
* `--rm` to delete the container after we're done. Build cache will be written out to host disk, so no worries.
* `-it` for better interactivity with the running container.
* `-i` Interactive - required if you want to send CTRL+C
* `-t` TTY - This lets colours work nicely etc?
* `-v` for mounting a volume to the container
Output files (and the cmake build cache) will have `uid`/`gid` not matching your host user. To override them, use `docker run` like this:
```bash
$ docker run --rm -it -v $(pwd):/sources \
-e USER_ID=$(id -u) -e GROUP_ID=$(id -g) \
infinitime-build
``` ```
If you only want to build a single CMake target, you can pass it in as the first parameter to the build script. This means calling the script explicitly as it will override the `CMD`. Here's an example For `pinetime-app`: If you only want to build a single CMake target, you can pass it in as the first parameter to the build script. This means calling the script explicitly as it will override the `CMD`. Here's an example For `pinetime-app`:
```bash ```bash
$ docker run --rm -it -v $(pwd):/sources infinitime-build /opt/build.sh pinetime-app docker run --rm -it -v $(pwd):/sources infinitime-build /opt/build.sh pinetime-app
``` ```
The image is built using 1000:1000 for the user id and group id. If this is different to your user or group ids (run `id -u` and `id -g` to find out what your id values are if you are unsure), you will need to override them via the `--user` parameter in order to prevent permission errors with the output files (and the cmake build cache).
Running with this image is the same as above, you just specify the ids to `docker run`
```bash
docker run --rm -it -v $(pwd):/sources --user $(id -u):$(id -g) pfeerick/infinitime-build
```
Or you can specify your user id and group id (by number, not by name) directly:
```bash
docker run --rm -it -v $(pwd):/sources --user 1234:1234 infinitime-build
```
## Using the image from Docker Hub
The image is avaiable via Docker Hub for both the amd64 and arm64v8 architectures at [pfeerick/infinitime-build](https://hub.docker.com/repository/docker/pfeerick/infinitime-build).
It can be pulled (downloaded) using the following command:
```bash
docker pull pfeerick/infinitime-build
```
The default `latest` tag *should* automatically identify the correct image architecture, but if for some reason Docker does not, you can specify it manually:
* For AMD64 (x86_64) systems: `docker pull pfeerick/infinitime-build:amd64`
* For ARM64v8 (ARM64/aarch64) systems: `docker pull pfeerick/infinitime-build:arm64v8`
## Build the image ## Build the image
The image is not (yet) available on DockerHub, you need to build it yourself, but that is quite easy. The following commands must be run from the root of the project. This operation will take some time but, when done, a new image named *infinitime-build* is available.
You can build the image yourself if you like!
The following commands must be run from the root of the project. This operation will take some time but, when done, a new image named *infinitime-build* is available.
```bash ```bash
$ docker image build -t infinitime-build ./docker docker image build -t infinitime-build ./docker
``` ```
The resulting The `PUID` and `PGID` build arguments are used to set the user and group ids used in the container, meaning you will not need to specify it later unless they change for some reason. Specifying them is not mandatory, as this can be over-ridden at build time via the `--user` flag, but doing so will make the command you need to run later a bit shorter. In the below examples, they are set to your current user id and group id automatically. You can specify them manually, but they must be specified by number, not by name.
You can bake your custom USER_ID and GROUP_ID numbers in to the image, then you don't have to pass them to `docker run` every time.
```bash ```bash
docker image build -t infinitime-build \ docker image build -t infinitime-build --build-arg PUID=$(id -u) --build-arg PGID=$(id -g) ./docker
--build-arg USER_ID=$(id -u) \
--build-arg GROUP_ID=$(id -g) \
./docker
``` ```

View File

@ -1,16 +1,24 @@
FROM ubuntu:18.04 FROM ubuntu:18.04
RUN \ ARG DEBIAN_FRONTEND=noninteractive
apt-get update -qq && \ RUN apt-get update -qq \
apt-get install -y \ && apt-get install -y \
# x86_64 / generic packages # x86_64 / generic packages
bash git gosu \ bash \
cmake make build-essential \ build-essential \
wget unzip \ cmake \
python3 python3-pip \ git \
make \
python3 \
python3-pip \
tar \
unzip \
wget \
# aarch64 packages # aarch64 packages
libffi-dev libssl-dev python3-dev \ libffi-dev \
&& rm -rf /var/lib/apt/lists/*; libssl-dev \
python3-dev \
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/*;
RUN pip3 install adafruit-nrfutil RUN pip3 install adafruit-nrfutil
@ -25,13 +33,10 @@ RUN bash -c "source /opt/build.sh; GetNrfSdk;"
# McuBoot # McuBoot
RUN bash -c "source /opt/build.sh; GetMcuBoot;" RUN bash -c "source /opt/build.sh; GetMcuBoot;"
# Set and arg and use it in the env for power to override at build AND runtime ARG PUID=1000
ARG USER_ID=33333 ARG PGID=1000
ARG GROUP_ID=33333 RUN groupadd --system --gid $PGID infinitime && useradd --system --uid $PUID --gid $PGID infinitime
ENV USER_ID $USER_ID
ENV GROUP_ID $GROUP_ID
USER infinitime:infinitime
ENV SOURCES_DIR /sources ENV SOURCES_DIR /sources
COPY entrypoint.sh /opt/
ENTRYPOINT ["/opt/entrypoint.sh"]
CMD ["/opt/build.sh"] CMD ["/opt/build.sh"]

View File

@ -27,7 +27,7 @@ main() {
mkdir -p "$BUILD_DIR" mkdir -p "$BUILD_DIR"
CmakeGenerate CmakeGenerate
CmakeBuild "$target" CmakeBuild $target
if [[ "$DISABLE_POSTBUILD" != "true" ]]; then if [[ "$DISABLE_POSTBUILD" != "true" ]]; then
source "$BUILD_DIR/post_build.sh" source "$BUILD_DIR/post_build.sh"
@ -67,7 +67,7 @@ CmakeGenerate() {
CmakeBuild() { CmakeBuild() {
local target="$1" local target="$1"
[[ -n "$target" ]] && target="--target $target" [[ -n "$target" ]] && target="--target $target"
cmake --build "$BUILD_DIR" --config $BUILD_TYPE "$target" -- -j$(nproc) cmake --build "$BUILD_DIR" --config $BUILD_TYPE $target -- -j$(nproc)
} }
[[ $SOURCED == "false" ]] && main "$@" || echo "Sourced!" [[ $SOURCED == "false" ]] && main "$@" || echo "Sourced!"

View File

@ -1,7 +0,0 @@
#!/bin/bash
set -e
## Create a user on-the-fly before running CMD
## This allows us to override at runtime, allowing use of a pre-built docker image
addgroup --gid $GROUP_ID user
adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user
exec gosu user:user /bin/bash -c "$@"