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 ""
diff --git a/contributing.md b/contributing.md
index b81766c..01a90f8 100644
--- a/contributing.md
+++ b/contributing.md
@@ -46,8 +46,11 @@
 
 *   Make commits of logical units. See these general [Git guidelines] for
     contributing to a project.
-*   Follow the [Linux coding style]; this style is re-used for the ARM Trusted
-    Firmware project.
+*   Follow the [Linux coding style]; this style is enforced for the ARM Trusted
+    Firmware project (style errors only, not warnings).
+    *   Use the checkpatch.pl script provided with the Linux source tree. A
+        Makefile target is provided for convenience (see section 2 in the
+        [User Guide]).
 *   Keep the commits on topic. If you need to fix another bug or make another
     enhancement, please create a separate [issue] and address it on a separate
     topic branch.
diff --git a/docs/user-guide.md b/docs/user-guide.md
index 0358e07..a0dbae7 100644
--- a/docs/user-guide.md
+++ b/docs/user-guide.md
@@ -141,6 +141,29 @@
 NOTE: The Foundation FVP does not provide a debugger interface.
 
 
+#### Checking source code style
+
+When making changes to the source for submission to the project, the source
+must be in compliance with the Linux style guide, and to assist with this check
+the project Makefile contains two targets, which both utilise the checkpatch.pl
+script that ships with the Linux source tree.
+
+To check the entire source tree, you must first download a copy of checkpatch.pl
+(or the full Linux source), set the CHECKPATCH environment variable to point to
+the script and build the target checkcodebase:
+
+    make CHECKPATCH=../linux/scripts/checkpatch.pl checkcodebase
+
+To just check the style on the files that differ between your local branch and
+the remote master, use:
+
+    make CHECKPATCH=../linux/scripts/checkpatch.pl checkpatch
+
+If you wish to check your patch against something other than the remote master,
+set the BASE_COMMIT variable to your desired branch.  By default, BASE_COMMIT
+is set to 'origin/master'.
+
+
 ### Obtaining the normal world software
 
 #### Obtaining UEFI