Make //third_party/zlib a component

//third_party/zlib currently gets duplicated in 418 DSOs when building 'all'.
This CL makes zlib a component so that component builds are a bit
smaller/faster.

Also rename zlib/names.h to zlib/chromeconf.h and remove it from patch files.

BUG=941663
R=thakis

Change-Id: I520d7ed30d0cfc322ebf1b9fb31bfbf5469f0209
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1545510
Commit-Queue: Thomas Anderson <thomasanderson@chromium.org>
Reviewed-by: Nico Weber <thakis@chromium.org>
Auto-Submit: Thomas Anderson <thomasanderson@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#648854}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: d7ee2f99fc936f2162a65b9bf087af94646248e8
diff --git a/BUILD.gn b/BUILD.gn
index 0aec27f..8cc0eac 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -10,6 +10,10 @@
   include_dirs = [ "." ]
 }
 
+config("zlib_internal_config") {
+  defines = [ "ZLIB_IMPLEMENTATION" ]
+}
+
 use_arm_neon_optimizations = false
 if (current_cpu == "arm" || current_cpu == "arm64") {
   if (arm_use_neon) {
@@ -56,6 +60,8 @@
     }
   }
 
+  configs += [ ":zlib_internal_config" ]
+
   public_configs = [ ":zlib_adler32_simd_config" ]
 }
 
@@ -105,6 +111,8 @@
       }
     }
 
+    configs += [ ":zlib_internal_config" ]
+
     public_configs = [ ":zlib_arm_crc32_config" ]
   }
 }
@@ -149,7 +157,10 @@
   }
 
   configs -= [ "//build/config/compiler:chromium_code" ]
-  configs += [ "//build/config/compiler:no_chromium_code" ]
+  configs += [
+    ":zlib_internal_config",
+    "//build/config/compiler:no_chromium_code",
+  ]
 
   public_configs = [ ":zlib_inflate_chunk_simd_config" ]
 }
@@ -177,10 +188,12 @@
     }
   }
 
+  configs += [ ":zlib_internal_config" ]
+
   public_configs = [ ":zlib_crc32_simd_config" ]
 }
 
-static_library("zlib_x86_simd") {
+source_set("zlib_x86_simd") {
   visibility = [ ":*" ]
 
   if (use_x86_x64_optimizations) {
@@ -202,7 +215,10 @@
   }
 
   configs -= [ "//build/config/compiler:chromium_code" ]
-  configs += [ "//build/config/compiler:no_chromium_code" ]
+  configs += [
+    ":zlib_internal_config",
+    "//build/config/compiler:no_chromium_code",
+  ]
 }
 
 config("zlib_warnings") {
@@ -211,7 +227,7 @@
   }
 }
 
