core: allow building against system lcms2

In a way similar to the existing USE_SYSTEM_ZLIB. The default is of course
still the bundled lcms2.

Change-Id: I219b50854b3c7870b0f4d94574ba39f6cb26f556
Reviewed-on: https://pdfium-review.googlesource.com/5870
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index debeab9..073fd57 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -705,8 +705,8 @@
   include_dirs = []
   deps = [
     ":fxcrt",
-    "third_party:fx_lcms2",
     "third_party:fx_libopenjpeg",
+    "third_party:lcms2",
     "third_party:zlib",
     "//third_party:jpeg",
   ]
diff --git a/core/fxcodec/codec/fx_codec_icc.cpp b/core/fxcodec/codec/fx_codec_icc.cpp
index 7592c05..ee562f0 100644
--- a/core/fxcodec/codec/fx_codec_icc.cpp
+++ b/core/fxcodec/codec/fx_codec_icc.cpp
@@ -6,7 +6,12 @@
 
 #include "core/fxcodec/codec/codec_int.h"
 #include "core/fxcodec/fx_codec.h"
+
+#if defined(USE_SYSTEM_LCMS2)
+#include <lcms2.h>
+#else
 #include "third_party/lcms2-2.6/include/lcms2.h"
+#endif
 
 struct CLcmsCmm {
   cmsHTRANSFORM m_hTransform;
diff --git a/core/fxcodec/codec/fx_codec_jpx_opj.cpp b/core/fxcodec/codec/fx_codec_jpx_opj.cpp
index 30e79de..9627025 100644
--- a/core/fxcodec/codec/fx_codec_jpx_opj.cpp
+++ b/core/fxcodec/codec/fx_codec_jpx_opj.cpp
@@ -16,9 +16,14 @@
 #include "core/fxcodec/fx_codec.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "third_party/base/ptr_util.h"
-#include "third_party/lcms2-2.6/include/lcms2.h"
 #include "third_party/libopenjpeg20/openjpeg.h"
 
+#if defined(USE_SYSTEM_LCMS2)
+#include <lcms2.h>
+#else
+#include "third_party/lcms2-2.6/include/lcms2.h"
+#endif
+
 static void fx_error_callback(const char* msg, void* client_data) {
   (void)client_data;
 }
diff --git a/pdfium.gni b/pdfium.gni
index b28ff8b..d9bdfb6 100644
--- a/pdfium.gni
+++ b/pdfium.gni
@@ -55,6 +55,9 @@
 
   # Don't build against bundled zlib.
   use_system_zlib = false
+
+  # Don't build against bundled lcms2.
+  use_system_lcms2 = false
 }
 
 if (pdf_use_skia && pdf_use_skia_paths) {
diff --git a/third_party/BUILD.gn b/third_party/BUILD.gn
index 4e12741..6d093f2 100644
--- a/third_party/BUILD.gn
+++ b/third_party/BUILD.gn
@@ -336,6 +336,21 @@
   }
 }
 
+config("system_lcms2_config") {
+  libs = [ "lcms2" ]
+  defines = [ "USE_SYSTEM_LCMS2" ]
+}
+
+group("lcms2") {
+  if (use_system_lcms2) {
+    public_configs = [ ":system_lcms2_config" ]
+  } else {
+    public_deps = [
+      ":fx_lcms2",
+    ]
+  }
+}
+
 config("fx_libopenjpeg_warnings") {
   visibility = [ ":*" ]
   if (is_win) {