diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e51b54a..3096ece 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,16 +31,18 @@ jobs: uses: ./ with: source-dir: test - build-dir: test/build - - - name: Run the build result - run: test/build/hello_world + build-dir: output + run-test: true + test-args: -R hello_world - name: Check if the default build directory does not exist - run: test ! -d build + run: test ! -d build && test ! -d test/build additional-flags-usage: - runs-on: ubuntu-latest + runs-on: ${{ matrix.compiler == 'msvc' && 'windows' || 'ubuntu' }}-latest + strategy: + matrix: + compiler: [gcc, msvc] steps: - name: Check out this repository uses: actions/checkout@v3.3.0 @@ -50,11 +52,11 @@ jobs: with: source-dir: test targets: test_c test_cpp - c-flags: -Wno-unused-variable - cxx-flags: -Wno-unused-variable - - - name: Run the build results - run: build/test_c && build/test_cpp + run-test: true + c-flags: ${{ matrix.compiler == 'msvc' && '/w /WX-' || '-Wno-unused-variable' }} + cxx-flags: ${{ matrix.compiler == 'msvc' && '/w /WX-' || '-Wno-unused-variable' }} + args: -D CHECK_SURPASS_WARNING=ON + test-args: -R test ${{ matrix.compiler == 'msvc' && '-C Debug' || '' }} specified-compiler-usage: runs-on: ${{ matrix.os }}-latest @@ -70,13 +72,12 @@ jobs: with: source-dir: test targets: test_c test_cpp + run-test: true generator: Ninja c-compiler: clang cxx-compiler: clang++ args: -D CHECK_USING_CLANG=ON - - - name: Run the build results - run: build/test_c && build/test_cpp + test-args: -R test specified-generator-usage: runs-on: ${{ matrix.os }}-latest @@ -91,7 +92,6 @@ jobs: uses: ./ with: source-dir: test + run-test: true generator: Ninja - - - name: Run the build result - run: build/hello_world + test-args: -R hello_world diff --git a/README.md b/README.md index e81da89..cc25a92 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,18 @@ [![license](https://img.shields.io/github/license/threeal/cmake-action)](./LICENSE) [![test status](https://img.shields.io/github/actions/workflow/status/threeal/cmake-action/test.yml?label=test&branch=main)](https://github.com/threeal/cmake-action/actions/workflows/test.yml) -Configure and build a [CMake](https://cmake.org/) project on [GitHub Actions](https://github.com/features/actions). +Configure, build, and test a [CMake](https://cmake.org/) project on [GitHub Actions](https://github.com/features/actions). +Use this action to simplify the workflow run of your CMake project. +This action will configure a build environment for your project using the `cmake` command, + then it will build your project by running a `cmake --build` command, + and last it could test your project using the `ctest` command. + +## Features + +- Configure and build a project using the [cmake](https://cmake.org/cmake/help/latest/manual/cmake.1.html) command. +- Optionally test a project using the [ctest](https://cmake.org/cmake/help/latest/manual/ctest.1.html) command. +- Auto-detect and install required dependencies. +- Specify multiple CMake options directly from the Action inputs. ## Usage @@ -15,14 +26,16 @@ For more information, see [action.yml](./action.yml) and [GitHub Actions guide]( | Name | Value Type | Description | | --- | --- | --- | | `source-dir` | Path | Source directory of the CMake project. Defaults to current directory. | -| `build-dir` | Path | Build directory of the CMake project. Defaults to `build` directory in current directory. | +| `build-dir` | Path | Build directory of the CMake project. Defaults to `build` directory inside the source directory. | | `targets` | Multiple strings | List of build targets. | +| `run-test` | `true` or `false` | If enabled, run testing using [CTest](https://cmake.org/cmake/help/latest/manual/ctest.1.html). Defaults to `false`. | | `generator` | String | Build system generator of the CMake project. | | `c-compiler` | String | Preferred executable for compiling C language files. | | `cxx-compiler` | String | Preferred executable for compiling C++ language files. | | `c-flags` | Multiple strings | Additional flags passed when compiling C language files. | | `cxx-flags` | Multiple strings | Additional flags passed when compiling C++ language files. | | `args` | Multiple strings | Additional arguments passed during the CMake configuration. | +| `test-args` | Multiple strings | Additional arguments passed during the CTest run. | > Note: Multiple strings mean that the input could be specified with more than one value. Separate each value with a space or a new line. @@ -45,7 +58,7 @@ jobs: uses: threeal/cmake-action@latest ``` -> Note: You can replace `@latest` with any version you like. +> Note: You can replace `@latest` with any version you like. See [this](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses). #### Specify the Source and the Build Directories @@ -54,21 +67,26 @@ jobs: uses: threeal/cmake-action@latest with: source-dir: submodules - build-dir: submodules/build + build-dir: submodules/out ``` -#### Specify the Build Targets and Additional Options +#### Specify the Build Targets ```yaml - name: Configure and build this project uses: threeal/cmake-action@latest with: - targets: hello_world_test fibonacci_test - c-flags: -Werror - cxx-flags: -Werror - args: | - -DCMAKE_BUILD_TYPE=Debug - -DBUILD_TESTING=ON + targets: hello_mars hello_sun +``` + +#### Run Unit Tests After Build + +```yaml +- name: Configure, build, and test this project + uses: threeal/cmake-action@latest + with: + args: -DBUILD_TESTING=ON + run-test: true ``` #### Using Ninja as the Generator and Clang as the Compiler diff --git a/action.yml b/action.yml index 97eff0d..ec6c6aa 100644 --- a/action.yml +++ b/action.yml @@ -1,5 +1,5 @@ name: CMake Action -description: Configure and build a CMake project +description: Configure, build, and test a CMake project author: Alfi Maulana branding: color: gray-dark @@ -8,14 +8,16 @@ inputs: source-dir: description: Source directory of the CMake project required: false - default: . build-dir: description: Build directory of the CMake project required: false - default: build targets: description: List of build targets required: false + run-test: + description: If enabled, run testing using CTest (true/false) + required: false + default: false generator: description: Build system generator of the CMake project required: false @@ -34,6 +36,9 @@ inputs: args: description: Additional arguments passed during the CMake configuration required: false + test-args: + description: Additional arguments passed during the CTest run + required: false runs: using: composite steps: @@ -41,31 +46,49 @@ runs: id: process_inputs shell: bash run: | - ARGS="${{ inputs.source-dir }} -B ${{ inputs.build-dir }}" - BUILD_ARGS="--build ${{ inputs.build-dir }}" + SOURCE_DIR="." + if [ -n '${{ inputs.source-dir }}' ]; then + SOURCE_DIR="${{ inputs.source-dir }}" + fi + BUILD_DIR="build" + if [ -n '${{ inputs.build-dir }}' ]; then + BUILD_DIR="${{ inputs.build-dir }}" + elif [ -n "${{ inputs.source-dir }}" ]; then + BUILD_DIR="${{ inputs.source-dir }}/build" + fi + ARGS="'$SOURCE_DIR' -B '$BUILD_DIR'" + BUILD_ARGS="--build '$BUILD_DIR'" + TEST_ARGS="" if [ -n '${{ inputs.targets }}' ]; then BUILD_ARGS="$BUILD_ARGS --target ${{ inputs.targets }}" fi + if [ '${{ inputs.run-test }}' == 'true' ]; then + TEST_ARGS="--test-dir '$BUILD_DIR' --output-on-failure --no-tests=error" + fi if [ -n '${{ inputs.generator }}' ]; then - ARGS="$ARGS -G ${{ inputs.generator }}" + ARGS="$ARGS -G '${{ inputs.generator }}'" fi if [ -n '${{ inputs.c-compiler }}' ]; then - ARGS="$ARGS -D CMAKE_C_COMPILER=${{ inputs.c-compiler }}" + ARGS="$ARGS -D CMAKE_C_COMPILER='${{ inputs.c-compiler }}'" fi if [ -n '${{ inputs.cxx-compiler }}' ]; then - ARGS="$ARGS -D CMAKE_CXX_COMPILER=${{ inputs.cxx-compiler }}" + ARGS="$ARGS -D CMAKE_CXX_COMPILER='${{ inputs.cxx-compiler }}'" fi if [ -n '${{ inputs.c-flags }}' ]; then - ARGS="$ARGS -D CMAKE_C_FLAGS=${{ inputs.c-flags }}" + ARGS="$ARGS -D CMAKE_C_FLAGS='${{ inputs.c-flags }}'" fi if [ -n '${{ inputs.cxx-flags }}' ]; then - ARGS="$ARGS -D CMAKE_CXX_FLAGS=${{ inputs.cxx-flags }}" + ARGS="$ARGS -D CMAKE_CXX_FLAGS='${{ inputs.cxx-flags }}'" fi if [ -n '${{ inputs.args }}' ]; then ARGS="$ARGS ${{ inputs.args }}" fi + if [ -n '${{ inputs.test-args }}' ]; then + TEST_ARGS="$TEST_ARGS ${{ inputs.test-args }}" + fi echo "cmake_args=${ARGS//[$'\t\r\n']}" >> $GITHUB_OUTPUT echo "cmake_build_args=${BUILD_ARGS//[$'\t\r\n']}" >> $GITHUB_OUTPUT + echo "cmake_test_args=${TEST_ARGS//[$'\t\r\n']}" >> $GITHUB_OUTPUT - name: Install Ninja if: ${{ inputs.generator == 'Ninja' }} @@ -78,9 +101,14 @@ runs: esac - name: Configure the CMake project - shell: bash + shell: ${{ runner.os == 'Windows' && 'pwsh' || 'bash' }} run: cmake ${{ steps.process_inputs.outputs.cmake_args }} - name: Build targets - shell: bash + shell: ${{ runner.os == 'Windows' && 'pwsh' || 'bash' }} run: cmake ${{ steps.process_inputs.outputs.cmake_build_args }} + + - name: Run tests + if: steps.process_inputs.outputs.cmake_test_args != '' + shell: ${{ runner.os == 'Windows' && 'pwsh' || 'bash' }} + run: ctest ${{ steps.process_inputs.outputs.cmake_test_args }} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 434f472..9c512eb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,11 +5,18 @@ option(CHECK_USING_CLANG "check if target is compiled using Clang" OFF) option(CHECK_SURPASS_WARNING "check if target could surpass a compiler warning" OFF) if(CHECK_SURPASS_WARNING) - set(CMAKE_C_FLAGS "-Werror -Wunused-variable ${CMAKE_C_FLAGS}") - set(CMAKE_CXX_FLAGS "-Werror -Wunused-variable ${CMAKE_CXX_FLAGS}") + if(MSVC) + set(CMAKE_C_FLAGS "/WX /W4 ${CMAKE_C_FLAGS}") + set(CMAKE_CXX_FLAGS "/WX /W4 ${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_C_FLAGS "-Werror -Wunused-variable ${CMAKE_C_FLAGS}") + set(CMAKE_CXX_FLAGS "-Werror -Wunused-variable ${CMAKE_CXX_FLAGS}") + endif() endif() +enable_testing() add_executable(hello_world hello_world.cpp) +add_test(NAME hello_world COMMAND $) list(APPEND LANGS c cpp) foreach(LANG ${LANGS}) @@ -21,4 +28,5 @@ foreach(LANG ${LANGS}) $<$:CHECK_USING_CLANG> $<$:CHECK_SURPASS_WARNING> ) + add_test(NAME test_${LANG} COMMAND $) endforeach()