avsync-lib: Remove build result from source folder [1/2]

PD#SWPL-150465

Problem:
Remove build result from git folder to build result folder

Solution:
add output folder from bb to Makefile,
1.set the make output files(.o, .so) to output folder
2.Move aml_version.h file to output folder
3.avoid change aml_version.h if content is the same as before
4.if not call version_config.sh, create a aml_version.h in Makefile

Verify:
local

Signed-off-by: guoping.li <guoping.li@amlogic.com>
Change-Id: I7628deeee838c57e3434beb54ab3bad5e0d42e07
diff --git a/src/Makefile b/src/Makefile
index ce16faa..dd8d8ab 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -4,6 +4,9 @@
 TEST = avsync_test
 PCR_TEST = pcr_test
 
+OUT_DIR ?= .
+$(info "OUT_DIR : $(OUT_DIR)")
+CC_FLAG += -I$(OUT_DIR)
 #BUILD_TEST = yes
 
 # rules
@@ -27,28 +30,40 @@
 CC_FLAG += -DENABLE_SYSLOG
 endif
 
+VERSION_FILE := ${OUT_DIR}/aml_version.h
+
+# If the version file is not exist, use the original file
+ifeq ($(wildcard $(VERSION_FILE)),)
+    $(shell cp ./aml_version.h.in ${VERSION_FILE})
+endif
+
+
+
 $(TARGET): $(OBJ)
-	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib $(LD_FLAG) $(OBJ) -shared -fPIC -o $@
+	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib $(LD_FLAG) $(OBJ) -shared -Wl,-soname,$(TARGET) -fPIC -o $(OUT_DIR)/$@
 
 $(TEST): $(TARGET) test.c
 	cp $(TARGET) $(STAGING_DIR)/usr/lib/
-	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lamlavsync test.c -o $@
+	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lamlavsync test.c -o $(OUT_DIR)/$@
 
 $(PCR_TEST): $(TARGET) pcr_test.c
 	cp $(TARGET) $(STAGING_DIR)/usr/lib/
-	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lpthread -lamlavsync pcr_test.c -o $@
+	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lpthread -lamlavsync pcr_test.c -o $(OUT_DIR)/$@
 
 .PHONY: clean
 
 clean:
-	rm -f *.o $(TARGET) $(TEST)
+	rm -f *.o $(OUT_DIR)/$(TARGET) $(OUT_DIR)/$(TEST) $(OUT_DIR)/$(PCR_TEST)
+	rm ${OUT_DIR}/aml_version.h
 
 install:
 	cp aml_avsync_log.h $(STAGING_DIR)/usr/include/
 	cp aml_avsync.h $(STAGING_DIR)/usr/include/
 	cp aml_queue.h $(STAGING_DIR)/usr/include/
-	cp $(TARGET) $(TARGET_DIR)/usr/lib/
+	cp $(OUT_DIR)/$(TARGET) $(TARGET_DIR)/usr/lib/
 ifeq ($(BUILD_TEST), yes)
-	cp $(TEST) $(TARGET_DIR)/usr/bin/
-	cp $(PCR_TEST) $(TARGET_DIR)/usr/bin/
+	cp $(OUT_DIR)/$(TEST) $(TARGET_DIR)/usr/bin/
+	cp $(OUT_DIR)/$(PCR_TEST) $(TARGET_DIR)/usr/bin/
 endif
+
+$(shell mkdir -p $(OUT_DIR))
\ No newline at end of file
diff --git a/src/aml_version.h b/src/aml_version.h.in
similarity index 99%
rename from src/aml_version.h
rename to src/aml_version.h.in
index 915db15..c2d8974 100644
--- a/src/aml_version.h
+++ b/src/aml_version.h.in
@@ -19,4 +19,4 @@
 #ifdef  __cplusplus
 }
 #endif
-#endif /*__AML_VERSION_H__*/
\ No newline at end of file
+#endif /*__AML_VERSION_H__*/
diff --git a/version_config.sh b/version_config.sh
index 233a5ae..e3a836a 100755
--- a/version_config.sh
+++ b/version_config.sh
@@ -2,6 +2,12 @@
 
 #version rule:MAJORVERSION.MINORVERSION.COMMIT_COUNT-g(COMMIT_ID)
 
+OUT_DIR=
+if [ $# == 1 ];then
+    OUT_DIR=$1/
+    mkdir -p "${OUT_DIR}src"
+fi
+
 BASE=$(pwd)
 echo $BASE
 
@@ -24,8 +30,9 @@
 COMMIT_ID=$(git rev-parse --short HEAD)
 echo commit id $COMMIT_ID
 
+cp "src/aml_version.h.in" "${OUT_DIR}src/aml_version.h.tmp"
 #find the module name line
-MODULE_NAME_LINE=`sed -n '/\"MM-module-name/=' src/aml_version.h`
+MODULE_NAME_LINE=`sed -n '/\"MM-module-name/=' ${OUT_DIR}src/aml_version.h.tmp`
 #echo $VERSION_LINE
 
 #version rule string
@@ -33,5 +40,22 @@
 
 #update the original version
 if [ ${MODULE_NAME_LINE} -gt 0 ]; then
-sed -i -e ${MODULE_NAME_LINE}s"/.*/\"${MODULE_NAME},version:${VERSION_STRING}\"\;/" src/aml_version.h
+sed -i -e ${MODULE_NAME_LINE}s"/.*/\"${MODULE_NAME},version:${VERSION_STRING}\"\;/" ${OUT_DIR}src/aml_version.h.tmp
+fi
+
+#if version.h already exist, compare the content, if it's the same, using the original file, avoid rebuild
+if [ -e "${OUT_DIR}src/aml_version.h" ]; then
+	file1_hash=$(md5sum ${OUT_DIR}src/aml_version.h | awk '{print $1}')
+	file2_hash=$(md5sum ${OUT_DIR}src/aml_version.h.tmp | awk '{print $1}')
+
+	if [ "$file1_hash" == "$file2_hash" ]; then
+		echo "version file content is the same"
+		rm ${OUT_DIR}src/aml_version.h.tmp
+	else
+		echo "version file content is not the same, use the tmp file"
+		mv ${OUT_DIR}src/aml_version.h.tmp ${OUT_DIR}src/aml_version.h
+	fi
+else
+	echo "no version file, use the tmp version file"
+	mv ${OUT_DIR}src/aml_version.h.tmp ${OUT_DIR}src/aml_version.h
 fi