andrew-hardin / cmake-git-version-tracking Goto Github PK
View Code? Open in Web Editor NEWEmbed Git metadata in a C/C++ compatible static library using CMake (e.g. commit ID, author, date, and message).
License: MIT License
Embed Git metadata in a C/C++ compatible static library using CMake (e.g. commit ID, author, date, and message).
License: MIT License
I want to put the variables obtained from the git_watcher script into a header file. Between the documentation and examples, I'm not entirely sure if this will still not trigger recompilation until something actually changes.
We use your code in a downstream project at https://gitlab.com/YottaDB/DBMS/YDBOcto. It has served us well.
We had some problems, so I am looking at the upstream version in order to see if they are resolved there.
There is a problem with sudo make install.
I made a simple example for you: Add this line to the CMakeLists.txt
in the better-example
folder.
install(TARGETS demo DESTINATION /usr/local/bin)
Run cmake .. && make && sudo make install
, and you will see this:
CMake Error at /home/sam/work/gitlab/cmake-git-version-tracking/git_watcher.cmake:139 (message):
fatal: unsafe repository
('/home/sam/work/gitlab/cmake-git-version-tracking' is owned by someone
else)
To add an exception for this directory, call:
git config --global --add safe.directory
/home/sam/work/gitlab/cmake-git-version-tracking
(/usr/bin/git status --porcelain -unormal)
Call Stack (most recent call first):
/home/sam/work/gitlab/cmake-git-version-tracking/git_watcher.cmake:162 (RunGitCommand)
/home/sam/work/gitlab/cmake-git-version-tracking/git_watcher.cmake:289 (GetGitState)
/home/sam/work/gitlab/cmake-git-version-tracking/git_watcher.cmake:356 (CheckGit)
/home/sam/work/gitlab/cmake-git-version-tracking/git_watcher.cmake:367 (Main)
This seems to be due to this change: https://github.blog/2022-04-12-git-security-vulnerability-announced/
I am trying to come up with a general purpose solution, but don't have any ideas right now.
The script name is also hard coded and can be replaced with ${CMAKE_CURRENT_LIST_FILE}
.
I noticed this while working on a vanilla RHEL7 machine: older versions of git don't support all the configuration options that we're passing to git show
.
The script should catch this behavior and/or use more common flags for git show
.
Time to migrate to GitHub actions...
Add
include(FetchContent)
to section Quickstart via FetchContent
of README.md
for completeness. Otherwise user gets error:
CMake Error at CMakeLists.txt:12 (FetchContent_Declare):
Unknown CMake command "FetchContent_Declare".
When building using cmake, if the cmake script fails I've run into into a situation where the failure of the cmake build makes the build significantly longer because the compiler errors accumulating from the failed build takes longer to process.
My suggestion would be to have a variation where the script always succeeds, but writes into an additional auxiliary function whether the related git functionality is truly available?
IE:
inline static bool is_git_repository();
Here's the commit message that'll break it:
This has a bad \ character.
-DCMAKE_AR=llvm-ar -DCMAKE_C_FLAGS="-flto=thin" -DCMAKE_...=... ...
[ 2%] Linking C static library libcmake_git_version_tracking.a
/usr/bin/ar: invalid option -- 'e'
Usage: /usr/bin/ar [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV] [--plugin <name>] [member-name] [count] archive-file file...
/usr/bin/ar -M [<mri-script]
.
.
I'm configuring my c++ project just like https://github.com/andrew-hardin/cmake-git-version-tracking/blob/master/better-example/CMakeLists.txt, but I need to run cmake .. && make
twice to get the linking works.
I found that the first time I run cmake .. && make
, my lib compiled without git.cc.
At the time I wrote this issue, I found my mistake:
file(GLOB_RECURSE SOURCES src/**/*.cpp "${PROJECT_BINARY_DIR}/git.cc")
# globbing in cmake will ignore non-existing source, so the first time of `cmake` (before `make` generating git.cc) will not include git.cc
set(PRE_CONFIGURE_FILE cmake/git.cc.in)
set(POST_CONFIGURE_FILE "${PROJECT_BINARY_DIR}/git.cc")
include(cmake/git_watcher.cmake)
GitStateChangedAction() # I need this extra line
It's not a mistake of your cmake script. Just firing an issue hopping it to be helpful for others.
Is there a reason GIT_COMMIT_BODY has quotes included in the value while all other strings don't?
Generated git.cc
has something like:
bool GitMetadata::Populated() {
return ;
}
bool GitMetadata::AnyUncommittedChanges() {
return ;
}
std::string GitMetadata::CommitBody() {
return ;
}
Seems env variables not properly set. (Not quite sure how did I get there. I'll provide a mimimum process to reproduce it later.)
I would suggest
bool GitMetadata::Populated() {
return "@GIT_RETRIEVED_STATE@" == std::string("false");
}
bool GitMetadata::AnyUncommittedChanges() {
return "@GIT_IS_DIRTY@" == std::string("false");
}
std::string GitMetadata::CommitBody() {
return "@GIT_COMMIT_BODY@";
}
and not surround these variables with "
quotation mark in cmake. This would be more robust.
If the generated header file goes missing but the git-state-hash is not also deleted (for example if make clean
is run), the header file will not be generated again, causing compilation to fail.
Example steps to reproduce using hello-world demo:
cd hello-world/
cmake .
make
# The project will build successfully
make clean
# git.h is deleted, but git-state-hash remains
make
# git-state-hash is present and up to date, but git.h does not regenerate
# Build fails with missing header
Can the build be configured such that if generated header is missing, it will be generated in all cases?
Less sure if this makes sense or not, but it might also be a good idea for make clean
to remove git-state-hash.
I can't build the hello-world
example:
cmake -G "MSYS Makefiles" ..
CMake Warning (dev) in CMakeLists.txt:
No project() command is present. The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command. Add a line of
code such as
project(ProjectName)
near the top of the file, but after cmake_minimum_required().
CMake is pretending there is a "project(Project)" command on the first
line.
This warning is for project developers. Use -Wno-dev to suppress it.
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: C:/msys64/mingw64/bin/gcc.exe
-- Check for working C compiler: C:/msys64/mingw64/bin/gcc.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/msys64/mingw64/bin/g++.exe
-- Check for working CXX compiler: C:/msys64/mingw64/bin/g++.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at git_watcher.cmake:1:
Parse error. Expected a command name, got unquoted argument with text
"../git_watcher.cmake".
Call Stack (most recent call first):
CMakeLists.txt:7 (include)
-- Configuring incomplete, errors occurred!
See also "cmake-git-version-tracking/hello-world/build/CMakeFiles/CMakeOutput.log".
The CMakeOutput.log
does not contain anything useful.
Consider adding R"(@variable@)"
to the C++ related files. Not sure yet how to handle C.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.