Skip to content

CMake/Build: if git is available in PATH, embed the github hash in a C String #1450

@ggarra13

Description

@ggarra13

Is your feature request related to a problem? Please describe.
Currently, in the issue #1439, there were a lot of commits done in rapid sucession fixing some bugs and making some old bugs appear again.
I tested in issue #1446 that I also got different results with those commits.

I am using FLTK

  • Version 1.5

Describe the solution you'd like
If git is available in PATH, I would like to have the git hash be embedded in the executable.
This is a common practice among some highly steemed projects like ffmpeg. I've borrowed the practice in my own mrv2 and I am attaching some sample cmake code on how I did it, if you guys care.

# Retrieve the current Git branch name
execute_process(
    COMMAND git rev-parse --abbrev-ref HEAD
    OUTPUT_VARIABLE GIT_BRANCH
    OUTPUT_STRIP_TRAILING_WHITESPACE  # This removes any trailing newline characters
)
# Retrieve the short Git commit hash
execute_process(
    COMMAND git rev-parse --short HEAD
    OUTPUT_VARIABLE GIT_SHORT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Optionally, print the branch name to verify
message(STATUS "Current Git Branch: ${GIT_BRANCH}")

# Define a preprocessor macro for the Git branch name and short hash
add_compile_definitions(GIT_BRANCH_NAME="${GIT_BRANCH}")
add_compile_definitions(GIT_SHORT_HASH="${GIT_SHORT_HASH}")

Metadata

Metadata

Assignees

Labels

CMakeCMake bug or needs CMake supportactiveSomebody is working on it
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions