# This patch applies the following fixes:
#  * allow CXXFLAGS addjustment
#  * generate and install man page
#  * build with system's tinyxml2 library
# Copyright (C) 2017 Georgi D. Sotirov <gdsotirov@dir.bg>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# $Id: cppcheck-1.80.diff,v 1.1 2017/10/13 17:13:31 gsotirov Exp $
#
diff -urNad cppcheck-1.80-orig/Makefile cppcheck-1.80/Makefile
--- cppcheck-1.80-orig/Makefile	2017-07-29 14:25:47.000000000 +0300
+++ cppcheck-1.80/Makefile	2017-10-13 14:56:23.896689655 +0300
@@ -81,6 +81,8 @@
 endif
 ifndef CXXFLAGS
     CXXFLAGS=-std=c++0x -O2 -include lib/cxx11emu.h -DNDEBUG -Wall -Wno-sign-compare
+else
+    CXXFLAGS+=-std=c++0x -O2 -include lib/cxx11emu.h -DNDEBUG -Wall -Wno-sign-compare
 endif
 
 ifeq (g++, $(findstring g++,$(CXX)))
@@ -107,22 +109,24 @@
 endif
 
 ifndef INCLUDE_FOR_LIB
-    INCLUDE_FOR_LIB=-Ilib -Iexternals/simplecpp -Iexternals/tinyxml
+    INCLUDE_FOR_LIB=-Ilib -Iexternals/simplecpp
 endif
 
 ifndef INCLUDE_FOR_CLI
-    INCLUDE_FOR_CLI=-Ilib -Iexternals/simplecpp -Iexternals/tinyxml
+    INCLUDE_FOR_CLI=-Ilib -Iexternals/simplecpp
 endif
 
 ifndef INCLUDE_FOR_TEST
-    INCLUDE_FOR_TEST=-Ilib -Icli -Iexternals/simplecpp -Iexternals/tinyxml
+    INCLUDE_FOR_TEST=-Ilib -Icli -Iexternals/simplecpp
 endif
 
-BIN=$(DESTDIR)$(PREFIX)/bin
+BIN=$(PREFIX)/bin
+MAN=$(PREFIX)/man
+ADDON=$(PREFIX)/share/cppcheck/addons
 
 # For 'make man': sudo apt-get install xsltproc docbook-xsl docbook-xml on Linux
-DB2MAN?=/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl
-XP=xsltproc -''-nonet -''-param man.charmap.use.subset "0"
+DB2MAN?=/etc/asciidoc/docbook-xsl/manpage.xsl
+XP=xsltproc -''-nonet -''-param man.charmap.use.subset "0" --output man/
 MAN_SOURCE=man/cppcheck.1.xml
 
 
@@ -175,8 +179,7 @@
               $(SRCDIR)/tokenlist.o \
               $(SRCDIR)/valueflow.o
 
-EXTOBJ =      externals/simplecpp/simplecpp.o \
-              externals/tinyxml/tinyxml2.o
+EXTOBJ =      externals/simplecpp/simplecpp.o
 
 CLIOBJ =      cli/cmdlineparser.o \
               cli/cppcheckexecutor.o \
@@ -243,6 +246,8 @@
               test/testvalueflow.o \
               test/testvarid.o
 
+LIBS += -ltinyxml2
+
 .PHONY: run-dmake tags
 
 
@@ -251,7 +256,7 @@
 cppcheck: $(LIBOBJ) $(CLIOBJ) $(EXTOBJ)
 	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $(CLIOBJ) $(LIBOBJ) $(EXTOBJ) $(LIBS) $(LDFLAGS) $(RDYNAMIC)
 
-all:	cppcheck testrunner
+all:	cppcheck testrunner man
 
 testrunner: $(TESTOBJ) $(LIBOBJ) $(EXTOBJ) cli/threadexecutor.o cli/cmdlineparser.o cli/cppcheckexecutor.o cli/filelister.o
 	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $(TESTOBJ) $(LIBOBJ) cli/threadexecutor.o cli/cppcheckexecutor.o cli/cmdlineparser.o cli/filelister.o $(EXTOBJ) $(LIBS) $(LDFLAGS) $(RDYNAMIC)
@@ -286,16 +291,19 @@
 tags:
 	ctags -R --exclude=doxyoutput --exclude=test/cfg cli externals gui lib test
 
-install: cppcheck
-	install -d ${BIN}
-	install cppcheck ${BIN}
-	install addons/*.py ${BIN}
-	install addons/*/*.py ${BIN}
-	install htmlreport/cppcheck-htmlreport ${BIN}
+install: cppcheck man/cppcheck.1
+	install -d ${DESTDIR}${BIN}
+	install cppcheck ${DESTDIR}${BIN}
+	install -d ${DESTDIR}${ADDON}
+	install addons/*.py ${DESTDIR}${ADDON}
+	install addons/*/*.py ${DESTDIR}${ADDON}
+	install htmlreport/cppcheck-htmlreport ${DESTDIR}${BIN}
 ifdef CFGDIR 
 	install -d ${DESTDIR}${CFGDIR}
 	install -m 644 cfg/* ${DESTDIR}${CFGDIR}
 endif
+	install -d ${DESTDIR}${MAN}/man1
+	install man/cppcheck.1 ${DESTDIR}${MAN}/man1/cppcheck.1
 
 # Validation of library files:
 ConfigFiles := $(wildcard cfg/*.cfg)
