Add use_system_libopenjpeg2 build argument

This cl adds the possibility to build pdfium on Linux using a system
provided copy of libopenjpeg2, as it is preferred by many distros.

Change-Id: Id2805ffbf7b75678c2c1a73469ae9dfb9278f97b
Reviewed-on: https://pdfium-review.googlesource.com/c/50750
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 7e7658e..6885fc2 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -20,11 +20,14 @@
   ldflags = []
   include_dirs = [ "." ]
   defines = [
-    "OPJ_STATIC",
     "PNG_PREFIX",
     "PNG_USE_READ_MACROS",
   ]
 
+  if (!use_system_libopenjpeg2) {
+    defines += [ "OPJ_STATIC" ]
+  }
+
   if (pdf_enable_click_logging) {
     defines += [ "PDF_ENABLE_CLICK_LOGGING" ]
   }
diff --git a/core/fxcodec/BUILD.gn b/core/fxcodec/BUILD.gn
index e09b7a6..66cfd71 100644
--- a/core/fxcodec/BUILD.gn
+++ b/core/fxcodec/BUILD.gn
@@ -70,8 +70,8 @@
   configs += [ "../../:pdfium_core_config" ]
   include_dirs = []
   deps = [
-    "../../third_party:fx_libopenjpeg",
     "../../third_party:lcms2",
+    "../../third_party:libopenjpeg2",
     "../../third_party:zlib",
     "../fxcrt",
     "../fxge",
diff --git a/core/fxcodec/codec/ccodec_jpxmodule.cpp b/core/fxcodec/codec/ccodec_jpxmodule.cpp
index 3514d77..fbfc077 100644
--- a/core/fxcodec/codec/ccodec_jpxmodule.cpp
+++ b/core/fxcodec/codec/ccodec_jpxmodule.cpp
@@ -17,8 +17,11 @@
 #include "core/fxcrt/fx_safe_types.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/stl_util.h"
+
+#ifndef USE_SYSTEM_LIBOPENJPEG2
 #include "third_party/libopenjpeg20/openjpeg.h"
 #include "third_party/libopenjpeg20/opj_malloc.h"
+#endif
 
 namespace {
 
@@ -512,7 +515,9 @@
     return false;
 
   m_Image = pTempImage;
+#ifndef USE_SYSTEM_LIBOPENJPEG2
   m_Image->pdfium_use_colorspace = !!m_ColorSpace;
+#endif
   return true;
 }
 
@@ -549,7 +554,11 @@
     // TODO(palmer): Using |opj_free| here resolves the crash described in
     // https://crbug.com/737033, but ultimately we need to harmonize the
     // memory allocation strategy across OpenJPEG and its PDFium callers.
+#ifndef USE_SYSTEM_LIBOPENJPEG2
     opj_free(m_Image->icc_profile_buf);
+#else
+    free(m_Image->icc_profile_buf);
+#endif
     m_Image->icc_profile_buf = nullptr;
     m_Image->icc_profile_len = 0;
   }
diff --git a/core/fxcodec/codec/cjpx_decoder.h b/core/fxcodec/codec/cjpx_decoder.h
index 940efd8..3b6f547 100644
--- a/core/fxcodec/codec/cjpx_decoder.h
+++ b/core/fxcodec/codec/cjpx_decoder.h
@@ -13,7 +13,12 @@
 #include "core/fxcodec/codec/codec_int.h"
 #include "core/fxcrt/unowned_ptr.h"
 #include "third_party/base/span.h"
+
+#ifdef USE_SYSTEM_LIBOPENJPEG2
+#include <openjpeg.h>
+#else
 #include "third_party/libopenjpeg20/openjpeg.h"
+#endif
 
 class CPDF_ColorSpace;
 
diff --git a/core/fxcodec/codec/codec_int.h b/core/fxcodec/codec/codec_int.h
index 4fa819b..484d92e 100644
--- a/core/fxcodec/codec/codec_int.h
+++ b/core/fxcodec/codec/codec_int.h
@@ -10,7 +10,12 @@
 #include <limits.h>
 
 #include "core/fxcodec/jbig2/JBig2_Context.h"
+
+#ifdef USE_SYSTEM_LIBOPENJPEG2
+#include <openjpeg.h>
+#else
 #include "third_party/libopenjpeg20/openjpeg.h"
+#endif
 
 class CPDF_ColorSpace;
 
diff --git a/pdfium.gni b/pdfium.gni
index f451c84..00a3886 100644
--- a/pdfium.gni
+++ b/pdfium.gni
@@ -62,6 +62,9 @@
   # Don't build against bundled lcms2.
   use_system_lcms2 = false
 
+  # Don't build against bundled libopenjpeg2.
+  use_system_libopenjpeg2 = false
+
   # Don't build against bundled libpng.
   use_system_libpng = false
 }
diff --git a/third_party/BUILD.gn b/third_party/BUILD.gn
index 5fa8207..51fd0ca 100644
--- a/third_party/BUILD.gn
+++ b/third_party/BUILD.gn
@@ -387,6 +387,22 @@
   }
 }
 
+if (use_system_libopenjpeg2) {
+  pkg_config("libopenjpeg2_from_pkgconfig") {
+    defines = [ "USE_SYSTEM_LIBOPENJPEG2" ]
+    packages = [ "libopenjp2" ]
+  }
+}
+group("libopenjpeg2") {
+  if (use_system_libopenjpeg2) {
+    public_configs = [ ":libopenjpeg2_from_pkgconfig" ]
+  } else {
+    public_deps = [
+      ":fx_libopenjpeg",
+    ]
+  }
+}
+
 config("fx_libopenjpeg_warnings") {
   visibility = [ ":*" ]
   if (is_win) {