diff --git a/CustomMakefile/Bela/CustomMakefileBottom.in b/CustomMakefile/Bela/CustomMakefileBottom.in new file mode 100644 index 0000000..57384af --- /dev/null +++ b/CustomMakefile/Bela/CustomMakefileBottom.in @@ -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 \ No newline at end of file diff --git a/CustomMakefile/Bela/CustomMakefileTop.in b/CustomMakefile/Bela/CustomMakefileTop.in new file mode 100644 index 0000000..fc9031c --- /dev/null +++ b/CustomMakefile/Bela/CustomMakefileTop.in @@ -0,0 +1 @@ +NO_PROJECT_TARGETS+=lib/libbelafull.a libbelafull \ No newline at end of file diff --git a/CustomMakefile/CustomMakefileTop.in b/CustomMakefile/Docker/CustomMakefileTop.in similarity index 100% rename from CustomMakefile/CustomMakefileTop.in rename to CustomMakefile/Docker/CustomMakefileTop.in diff --git a/Dockerfile b/Dockerfile index 026095c..d9ede34 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,8 @@ 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 ./ @@ -13,7 +15,13 @@ 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 @@ -21,9 +29,8 @@ 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 \ No newline at end of file diff --git a/README.md b/README.md index d4d81de..10d1de4 100644 --- a/README.md +++ b/README.md @@ -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 root@192.168.7.2:Bela/projects/ +``` + +and run it: +```bash +ssh -t root@192.168.7.2 ./Bela/projects/basic/basic +``` + +## Building the docker image You will need to have [Docker](https://docs.docker.com/get-docker/) installed and running. @@ -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 root@192.168.7.2:~/Bela/projects/sinetone +rsync --timeout=10 -avzP /sysroot/root/basic/build/basic root@192.168.7.2:~/ ``` You can now run the project in Bela: ```bash -ssh root@bela.local -cd Bela/projects -./sinetone +ssh -t root@bela.local ./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): @@ -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): diff --git a/example-project/CMakeLists.txt b/basic/CMakeLists.txt similarity index 95% rename from example-project/CMakeLists.txt rename to basic/CMakeLists.txt index cbde398..45bbd12 100644 --- a/example-project/CMakeLists.txt +++ b/basic/CMakeLists.txt @@ -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}) diff --git a/example-project/render.cpp b/basic/render.cpp similarity index 100% rename from example-project/render.cpp rename to basic/render.cpp diff --git a/scripts/build_bela.sh b/scripts/build_bela.sh index 0be271b..d96a309 100755 --- a/scripts/build_bela.sh +++ b/scripts/build_bela.sh @@ -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 diff --git a/scripts/build_libbelafull.sh b/scripts/build_libbelafull.sh new file mode 100755 index 0000000..8bf419f --- /dev/null +++ b/scripts/build_libbelafull.sh @@ -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" \ No newline at end of file diff --git a/scripts/build_packages.sh b/scripts/build_packages.sh index d7d07a4..db6c4b7 100755 --- a/scripts/build_packages.sh +++ b/scripts/build_packages.sh @@ -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..." \ No newline at end of file diff --git a/scripts/build_settings b/scripts/build_settings index 02b013e..831e581 100644 --- a/scripts/build_settings +++ b/scripts/build_settings @@ -1,2 +1,2 @@ BBB_HOSTNAME=192.168.7.2 -BELA_COMMIT=7005724 \ No newline at end of file +BELA_COMMIT=4875233 \ No newline at end of file