From 515c14d1b8073be4d936d7a601035c02bc71496b Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 8 Jan 2023 11:22:57 +0700 Subject: [PATCH 1/7] add `c-compiler` input option --- action.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/action.yml b/action.yml index dfd4d4b..c01737c 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,9 @@ inputs: description: The build directory of CMake project required: false default: build + c-compiler: + description: Preferred executable for compiling C language files + required: false runs: using: composite steps: @@ -21,6 +24,9 @@ runs: run: | CONFIGURE_ARGS="${{ inputs.source-dir }} -B ${{ inputs.build-dir }}" BUILD_ARGS="--build ${{ inputs.build-dir }}" + if [ -n '${{ inputs.c-compiler }}' ]; then + CONFIGURE_ARGS="$CONFIGURE_ARGS -D CMAKE_C_COMPILER=${{ inputs.c-compiler }}" + fi echo "CMAKE_CONFIGURE_ARGS=$CONFIGURE_ARGS" >> $GITHUB_ENV echo "CMAKE_BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV From 0b67ed5990d3c8eb4cfde771f4e215bd7c83a0a4 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 8 Jan 2023 12:18:05 +0700 Subject: [PATCH 2/7] add `cxx-compiler` input option --- action.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/action.yml b/action.yml index 4be2805..26305f8 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,9 @@ inputs: c-compiler: description: The preferred executable for compiling C language files required: false + cxx-compiler: + description: The preferred executable for compiling CXX language files + required: false args: description: Additional arguments passed during CMake configuration required: false @@ -30,6 +33,9 @@ runs: if [ -n '${{ inputs.c-compiler }}' ]; then CONFIGURE_ARGS="$CONFIGURE_ARGS -D CMAKE_C_COMPILER=${{ inputs.c-compiler }}" fi + if [ -n '${{ inputs.cxx-compiler }}' ]; then + CONFIGURE_ARGS="$CONFIGURE_ARGS -D CMAKE_CXX_COMPILER=${{ inputs.cxx-compiler }}" + fi if [ -n '${{ inputs.args }}' ]; then CONFIGURE_ARGS="$CONFIGURE_ARGS ${{ inputs.args }}" fi From 582c9a40fa53862614fd3fb41a8ec2b3471dfabf Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 8 Jan 2023 12:22:23 +0700 Subject: [PATCH 3/7] add workflow job to test this action with specified compiler --- .github/workflows/test.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5575fa6..6d9a62b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -53,6 +53,22 @@ jobs: - name: Check if default build directory is not exist run: test ! -d build + use-action-with-specified-compiler: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3.3.0 + + - name: Use this action with specified compiler + uses: ./ + with: + source-dir: test + c-compiler: clang + cxx-compiler: clang++ + + - name: Run build result + run: build/hello_world + use-action-with-additional-args: runs-on: ubuntu-latest steps: From 2511f03a889980142d2a134f25aeb3b98f716723 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 8 Jan 2023 12:35:22 +0700 Subject: [PATCH 4/7] add support in test project to choose C and/or C++ to compile --- test/CMakeLists.txt | 10 +++++++++- test/hello_world.c | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/hello_world.c diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 135aa7f..27c7acd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,17 @@ cmake_minimum_required(VERSION 3.0) project(test) +option(BUILD_C "build hello world in C Language" OFF) +option(BUILD_CXX "build hello world in CXX Language" ON) option(BUILD_TXT "build hello world txt file" OFF) -add_executable(hello_world hello_world.cpp) +if(BUILD_C) + add_executable(hello_world_c hello_world.c) +endif() + +if(BUILD_CXX) + add_executable(hello_world hello_world.cpp) +endif() if(BUILD_TXT) add_custom_target( diff --git a/test/hello_world.c b/test/hello_world.c new file mode 100644 index 0000000..7791fbb --- /dev/null +++ b/test/hello_world.c @@ -0,0 +1,6 @@ +#include + +int main() { + printf("Hello world!\n"); + return 0; +} From c7a5566aa68d2efba3657a3981d2891bd4b71dd7 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 8 Jan 2023 12:44:44 +0700 Subject: [PATCH 5/7] use matrix for testing action with specified C and CXX compiler --- .github/workflows/test.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6d9a62b..33af546 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -55,6 +55,9 @@ jobs: use-action-with-specified-compiler: runs-on: ubuntu-latest + strategy: + matrix: + lang: [C, CXX] steps: - name: Checkout repository uses: actions/checkout@v3.3.0 @@ -63,11 +66,14 @@ jobs: uses: ./ with: source-dir: test - c-compiler: clang - cxx-compiler: clang++ + c-compiler: ${{ matrix.lang == 'C' && 'clang' || '' }} + cxx-compiler: ${{ matrix.lang == 'CXX' && 'clang++' || '' }} + args: | + -D BUILD_C=${{ matrix.lang == 'C' && 'ON' || 'OFF' }} + -D BUILD_CXX=${{ matrix.lang == 'CXX' && 'ON' || 'OFF' }} - name: Run build result - run: build/hello_world + run: build/${{ matrix.lang == 'C' && 'hello_world_c' || 'hello_world' }} use-action-with-additional-args: runs-on: ubuntu-latest From e21712692c037471ebe906dd869c1b746c8926fb Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 8 Jan 2023 13:38:38 +0700 Subject: [PATCH 6/7] fix multiline input by removing newline and other specifiers --- action.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 26305f8..801c3ea 100644 --- a/action.yml +++ b/action.yml @@ -39,8 +39,8 @@ runs: if [ -n '${{ inputs.args }}' ]; then CONFIGURE_ARGS="$CONFIGURE_ARGS ${{ inputs.args }}" fi - echo "CMAKE_CONFIGURE_ARGS=$CONFIGURE_ARGS" >> $GITHUB_ENV - echo "CMAKE_BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV + echo "CMAKE_CONFIGURE_ARGS=${CONFIGURE_ARGS//[$'\t\r\n']}" >> $GITHUB_ENV + echo "CMAKE_BUILD_ARGS=${BUILD_ARGS//[$'\t\r\n']}" >> $GITHUB_ENV - name: Configure CMake shell: bash From 0501ce589be5fea12fbc4e0b3585fea34ecd8e1a Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 8 Jan 2023 13:47:54 +0700 Subject: [PATCH 7/7] make use action with specified compiler to test invalid compiler name --- .github/workflows/test.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 33af546..9a79062 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -62,6 +62,18 @@ jobs: - name: Checkout repository uses: actions/checkout@v3.3.0 + - name: Use this action with specified invalid compiler + id: failed_step + continue-on-error: true + uses: ./ + with: + source-dir: test + c-compiler: ${{ matrix.lang == 'C' && 'invalid' || '' }} + cxx-compiler: ${{ matrix.lang == 'CXX' && 'invalid' || '' }} + + - name: Check if previous step is failing + run: ${{ steps.failed_step.outcome == 'failure' && 'true' || 'false' }} + - name: Use this action with specified compiler uses: ./ with: