From c28fc11363fb86c25b447817a6554874ad65f09a Mon Sep 17 00:00:00 2001
From: Benoit Rat <benoit@sevensols.com>
Date: Wed, 28 Mar 2012 18:54:06 +0200
Subject: [PATCH 05/19] version: Improve tracking bin versions adding git
 version in Makefile

---
 .gitignore |    1 +
 Makefile   |   25 ++++++++++++++++++++++++-
 main.c     |    6 ++++++
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index a973a59..8b91fe5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,3 +22,4 @@ binaries
 tags
 *.swp
 .tmpconfig*
+version.c
diff --git a/Makefile b/Makefile
index 6216632..c61ee3e 100644
--- a/Makefile
+++ b/Makefile
@@ -220,7 +220,7 @@ include		driver/driver.mk
 
 SRCS	:= $(COBJS-y:.o=.c)
 
-OBJS	:= $(SOBJS-y) $(COBJS-y)
+OBJS	:= $(SOBJS-y) $(COBJS-y) version.o
 
 INCL=board/$(BOARD)
 
@@ -273,6 +273,29 @@ PHONY:=all gen_bin
 
 all: PrintFlags gen_bin ChkFileSize
 
+## If not git is found
+ifeq ($(shell  git status -s | grep -v "fatal*"),)
+version.c: $(SOBJS-y) $(COBJS-y)
+	@echo "/**" > $@ 
+	@echo " * File automatically generated by Makefile (DO NOT MODIFIED)\n *\n * To use this you in a c code just add the following lines:\n * " >> $@
+	@echo "\textern const char build_time[];\n\textern const char git_user[];\n\textren const char git_revision[];\n * " >> $@ 
+	@echo "**/" >> $@   
+	@echo 'const char build_time[] = __DATE__ " @ " __TIME__ ;' >> $@
+	@echo "const char git_user[] = \"$(shell id -nu)\";" >> $@
+	@echo "const char git_revision[] = \"\";" >> $@
+	@echo "" >> $@
+else
+version.c: $(SOBJS-y) $(COBJS-y) .git/HEAD .git/index
+	@echo "/**" > $@ 
+	@echo " * File automatically generated by Makefile (DO NOT MODIFIED)\n *\n * To use this you in a c code just add the following lines:\n * " >> $@
+	@echo "\textern const char build_time[];\n\textern const char git_user[];\n\textren const char git_revision[];\n * " >> $@ 
+	@echo "**/" >> $@   
+	@echo 'const char build_time[] = __DATE__ " @ " __TIME__ ;' >> $@
+	@echo "const char git_user[] = \"$(shell git config --get user.name)\";" >> $@
+	@echo "const char git_revision[] = \"$(shell git rev-parse HEAD)$(shell if git status -s > /dev/null; then echo '+'; fi;)\";" >> $@
+	@echo "" >> $@
+endif
+
 PrintFlags:
 	@echo as FLAGS
 	@echo ========
diff --git a/main.c b/main.c
index 8810324..0ea1716 100644
--- a/main.c
+++ b/main.c
@@ -83,6 +83,10 @@ void Wait(unsigned int count)
 /*------------------------------------------------------------------------------*/
 int main(void)
 {
+    extern const char build_time[];
+    extern const char git_user[];
+    extern const char git_revision[];
+  
     /*
      * ================== 1st step: Hardware Initialization ================= 
      *
@@ -92,6 +96,8 @@ int main(void)
     hw_init();
 #endif
 
+    pp_printf("Compiled by %s (%s)\r\ngit rev:%s\r\n\r\n",git_user,build_time,git_revision);
+    
 #ifdef CONFIG_USER_HW_INIT
     user_hw_init();
 #endif
-- 
1.7.9.5

