Skip to content

Commit 5be43ab

Browse files
authored
Merge pull request #31 from threeal/main
Version 1.1.0 Release
2 parents 894b72b + 20b9c20 commit 5be43ab

File tree

4 files changed

+96
-42
lines changed

4 files changed

+96
-42
lines changed

.github/workflows/test.yml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ jobs:
3131
uses: ./
3232
with:
3333
source-dir: test
34-
build-dir: test/build
35-
36-
- name: Run the build result
37-
run: test/build/hello_world
34+
build-dir: output
35+
run-test: true
36+
test-args: -R hello_world
3837

3938
- name: Check if the default build directory does not exist
40-
run: test ! -d build
39+
run: test ! -d build && test ! -d test/build
4140

4241
additional-flags-usage:
43-
runs-on: ubuntu-latest
42+
runs-on: ${{ matrix.compiler == 'msvc' && 'windows' || 'ubuntu' }}-latest
43+
strategy:
44+
matrix:
45+
compiler: [gcc, msvc]
4446
steps:
4547
- name: Check out this repository
4648
uses: actions/[email protected]
@@ -50,11 +52,11 @@ jobs:
5052
with:
5153
source-dir: test
5254
targets: test_c test_cpp
53-
c-flags: -Wno-unused-variable
54-
cxx-flags: -Wno-unused-variable
55-
56-
- name: Run the build results
57-
run: build/test_c && build/test_cpp
55+
run-test: true
56+
c-flags: ${{ matrix.compiler == 'msvc' && '/w /WX-' || '-Wno-unused-variable' }}
57+
cxx-flags: ${{ matrix.compiler == 'msvc' && '/w /WX-' || '-Wno-unused-variable' }}
58+
args: -D CHECK_SURPASS_WARNING=ON
59+
test-args: -R test ${{ matrix.compiler == 'msvc' && '-C Debug' || '' }}
5860

5961
specified-compiler-usage:
6062
runs-on: ${{ matrix.os }}-latest
@@ -70,13 +72,12 @@ jobs:
7072
with:
7173
source-dir: test
7274
targets: test_c test_cpp
75+
run-test: true
7376
generator: Ninja
7477
c-compiler: clang
7578
cxx-compiler: clang++
7679
args: -D CHECK_USING_CLANG=ON
77-
78-
- name: Run the build results
79-
run: build/test_c && build/test_cpp
80+
test-args: -R test
8081

8182
specified-generator-usage:
8283
runs-on: ${{ matrix.os }}-latest
@@ -91,7 +92,6 @@ jobs:
9192
uses: ./
9293
with:
9394
source-dir: test
95+
run-test: true
9496
generator: Ninja
95-
96-
- name: Run the build result
97-
run: build/hello_world
97+
test-args: -R hello_world

