diff --git a/.github/workflows/build_examples.yaml b/.github/workflows/build_examples.yaml new file mode 100644 index 0000000..be4c268 --- /dev/null +++ b/.github/workflows/build_examples.yaml @@ -0,0 +1,77 @@ +name: CI - Build examples + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + # generate the build matrix + gen_matrix: + runs-on: ubuntu-latest + outputs: + examples_matrix: ${{ steps.gen_matrix.outputs.matrix }} + steps: + # checkout the repository + - uses: actions/checkout@v4 + + # generate the build matrix, containing all subdirectories in the 'examples' directory + # that contain a 'platformio.ini' file + - name: Generate build matrix + id: gen_matrix + run: | + # Get all subdirectories containing platformio.ini in the ./example directory + subdirectories=$(find ./examples -type f -name "platformio.ini" -exec dirname {} \; | sort -u) + + # Convert subdirectories to JSON array + json_array="[" + for subdir in $subdirectories; do + json_array+="\"$subdir\", " + done + json_array="${json_array%, }]" + + echo "$json_array" + echo "matrix=$json_array" >> $GITHUB_OUTPUT + + # build the CI environment in all examples + build: + runs-on: ubuntu-latest + needs: gen_matrix + + # run all examples in a matrix + strategy: + fail-fast: false + matrix: + example: ${{ fromJson(needs.gen_matrix.outputs.examples_matrix) }} + + steps: + # checkout the repository + - uses: actions/checkout@v4 + + # enable caching of pip and platformio + - uses: actions/cache@v4 + with: + path: | + ~/.cache/pip + ~/.platformio/.cache + key: ${{ runner.os }}-pio + + # install python + - uses: actions/setup-python@v5 + with: + python-version: '3.9' + + # install platformio + - name: Install PlatformIO core + run: pip install --upgrade platformio + + # print the platformio.ini file path + - name: Print matrix value for 'example' + run: echo ${{ matrix.example }} + + # build the 'CI' environment + - name: Build 'ci' environment + run: pio run --project-dir ${{ matrix.example }} --environment ci \ No newline at end of file diff --git a/.github/workflows/ci.yaml b/.github/workflows/unit_tests.yaml similarity index 53% rename from .github/workflows/ci.yaml rename to .github/workflows/unit_tests.yaml index 6bee13f..da45d94 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/unit_tests.yaml @@ -1,4 +1,4 @@ -name: CI +name: CI - Unit Tests on: push: @@ -9,36 +9,7 @@ on: - main jobs: - # smoke-test build - build: - runs-on: ubuntu-latest - - steps: - # checkout the repository - - uses: actions/checkout@v4 - - # enable caching of pip and platformio - - uses: actions/cache@v4 - with: - path: | - ~/.cache/pip - ~/.platformio/.cache - key: ${{ runner.os }}-pio - - # install python - - uses: actions/setup-python@v5 - with: - python-version: '3.9' - - # install platformio - - name: Install PlatformIO core - run: pip install --upgrade platformio - - # build the 'blink' environment - - name: Build 'blink' environment - run: pio run --environment blink - - # unit tests + # run unit tests test: runs-on: ubuntu-latest @@ -68,17 +39,17 @@ jobs: - name: Install PlatformIO core run: pip install --upgrade platformio - # create .pio/test/ folder - - name: Create .pio/test folder - run: mkdir -p .pio/test + # create test/.pio/test/ folder + - name: Create test/.pio/test folder + run: mkdir -p test/.pio/test # run unit tests using 'test-native' environment - name: Run Unit Tests (native) - run: pio test --environment test_native --junit-output-path '.pio/test/native.xml' + run: pio test --project-dir ./test --environment native --junit-output-path 'test/.pio/test/native.xml' # upload test results - uses: test-summary/action@v2 with: - paths: ".pio/test/*.xml" + paths: "test/.pio/test/*.xml" show: "all" if: always() diff --git a/examples/blink/.gitignore b/examples/blink/.gitignore new file mode 100644 index 0000000..b9f3806 --- /dev/null +++ b/examples/blink/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode diff --git a/examples/blink/platformio.ini b/examples/blink/platformio.ini new file mode 100644 index 0000000..81486f3 --- /dev/null +++ b/examples/blink/platformio.ini @@ -0,0 +1,13 @@ +[env] +platform = https://github.com/shadow578/platform-hc32f46x/archive/1.0.0.zip +framework = arduino +board = generic_hc32f460 + +[env:default] + +# required only for CI +[env:ci] +# override theframework-arduino-hc32f46x package with the local one +board_build.arduino_package_dir = ../../ +extra_scripts = + pre:../../tools/ci/patch_get_package_dir.py \ No newline at end of file diff --git a/examples/blink/main.cpp b/examples/blink/src/main.cpp similarity index 100% rename from examples/blink/main.cpp rename to examples/blink/src/main.cpp diff --git a/platformio.ini b/platformio.ini deleted file mode 100644 index 946073e..0000000 --- a/platformio.ini +++ /dev/null @@ -1,43 +0,0 @@ -# -# framework-arduino-hc32f46x CI environment -# -# use the 'blink' environment to build the blink example in /examples/blink -# use the 'test_native' or 'test_win32' environments to run unit tests -# -[platformio] -src_dir = examples/blink -test_dir = test - -[env] -test_ignore = * -platform_packages = - framework-arduino-hc32f46x @ file://./ -extra_scripts = - pre:tools/ci/patch_get_package_dir.py - - -# build the blink example for the hc32f460 platform -[env:blink] -platform = https://github.com/shadow578/platform-hc32f46x/archive/1.0.0.zip -framework = arduino -board = generic_hc32f460 - - -# common test environment -[testenv] -extends = env -test_framework = googletest -test_ignore = # run all tests -build_flags = - -Icores/arduino # add arduino core to include path - -Itest/stubs # add stubs to include path - -# run tests on the native platform and toolchain -[env:test_native] -extends = testenv -platform = native - -# run tests on windows, without having to manually install the toolchain -[env:test_win32] -extends = testenv -platform = windows_x86 diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..b9f3806 --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +.pio +.vscode diff --git a/test/platformio.ini b/test/platformio.ini new file mode 100644 index 0000000..2bc3f1b --- /dev/null +++ b/test/platformio.ini @@ -0,0 +1,26 @@ +# +# framework-arduino-hc32f46x unit test environment +# +# use the 'test_native' or 'test_win32' environments to run unit tests +# +[env] +test_framework = googletest +test_ignore = # run all tests +build_flags = + -I../cores/arduino # add arduino core to include path + -Itest/stubs # add stubs to include path + +# override the framework-arduino-hc32f46x package with the local one +board_build.arduino_package_dir = ../ +extra_scripts = + pre:../tools/ci/patch_get_package_dir.py + +# run tests on the native platform and toolchain +[env:native] +extends = testenv +platform = native + +# run tests on windows, without having to manually install the toolchain +[env:win32] +extends = testenv +platform = windows_x86 diff --git a/test/README.md b/test/test/README.md similarity index 100% rename from test/README.md rename to test/test/README.md diff --git a/test/stubs/addon_gpio.h b/test/test/stubs/addon_gpio.h similarity index 100% rename from test/stubs/addon_gpio.h rename to test/test/stubs/addon_gpio.h diff --git a/test/stubs/hc32_ddl.h b/test/test/stubs/hc32_ddl.h similarity index 100% rename from test/stubs/hc32_ddl.h rename to test/test/stubs/hc32_ddl.h diff --git a/test/stubs/variant.h b/test/test/stubs/variant.h similarity index 100% rename from test/stubs/variant.h rename to test/test/stubs/variant.h diff --git a/test/test.h b/test/test/test.h similarity index 100% rename from test/test.h rename to test/test/test.h diff --git a/test/test_arduino/arduino.cpp b/test/test/test_arduino/arduino.cpp similarity index 100% rename from test/test_arduino/arduino.cpp rename to test/test/test_arduino/arduino.cpp diff --git a/test/test_dtostrf/dtostrf.cpp b/test/test/test_dtostrf/dtostrf.cpp similarity index 100% rename from test/test_dtostrf/dtostrf.cpp rename to test/test/test_dtostrf/dtostrf.cpp diff --git a/test/test_itoa/itoa.cpp b/test/test/test_itoa/itoa.cpp similarity index 100% rename from test/test_itoa/itoa.cpp rename to test/test/test_itoa/itoa.cpp diff --git a/test/test_ringbuffer/ringbuffer.cpp b/test/test/test_ringbuffer/ringbuffer.cpp similarity index 100% rename from test/test_ringbuffer/ringbuffer.cpp rename to test/test/test_ringbuffer/ringbuffer.cpp diff --git a/test/test_wcharacter/wcharacter.cpp b/test/test/test_wcharacter/wcharacter.cpp similarity index 100% rename from test/test_wcharacter/wcharacter.cpp rename to test/test/test_wcharacter/wcharacter.cpp diff --git a/tools/ci/patch_get_package_dir.py b/tools/ci/patch_get_package_dir.py index 66c8ec5..edfee55 100644 --- a/tools/ci/patch_get_package_dir.py +++ b/tools/ci/patch_get_package_dir.py @@ -2,9 +2,10 @@ # does not apply to 'DefaultEnvironment().PioPlatform().get_package_dir()', which is used # by the hc32f46x build system... # -# this script is a workaround, patching the 'get_package_dir' method to return the project directory -# for the 'framework-arduino-hc32f46x' package. +# this script is a workaround, patching the 'get_package_dir' method to return the directory +# defined by 'board_build.arduino_package_dir' (relative to $PROJECT_DIR) for the 'framework-arduino-hc32f46x' package. from SCons.Script import DefaultEnvironment +from os.path import abspath, join env = DefaultEnvironment() platform = env.PioPlatform() @@ -12,8 +13,15 @@ def get_package_dir_override(name): if name == "framework-arduino-hc32f46x": - return env.subst("$PROJECT_DIR") + project_dir = env.subst("$PROJECT_DIR") + arduino_package_dir = env.BoardConfig().get("build.arduino_package_dir", "") + if arduino_package_dir == "": + raise ValueError("board_build.arduino_package_dir is not defined") + + package_dir = abspath(join(project_dir, arduino_package_dir)) + print("Using package dir: " + package_dir) + return package_dir else: return original_get_package_dir(name) -platform.get_package_dir = get_package_dir_override +platform.get_package_dir = get_package_dir_override \ No newline at end of file