Allow style checking of tree and local changes

New phony Makefile targets have been added:

 * checkcodebase
 * checkpatch

The checkcodebase target will run a Linux style compliance check over the
entire codebase, and honours the V=1 Makefile verbose setting and so will
show more information when this is enabled.

If the local directory is a git checkout then the output of git ls-files is
used to decide which files to test for compliance.  If the local directory
is not under git control then a 'best attempt' is made, but in this case it
should be noted that it is possible for additional non-codebase files to be
tested, so care should be taken when parsing the output.

The checkpatch target will compare local changes against the git origin/master
to allow issues with the last set of changes to be identified.  To override
the change comparision location, set the BASE_COMMIT variable to your
desired git branch.

Both targets rely on the Linux source tree script checkpatch.pl to do the
syntax checking, and expects that the CHECKPATCH environment variable points
to the location of this file.

Notes on the usage of these targets have been added to the contributing.md
and docs/user-guide.md text files.

Change-Id: I6d73c97af578e24a34226d972afadab9d30f1d8d
diff --git a/Makefile b/Makefile
index 197e01b..b48fbf5 100644
--- a/Makefile
+++ b/Makefile
@@ -36,8 +36,12 @@
   KBUILD_VERBOSE = 0
 endif
 
+CHECKPATCH_ARGS		=	--no-tree --no-signoff
+CHECKCODE_ARGS		=	--no-patch --no-tree --no-signoff
+
 ifeq "${KBUILD_VERBOSE}" "0"
 	Q=@
+	CHECKCODE_ARGS	+=	--no-summary --terse
 else
 	Q=
 endif
@@ -102,7 +106,7 @@
   include bl31/bl31.mk
 endif
 
-.PHONY:			all msg_start ${PLATFORMS} dump clean realclean distclean bl1 bl2 bl31 cscope
+.PHONY:			all msg_start ${PLATFORMS} dump clean realclean distclean bl1 bl2 bl31 cscope locate-checkpatch checkcodebase checkpatch
 .SUFFIXES:
 
 
@@ -167,11 +171,23 @@
 bl2:			${BUILD_BL2} ${BUILD_PLAT}/bl2.bin
 bl31:			${BUILD_BL31} ${BUILD_PLAT}/bl31.bin
 
+BASE_COMMIT		?=	origin/master
+
 ifeq (${PLAT},all)
   ifeq (${MAKECMDGOALS},clean)
     $(error "Please select a platform with PLAT=<platform>. You can use 'make distclean' to clean up all platform builds")
   endif
 endif
+
+locate-checkpatch:
+ifndef CHECKPATCH
+	$(error "Please set CHECKPATCH to point to the Linux checkpatch.pl file, eg: CHECKPATCH=../linux/script/checkpatch.pl")
+else
+ifeq (,$(wildcard ${CHECKPATCH}))
+	$(error "The file CHECKPATCH points to cannot be found, use eg: CHECKPATCH=../linux/script/checkpatch.pl")
+endif
+endif
+
 clean:
 			@echo "  CLEAN"
 			${Q}rm -rf ${BUILD_PLAT}
@@ -187,6 +203,18 @@
 			${Q}${OD} -d ${BUILD_BL2}/bl2.elf > ${BUILD_BL2}/bl2.dump
 			${Q}${OD} -d ${BUILD_BL31}/bl31.elf > ${BUILD_BL31}/bl31.dump
 
+checkcodebase:		locate-checkpatch
+			@echo "  CHECKING STYLE"
+			@if test -d .git ; then	\
+				git ls-files | while read GIT_FILE ; do ${CHECKPATCH} ${CHECKCODE_ARGS} -f $$GIT_FILE ; done ;	\
+			 else			\
+				 find . -type f -not -iwholename "*.git*" -not -iwholename "*build*" -exec ${CHECKPATCH} ${CHECKCODE_ARGS} -f {} \; ;	\
+			 fi
+
+checkpatch:		locate-checkpatch
+			@echo "  CHECKING STYLE"
+			@git format-patch --stdout ${BASE_COMMIT} | ${CHECKPATCH} ${CHECKPATCH_ARGS} - || true
+
 ${BUILD_DIRS}:
 			${Q}mkdir -p "$@"
 
@@ -267,19 +295,22 @@
 	${Q}cscope -b -q -k
 
 help:
-	@echo "usage: ${MAKE} PLAT=<all|${HELP_PLATFORMS}> <all|bl1|bl2|bl31|distclean|clean|dump>"
+	@echo "usage: ${MAKE} PLAT=<all|${HELP_PLATFORMS}> <all|bl1|bl2|bl31|distclean|clean|checkcodebase|checkpatch|dump>"
 	@echo ""
 	@echo "PLAT is used to specify which platform you wish to build."
 	@echo ""
 	@echo "Supported Targets:"
-	@echo "  all        build the BL1, BL2 and BL31 binaries"
-	@echo "  bl1        build the BL1 binary"
-	@echo "  bl2        build the BL2 binary"
-	@echo "  bl31       build the BL31 binary"
-	@echo "  clean      Clean the build for the selected platform"
+	@echo "  all            Build the BL1, BL2 and BL31 binaries"
+	@echo "  bl1            Build the BL1 binary"
+	@echo "  bl2            Build the BL2 binary"
+	@echo "  bl31           Build the BL31 binary"
+	@echo "  checkcodebase  Check the coding style of the entire source tree"
+	@echo "  checkpatch     Check the coding style on changes in the current"
+	@echo "                 branch against BASE_COMMIT (default origin/master)"
+	@echo "  clean          Clean the build for the selected platform"
 	@echo "  cscope     Generate cscope index"
-	@echo "  distclean  Remove all build artifacts for all platforms"
-	@echo "  dump       Generate object file dumps"
+	@echo "  distclean      Remove all build artifacts for all platforms"
+	@echo "  dump           Generate object file dumps"
 	@echo ""
 	@echo "note: most build targets require PLAT to be set to a specific platform."
 	@echo ""