name: CMake Action description: Configure, build, and test a CMake project author: Alfi Maulana branding: color: gray-dark icon: terminal inputs: source-dir: description: Source directory of the CMake project required: false build-dir: description: Build directory of the CMake project required: false 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 c-compiler: description: Preferred executable for compiling C language files required: false cxx-compiler: description: Preferred executable for compiling C++ language files required: false c-flags: description: Additional flags passed when compiling C language files required: false cxx-flags: description: Additional flags passed when compiling C++ language files required: false 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: - name: Process the inputs id: process_inputs shell: bash run: | 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 }}'" fi if [ -n '${{ inputs.c-compiler }}' ]; then 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 }}'" fi if [ -n '${{ inputs.c-flags }}' ]; then 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 }}'" 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' }} shell: bash run: | case "$OSTYPE" in darwin*) brew install ninja ;; linux*) sudo apt install -y ninja-build ;; *) choco install ninja ;; esac - name: Configure the CMake project shell: ${{ runner.os == 'Windows' && 'pwsh' || 'bash' }} run: cmake ${{ steps.process_inputs.outputs.cmake_args }} - name: Build targets 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 }}