Skip to content

Commit

Permalink
Merge pull request #31 from threeal/main
Browse files Browse the repository at this point in the history
Version 1.1.0 Release
  • Loading branch information
threeal authored Jan 23, 2023
2 parents 894b72b + 20b9c20 commit 5be43ab
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 42 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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

Expand All @@ -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
Expand Down
52 changes: 40 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -34,38 +36,59 @@ 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:
- name: Process the inputs
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' }}
Expand All @@ -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 }}
12 changes: 10 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:hello_world>)

list(APPEND LANGS c cpp)
foreach(LANG ${LANGS})
Expand All @@ -21,4 +28,5 @@ foreach(LANG ${LANGS})
$<$<BOOL:${CHECK_USING_CLANG}>:CHECK_USING_CLANG>
$<$<BOOL:${CHECK_SURPASS_WARNING}>:CHECK_SURPASS_WARNING>
)
add_test(NAME test_${LANG} COMMAND $<TARGET_FILE:test_${LANG}>)
endforeach()

0 comments on commit 5be43ab

Please sign in to comment.