From afaed82084071e9d11663b7ce593fa83e6eabbe5 Mon Sep 17 00:00:00 2001
From: Justus Winter <justus@gnupg.org>
Date: Fri, 19 Feb 2016 14:06:41 +0100
Subject: [PATCH 1/6] Makefile: Drop spurious '$(3)'

* Makefile: Drop spurious '$(3)', likely a left-over from copying
'COMPILE_template'.
---
 Makefile | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/Makefile
+++ b/Makefile
@@ -16,17 +16,7 @@
 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #
 
-INCLUDEOPT:=-I$(CURDIR)/src -I$(CURDIR)/obj \
-            -I$(CURDIR)/src/external/zlib-1.2.8 \
-            -I$(CURDIR)/src/external/md5-1.1.2/src \
-            -I$(CURDIR)/src/external/lua-5.1.5/src \
-            -I$(CURDIR)/src/external/luafilesystem-1.5.0/src \
-            -I$(CURDIR)/src/external/lpeg-0.10.2 \
-            -I$(CURDIR)/src/external/expat-2.0.1/lib \
-            -I$(CURDIR)/src/external/bzip2-1.0.6 \
-            -I$(CURDIR)/src/external/samtools-0.1.18 \
-            -I$(CURDIR)/src/external/sqlite-3.8.7.1 \
-            -I$(CURDIR)/src/external/tre/include/tre
+INCLUDEOPT:=-I$(CURDIR)/src -I$(CURDIR)/obj
 
 ifeq ($(shell pkg-config --version > /dev/null 2> /dev/null; echo $$?),0)
   HAS_PKGCONFIG:=yes
@@ -37,6 +27,48 @@
   HAS_PKGCONFIG:=no
 endif
 
+# add necessary shared lib dependencies instead of building them ourselves
+ifeq ($(useshared),yes)
+  ifeq ($(HAS_PKGCONFIG),yes)
+    DEPLIBS:=-lbz2 \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs zlib) \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs expat) \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs lua-5.1) \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs lua5.1-lpeg) \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs lua5.1-md5) \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs lua5.1-filesystem) \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs lua5.1-des56) \
+             -lbam \
+             $(shell $(OVERRIDE_PC_PATH) pkg-config --libs tre) \
+             -lm -lpthread
+    INCLUDEOPT+=$(shell $(OVERRIDE_PC_PATH) pkg-config --cflags zlib) \
+                $(shell $(OVERRIDE_PC_PATH) pkg-config --cflags expat) \
+                $(shell $(OVERRIDE_PC_PATH) pkg-config --cflags lua-5.1) \
+                $(shell $(OVERRIDE_PC_PATH) pkg-config --cflags lua5.1-lpeg) \
+                $(shell $(OVERRIDE_PC_PATH) pkg-config --cflags lua5.1-md5) \
+                $(shell $(OVERRIDE_PC_PATH) pkg-config --cflags lua5.1-filesystem) \
+                $(shell $(OVERRIDE_PC_PATH) pkg-config --cflags lua5.1-des56) \
+                $(shell $(OVERRIDE_PC_PATH) pkg-config --cflags tre)
+  else
+    # Requested the use of shared libraries without having pkg-config.
+    # You are on your own.  Good luck.
+    DEPLIBS:=-lbz2 -lz -lexpat -llua5.1-lpeg -llua5.1 -llua5.1-md5 \
+             -llua5.1-filesystem -llua5.1-des56 -lbam -ltre -lm -lpthread
+  endif
+else
+  DEPLIBS:=
+  INCLUDEOPT+=-I$(CURDIR)/src/external/zlib-1.2.8 \
+              -I$(CURDIR)/src/external/md5-1.1.2/src \
+              -I$(CURDIR)/src/external/lua-5.1.5/src \
+              -I$(CURDIR)/src/external/luafilesystem-1.5.0/src \
+              -I$(CURDIR)/src/external/lpeg-0.10.2 \
+              -I$(CURDIR)/src/external/expat-2.0.1/lib \
+              -I$(CURDIR)/src/external/bzip2-1.0.6 \
+              -I$(CURDIR)/src/external/samtools-0.1.18 \
+              -I$(CURDIR)/src/external/sqlite-3.8.7.1 \
+              -I$(CURDIR)/src/external/tre/include
+endif
+
 ifneq ($(cairo),no)
   # XXX: check for existence of packages, and emit warning and disable cairo
   # if packages are not available
@@ -125,6 +157,7 @@
 LIBEXPAT_DEP:=$(LIBEXPAT_SRC:%.c=obj/%.d)
 
 TRE_DIR:=src/external/tre/lib
+TRE_CPPFLAGS=-I$(CURDIR)/src/external/tre/include/tre
 LIBTRE_SRC:=$(TRE_DIR)/regcomp.c $(TRE_DIR)/regerror.c $(TRE_DIR)/regexec.c \
             $(TRE_DIR)/tre-ast.c $(TRE_DIR)/tre-compile.c \
             $(TRE_DIR)/tre-filter.c $(TRE_DIR)/tre-match-backtrack.c \
@@ -743,6 +776,15 @@
 	@$(CC) -c $< -o $(@:.o=.d) -DHAVE_MALLOC_USABLE_SIZE $(EXP_CPPFLAGS) \
 	  $(GT_CPPFLAGS) $(3) -MM -MP -MT $@ $(FPIC)
 
