From 34b2a421828e6203dbc568dc602c5b85e5944239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20P=C3=A9ron?= Date: Sat, 10 Feb 2024 12:27:07 +0100 Subject: [PATCH] cmake: optional use git if found get branch and commit --- .github/workflows/build_and_test.yml | 15 +++++++++++---- .github/workflows/codeql.yml | 2 +- .github/workflows/package.yml | 6 +++++- CMakeLists.txt | 18 ++++++++++++++++-- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 4064c03a41..641f1ff6ad 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -17,11 +17,14 @@ jobs: - name: Checkout code uses: actions/checkout@v3 + # Checkout is performed out of the container and doesn't match our user + - name: Fix checkout ownership + run: chown -R "$(id -u):$(id -g)" "$GITHUB_WORKSPACE" + - name: Install dependencies run: | apk update && apk add cppcheck python3-dev python3 -m pip install cmake-format - git config --global --add safe.directory "$GITHUB_WORKSPACE" - uses: pre-commit/action@v3.0.0 @@ -39,7 +42,7 @@ jobs: git diff --exit-code linux: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest container: seladb/${{ matrix.image }} strategy: matrix: @@ -72,6 +75,10 @@ jobs: - name: Checkout code uses: actions/checkout@v3 + # Checkout is performed out of the container and doesn't match our user + - name: Fix checkout ownership + run: chown -R "$(id -u):$(id -g)" "$GITHUB_WORKSPACE" + - name: Setup Intel Compiler variables if: contains(matrix.image, 'icpx') run: | @@ -125,7 +132,7 @@ jobs: verbose: true dpdk: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest container: seladb/${{ matrix.image }} strategy: matrix: @@ -174,7 +181,7 @@ jobs: run: cd build_examples/tutorials_bin && ./Tutorial-HelloWorld pfring: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest container: seladb/ubuntu2004-pfring strategy: matrix: diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index c7aff3da5e..95f692b9b1 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -10,7 +10,7 @@ on: jobs: analyze: name: Analyze - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest container: seladb/ubuntu2204 permissions: security-events: write diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index c67e9c19c5..7fbd727207 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -10,7 +10,7 @@ env: jobs: linux: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest container: seladb/${{ matrix.image }} strategy: matrix: @@ -35,6 +35,10 @@ jobs: - name: Checkout code uses: actions/checkout@v3 + # Checkout is performed out of the container and doesn't match our user + - name: Fix checkout ownership + run: chown -R "$(id -u):$(id -g)" "$GITHUB_WORKSPACE" + - name: Setup Intel Compiler variables if: contains(matrix.image, 'icpx') run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index cff66721db..58efe69eb0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,8 +203,22 @@ if(PCAPPP_USE_DPDK) endif() # Git Commit and Branch -add_compile_definitions(GIT_COMMIT="${GIT_HASH}") -add_compile_definitions(GIT_BRANCH="${GIT_BRANCH}") +find_package(Git) +if(Git_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 --format=%h + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE PCAPPP_GIT_COMMIT) + string(STRIP "${PCAPPP_GIT_COMMIT}" PCAPPP_GIT_COMMIT) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + OUTPUT_VARIABLE PCAPPP_GIT_BRANCH) + string(STRIP "${PCAPPP_GIT_BRANCH}" PCAPPP_GIT_BRANCH) + message(STATUS "Building from commit:${PCAPPP_GIT_COMMIT} on branch:${PCAPPP_GIT_BRANCH}") + add_compile_definitions(GIT_COMMIT="${PCAPPP_GIT_COMMIT}") + add_compile_definitions(GIT_BRANCH="${PCAPPP_GIT_BRANCH}") +endif() if(PCAPPP_USE_PF_RING) find_package(PF_RING)