|
| 1 | +# @copyright Copyright (c) contributors to Project Ocre, |
| 2 | +# which has been established as Project Ocre a Series of LF Projects, LLC |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +# Check if we are in a git repository |
| 7 | +execute_process( |
| 8 | + COMMAND git rev-parse --git-dir |
| 9 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 10 | + OUTPUT_VARIABLE GIT_DIR |
| 11 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 12 | + ERROR_QUIET |
| 13 | +) |
| 14 | + |
| 15 | +if(GIT_DIR) |
| 16 | + message(STATUS "Detected git repository. Automatic commit ID tracking is enabled.") |
| 17 | + |
| 18 | + # Generate commit ID header file in source dir if we are in a git repository |
| 19 | + add_custom_command( |
| 20 | + OUTPUT include/ocre/commit_id.h |
| 21 | + COMMAND sh -c "mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include/ocre" |
| 22 | + COMMAND sh -c "echo '/* Auto-generated file. DO NOT EDIT */' > ${CMAKE_CURRENT_BINARY_DIR}/include/ocre/commit_id.h" |
| 23 | + COMMAND sh -c "echo \"#define GIT_COMMIT_ID \\\"$(git describe --always --abbrev=0 --dirty)\\\"\" >> ${CMAKE_CURRENT_BINARY_DIR}/include/ocre/commit_id.h" |
| 24 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 25 | + VERBATIM |
| 26 | + DEPENDS always_rebuild |
| 27 | + ) |
| 28 | +endif() |
| 29 | + |
| 30 | +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/ocre) |
| 31 | + |
| 32 | +# Generate build info header file in binary dir for every build |
| 33 | +add_custom_command( |
| 34 | + OUTPUT include/ocre/build_info.h |
| 35 | + COMMAND sh -c "echo \"#define OCRE_BUILD_HOST_INFO \\\"$ENV{USER} @ $(uname -a)\\\"\" > include/ocre/build_info.h" |
| 36 | + COMMAND sh -c "echo \"#define OCRE_BUILD_DATE \\\"$(date +'%Y-%m-%d %H:%M:%S %Z')\\\"\" >> include/ocre/build_info.h" |
| 37 | + VERBATIM |
| 38 | + DEPENDS always_rebuild |
| 39 | +) |
| 40 | + |
| 41 | +# dummy command used to make the custom commands be re-run on every build (not just configure) |
| 42 | +add_custom_command( |
| 43 | + OUTPUT always_rebuild |
| 44 | + COMMAND cmake -E echo |
| 45 | +) |
| 46 | + |
| 47 | +add_library(OcreCommon INTERFACE) |
| 48 | + |
| 49 | +# if we are not in a git repository, just fail automatically if we are missing commit_id.h |
| 50 | +target_sources(OcreCommon |
| 51 | + PRIVATE |
| 52 | + include/ocre/build_info.h |
| 53 | + include/ocre/commit_id.h |
| 54 | +) |
| 55 | + |
| 56 | +target_include_directories(OcreCommon |
| 57 | + INTERFACE |
| 58 | + include |
| 59 | + ${CMAKE_CURRENT_BINARY_DIR}/include |
| 60 | +) |
0 commit comments