From: Faidon Liambotis <paravoid@debian.org>
Date: Wed, 3 Sep 2025 17:26:39 +0300
Subject: Add --enable-cjson-ext option to build with an external libcjson

Bug: https://github.com/confluentinc/librdkafka/issues/5186
---
 configure.self                      | 8 ++++++++
 src/Makefile                        | 6 +++++-
 src/rdhttp.h                        | 4 ++++
 src/rdkafka.c                       | 4 ++++
 src/rdkafka_sasl_oauthbearer_oidc.c | 4 ++++
 5 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/configure.self b/configure.self
index 6912783..45c7bdb 100644
--- a/configure.self
+++ b/configure.self
@@ -39,6 +39,8 @@ mkl_toggle_option "Feature" ENABLE_LZ4_EXT "--enable-lz4" "Deprecated: alias for
 
 mkl_toggle_option "Feature" ENABLE_REGEX_EXT "--enable-regex-ext" "Enable external (libc) regex (else use builtin)" "y"
 
+mkl_toggle_option "Feature" ENABLE_CJSON_EXT "--enable-cjson-ext" "Enable external cJSON (else use builtin)" "y"
+
 # librdkafka with TSAN won't work with glibc C11 threads on Ubuntu 19.04.
 # This option allows disabling libc-based C11 threads and instead
 # use the builtin tinycthread alternative.
@@ -120,6 +122,12 @@ void foo (void) {
                       "#include <lz4frame.h>"
     fi
 
+    # Use builtin cjson if linking statically or if --disable-cjson-ext is used.
+    if [[ $MKL_SOURCE_DEPS_ONLY != y ]] && [[ $WITH_STATIC_LINKING != y ]] && [[ $ENABLE_CJSON_EXT == y ]]; then
+        mkl_lib_check "libcjson" "WITH_CJSON_EXT" disable CC "-lcjson" \
+                      "#include <cJSON.h>"
+    fi
+
     if [[ $ENABLE_SYSLOG == y ]]; then
         mkl_compile_check "syslog" "WITH_SYSLOG" disable CC "" \
                           '
diff --git a/src/Makefile b/src/Makefile
index 0d0635c..6838516 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -33,6 +33,10 @@ SRCS_y += rdkafka_lz4.c $(SRCS_LZ4)
 SRCS_$(WITH_LIBDL) += rddl.c
 SRCS_$(WITH_PLUGINS) += rdkafka_plugin.c
 
+ifneq ($(WITH_CJSON_EXT), y)
+SRCS_y += cJSON.c
+endif
+
 ifneq ($(HAVE_REGEX), y)
 SRCS_y += regexp.c
 endif
@@ -46,7 +50,7 @@ SRCS=		rdkafka.c rdkafka_broker.c rdkafka_msg.c rdkafka_topic.c \
 		rdkafka_assignor.c rdkafka_range_assignor.c \
 		rdkafka_roundrobin_assignor.c rdkafka_sticky_assignor.c \
 		rdkafka_feature.c \
-		rdcrc32.c crc32c.c rdmurmur2.c rdfnv1a.c cJSON.c \
+		rdcrc32.c crc32c.c rdmurmur2.c rdfnv1a.c \
 		rdaddr.c rdrand.c rdlist.c \
 		tinycthread.c tinycthread_extra.c \
 		rdlog.c rdstring.c rdkafka_event.c rdkafka_metadata.c \
diff --git a/src/rdhttp.h b/src/rdhttp.h
index eb93006..95c8503 100644
--- a/src/rdhttp.h
+++ b/src/rdhttp.h
@@ -31,7 +31,11 @@
 #define _RDHTTP_H_
 
 #define CJSON_HIDE_SYMBOLS
+#if WITH_CJSON_EXT
+#include <cJSON.h>
+#else
 #include "cJSON.h"
+#endif
 
 typedef struct rd_http_error_s {
         int code;
diff --git a/src/rdkafka.c b/src/rdkafka.c
index c6f89ad..3c36c45 100644
--- a/src/rdkafka.c
+++ b/src/rdkafka.c
@@ -75,7 +75,11 @@
 #endif
 
 #define CJSON_HIDE_SYMBOLS
+#if WITH_CJSON_EXT
+#include <cJSON.h>
+#else
 #include "cJSON.h"
+#endif
 
 #if WITH_CURL
 #include "rdhttp.h"
diff --git a/src/rdkafka_sasl_oauthbearer_oidc.c b/src/rdkafka_sasl_oauthbearer_oidc.c
index b1d7296..9dab085 100644
--- a/src/rdkafka_sasl_oauthbearer_oidc.c
+++ b/src/rdkafka_sasl_oauthbearer_oidc.c
@@ -35,7 +35,11 @@
 #include "rdkafka_int.h"
 #include "rdkafka_sasl_int.h"
 #include "rdunittest.h"
+#if WITH_CJSON_EXT
+#include <cJSON.h>
+#else
 #include "cJSON.h"
+#endif
 #include <curl/curl.h>
 #include "rdhttp.h"
 #include "rdkafka_sasl_oauthbearer_oidc.h"
