4

I'm working with C++ in Visual Studio 2015 and using Git as source control. I'm writing a multi-platform application and would like my application to include the Git branch and the commit it was built from.

On platforms other than Visual Studio I put the following lines in my makefile:

GIT_CUR_COMMIT := $(shell git rev-parse --verify HEAD)
GIT_CUR_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
DEPS += -DGIT_COMMIT=\"$(GIT_CUR_COMMIT)\" -DGIT_BRANCH=\"$(GIT_CUR_BRANCH)\"

And then in my application I have code to read these preprocessor variables and expose them programmatically:

std::string getGitCommit()
{
    #ifdef GIT_COMMIT
    return GIT_COMMIT;
    #endif
    return "unavailable";
}

std::string getGitBranch()
{
    #ifdef GIT_BRANCH
    return GIT_BRANCH;
    #endif
    return "unavailable";   
}

But in Visual Studio there are no makefiles. How can I define these two variables (GIT_CUR_COMMIT and GIT_CUR_BRANCH) at compile time and achieve the same behavior?

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
seladb
  • 852
  • 1
  • 13
  • 29
  • I build using makefiles in Visual Studio. So "there are no makefiles" is not a truism, but rather how you choose to build with Visual Studio. I assume you are using vcproj based building? – Yakk - Adam Nevraumont Sep 27 '17 at 11:16
  • Yes, I'm sorry for not making this clear. I'm using vcproj based building – seladb Sep 27 '17 at 11:19

1 Answers1

4

Probably - you can do it exactly same way in VS - But I feel the following procedure would be a lot easier to implement

In the project properties select the "Build Events" / "Pre-Build Event"

Add following commands to the Command Line

echo | set /p dummyName=#define GIT_CUR_COMMIT >  gitparams.h
git rev-parse --verify HEAD >>  gitparams.h

echo | set /p dummyName=#define GIT_BRANCH >>  gitparams.h
git rev-parse --abbrev-ref HEAD >>  gitparams.h

This should generate the following header file (gitparams.h) each time you compile your project

#define GIT_CUR_COMMIT 8a6b3f147fababda57ddd39262df5aa83bc853c4
#define GIT_BRANCH master

Make sure you add it to .gitignore

And in your code you can use

#include "gitparams.h"

#define ADD_QUOTES_HELPER(s) #s
#define ADD_QUOTES(s) ADD_QUOTES_HELPER(s)

std::string getGitCommit()
{
#ifdef GIT_CURR_COMMIT
    return ADD_QUOTES(GIT_CURR_COMMIT);
#endif
    return "unavailable";
}
Artemy Vysotsky
  • 2,694
  • 11
  • 20
  • Since I'm writing a cross-platform application I'd like my code to be as similar as possible on different platforms. You mentioned there is a way to do exactly what I did in VS. Could you please elaborate what it is? – seladb Sep 27 '17 at 12:49
  • You probably could set some environment vars in the prebuild step and use it in your compilation step. But why not to create a crossplatform makefile that will generate the gitparams.h for you - so your code will be the same for all platforms? – Artemy Vysotsky Sep 27 '17 at 12:54
  • I could do that, but my code already works with environment variables and I prefer not to change that if possible. Could you write down the full solution with environment variables? – seladb Sep 27 '17 at 13:00
  • I really see no difference between macros set by #define or by -D compilation flag in regards to your code. But if you still want to investigate the environment varible approach see this https://stackoverflow.com/questions/419979/setting-environment-variables-in-pre-build-event-and-using-in-compilation-step – Artemy Vysotsky Sep 27 '17 at 13:08
  • I think that the approach of writing new files like "gitparams.h" is not as clean as defining a preprocessor variable in a Makefile. Anyway, I followed that link you sent and the conclusion there is that it's not possible without installing add-ins or doing really complicated tweaks. I found it surprising there isn't a simple solution for it – seladb Sep 27 '17 at 19:21
  • You can always switch to makefile project in VS as well to have same functionality as in linux – Artemy Vysotsky Sep 27 '17 at 19:35
  • That's may be a question for another thread - but are VS Makefiles fully compatible to GNU Makefiles? – seladb Sep 27 '17 at 20:03