core: allow building against system zlib

In a way similar to the existing USE_SYSTEM_LIBJPEG. The default is of
course still the DEPS-based zlib checkout.

Changes in v2:

1) Since Chromium doesn't have //third_party:zlib (AKA a "zlib" target in
//third_party/BUILD.GN) all the targets that depended on //third_party:zlib now
depend on third_party:zlib, which always points to PDFium's
third_party/BUILD.GN. The targets that depended on //third_party:zlib in
third_party/BUILD.GN just depend on :zlib instead.

2) Move the zlib bits out of !build_with_chromium block.

Change-Id: Id73eb07591418ff2ae71b83b87c18af71b49a22a
Reviewed-on: https://pdfium-review.googlesource.com/5030
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/AUTHORS b/AUTHORS
index 6c2a295..7499c10 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -36,6 +36,7 @@
 Sam Clegg <sbc@chromium.org>
 Thomas Sepez <tsepez@chromium.org>
 
+Collabora Ltd. <*@collabora.co.uk>
 DocsCorp Pty Ltd. <*@docscorp.com>
 Foxit Software Inc <*@foxitsoftware.com>
 Google Inc. <*@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 5f74122..73d339f 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -702,8 +702,8 @@
     ":fxcrt",
     "third_party:fx_lcms2",
     "third_party:fx_libopenjpeg",
+    "third_party:zlib",
     "//third_party:jpeg",
-    "//third_party/zlib:zlib",
   ]
 
   if (pdf_enable_xfa) {
diff --git a/core/fxcodec/codec/fx_codec_flate.cpp b/core/fxcodec/codec/fx_codec_flate.cpp
index 4d1e034..5af3e99 100644
--- a/core/fxcodec/codec/fx_codec_flate.cpp
+++ b/core/fxcodec/codec/fx_codec_flate.cpp
@@ -15,7 +15,12 @@
 #include "core/fxcrt/fx_extension.h"
 #include "third_party/base/numerics/safe_conversions.h"
 #include "third_party/base/ptr_util.h"
+
+#if defined(USE_SYSTEM_ZLIB)
+#include <zlib.h>
+#else
 #include "third_party/zlib/zlib.h"
+#endif
 
 extern "C" {
 
diff --git a/pdfium.gni b/pdfium.gni
index 5fda06f..d570385 100644
--- a/pdfium.gni
+++ b/pdfium.gni
@@ -52,4 +52,7 @@
 
   # Enable coverage information
   use_coverage = false
+
+  # Don't build against bundled zlib.
+  use_system_zlib = false
 }
diff --git a/skia/BUILD.gn b/skia/BUILD.gn
index 0760246..9f60fcd 100644
--- a/skia/BUILD.gn
+++ b/skia/BUILD.gn
@@ -317,8 +317,8 @@
 
   deps = [
     ":skia_opts",
+    "../third_party:zlib",
     "//:freetype_common",
-    "//third_party/zlib:zlib",
   ]
 
   if (is_linux) {
diff --git a/third_party/BUILD.gn b/third_party/BUILD.gn
index 7730212..4e12741 100644
--- a/third_party/BUILD.gn
+++ b/third_party/BUILD.gn
@@ -321,6 +321,21 @@
   }
 }
 
+config("system_zlib_config") {
+  libs = [ "z" ]
+  defines = [ "USE_SYSTEM_ZLIB" ]
+}
+
+group("zlib") {
+  if (use_system_zlib) {
+    public_configs = [ ":system_zlib_config" ]
+  } else {
+    public_deps = [
+      "//third_party/zlib",
+    ]
+  }
+}
+
 config("fx_libopenjpeg_warnings") {
   visibility = [ ":*" ]
   if (is_win) {
@@ -398,7 +413,7 @@
   defines = []
   cflags = []
   deps = [
-    "//third_party/zlib",
+    ":zlib",
   ]
 
   if (current_cpu == "x86" || current_cpu == "x64") {
@@ -437,8 +452,8 @@
       configs -= [ "//build/config/win:lean_and_mean" ]
     }
     deps = [
+      ":zlib",
       "//third_party:jpeg",
-      "//third_party/zlib",
     ]
     sources = [
       "libtiff/tif_aux.c",