Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter-06/Recipe-07 does not seem to update the git hash every time the code is built #506

Open
tjwrona opened this issue Feb 8, 2019 · 9 comments

Comments

@tjwrona
Copy link

tjwrona commented Feb 8, 2019

Expected Behavior

When creating a new commit and running cmake --build . it should update the header file with the new commit hash.

Current Behavior

The commit hash is not updated after cmake --build .

Steps to Reproduce (for bugs)

Create a git repo as follows:

git init
git add --all
git commit

The build the project:

mkdir build
cd build
cmake ..
cmake --build .

Run the project:

./Debug/example.exe

This code has been built from version 6886ebb

Create a new git commit:

cd ..
git commit --allow-empty

Rebuild the project (without re-running CMake)

cd build
cmake --build .

Run the project:

./Debug/example.exe

This code has been built from version 6886ebb


Note that the code has not picked up any change to the commit hash; however running the Git command to print the hash reveals that the hash should be different.

git log -1 --pretty=format:%h

3b384b1

Your Environment

CMake version 3.13
Windows 7
Visual Studio 2017

@robertodr
Copy link
Collaborator

Thanks for the very detailed report. I followed your steps and I cannot reproduce the problem. The example.cpp is recompiled and the Git hash updated.

@tjwrona
Copy link
Author

tjwrona commented Feb 8, 2019

That is very interesting... I tried it a few times from start to finish with a fresh build directory and the results are consistent. Have you tried in a Windows environment (using Visual Studio, not MSYS) with CMake 3.13?

@tjwrona
Copy link
Author

tjwrona commented Feb 8, 2019

I had previously tried it from my work PC (Windows 7), but I just tried again from my home PC (Windows 10) and got the same result. It may be related to either Visual Studio, or just the newer version of CMake. I am using Visual Studio 2017 and CMake 3.13.3 in both cases.

@robertodr
Copy link
Collaborator

I ran on my local copy of the repository on Linux with CMake 3.12 I'll try in the Docker image after updating to latest CMake and report.

@tjwrona
Copy link
Author

tjwrona commented Feb 8, 2019

Also note that I'm doing agit commit --allow-empty so none of the files are changing in any way with the new commit, not even timestamps. The only thing that changes is a new commit identical to the last was created. It's possible this could have some impact on the way it is acting, although I was under the impression that add_custom_command() would be called every time you do a build regardless of whether anything changes on the file-system.

@robertodr
Copy link
Collaborator

I am sorry, cannot reproduce even in a pristine Linux environment with CMake 3.13 Maybe printing the output and error streams from the execute_process call in git-hash.cmake can shed some additional light?

@tjwrona
Copy link
Author

tjwrona commented Feb 8, 2019

Hmm... It must be Windows related somehow. As far as i can tell executing cmake --build . without making any changes to the project never even calls git-hash.cmake at all. The message "Git hash is xxxxx" is never printed. I even sent build output to a file and did a grep to be sure the message is never printed.

@tjwrona
Copy link
Author

tjwrona commented Feb 8, 2019

Changing add_custom_command to directly reference the get_git_hash custom target seemed to do the trick.

After making the change the CMakeLists.txt file looks like this:

# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

# project name and language
project(recipe-07 LANGUAGES CXX)

# require C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# example code
add_executable(example example.cpp)

# needs to find the generated header file
target_include_directories(example
  PRIVATE
    ${CMAKE_CURRENT_BINARY_DIR}/generated
  )

# rebuild version.hpp every time
add_custom_target(
  get_git_hash
  ALL
  )
  
add_custom_command(
  TARGET
    get_git_hash
  COMMAND
    ${CMAKE_COMMAND} -D TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} -P ${CMAKE_CURRENT_SOURCE_DIR}/git-hash.cmake
  WORKING_DIRECTORY
    ${CMAKE_CURRENT_SOURCE_DIR}
  )

# version.hpp has to be generated
# before we start building example
add_dependencies(example get_git_hash)

Although... if add_custom_command() is supposed to execute at build-time regardless of whether it is linked to a target or not this seems like it may be a bug in the Visual Studio Generator for CMake.

@bast
Copy link
Member

bast commented Feb 9, 2019

I will try to reproduce it on Windows but I need to (re-)create a virtual machine for that so it might take few days. Other thing to try is to build using Ninja. I will hopefully report soon about my findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants