From 48b45a3515b82903f8776182dffccc8caa52821c Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Thu, 18 Jul 2024 12:20:38 +0800 Subject: [PATCH] Add preliminary CI scripts Indent all C source files with clang-format-12. At present, "make" is the only way to validate the build. --- .ci/check-format.sh | 14 ++++++++++ .ci/check-newline.sh | 19 ++++++++++++++ .github/workflows/main.yml | 53 ++++++++++++++++++++++++++++++++++++++ src/diskimg.c | 2 +- src/virtio-blk.c | 2 +- src/virtio-blk.h | 2 +- src/virtio-pci.h | 2 +- src/virtq.c | 2 +- src/virtq.h | 2 +- 9 files changed, 92 insertions(+), 6 deletions(-) create mode 100755 .ci/check-format.sh create mode 100755 .ci/check-newline.sh create mode 100644 .github/workflows/main.yml diff --git a/.ci/check-format.sh b/.ci/check-format.sh new file mode 100755 index 0000000..ee3000b --- /dev/null +++ b/.ci/check-format.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e -u -o pipefail + +SOURCES=$(find $(git rev-parse --show-toplevel) | egrep "\.(c|cxx|cpp|h|hpp)\$") + +set -x + +for file in ${SOURCES}; +do + clang-format-12 ${file} > expected-format + diff -u -p --label="${file}" --label="expected coding style" ${file} expected-format +done +exit $(clang-format-12 --output-replacements-xml ${SOURCES} | egrep -c "") diff --git a/.ci/check-newline.sh b/.ci/check-newline.sh new file mode 100755 index 0000000..2e23cb5 --- /dev/null +++ b/.ci/check-newline.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +set -e -u -o pipefail + +ret=0 +show=0 +# Reference: https://medium.com/@alexey.inkin/how-to-force-newline-at-end-of-files-and-why-you-should-do-it-fdf76d1d090e +while IFS= read -rd '' f; do + if file --mime-encoding "$f" | grep -qv binary; then + tail -c1 < "$f" | read -r _ || show=1 + if [ $show -eq 1 ]; then + echo "Warning: No newline at end of file $f" + ret=1 + show=0 + fi + fi +done < <(git ls-files -z src tests/arch-test-target) + +exit $ret diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..ba92e9f --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,53 @@ +name: CI + +on: [push, pull_request] + +jobs: + detect-code-related-file-changes: + runs-on: ubuntu-22.04 + outputs: + has_code_related_changes: ${{ steps.set_has_code_related_changes.outputs.has_code_related_changes }} + steps: + - name: Check out the repo + uses: actions/checkout@v4 + - name: Test changed files + id: changed-files + uses: tj-actions/changed-files@v44 + with: + files: | + .ci/** + mk/** + src/** + target/** + .clang-format + Makefile + - name: Set has_code_related_changes + id: set_has_code_related_changes + run: | + if [[ ${{ steps.changed-files.outputs.any_changed }} == true ]]; then + echo "has_code_related_changes=true" >> $GITHUB_OUTPUT + else + echo "has_code_related_changes=false" >> $GITHUB_OUTPUT + fi + + host-x64: + needs: [detect-code-related-file-changes] + if: needs.detect-code-related-file-changes.outputs.has_code_related_changes == 'true' + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: default build + run: make + + coding-style: + needs: [detect-code-related-file-changes] + if: needs.detect-code-related-file-changes.outputs.has_code_related_changes == 'true' + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: coding convention + run: | + sudo apt-get install -q -y clang-format-12 + .ci/check-newline.sh + .ci/check-format.sh + shell: bash diff --git a/src/diskimg.c b/src/diskimg.c index a4543e0..c2c6e5a 100644 --- a/src/diskimg.c +++ b/src/diskimg.c @@ -36,4 +36,4 @@ int diskimg_init(struct diskimg *diskimg, const char *file_path) void diskimg_exit(struct diskimg *diskimg) { close(diskimg->fd); -} \ No newline at end of file +} diff --git a/src/virtio-blk.c b/src/virtio-blk.c index b8794a6..ca1f3a4 100644 --- a/src/virtio-blk.c +++ b/src/virtio-blk.c @@ -194,4 +194,4 @@ void virtio_blk_exit(struct virtio_blk_dev *dev) virtio_pci_exit(&dev->virtio_pci_dev); close(dev->irqfd); close(dev->ioeventfd); -} \ No newline at end of file +} diff --git a/src/virtio-blk.h b/src/virtio-blk.h index adb8a4d..03cbb2b 100644 --- a/src/virtio-blk.h +++ b/src/virtio-blk.h @@ -41,4 +41,4 @@ void virtio_blk_init_pci(struct virtio_blk_dev *dev, struct diskimg *diskimg, struct pci *pci, struct bus *io_bus, - struct bus *mmio_bus); \ No newline at end of file + struct bus *mmio_bus); diff --git a/src/virtio-pci.h b/src/virtio-pci.h index ee7cdef..bb04153 100644 --- a/src/virtio-pci.h +++ b/src/virtio-pci.h @@ -54,4 +54,4 @@ void virtio_pci_init(struct virtio_pci_dev *dev, struct pci *pci, struct bus *io_bus, struct bus *mmio_bus); -void virtio_pci_exit(); \ No newline at end of file +void virtio_pci_exit(); diff --git a/src/virtq.c b/src/virtq.c index e36ba56..494b691 100644 --- a/src/virtq.c +++ b/src/virtq.c @@ -63,4 +63,4 @@ void virtq_handle_avail(struct virtq *vq) virtq_complete_request(vq); if (vq->guest_event->flags == VRING_PACKED_EVENT_FLAG_ENABLE) virtq_notify_used(vq); -} \ No newline at end of file +} diff --git a/src/virtq.h b/src/virtq.h index 20af79f..4a524bd 100644 --- a/src/virtq.h +++ b/src/virtq.h @@ -42,4 +42,4 @@ void virtq_complete_request(struct virtq *vq); void virtq_notify_used(struct virtq *vq); void virtq_deassert_irq(struct virtq *vq); void virtq_handle_avail(struct virtq *vq); -void virtq_init(struct virtq *vq, void *dev, struct virtq_ops *ops); \ No newline at end of file +void virtq_init(struct virtq *vq, void *dev, struct virtq_ops *ops);