README.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44
[![license](https://img.shields.io/github/license/threeal/cmake-action)](./LICENSE)
55
[![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)
66

7-
Configure and build a [CMake](https://cmake.org/) project on [GitHub Actions](https://github.com/features/actions).
7+
Configure, build, and test a [CMake](https://cmake.org/) project on [GitHub Actions](https://github.com/features/actions).
8+
Use this action to simplify the workflow run of your CMake project.
9+
This action will configure a build environment for your project using the `cmake` command,
10+
then it will build your project by running a `cmake --build` command,
11+
and last it could test your project using the `ctest` command.
12+
13+
## Features
14+
15+
- Configure and build a project using the [cmake](https://cmake.org/cmake/help/latest/manual/cmake.1.html) command.
16+
- Optionally test a project using the [ctest](https://cmake.org/cmake/help/latest/manual/ctest.1.html) command.
17+
- Auto-detect and install required dependencies.
18+
- Specify multiple CMake options directly from the Action inputs.
819

920
## Usage
1021

@@ -15,14 +26,16 @@ For more information, see [action.yml](./action.yml) and [GitHub Actions guide](
1526
| Name | Value Type | Description |
1627
| --- | --- | --- |
1728
| `source-dir` | Path | Source directory of the CMake project. Defaults to current directory. |
18-
| `build-dir` | Path | Build directory of the CMake project. Defaults to `build` directory in current directory. |
29+
| `build-dir` | Path | Build directory of the CMake project. Defaults to `build` directory inside the source directory. |
1930
| `targets` | Multiple strings | List of build targets. |
31+
| `run-test` | `true` or `false` | If enabled, run testing using [CTest](https://cmake.org/cmake/help/latest/manual/ctest.1.html). Defaults to `false`. |
2032
| `generator` | String | Build system generator of the CMake project. |
2133
| `c-compiler` | String | Preferred executable for compiling C language files. |
2234
| `cxx-compiler` | String | Preferred executable for compiling C++ language files. |
2335
| `c-flags` | Multiple strings | Additional flags passed when compiling C language files. |
2436
| `cxx-flags` | Multiple strings | Additional flags passed when compiling C++ language files. |
2537
| `args` | Multiple strings | Additional arguments passed during the CMake configuration. |
38+
| `test-args` | Multiple strings | Additional arguments passed during the CTest run. |
2639

2740
> 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.
2841
@@ -45,7 +58,7 @@ jobs:
4558
uses: threeal/cmake-action@latest
4659
```
4760
48-
> Note: You can replace `@latest` with any version you like.
61+
> 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).
4962

5063
#### Specify the Source and the Build Directories
5164

@@ -54,21 +67,26 @@ jobs:
5467
uses: threeal/cmake-action@latest
5568
with:
5669
source-dir: submodules
57-
build-dir: submodules/build
70+
build-dir: submodules/out
5871
```
5972

60-
#### Specify the Build Targets and Additional Options
73+
#### Specify the Build Targets
6174

6275
```yaml
6376
- name: Configure and build this project
6477
uses: threeal/cmake-action@latest
6578
with:
66-
targets: hello_world_test fibonacci_test
67-
c-flags: -Werror
68-
cxx-flags: -Werror
69-
args: |
70-
-DCMAKE_BUILD_TYPE=Debug
71-
-DBUILD_TESTING=ON
79+
targets: hello_mars hello_sun
80+
```
81+
82+
#### Run Unit Tests After Build
83+
84+
```yaml
85+
- name: Configure, build, and test this project
86+
uses: threeal/cmake-action@latest
87+
with:
88+
args: -DBUILD_TESTING=ON
89+
run-test: true
7290
```
7391

7492
#### Using Ninja as the Generator and Clang as the Compiler

action.yml

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: CMake Action
2-
description: Configure and build a CMake project
2+
description: Configure, build, and test a CMake project
33
author: Alfi Maulana
44
branding:
55
color: gray-dark
@@ -8,14 +8,16 @@ inputs:
88
source-dir:
99
description: Source directory of the CMake project
1010
required: false
11-
default: .
1211
build-dir:
1312
description: Build directory of the CMake project
1413
required: false
15-
default: build
1614
targets:
1715
description: List of build targets
1816
required: false
17+
run-test:
18+
description: If enabled, run testing using CTest (true/false)
19+
required: false
20+
default: false
1921
generator:
2022
description: Build system generator of the CMake project
2123
required: false
@@ -34,38 +36,59 @@ inputs:
3436
args:
3537
description: Additional arguments passed during the CMake configuration
3638
required: false
39+
test-args:
40+
description: Additional arguments passed during the CTest run
41+
required: false
3742
runs:
3843
using: composite
3944
steps:
4045
- name: Process the inputs
4146
id: process_inputs
4247
shell: bash
4348
run: |
44-
ARGS="${{ inputs.source-dir }} -B ${{ inputs.build-dir }}"
45-
BUILD_ARGS="--build ${{ inputs.build-dir }}"
49+
SOURCE_DIR="."
50+
if [ -n '${{ inputs.source-dir }}' ]; then
51+
SOURCE_DIR="${{ inputs.source-dir }}"
52+
fi
53+
BUILD_DIR="build"
54+
if [ -n '${{ inputs.build-dir }}' ]; then
55+
BUILD_DIR="${{ inputs.build-dir }}"
56+
elif [ -n "${{ inputs.source-dir }}" ]; then
57+
BUILD_DIR="${{ inputs.source-dir }}/build"
58+
fi
59+
ARGS="'$SOURCE_DIR' -B '$BUILD_DIR'"
60+
BUILD_ARGS="--build '$BUILD_DIR'"
61+
TEST_ARGS=""
4662
if [ -n '${{ inputs.targets }}' ]; then
4763
BUILD_ARGS="$BUILD_ARGS --target ${{ inputs.targets }}"
4864
fi
65+
if [ '${{ inputs.run-test }}' == 'true' ]; then
66+
TEST_ARGS="--test-dir '$BUILD_DIR' --output-on-failure --no-tests=error"
67+
fi
4968
if [ -n '${{ inputs.generator }}' ]; then
50-
ARGS="$ARGS -G ${{ inputs.generator }}"
69+
ARGS="$ARGS -G '${{ inputs.generator }}'"
5170
fi
5271
if [ -n '${{ inputs.c-compiler }}' ]; then
53-
ARGS="$ARGS -D CMAKE_C_COMPILER=${{ inputs.c-compiler }}"
72+
ARGS="$ARGS -D CMAKE_C_COMPILER='${{ inputs.c-compiler }}'"
5473
fi
5574
if [ -n '${{ inputs.cxx-compiler }}' ]; then
56-
ARGS="$ARGS -D CMAKE_CXX_COMPILER=${{ inputs.cxx-compiler }}"
75+
ARGS="$ARGS -D CMAKE_CXX_COMPILER='${{ inputs.cxx-compiler }}'"
5776
fi
5877
if [ -n '${{ inputs.c-flags }}' ]; then
59-
ARGS="$ARGS -D CMAKE_C_FLAGS=${{ inputs.c-flags }}"
78+
ARGS="$ARGS -D CMAKE_C_FLAGS='${{ inputs.c-flags }}'"
6079
fi
6180
if [ -n '${{ inputs.cxx-flags }}' ]; then
62-
ARGS="$ARGS -D CMAKE_CXX_FLAGS=${{ inputs.cxx-flags }}"
81+
ARGS="$ARGS -D CMAKE_CXX_FLAGS='${{ inputs.cxx-flags }}'"
6382
fi
6483
if [ -n '${{ inputs.args }}' ]; then
6584
ARGS="$ARGS ${{ inputs.args }}"
6685
fi
86+
if [ -n '${{ inputs.test-args }}' ]; then
87+
TEST_ARGS="$TEST_ARGS ${{ inputs.test-args }}"
88+
fi
6789
echo "cmake_args=${ARGS//[$'\t\r\n']}" >> $GITHUB_OUTPUT
6890
echo "cmake_build_args=${BUILD_ARGS//[$'\t\r\n']}" >> $GITHUB_OUTPUT
91+
echo "cmake_test_args=${TEST_ARGS//[$'\t\r\n']}" >> $GITHUB_OUTPUT
6992
7093
- name: Install Ninja
7194
if: ${{ inputs.generator == 'Ninja' }}
@@ -78,9 +101,14 @@ runs:
78101
esac
79102
80103
- name: Configure the CMake project
81-
shell: bash
104+
shell: ${{ runner.os == 'Windows' && 'pwsh' || 'bash' }}
82105
run: cmake ${{ steps.process_inputs.outputs.cmake_args }}
83106

84107
- name: Build targets
85-
shell: bash
108+
shell: ${{ runner.os == 'Windows' && 'pwsh' || 'bash' }}
86109
run: cmake ${{ steps.process_inputs.outputs.cmake_build_args }}
110+
111+
- name: Run tests
112+
if: steps.process_inputs.outputs.cmake_test_args != ''
113+
shell: ${{ runner.os == 'Windows' && 'pwsh' || 'bash' }}
114+
run: ctest ${{ steps.process_inputs.outputs.cmake_test_args }}

test/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ option(CHECK_USING_CLANG "check if target is compiled using Clang" OFF)
55
option(CHECK_SURPASS_WARNING "check if target could surpass a compiler warning" OFF)
66

77
if(CHECK_SURPASS_WARNING)
8-
set(CMAKE_C_FLAGS "-Werror -Wunused-variable ${CMAKE_C_FLAGS}")
9-
set(CMAKE_CXX_FLAGS "-Werror -Wunused-variable ${CMAKE_CXX_FLAGS}")
8+
if(MSVC)
9+
set(CMAKE_C_FLAGS "/WX /W4 ${CMAKE_C_FLAGS}")
10+
set(CMAKE_CXX_FLAGS "/WX /W4 ${CMAKE_CXX_FLAGS}")
11+
else()
12+
set(CMAKE_C_FLAGS "-Werror -Wunused-variable ${CMAKE_C_FLAGS}")
13+
set(CMAKE_CXX_FLAGS "-Werror -Wunused-variable ${CMAKE_CXX_FLAGS}")
14+
endif()
1015
endif()
1116

17+
enable_testing()
1218
add_executable(hello_world hello_world.cpp)
19+
add_test(NAME hello_world COMMAND $<TARGET_FILE:hello_world>)
1320

1421
list(APPEND LANGS c cpp)
1522
foreach(LANG ${LANGS})
@@ -21,4 +28,5 @@ foreach(LANG ${LANGS})
2128
$<$<BOOL:${CHECK_USING_CLANG}>:CHECK_USING_CLANG>
2229
$<$<BOOL:${CHECK_SURPASS_WARNING}>:CHECK_SURPASS_WARNING>
2330
)
31+
add_test(NAME test_${LANG} COMMAND $<TARGET_FILE:test_${LANG}>)
2432
endforeach()

0 commit comments

Comments
 (0)