+# TRE needs special attention
+obj/$(TRE_DIR)/%.o: $(TRE_DIR)/%.c
+	$(V_ECHO) "[compile $(@F)]"
+	$(V_DO)test -d $(@D) || mkdir -p $(@D)
+	$(V_DO)$(CC) -c $< -o $@ $(EXP_CPPFLAGS) \
+	  $(GT_CPPFLAGS) $(EXP_CFLAGS) $(TRE_CPPFLAGS) $(FPIC)
+	$(V_DO)$(CC) -c $< -o $(@:.o=.d) $(EXP_CPPFLAGS) \
+	  $(GT_CPPFLAGS)  $(TRE_CPPFLAGS) -MM -MP -MT $@ $(FPIC)
+
 define COMPILE_template
 $(1): $(2)
 	@echo "[compile $$(@F)]"
@@ -1001,10 +1043,10 @@
 	src_check src/ltr/*
 
 splintclean:
-	find obj -name '*.splint' | xargs rm -f
+	find obj -name '*.splint' -delete
 
 sbclean:
-	find obj -name '*.sb' | xargs rm -f
+	find obj -name '*.sb' -delete
 
 obj/%.sb: src/match/%.c
 	@echo "scan-build $<"
@@ -1070,7 +1112,7 @@
 headercheck:${ALLHEADER}
 
 headerclean:
-	find obj -name '*.check' | xargs rm -f
+	find obj -name '*.check' -delete
 
 obj/%.check: ${CURDIR}/src/match/%.h
 	@echo "check include $<"
@@ -1139,7 +1181,7 @@
 
 clean:
 	rm -rf lib
-	find obj -name '*.o' |xargs rm -f
+	find obj -name '*.o' -delete
 	rm -f obj/amalgamation.c
 	rm -rf testsuite/stest_testsuite testsuite/stest_stest_tests
 	$(MAKE) -s -C $(CURDIR)/doc/devguide clean
--- a/src/core/grep.c
+++ b/src/core/grep.c
@@ -23,7 +23,7 @@
 #include "core/ma.h"
 #include "core/str.h"
 #include "core/unused_api.h"
-#include "tre.h"
+#include <tre/tre.h>
 
 static void grep_error(int errcode, regex_t *matcher, GtError *err)
 {
--- a/src/extended/rcr.c
+++ b/src/extended/rcr.c
@@ -48,7 +48,7 @@
 #include "extended/sam_alignment.h"
 #include "extended/sam_query_name_iterator.h"
 #include "extended/samfile_iterator.h"
-#include "sam.h"
+#include <samtools/sam.h>
 
 #define BAMBASEA 1
 #define BAMBASEC 2
--- a/src/extended/sam_alignment.c
+++ b/src/extended/sam_alignment.c
@@ -16,7 +16,7 @@
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
-#include <sam.h>
+#include <samtools/sam.h>
 #include "core/alphabet_api.h"
 #include "core/ensure.h"
 #include "core/ma_api.h"
--- a/src/extended/sam_alignment_rep.h
+++ b/src/extended/sam_alignment_rep.h
@@ -23,7 +23,7 @@
 /* The contents of this file is to be considered private implementation detail.
 */
 
-#include <sam.h>
+#include <samtools/sam.h>
 #include "core/alphabet_api.h"
 
 struct GtSamAlignment{
--- a/src/extended/samfile_iterator.c
+++ b/src/extended/samfile_iterator.c
@@ -15,7 +15,7 @@
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
 
-#include <sam.h>
+#include <samtools/sam.h>
 #include "core/cstr_api.h"
 #include "core/ensure.h"
 #include "core/error_api.h"
--- /dev/null
+++ b/src/external/lpeg-0.10.2/lua-lpeg.h
@@ -0,0 +1,39 @@
+/*
+** $Id: lpeg.h,v 1.1 2009/12/23 16:15:36 roberto Exp $
+** LPeg - PEG pattern matching for Lua
+** Copyright 2009, Lua.org & PUC-Rio  (see 'lpeg.html' for license)
+** written by Roberto Ierusalimschy
+*/
+
+#ifndef lpeg_h
+#define lpeg_h
+
+#include "lua.h"
+
+int luaopen_lpeg (lua_State *L);
+
+#define KEYNEWPATT	"lpeg.newpf"
+
+
+/*
+** type of extension functions that define new "patterns" for LPEG
+** It should return the new current position or NULL if match fails
+*/
+typedef const char *(*PattFunc) (const char *s,  /* current position */
+                                 const char *e,  /* string end */
+                                 const char *o,  /* string start */
+                                 const void *ud);  /* user data */
+
+/*
+** function to create new patterns based on 'PattFunc' functions.
+** This function is available at *registry[KEYNEWPATT]. (Notice
+** the extra indirection; the userdata at the registry points to
+** a variable that points to the function. In ANSI C a void* cannot
+** point to a function.)
+*/
+typedef void (*Newpf) (lua_State *L,
+                       PattFunc f,  /* pattern */
+                       const void *ud,  /* (user) data to be passed to 'f' */
+                       size_t l);  /* size of data to be passed to 'f' */
+
+#endif
--- a/src/gtr.c
+++ b/src/gtr.c
@@ -24,7 +24,7 @@
 #include "lauxlib.h"
 #include "lualib.h"
 #include "lfs.h"
-#include "lpeg.h"
+#include "lua-lpeg.h"
 #include "md5.h"
 #include "ldes56.h"
 #include "core/compat.h"