-static_library("zlib") {
+component("zlib") {
   if (!is_win) {
     # Don't stomp on "libzlib" on other platforms.
     output_name = "chrome_zlib"
@@ -219,6 +235,7 @@
 
   sources = [
     "adler32.c",
+    "chromeconf.h",
     "compress.c",
     "crc32.c",
     "crc32.h",
@@ -236,7 +253,6 @@
     "inflate.h",
     "inftrees.c",
     "inftrees.h",
-    "names.h",
     "trees.c",
     "trees.h",
     "uncompr.c",
@@ -269,6 +285,7 @@
 
   configs -= [ "//build/config/compiler:chromium_code" ]
   configs += [
+    ":zlib_internal_config",
     "//build/config/compiler:no_chromium_code",
 
     # Must be after no_chromium_code for warning flags to be ordered correctly.
diff --git a/README.chromium b/README.chromium
index 9cb10fe..3d90f79 100644
--- a/README.chromium
+++ b/README.chromium
@@ -22,6 +22,7 @@
    imported.
  - The contents of the google directory are original Chromium-specific
    additions.
+ - Added chromeconf.h
  - Plus the changes in 'patches' folder.
  - Code in contrib/ other than contrib/minizip was added to match zlib's
    contributor layout.
diff --git a/names.h b/chromeconf.h
similarity index 93%
rename from names.h
rename to chromeconf.h
index c61f2a7..3414831 100644
--- a/names.h
+++ b/chromeconf.h
@@ -2,8 +2,20 @@
  * Use of this source code is governed by a BSD-style license that can be
  * found in the LICENSE file. */
 
-#ifndef THIRD_PARTY_ZLIB_NAMES_H_
-#define THIRD_PARTY_ZLIB_NAMES_H_
+#ifndef THIRD_PARTY_ZLIB_CHROMECONF_H_
+#define THIRD_PARTY_ZLIB_CHROMECONF_H_
+
+#if defined(COMPONENT_BUILD)
+#if defined(WIN32)
+#if defined(ZLIB_IMPLEMENTATION)
+#define ZEXTERN __declspec(dllexport)
+#else
+#define ZEXTERN __declspec(dllimport)
+#endif
+#elif defined(ZLIB_IMPLEMENTATION)
+#define ZEXPORT __attribute__((visibility("default")))
+#endif
+#endif
 
 /* Rename all zlib names with a Cr_z_ prefix. This is based on the Z_PREFIX
  * option from zconf.h, but with a custom prefix. Where zconf.h would rename
@@ -180,4 +192,4 @@
 #define arm_check_features Cr_z_arm_check_features
 #define armv8_crc32_little Cr_z_armv8_crc32_little
 
-#endif  /* THIRD_PARTY_ZLIB_NAMES_H_ */
+#endif /* THIRD_PARTY_ZLIB_CHROMECONF_H_ */
diff --git a/patches/0000-build.patch b/patches/0000-build.patch
index f16dacc..c27111d 100644
--- a/patches/0000-build.patch
+++ b/patches/0000-build.patch
@@ -141,177 +141,17 @@
  int ZEXPORT gzgetc(file)
      gzFile file;
  {
-diff --git a/names.h b/names.h
-new file mode 100644
-index 000000000000..f18df5684dc5
---- /dev/null
-+++ b/names.h
-@@ -0,0 +1,155 @@
-+/* Copyright 2017 The Chromium Authors. All rights reserved.
-+ * Use of this source code is governed by a BSD-style license that can be
-+ * found in the LICENSE file. */
-+
-+#ifndef THIRD_PARTY_ZLIB_NAMES_H_
-+#define THIRD_PARTY_ZLIB_NAMES_H_
-+
-+/* Rename all zlib names with a Cr_z_ prefix. This is based on the Z_PREFIX
-+ * option from zconf.h, but with a custom prefix. Where zconf.h would rename
-+ * both a macro and its underscore-suffixed internal implementation (such as
-+ * deflateInit2 and deflateInit2_), only the implementation is renamed here.
-+ * The Byte type is also omitted.
-+ *
-+ * To generate this list, run
-+ * sed -rn -e 's/^# *define +([^ ]+) +(z_[^ ]+)$/#define \1 Cr_\2/p' zconf.h
-+ * (use -E instead of -r on macOS).
-+ *
-+ * gzread is also addressed by modifications in gzread.c and zlib.h. */
-+
-+#define Z_CR_PREFIX_SET
-+
-+#define _dist_code Cr_z__dist_code
-+#define _length_code Cr_z__length_code
-+#define _tr_align Cr_z__tr_align
-+#define _tr_flush_bits Cr_z__tr_flush_bits
-+#define _tr_flush_block Cr_z__tr_flush_block
-+#define _tr_init Cr_z__tr_init
-+#define _tr_stored_block Cr_z__tr_stored_block
-+#define _tr_tally Cr_z__tr_tally
-+#define adler32 Cr_z_adler32
-+#define adler32_combine Cr_z_adler32_combine
-+#define adler32_combine64 Cr_z_adler32_combine64
-+#define adler32_z Cr_z_adler32_z
-+#define compress Cr_z_compress
-+#define compress2 Cr_z_compress2
-+#define compressBound Cr_z_compressBound
-+#define crc32 Cr_z_crc32
-+#define crc32_combine Cr_z_crc32_combine
-+#define crc32_combine64 Cr_z_crc32_combine64
-+#define crc32_z Cr_z_crc32_z
-+#define deflate Cr_z_deflate
-+#define deflateBound Cr_z_deflateBound
-+#define deflateCopy Cr_z_deflateCopy
-+#define deflateEnd Cr_z_deflateEnd
-+#define deflateGetDictionary Cr_z_deflateGetDictionary
-+/* #undef deflateInit */
-+/* #undef deflateInit2 */
-+#define deflateInit2_ Cr_z_deflateInit2_
-+#define deflateInit_ Cr_z_deflateInit_
-+#define deflateParams Cr_z_deflateParams
-+#define deflatePending Cr_z_deflatePending
-+#define deflatePrime Cr_z_deflatePrime
-+#define deflateReset Cr_z_deflateReset
-+#define deflateResetKeep Cr_z_deflateResetKeep
-+#define deflateSetDictionary Cr_z_deflateSetDictionary
-+#define deflateSetHeader Cr_z_deflateSetHeader
-+#define deflateTune Cr_z_deflateTune
-+#define deflate_copyright Cr_z_deflate_copyright
-+#define get_crc_table Cr_z_get_crc_table
-+#define gz_error Cr_z_gz_error
-+#define gz_intmax Cr_z_gz_intmax
-+#define gz_strwinerror Cr_z_gz_strwinerror
-+#define gzbuffer Cr_z_gzbuffer
-+#define gzclearerr Cr_z_gzclearerr
-+#define gzclose Cr_z_gzclose
-+#define gzclose_r Cr_z_gzclose_r
-+#define gzclose_w Cr_z_gzclose_w
-+#define gzdirect Cr_z_gzdirect
-+#define gzdopen Cr_z_gzdopen
-+#define gzeof Cr_z_gzeof
-+#define gzerror Cr_z_gzerror
-+#define gzflush Cr_z_gzflush
-+#define gzfread Cr_z_gzfread
-+#define gzfwrite Cr_z_gzfwrite
-+#define gzgetc Cr_z_gzgetc
-+#define gzgetc_ Cr_z_gzgetc_
-+#define gzgets Cr_z_gzgets
-+#define gzoffset Cr_z_gzoffset
-+#define gzoffset64 Cr_z_gzoffset64
-+#define gzopen Cr_z_gzopen
-+#define gzopen64 Cr_z_gzopen64
-+#define gzopen_w Cr_z_gzopen_w
-+#define gzprintf Cr_z_gzprintf
-+#define gzputc Cr_z_gzputc
-+#define gzputs Cr_z_gzputs
-+#define gzread Cr_z_gzread
-+#define gzrewind Cr_z_gzrewind
-+#define gzseek Cr_z_gzseek
-+#define gzseek64 Cr_z_gzseek64
-+#define gzsetparams Cr_z_gzsetparams
-+#define gztell Cr_z_gztell
-+#define gztell64 Cr_z_gztell64
-+#define gzungetc Cr_z_gzungetc
-+#define gzvprintf Cr_z_gzvprintf
-+#define gzwrite Cr_z_gzwrite
-+#define inflate Cr_z_inflate
-+#define inflateBack Cr_z_inflateBack
-+#define inflateBackEnd Cr_z_inflateBackEnd
-+/* #undef inflateBackInit */
-+#define inflateBackInit_ Cr_z_inflateBackInit_
-+#define inflateCodesUsed Cr_z_inflateCodesUsed
-+#define inflateCopy Cr_z_inflateCopy
-+#define inflateEnd Cr_z_inflateEnd
-+#define inflateGetDictionary Cr_z_inflateGetDictionary
-+#define inflateGetHeader Cr_z_inflateGetHeader
-+/* #undef inflateInit */
-+/* #undef inflateInit2 */
-+#define inflateInit2_ Cr_z_inflateInit2_
-+#define inflateInit_ Cr_z_inflateInit_
-+#define inflateMark Cr_z_inflateMark
-+#define inflatePrime Cr_z_inflatePrime
-+#define inflateReset Cr_z_inflateReset
-+#define inflateReset2 Cr_z_inflateReset2
-+#define inflateResetKeep Cr_z_inflateResetKeep
-+#define inflateSetDictionary Cr_z_inflateSetDictionary
-+#define inflateSync Cr_z_inflateSync
-+#define inflateSyncPoint Cr_z_inflateSyncPoint
-+#define inflateUndermine Cr_z_inflateUndermine
-+#define inflateValidate Cr_z_inflateValidate
-+#define inflate_copyright Cr_z_inflate_copyright
-+#define inflate_fast Cr_z_inflate_fast
-+#define inflate_table Cr_z_inflate_table
-+#define uncompress Cr_z_uncompress
-+#define uncompress2 Cr_z_uncompress2
-+#define zError Cr_z_zError
-+#define zcalloc Cr_z_zcalloc
-+#define zcfree Cr_z_zcfree
-+#define zlibCompileFlags Cr_z_zlibCompileFlags
-+#define zlibVersion Cr_z_zlibVersion
-+/* #undef Byte */
-+#define Bytef Cr_z_Bytef
-+#define alloc_func Cr_z_alloc_func
-+#define charf Cr_z_charf
-+#define free_func Cr_z_free_func
-+#define gzFile Cr_z_gzFile
-+#define gz_header Cr_z_gz_header
-+#define gz_headerp Cr_z_gz_headerp
-+#define in_func Cr_z_in_func
-+#define intf Cr_z_intf
-+#define out_func Cr_z_out_func
-+#define uInt Cr_z_uInt
-+#define uIntf Cr_z_uIntf
-+#define uLong Cr_z_uLong
-+#define uLongf Cr_z_uLongf
-+#define voidp Cr_z_voidp
-+#define voidpc Cr_z_voidpc
-+#define voidpf Cr_z_voidpf
-+#define gz_header_s Cr_z_gz_header_s
-+#define internal_state Cr_z_internal_state
-+/* #undef z_off64_t */
-+
-+/* An exported symbol that isn't handled by Z_PREFIX in zconf.h */
-+#define z_errmsg Cr_z_z_errmsg
-+
-+#endif  /* THIRD_PARTY_ZLIB_NAMES_H_ */
 diff --git a/zconf.h b/zconf.h
 index 5e1d68a004e9..a7a815f575a7 100644
 --- a/zconf.h
 +++ b/zconf.h
-@@ -8,6 +8,9 @@
+@@ -8,6 +8,10 @@
  #ifndef ZCONF_H
  #define ZCONF_H
  
-+/* This include does prefixing as below, but with an updated set of names */
-+#include "names.h"
++/* This include does prefixing as below, but with an updated set of names.  Also
++ * sets up export macros in component builds. */
++#include "chromeconf.h"
 +
  /*
   * If you *really* need a unique prefix for all types and library functions,
diff --git a/patches/0001-simd.patch b/patches/0001-simd.patch
index 1fbf195..9434ca0 100644
--- a/patches/0001-simd.patch
+++ b/patches/0001-simd.patch
@@ -1037,27 +1037,6 @@
 +    Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
 +           "not enough room for search");
 +}
-diff --git a/names.h b/names.h
-index f18df5684dc5..3436baa4eb57 100644
---- a/names.h
-+++ b/names.h
-@@ -152,4 +152,16 @@
- /* An exported symbol that isn't handled by Z_PREFIX in zconf.h */
- #define z_errmsg Cr_z_z_errmsg
- 
-+/* Symbols added in simd.patch */
-+#define copy_with_crc Cr_z_copy_with_crc
-+#define crc_finalize Cr_z_crc_finalize
-+#define crc_fold_512to32 Cr_z_crc_fold_512to32
-+#define crc_fold_copy Cr_z_crc_fold_copy
-+#define crc_fold_init Cr_z_crc_fold_init
-+#define crc_reset Cr_z_crc_reset
-+#define fill_window_sse Cr_z_fill_window_sse
-+#define deflate_read_buf Cr_z_deflate_read_buf
-+#define x86_check_features Cr_z_x86_check_features
-+#define x86_cpu_enable_simd Cr_z_x86_cpu_enable_simd
-+
- #endif  /* THIRD_PARTY_ZLIB_NAMES_H_ */
 diff --git a/simd_stub.c b/simd_stub.c
 new file mode 100644
 index 000000000000..c6d46051498f
diff --git a/zconf.h b/zconf.h
index 1843c0a..8e433e6 100644
--- a/zconf.h
+++ b/zconf.h
@@ -8,8 +8,9 @@
 #ifndef ZCONF_H
 #define ZCONF_H
 
-/* This include does prefixing as below, but with an updated set of names */
-#include "names.h"
+/* This include does prefixing as below, but with an updated set of names.  Also
+ * sets up export macros in component builds. */
+#include "chromeconf.h"
 
 /*
  * If you *really* need a unique prefix for all types and library functions,