Skip to content

Commit

Permalink
supports both default bela make and custom cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
pelinski committed Aug 19, 2024
1 parent 4989d70 commit 847e05b
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 38 deletions.
14 changes: 14 additions & 0 deletions CustomMakefile/Bela/CustomMakefileBottom.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LIB_FULL_SO = libbelafull.so
LIB_FULL_A = libbelafull.a
LIB_FULL_OBJS = build/*/*.o libraries/*/build/*.o

lib/$(LIB_FULL_SO): $(LIB_FULL_OBJS)
$(AT) echo Building lib/$(LIB_FULL_SO)
$(AT) $(CXX) $(BELA_LDFLAGS) $(LDFLAGS) -shared -Wl,-soname,$(LIB_FULL_SO) -o lib/$(LIB_FULL_SO) $(LIB_FULL_OBJS) $(LDLIBS) $(BELA_EXTRA_LDLIBS)
$(AT) ldconfig $(BELA_DIR)/$@

lib/$(LIB_FULL_A): $(LIB_FULL_OBJS) $(PRU_OBJS) $(LIB_DEPS)
$(AT) echo Building lib/$(LIB_FULL_A)
$(AT) ar rcs lib/$(LIB_FULL_A) $(LIB_FULL_OBJS)

libbelafull: lib/libbelafull.so lib/libbelafull.a
1 change: 1 addition & 0 deletions CustomMakefile/Bela/CustomMakefileTop.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NO_PROJECT_TARGETS+=lib/libbelafull.a libbelafull
File renamed without changes.
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@ ARG TARGETPLATFORM
ARG BUILDPLATFORM
RUN echo "running on $BUILDPLATFORM, building for $TARGETPLATFORM"

ENV BELA_ROOT=/sysroot/root/Bela

COPY scripts/build_settings ./

COPY scripts/build_packages.sh ./
RUN ./build_packages.sh && rm build_packages.sh

COPY scripts/build_env.sh ./
RUN ./build_env.sh && rm build_env.sh
COPY CustomMakefile/CustomMakefileTop.in /sysroot/root/Bela/

COPY CustomMakefile/Bela/CustomMakefile* /tmp/
COPY scripts/build_libbelafull.sh ./
RUN ./build_libbelafull.sh && rm build_libbelafull.sh && rm /tmp/CustomMakefile*
COPY Toolchain.cmake ${BELA_ROOT}/

COPY CustomMakefile/Docker/CustomMakefileTop.in ${BELA_ROOT}/

COPY scripts/build_libs.sh ./
RUN ./build_libs.sh && rm build_libs.sh

COPY scripts/build_bela.sh ./
RUN ./build_bela.sh && rm build_bela.sh && rm build_settings

COPY example-project/* /sysroot/root/Bela/projects/basic/
COPY basic/render.cpp ${BELA_ROOT}/projects/basic/

WORKDIR /sysroot/root/
COPY Toolchain.cmake ./

CMD /bin/bash
89 changes: 58 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,60 @@

Docker image for [Bela](https://bela.io/) development and cross-compilation. Uses GCC 10, CMake and Make for a fast and modular build. By containerizing the cross-compilation toolchain, Bela code can be written and compiled on any host OS that can run Docker, and is compiled much faster and with more flexibility than in the Bela IDE.

## Quickstart
## Quickstart and basic usage

**You can pull the xc-bela-container image from the Docker image hub:**

```bash
docker pull pelinski/xc-bela-container:v1.0.0
docker pull pelinski/xc-bela-container:v1.1.0
```
There are images for both `amd64` and `arm64` architectures, the pull command will pull the correct one for your machine. If you are on a different machine you will have to build the image yourself (see below).

You can then start a container with (replace the `BBB_HOSTNAME` with the IP address of your Bela – if you are on Windows, it's `192.168.6.2`):

```bash
docker run -it --name bela-container -e BBB_HOSTNAME=192.168.7.2 pelinski/xc-bela-container:v1.0.0
docker run -it --name bela-container -e BBB_HOSTNAME=192.168.7.2 pelinski/xc-bela-container:v1.1.0
```

## Building the docker image and starting a container
You can quit the container with `Ctrl+D` or `exit`. You can start it again with:

```bash
docker start -ia bela-container
```


## Usage tutorial
In this quick tutorial we will cross-compile the `basic` example project in this repo. First we need to copy our project to the Bela projects folder in the container.

```bash
docker cp basic bela-container:/sysroot/root/Bela/projects/
```

Now we need to log into the container:

```bash
docker start -ia bela-container
```

Inside the container, we can compile the project with the following commands (note that these commands are the same you would use in Bela):

```bash
cd /sysroot/root/Bela
make PROJECT=basic -j5
```

Now we can copy the compiled project to Bela:

```bash
rsync -av /sysroot/root/Bela/projects/basic [email protected]:Bela/projects/
```

and run it:
```bash
ssh -t [email protected] ./Bela/projects/basic/basic
```

## Building the docker image

You will need to have [Docker](https://docs.docker.com/get-docker/) installed and running.

Expand All @@ -42,60 +80,47 @@ Once the image is built is built you can start a container with:
```bash
docker run -it --name bela-container -e BBB_HOSTNAME=192.168.7.2 xc-bela
```
## Advanced stuff
### Cross-compiling with cmake
If you want to build more complex projects, you can use CMakeLists instead of the default Bela Makefile.

You can quit it with `Ctrl+D` or `exit`. You can start it again with:

```bash
docker start -ia bela-container
```

## Usage tutorial
In this quick tutorial we will cross-compile the `example-project` in this repo (it's the Bela `fundamentals/sinetone` example).

### Copy libbelafull.so to Bela

First, for the cross-compiled binaries to run in your Bela, you will need to copy the `libbelafull.so` library from the Docker container into your Bela. You can do so by running, inside the Docker container:
First, you will need to copy the `libbelafull.so` library from the Docker container into your Bela. You can do so by running, inside the Docker container (you can start it with `docker start -ia bela-container`):

```bash
scp /sysroot/root/Bela/lib/libbelafull.so root@$BBB_HOSTNAME:Bela/lib/libbelafull.so
```

### Copy Bela project into Docker

To cross-compile a project in Docker, copy the project folder into docker.
Now you can copy the project to the container (since we are using CMake, we don't need to copy it into `sysroot/root/Bela/projects/`):

```bash
docker cp example-project bela-container:/workspace/
docker cp basic bela-container:/sysroot/root/
```

### Cross-compile project

To cross-compile the project, we need to tell the compiler that we are cross-compiling for Bela. That information is inside the docker, in the `/workspace/Toolchain.cmake` file.
To cross-compile the project, we need to tell the compiler that we are cross-compiling for Bela. That information is inside the container, in the `/sysroot/root/Bela/Toolchain.cmake` file.

You can cross-compile a project by running the following commands inside the docker container (you can access it with `docker start -ia bela`):
You can cross-compile a project by running the following commands inside the container:

```shell
cd /workspace/example-project
cd /sysroot/root/basic # path to the project
mkdir build && cd build
cmake -DCMAKE_TOOLCHAIN_FILE=/workspace/Toolchain.cmake -DPROJECT_NAME=sinetone ../
cmake -DCMAKE_TOOLCHAIN_FILE=/sysroot/root/Bela/Toolchain.cmake -DPROJECT_NAME=basic ../
cmake --build .
```

You can then copy the compiled project to Bela by running:

```bash
rsync --timeout=10 -avzP /workspace/example-project/build/sinetone [email protected]:~/Bela/projects/sinetone
rsync --timeout=10 -avzP /sysroot/root/basic/build/basic [email protected]:~/
```

You can now run the project in Bela:

```bash
ssh [email protected]
cd Bela/projects
./sinetone
ssh -t [email protected] ./basic
```

## Advanced: building docker images for different architectures

### Building docker images for different architectures
When you build the docker image, it will be built for the architecture of your host machine. If you want to build a docker image for a different architecture, you can use the `buildx` command.

First, you need to enable the `buildx` command. You can do so by running (these commands have been tested on a MacbookPro Intel):
Expand All @@ -105,6 +130,8 @@ docker buildx create --name xc-builder --use --driver docker-container
docker run --privileged linuxkit/binfmt:v1.0.0
docker buildx inspect --bootstrap
```
(If you get an error saying that the `xc-builder` already exists, you can remove it by running `docker buildx rm xc-builder`.)


Then, you can build the image for a different architecture by running (replace the `linux/arm64` with the architecture you want to build for):

Expand Down
2 changes: 1 addition & 1 deletion example-project/CMakeLists.txt → basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ file(GLOB SRC_FILES *.cpp)
# If main.cpp is not found, append default_main.cpp
list(FIND SRC_FILES main.cpp main_index)
if(main_index EQUAL -1)
list(APPEND SRC_FILES /sysroot/root/Bela/core/default_main.cpp)
list(APPEND SRC_FILES ${BELA_ROOT}/core/default_main.cpp)
endif()

add_executable(${EXE_NAME} ${SRC_FILES})
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion scripts/build_bela.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ rsync -avz --out-format=" %n" $BBB_ADDRESS:/root/Bela/include /sysroot/root/Be
rsync -avz --out-format=" %n" $BBB_ADDRESS:/root/Bela/build/pru/pru_rtaudio_irq_bin.h /sysroot/root/Bela/include
rsync -avz --out-format=" %n" $BBB_ADDRESS:/root/Bela/build/pru/pru_rtaudio_bin.h /sysroot/root/Bela/include
rsync -avz --out-format=" %n" $BBB_ADDRESS:/root/Bela/lib /sysroot/root/Bela
rsync -avz --out-format=" %n" $BBB_ADDRESS:/root/Bela/core/default_main.cpp /sysroot/root/Bela/core/


# alsa
Expand Down
11 changes: 11 additions & 0 deletions scripts/build_libbelafull.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash -e

source build_settings
BBB_ADDRESS=root@$BBB_HOSTNAME

rsync \
--timeout=10 \
-avzP /tmp/CustomMakefile* \
$BBB_ADDRESS:Bela/

ssh $BBB_ADDRESS "cd Bela && make libbelafull"
3 changes: 2 additions & 1 deletion scripts/build_packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ apt-get install --no-install-recommends -y \
ssh \
cmake \
git \
gdb
gdb \
nano
rm -rf /var/lib/apt/lists/*

echo "Finishing up..."
2 changes: 1 addition & 1 deletion scripts/build_settings
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
BBB_HOSTNAME=192.168.7.2
BELA_COMMIT=7005724
BELA_COMMIT=4875233

0 comments on commit 847e05b

Please sign in to comment.