Skip to content

Commit b4fb889

Browse files
authored
Merge pull request #149 from Nuxij/unified-dockerfile
Unify the Dockerfiles by fleshing out build.sh
2 parents 276c8aa + 5cdd3f6 commit b4fb889

8 files changed

Lines changed: 183 additions & 129 deletions

File tree

.gitignore

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
.idea/
22
# CMake
3-
cmake-build-*/
4-
CMakeFiles/
3+
cmake-build-*
4+
cmake-*
5+
CMakeFiles
56
**/CMakeCache.txt
67
cmake_install.cmake
78
Makefile
8-
build/
9+
build
10+
tools
911

1012
# Resulting binary files
1113
*.a

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ endif()
6565

6666
set(VERSION_EDIT_WARNING "// Do not edit this file, it is automatically generated by CMAKE!")
6767
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/src/Version.h)
68-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docker/post_build.sh.in ${CMAKE_CURRENT_SOURCE_DIR}/docker/post_build.sh)
68+
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/docker/post_build.sh.in ${CMAKE_CURRENT_BINARY_DIR}/post_build.sh)
6969

7070

7171
add_subdirectory(src)

doc/buildWithDocker.md

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,73 @@
11
# Build the project using Docker
2-
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.
32

4-
## Using the image from Docker Hub
5-
6-
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).
3+
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.
74

8-
It can be pulled (downloaded) using the following command:
5+
Based on Ubuntu 18.04 with the following build dependencies:
96

10-
```
11-
docker pull pfeerick/infinitime-build
12-
```
7+
* ARM GCC Toolchain
8+
* nRF SDK
9+
* MCUBoot
10+
* adafruit-nrfutil
1311

14-
The default `latest` tag *should* automatically identify the correct image architecture, but if for some reason Docker does not, you can specify it manually:
12+
## Run a container to build the project
1513

16-
* For AMD64 (x86_64) systems: `docker pull pfeerick/infinitime-build:amd64`
14+
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.
1715

18-
* For ARM64v8 (ARM64/aarch64) systems: `docker pull pfeerick/infinitime-build:arm64v8`
16+
This example will build the firmware, generate the MCUBoot image and generate the DFU file. Outputs will be written to **<project_root>/build/output**:
1917

20-
The Docker Hub images are 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 during and after compilation.
18+
```bash
19+
cd <project_root> # e.g. cd ./work/Pinetime
20+
docker run --rm -it -v $(pwd):/sources infinitime-build
21+
```
2122

22-
The below example will run the container, setting the user and group ids automatically:
23+
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`:
2324

25+
```bash
26+
docker run --rm -it -v $(pwd):/sources infinitime-build /opt/build.sh pinetime-app
2427
```
25-
docker run --rm -v <project_root>:/sources --user $(id -u):$(id -g) pfeerick/infinitime-build
28+
29+
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).
30+
31+
Running with this image is the same as above, you just specify the ids to `docker run`
32+
33+
```bash
34+
docker run --rm -it -v $(pwd):/sources --user $(id -u):$(id -g) pfeerick/infinitime-build
2635
```
2736

2837
Or you can specify your user id and group id (by number, not by name) directly:
2938

30-
```
31-
docker run --rm -v <project_root>:/sources --user uid_num:gid_num pfeerick/infinitime-build
39+
```bash
40+
docker run --rm -it -v $(pwd):/sources --user 1234:1234 infinitime-build
3241
```
3342

34-
## Build the image yourself
35-
Building the docker images yourself is quite easy. The following commands must be run from the root of the project.
43+
## Using the image from Docker Hub
3644

37-
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.
45+
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).
3846

39-
If you are running on a AMD64 (x86_64) computer:
40-
```
41-
docker image build -t infinitime-build --build-arg PUID=$(id -u) --build-arg PGID=$(id -g) docker/amd64/
42-
```
47+
It can be pulled (downloaded) using the following command:
4348

44-
If you are running on an ARM64 computer (tested on Raspberry Pi 4 and Pine64 Pinebook Pro):
45-
```
46-
docker image build -t infinitime-build --build-arg PUID=$(id -u) --build-arg PGID=$(id -g) docker/arm64v8/
49+
```bash
50+
docker pull pfeerick/infinitime-build
4751
```
4852

49-
This operation will take some time, as it builds a Docker image based on Ubuntu, installs some required packages, downloads the ARM toolchain, the NRF SDK, MCUBoot and adafruit-nrfutil.
50-
51-
When this is done, a new image named *infinitime-build* is available.
53+
The default `latest` tag *should* automatically identify the correct image architecture, but if for some reason Docker does not, you can specify it manually:
5254

53-
## Run a container to build the project:
55+
* For AMD64 (x86_64) systems: `docker pull pfeerick/infinitime-build:amd64`
5456

55-
The command to run the container is essentially the same, regardless of whether you built it yourself from the dockerfiles, or are using the Docker Hub images (use `pfeerick/infinitime-build` instead of `infinitime-build` for the later):
57+
* For ARM64v8 (ARM64/aarch64) systems: `docker pull pfeerick/infinitime-build:arm64v8`
5658

57-
```
58-
docker run --rm -v <project_root>:/sources infinitime-build
59-
```
59+
## Build the image
6060

61-
This will start a container (removing it when finished), build the firmware and generate the MCUBoot image and DFU file. The output of the build is stored in `<project_root>/built/output`.
61+
You can build the image yourself if you like!
6262

63-
Replace *<project_root>* by the path of the root of the project on your computer. For example:
63+
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.
6464

65-
```
66-
docker run --rm -v /home/jf/git/PineTime:/sources infinitime-build
65+
```bash
66+
docker image build -t infinitime-build ./docker
6767
```
6868

69-
If you encounter permission errors (due to being logged in as a different user, changed user id, running the docker hub image, etc.), see the `--user` parameter mentioned above in the Docker Hub image section to see if this resolves the issue for you.
69+
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.
70+
71+
```bash
72+
docker image build -t infinitime-build --build-arg PUID=$(id -u) --build-arg PGID=$(id -g) ./docker
73+
```

docker/Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM ubuntu:18.04
2+
3+
ARG DEBIAN_FRONTEND=noninteractive
4+
RUN apt-get update -qq \
5+
&& apt-get install -y \
6+
# x86_64 / generic packages
7+
bash \
8+
build-essential \
9+
cmake \
10+
git \
11+
make \
12+
python3 \
13+
python3-pip \
14+
tar \
15+
unzip \
16+
wget \
17+
# aarch64 packages
18+
libffi-dev \
19+
libssl-dev \
20+
python3-dev \
21+
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/*;
22+
23+
RUN pip3 install adafruit-nrfutil
24+
25+
# build.sh knows how to compile
26+
COPY build.sh /opt/
27+
28+
# Lets get each in a separate docker layer for better downloads
29+
# GCC
30+
RUN bash -c "source /opt/build.sh; GetGcc;"
31+
# NrfSdk
32+
RUN bash -c "source /opt/build.sh; GetNrfSdk;"
33+
# McuBoot
34+
RUN bash -c "source /opt/build.sh; GetMcuBoot;"
35+
36+
ARG PUID=1000
37+
ARG PGID=1000
38+
RUN groupadd --system --gid $PGID infinitime && useradd --system --uid $PUID --gid $PGID infinitime
39+
40+
USER infinitime:infinitime
41+
ENV SOURCES_DIR /sources
42+
CMD ["/opt/build.sh"]

docker/amd64/Dockerfile

Lines changed: 0 additions & 34 deletions
This file was deleted.

docker/arm64v8/Dockerfile

Lines changed: 0 additions & 37 deletions
This file was deleted.

docker/build.sh

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,76 @@
1-
#!/bin/sh
1+
#!/bin/bash
2+
(return 0 2>/dev/null) && SOURCED="true" || SOURCED="false"
23
export LC_ALL=C.UTF-8
34
export LANG=C.UTF-8
45
set -x
6+
set -e
57

6-
mkdir /sources/build
7-
cd /sources/build
8+
# Default locations if the var isn't already set
9+
export TOOLS_DIR="${TOOLS_DIR:=/opt}"
10+
export SOURCES_DIR="${SOURCES_DIR:=/sources}"
11+
export BUILD_DIR="${BUILD_DIR:=$SOURCES_DIR/build}"
12+
export OUTPUT_DIR="${OUTPUT_DIR:=$BUILD_DIR/output}"
813

9-
cmake -DARM_NONE_EABI_TOOLCHAIN_PATH=/opt/gcc-arm-none-eabi-9-2020-q2-update -DNRF5_SDK_PATH=/opt/nRF5_SDK_15.3.0_59ac345 -DUSE_OPENOCD=1 ../
10-
make -j$(nproc)
14+
export BUILD_TYPE=${BUILD_TYPE:=Release}
15+
export GCC_ARM_VER=${GCC_ARM_VER:="gcc-arm-none-eabi-9-2020-q2-update"}
16+
export NRF_SDK_VER=${NRF_SDK_VER:="nRF5_SDK_15.3.0_59ac345"}
1117

12-
sh /sources/docker/post_build.sh
18+
MACHINE="$(uname -m)"
19+
[[ "$MACHINE" == "arm64" ]] && MACHINE="aarch64"
20+
21+
main() {
22+
local target="$1"
23+
24+
mkdir -p "$TOOLS_DIR"
25+
26+
[[ ! -d "$TOOLS_DIR/$GCC_ARM_VER" ]] && GetGcc
27+
[[ ! -d "$TOOLS_DIR/$NRF_SDK_VER" ]] && GetNrfSdk
28+
[[ ! -d "$TOOLS_DIR/mcuboot" ]] && GetMcuBoot
29+
30+
mkdir -p "$BUILD_DIR"
31+
32+
CmakeGenerate
33+
CmakeBuild $target
34+
35+
if [[ "$DISABLE_POSTBUILD" != "true" ]]; then
36+
source "$BUILD_DIR/post_build.sh"
37+
fi
38+
}
39+
40+
GetGcc() {
41+
GCC_SRC="$GCC_ARM_VER-$MACHINE-linux.tar.bz"
42+
wget -q https://developer.arm.com/-/media/Files/downloads/gnu-rm/9-2020q2/$GCC_SRC -O - | tar -xj -C $TOOLS_DIR/
43+
}
44+
45+
GetMcuBoot() {
46+
git clone https://github.com/JuulLabs-OSS/mcuboot.git "$TOOLS_DIR/mcuboot"
47+
pip3 install -r "$TOOLS_DIR/mcuboot/scripts/requirements.txt"
48+
}
49+
50+
GetNrfSdk() {
51+
wget -q "https://developer.nordicsemi.com/nRF5_SDK/nRF5_SDK_v15.x.x/$NRF_SDK_VER.zip" -O /tmp/$NRF_SDK_VER
52+
unzip -q /tmp/$NRF_SDK_VER -d "$TOOLS_DIR/"
53+
rm /tmp/$NRF_SDK_VER
54+
}
55+
56+
CmakeGenerate() {
57+
# We can swap the CD and trailing SOURCES_DIR for -B and -S respectively
58+
# once we go to newer CMake (Ubuntu 18.10 gives us CMake 3.10)
59+
cd "$BUILD_DIR"
60+
61+
cmake -G "Unix Makefiles" \
62+
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
63+
-DUSE_OPENOCD=1 \
64+
-DARM_NONE_EABI_TOOLCHAIN_PATH="$TOOLS_DIR/$GCC_ARM_VER" \
65+
-DNRF5_SDK_PATH="$TOOLS_DIR/$NRF_SDK_VER" \
66+
"$SOURCES_DIR"
67+
cmake -L -N .
68+
}
69+
70+
CmakeBuild() {
71+
local target="$1"
72+
[[ -n "$target" ]] && target="--target $target"
73+
cmake --build "$BUILD_DIR" --config $BUILD_TYPE $target -- -j$(nproc)
74+
}
75+
76+
[[ $SOURCED == "false" ]] && main "$@" || echo "Sourced!"

docker/post_build.sh.in

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
#!/bin/sh
22
export LC_ALL=C.UTF-8
33
export LANG=C.UTF-8
4-
set -x
4+
set -e
5+
set +x
56

6-
mkdir -p /sources/build/output
7-
/opt/mcuboot/scripts/imgtool.py create --align 4 --version 1.0.0 --header-size 32 --slot-size 475136 --pad-header /sources/build/src/pinetime-mcuboot-app-@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@.bin /sources/build/output/image-@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@.bin
8-
adafruit-nrfutil dfu genpkg --dev-type 0x0052 --application /sources/build/output/image-@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@.bin /sources/build/output/dfu-@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@.zip
7+
export PROJECT_VERSION="@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@.@PROJECT_VERSION_PATCH@"
98

10-
cp /sources/build/src/*.bin /sources/build/output/
11-
cp /sources/build/src/*.hex /sources/build/output/
12-
cp /sources/build/src/*.out /sources/build/output/
13-
cp /sources/build/src/*.map /sources/build/output/
14-
cp /sources/bootloader/bootloader-5.0.4.bin /sources/build/output/bootloader.bin
9+
mkdir -p "$OUTPUT_DIR"
1510

11+
cp "$SOURCES_DIR"/bootloader/bootloader-5.0.4.bin $OUTPUT_DIR/bootloader.bin
1612

13+
"$TOOLS_DIR"/mcuboot/scripts/imgtool.py create --version 1.0.0 \
14+
--align 4 --header-size 32 --slot-size 475136 --pad-header \
15+
"$BUILD_DIR/src/pinetime-mcuboot-app-$PROJECT_VERSION.bin" \
16+
"$OUTPUT_DIR/image-$PROJECT_VERSION.bin"
17+
18+
adafruit-nrfutil dfu genpkg --dev-type 0x0052 \
19+
--application "$OUTPUT_DIR/image-$PROJECT_VERSION.bin" \
20+
"$OUTPUT_DIR/dfu-$PROJECT_VERSION.zip"
21+
22+
mkdir -p "$OUTPUT_DIR/src"
23+
cd "$BUILD_DIR"
24+
cp src/*.bin "$OUTPUT_DIR/src"
25+
cp src/*.hex "$OUTPUT_DIR/src"
26+
cp src/*.out "$OUTPUT_DIR/src"
27+
cp src/*.map "$OUTPUT_DIR/src"
28+
29+
ls -RUv1 "$OUTPUT_DIR" | sed 's;^\([^/]\); \1;g'

0 commit comments

Comments
 (0)