From 6f918e7cac5dc904b7f6ffa5b2c7189017528a3e Mon Sep 17 00:00:00 2001 From: JasonHsu <53471811+Jason-xy@users.noreply.github.com> Date: Thu, 25 Jan 2024 17:01:27 +0800 Subject: [PATCH] Docker compile (#13) * [Docker][other]: support docker compile for v1.14.x * [Doc][other]: update doc for containerized compilation * [Docker][other]: support github action --- .github/workflows/manual_build.yml | 41 ++++++++ .github/workflows/px4-autopilot.yml | 8 +- PX4-Autopilot | 2 +- README.md | 9 +- docker_build.sh | 139 ++++++++++++++++++++++++++++ 5 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/manual_build.yml create mode 100755 docker_build.sh diff --git a/.github/workflows/manual_build.yml b/.github/workflows/manual_build.yml new file mode 100644 index 0000000..df164d7 --- /dev/null +++ b/.github/workflows/manual_build.yml @@ -0,0 +1,41 @@ +name: Manual Build + +on: + workflow_dispatch: + inputs: + branch_name: + description: 'Branch name' + type: string + required: true + default: 'develop_v1.14.x' + + target_name: + description: 'Target name' + type: string + required: true + default: 'hkust_nxt-v1' + +jobs: + build_firmware: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Submodule update + run: git submodule update --init --recursive + + - name: Build Bootloader + run: ./docker_build.sh ${{ inputs.branch_name }} ${{ inputs.target_name }} y + - name: Upload Artifacts + uses: actions/upload-artifact@v3 + with: + name: hkust_nxt_bootloader + path: PX4-Autopilot/build/${{ inputs.target_name }}_bootloader/${{ inputs.target_name }}_bootloader.elf + + - name: Build PX4-Autopilot + run: ./docker_build.sh ${{ inputs.branch_name }} ${{ inputs.target_name }}_bootloader y + - name: Upload Artifacts + uses: actions/upload-artifact@v3 + with: + name: hkust_nxt_default + path: PX4-Autopilot/build/${{ inputs.target_name }}_default/${{ inputs.target_name }}_default.px4 \ No newline at end of file diff --git a/.github/workflows/px4-autopilot.yml b/.github/workflows/px4-autopilot.yml index 5c4bdcc..4d830a5 100644 --- a/.github/workflows/px4-autopilot.yml +++ b/.github/workflows/px4-autopilot.yml @@ -23,17 +23,17 @@ jobs: run: git submodule update --init --recursive - name: Build Bootloader - run: PX4-Autopilot/start_docker.sh hkust_nxt_bootloader + run: ./docker_build.sh develop_v1.14.x hkust_nxt-v1_bootloader y - name: Upload Artifacts uses: actions/upload-artifact@v3 with: name: hkust_nxt_bootloader - path: PX4-Autopilot/build/hkust_nxt_bootloader/hkust_nxt_bootloader.elf + path: PX4-Autopilot/build/hkust_nxt-v1_bootloader/hkust_nxt-v1_bootloader.elf - name: Build PX4-Autopilot - run: PX4-Autopilot/start_docker.sh hkust_nxt + run: ./docker_build.sh develop_v1.14.x hkust_nxt-v1 y - name: Upload Artifacts uses: actions/upload-artifact@v3 with: name: hkust_nxt_default - path: PX4-Autopilot/build/hkust_nxt_default/hkust_nxt_default.px4 \ No newline at end of file + path: PX4-Autopilot/build/hkust_nxt-v1_default/hkust_nxt-v1_default.px4 \ No newline at end of file diff --git a/PX4-Autopilot b/PX4-Autopilot index 5003827..895570d 160000 --- a/PX4-Autopilot +++ b/PX4-Autopilot @@ -1 +1 @@ -Subproject commit 5003827fdecb532fadf2598d3974878826fd9948 +Subproject commit 895570dc0d3514ecfb7261f09b73dd382223d491 diff --git a/README.md b/README.md index d1abe10..ff38b77 100644 --- a/README.md +++ b/README.md @@ -153,8 +153,13 @@ The pre-build firware and bootloader are in `./firmware` dir, donwload and flas #### Containerized compilation (recommended) -* Frameware compile: start_docker.sh hkust_nxt -* bootloader compile: start_docker.sh hkust_nxt_bootloader +Build a specific version of PX4 firmware using docker container. + +```bash +Usage: ./docker_build.sh +Example: ./docker_build.sh develop_v1.14.x hkust_nxt-v1 + ./docker_build.sh develop_v1.14.x hkust_nxt-v1_bootloader +``` ### Develop diff --git a/docker_build.sh b/docker_build.sh new file mode 100755 index 0000000..b00ce5a --- /dev/null +++ b/docker_build.sh @@ -0,0 +1,139 @@ +#!/bin/bash +script_dir=$(cd $(dirname $0);pwd) + +# Config +# version_map[]= +declare -A version_map +version_map["develop_v1.14.x"]="px4io/px4-dev-nuttx-focal:2023-12-04" + +# Set parameters +DOCEKR_NAME=""; +DOCKER_IMAGE_VERSION=""; +VOLUME_PATH=""; +set_param(){ + DOCKER_IMAGE_VERSION=${version_map[$1]} + DOCEKR_NAME="$2" + echo "Docker image version: ${DOCKER_IMAGE_VERSION}" +} + +# Git functions +switch_branch(){ + cd $script_dir/PX4-Autopilot + + # Check if branch exists + if git branch --list | grep -q "$1"; then + echo "Branch $1 exists" + else + echo "Branch $1 does not exist" + fi + + # Switch branch + if [[ "$2" != [Yy] ]]; then + echo "Warning: this will remove all changes including untracked files" + read -p "Confirm?(Y/N)" ans + + if [[ "$ans" == [Yy] ]]; then + echo "Confirmed" + git checkout -f $1 + git clean -xdf -f + git submodule update --recursive + git submodule foreach --recursive git fetch --all + git submodule foreach --recursive git reset --hard + git submodule foreach --recursive git clean -fdx -f + + echo "Successfully switched to branch $1" + else + echo "Canceled" + exit 1 + fi + fi +} + +# Docker functions +pull_docker(){ + docker pull "${DOCKER_IMAGE_VERSION}" +} + +check_image(){ + docker_image_exist=$(docker images ${DOCKER_IMAGE_VERSION}) + if [ -n "$docker_image_exist" ] + then + echo "Docker image is downloaded will build the containter" + return 0 + else + echo "erro: Docker images need to be downloaded" + return 1 + fi +} + +stop_container(){ + docker stop ${DOCEKR_NAME} +} + +start_containter(){ + docker start ${DOCEKR_NAME} +} + +build_frameware(){ + docker exec ${DOCEKR_NAME} bash -c "cd /src/NxtPX4/PX4-Autopilot; make clean ; make $1; chmod 777 -R /src/NxtPX4/PX4-Autopilot" + stop_container +} + +build_run_container(){ + # check mount dir + VOLUME_PATH=$script_dir; + echo "Mount ${VOLUME_PATH} to docker:/src/NxtPX4/ " + if [ ! -d $VOLUME_PATH ] + then + echo "Path: ${VOLUME_PATH} is not NxtPX4 repo please run this script under xxx/NxtPX4/PX4-Autopilot/" + fi + + docker run --rm -dit --privileged \ + --env=LOCAL_USER_ID="$(id -u)" \ + -v ${VOLUME_PATH}:/src/NxtPX4/:rw \ + -v /tmp/.X11-unix:/tmp/.X11-unix:ro \ + -e DISPLAY=:0 \ + -p 14556:14556/udp \ + --name=${DOCEKR_NAME} ${DOCKER_IMAGE_VERSION} + return 0 +} + +help(){ + echo "Usage: ./docker_build.sh " + echo "Example: ./docker_build.sh develop_v1.14.x hkust_nxt-v1" + echo " ./docker_build.sh develop_v1.14.x hkust_nxt-v1_bootloader" +} + +check_docker(){ + echo "Check Docker......" + docker -v + if [ $? -ne 0 ] + then + echo "Install docker using the following command:" + echo "curl -sSL https://get.daocloud.io/docker | sh" + exit 1 + fi +} + +main(){ + switch_branch $1 $3 + set_param $1 $2 + check_docker + docker_exist=$(docker ps -a|grep ${DOCEKR_NAME}) + if [ -n "$docker_exist" ] + then + echo "Docker container exist start container to compile" + start_containter + else + echo "build run container" + build_run_container + fi + build_frameware $2 +} + +if [ $# -ne 2 ] && [ $# -ne 3 ] + then + help + else + main $1 $2 $3 +fi \ No newline at end of file