skaravos / CMakeVersionInfoTarget

CMake - VersionInfoTarget
MIT License
0 stars 1 forks source link

Allow GIT info to be passed as (env?) variables #4

Open skaravos opened 2 months ago

skaravos commented 2 months ago

When projects are compiled inside containerized CI/CD environments, the (docker) container that actually runs the configuration & compilation may not have access to git, or the project's .git repository information may not be present. In these cases, even though the build container can't access the git info, the CI/CD runner should have access to all the git repository information before the job is dispatched to the build container. So it would be nice to be able to forward this knowledge along to the submodule in the form of environment variables.

I'd need to test the idea more thoroughly, but I'm thinking something along the lines of:

# example gitlab-ci.yml build
build:
  stage: build
  image: SOME_DOCKER_IMAGE_WITHOUT_GIT
  tags:
    - docker
  script:
    # gitlab's CI_COMMIT_AUTHOR var is in the form "Name <email>"
    - commit_author_info=${CI_COMMIT_AUTHOR}
    - commit_user_name=${commit_author_info% <*}
    - commit_email=${commit_author_info#*<}; commit_email=${commit_email%*>}
    # export git info vars for use by the cmake version info target submodule
    - export GIT_UNCOMMITTED_CHANGES=0
    - export GIT_COMMIT_HASH=${CI_COMMIT_SHA}
    - export GIT_COMMIT_DATE=${CI_COMMIT_TIMESTAMP}
    - export GIT_AUTHOR_NAME=${commit_user_name}
    - export GIT_AUTHOR_EMAIL=${commit_email}
    # run configuration & build...
    # the VersionInfo submodule should use the env var values as a fallback
    - cmake -S . -B _build -DCMAKE_INSTALL_PREFIX=_install
    - cmake --build _build --target install
skaravos commented 2 months ago

On a somewhat related note.

Right now the module has a hard-requirement on finding the GIT_EXECUTABLE if the GIT_WORK_TREE argument is provided in the call to add_version_info_target(). This causes problems if the project is being compiled from a standalone copy of the source code.

A project being compiled from source may not always be compiled from inside its git repository, and it would be deranged to expect the user to edit the CMakeLists.txt file to remove the GIT_WORK_TREE argument every time they are compiling the project from a non-repo directory.

Would it be better to instead emit a WARNING message instead of an ERROR when either the git executable cannot be found, or if the given work tree isn't a repository?

brainstorming for my future self