From 04a0a30412805d853e3f7e898852ebd537ccc91c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
Date: Wed, 1 Apr 2015 12:42:51 +0200
Subject: [PATCH] Use system YAJL library if possible
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If standalone YAJL library is installed in the system, then it is used
instead of the bundled sources.

Signed-off-by: Petr Písař <ppisar@redhat.com>
---
 build/autotools/FindDependencies.m4        | 3 +++
 build/autotools/PrintBuildConfiguration.m4 | 1 +
 src/Makefile.am                            | 6 +++++-
 src/yajl/yajl_alloc.h                      | 4 ++++
 src/yajl/yajl_buf.h                        | 4 ++++
 src/yajl/yajl_bytestack.h                  | 4 ++++
 src/yajl/yajl_lex.h                        | 4 ++++
 src/yajl/yajl_parser.h                     | 4 ++++
 8 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/build/autotools/FindDependencies.m4 b/build/autotools/FindDependencies.m4
index 10b49a2..739a279 100644
--- a/build/autotools/FindDependencies.m4
+++ b/build/autotools/FindDependencies.m4
@@ -10,6 +10,9 @@ AC_CHECK_FUNC(snprintf, [AC_SUBST(BSON_HAVE_SNPRINTF, 1)])
 AC_SUBST(BSON_HAVE_CLOCK_GETTIME, 0)
 AC_SEARCH_LIBS([clock_gettime], [rt], [AC_SUBST(BSON_HAVE_CLOCK_GETTIME, 1)])
 
+# Prefer system YAJL
+AC_CHECK_LIB([yajl], [yajl_alloc])
+AM_CONDITIONAL([HAVE_LIBYAJL], [test "$ac_cv_lib_yajl_yajl_alloc" != "no"])
 
 # Check for pthreads. We might need to make this better to handle mingw,
 # but I actually think it is okay to just check for it even though we will
diff --git a/build/autotools/PrintBuildConfiguration.m4 b/build/autotools/PrintBuildConfiguration.m4
index f494bcf..ded1ba3 100644
--- a/build/autotools/PrintBuildConfiguration.m4
+++ b/build/autotools/PrintBuildConfiguration.m4
@@ -34,6 +34,7 @@ Build configuration:
   Cross Compiling                                  : ${enable_crosscompile}
   Big endian                                       : ${enable_bigendian}
   Link Time Optimization (experimental)            : ${enable_lto}
+  Link to system YAJL library                      : ${ac_cv_lib_yajl_yajl_alloc}
 
 Documentation:
   man                                              : ${enable_man_pages}
diff --git a/src/Makefile.am b/src/Makefile.am
index 9355d06..a95049c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,8 +3,10 @@ lib_LTLIBRARIES += libbson-1.0.la
 libbson_1_0_la_SOURCES =
 libbson_1_0_la_LIBADD = \
 	libbson.la \
-	libyajl.la \
 	-lm
+if !HAVE_LIBYAJL
+libbson_1_0_la_LIBADD += libyajl.la
+endif
 libbson_1_0_la_LDFLAGS = \
 	$(OPTIMIZE_LDFLAGS) \
 	$(BSON_LT_LDFLAGS) \
@@ -18,7 +20,9 @@ libbson_1_0_la_LDFLAGS += -export-symbols-regex "^(bson_|bcon_)"
 endif
 
 include src/bson/Makefile.am
+if !HAVE_LIBYAJL
 include src/yajl/Makefile.am
+endif
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = $(top_builddir)/src/libbson-1.0.pc
diff --git a/src/yajl/yajl_alloc.h b/src/yajl/yajl_alloc.h
index 963d8b0..6512c6f 100644
--- a/src/yajl/yajl_alloc.h
+++ b/src/yajl/yajl_alloc.h
@@ -23,7 +23,11 @@
 #ifndef __YAJL_ALLOC_H__
 #define __YAJL_ALLOC_H__
 
+#ifdef HAVE_LIBYAJL
+#include <yajl/yajl_common.h>
+#else
 #include "yajl_common.h"
+#endif
 
 #define YA_MALLOC(afs, sz) (afs)->malloc((afs)->ctx, (sz))
 #define YA_FREE(afs, ptr) (afs)->free((afs)->ctx, (ptr))
diff --git a/src/yajl/yajl_buf.h b/src/yajl/yajl_buf.h
index 0ae73aa..a39e180 100644
--- a/src/yajl/yajl_buf.h
+++ b/src/yajl/yajl_buf.h
@@ -17,7 +17,11 @@
 #ifndef __YAJL_BUF_H__
 #define __YAJL_BUF_H__
 
+#ifdef HAVE_LIBYAJL
+#include <yajl/yajl_common.h>
+#else
 #include "yajl_common.h"
+#endif
 #include "yajl_alloc.h"
 
 /*
diff --git a/src/yajl/yajl_bytestack.h b/src/yajl/yajl_bytestack.h
index d75e456..b712964 100644
--- a/src/yajl/yajl_bytestack.h
+++ b/src/yajl/yajl_bytestack.h
@@ -22,7 +22,11 @@
 #ifndef __YAJL_BYTESTACK_H__
 #define __YAJL_BYTESTACK_H__
 
+#ifdef HAVE_LIBYAJL
+#else
+#include <yajl/yajl_common.h>
 #include "yajl_common.h"
+#endif
 
 #define YAJL_BS_INC 128
 
diff --git a/src/yajl/yajl_lex.h b/src/yajl/yajl_lex.h
index 40c4344..f6180f4 100644
--- a/src/yajl/yajl_lex.h
+++ b/src/yajl/yajl_lex.h
@@ -17,7 +17,11 @@
 #ifndef __YAJL_LEX_H__
 #define __YAJL_LEX_H__
 
+#ifdef HAVE_LIBYAJL
+#include <yajl/yajl_common.h>
+#else
 #include "yajl_common.h"
+#endif
 
 typedef enum {
     yajl_tok_bool,         
diff --git a/src/yajl/yajl_parser.h b/src/yajl/yajl_parser.h
index 2252848..04272ea 100644
--- a/src/yajl/yajl_parser.h
+++ b/src/yajl/yajl_parser.h
@@ -19,7 +19,11 @@
 
 #include "bson-compat.h"
 
+#ifdef HAVE_LIBYAJL
+#include <yajl/yajl_parse.h>
+#else
 #include "yajl_parse.h"
+#endif
 #include "yajl_bytestack.h"
 #include "yajl_buf.h"
 #include "yajl_lex.h"
-- 
2.1.0

