From bf0cd9a0b72047cb7104316bcaaf7687e3d385c3 Mon Sep 17 00:00:00 2001
From: Benoit Rat <benoit@sevensols.com>
Date: Fri, 23 Mar 2012 13:42:57 +0100
Subject: [PATCH 3/7] build: Add gitversion to binary, and a script to compile
 DF & NF

---
 .gitignore |    4 ++++
 Makefile   |   25 ++++++++++++++++++++++++-
 build.sh   |   21 +++++++++++++++++++++
 main.c     |    6 ++++++
 4 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100755 build.sh

diff --git a/.gitignore b/.gitignore
index d78652e..69a0676 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,8 @@ config/zconf.tab.o
 config/.depend
 .config.cmd
 .config.old
+.cproject
+.project
 .auto.deps
 build
 result
@@ -21,3 +23,5 @@ binaries
 *.o
 tags
 *.swp
+.tmpconfig*
+version.c
diff --git a/Makefile b/Makefile
index 6216632..88dfffc 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
 
+## Git revision can refer to the at91bootstrap repo itself (in this case .git exist) or to a parent repo (wr-switch-sw) that we used with patch (In this case we need to pass git value using exported variable). 
+ifneq (.git,$(wildcard .git))
+version.c: $(SOBJS-y) $(COBJS-y)
+	@echo "/**" > $@ 
+	@echo " * File automatically generated by Makefile (DO NOT MODIFY)\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 " * GIT values given by exported shell variable\n**/" >> $@   
+	@echo 'const char build_time[] = __DATE__ " @ " __TIME__ ;' >> $@
+	@echo "const char git_user[] = \"$(shell echo ${WRS_GIT_USER})\";" >> $@
+	@echo "const char git_revision[] = \"$(shell echo ${WRS_GIT_REV})\";" >> $@
+	@echo "" >> $@
+else
+version.c: $(SOBJS-y) $(COBJS-y) .git/HEAD .git/index Makefile
+	@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 describe --always --dirty=+)\";" >> $@
+	@echo "" >> $@
+endif
+
 PrintFlags:
 	@echo as FLAGS
 	@echo ========
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..a9711d9
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+showhelp()
+{
+	echo "Usage: $0 [options]"
+	echo "options:"
+	echo "		--help: show this little help"
+	echo "		--df: compile only for dataflash"
+	echo "		--nf: compile only for nandflash"
+}
+
+
+
+case "$1" in
+	--help) showhelp;; 
+	--nf) 	yes "" | make at91sam9g45nf_defconfig > /dev/null; make;;
+	--df) 	yes "" | make at91sam9g45df_defconfig > /dev/null; make;;
+	*) 	yes "" | make at91sam9g45df_defconfig > /dev/null; make; yes "" | make at91sam9g45nf_defconfig > /dev/null; make;;
+esac
+
+
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

