Skip to content
File

Blob: build/BUILD.zlib

7.5 KB
1# This config closely follows the original GN build file
2# Ref: https://chromium.googlesource.com/chromium/src/third_party/zlib.git/+/refs/heads/main/BUILD.gn
3# The main difference is that we don't set ZLIB_DLL because it messes things up on Windows.
4# Other aspects that are not ported to make this easier to maintain: Support for architectures other
5# than x86_64 or arm64
6 
7load("@bazel_skylib//lib:selects.bzl", "selects")
8load("@rules_cc//cc:cc_library.bzl", "cc_library")
9 
10zlib_warnings = [
11 "-Wno-incompatible-pointer-types",
12 "-Wunused-variable",
13]
14 
15selects.config_setting_group(
16 name = "x86_linux",
17 match_all = [
18 "@platforms//cpu:x86_64",
19 "@platforms//os:linux",
20 ],
21)
22 
23selects.config_setting_group(
24 name = "x86_macos",
25 match_all = [
26 "@platforms//cpu:x86_64",
27 "@platforms//os:macos",
28 ],
29)
30 
31selects.config_setting_group(
32 name = "x86_windows",
33 match_all = [
34 "@platforms//cpu:x86_64",
35 "@platforms//os:windows",
36 ],
37)
38 
39selects.config_setting_group(
40 name = "arm64_linux",
41 match_all = [
42 "@platforms//cpu:aarch64",
43 "@platforms//os:linux",
44 ],
45)
46 
47selects.config_setting_group(
48 name = "arm64_macos",
49 match_all = [
50 "@platforms//cpu:aarch64",
51 "@platforms//os:macos",
52 ],
53)
54 
55selects.config_setting_group(
56 name = "arm64_windows",
57 match_all = [
58 "@platforms//cpu:aarch64",
59 "@platforms//os:windows",
60 ],
61)
62 
63cc_library(
64 name = "zlib_common_headers",
65 hdrs = [
66 "chromeconf.h",
67 "deflate.h",
68 "inffast.h",
69 "inffixed.h",
70 "inflate.h",
71 "inftrees.h",
72 "zconf.h",
73 "zlib.h",
74 "zutil.h",
75 ],
76 features = ["-parse_headers"],
77 # For zlib to be found using <zlib.h> before system zlib, use this flag for the include to also be added using isystem.
78 includes = ["."],
79 local_defines = ["ZLIB_IMPLEMENTATION"],
80)
81 
82cc_library(
83 name = "zlib_adler32_simd",
84 srcs = [
85 "adler32_simd.c",
86 "adler32_simd.h",
87 ],
88 copts = select({
89 "@platforms//cpu:x86_64": ["-mssse3"],
90 "//conditions:default": [],
91 }),
92 local_defines = ["ZLIB_IMPLEMENTATION"] + select({
93 "@platforms//cpu:x86_64": ["ADLER32_SIMD_SSSE3"],
94 "@platforms//cpu:aarch64": ["ADLER32_SIMD_NEON"],
95 }) + select({
96 ":x86_linux": ["X86_NOT_WINDOWS"],
97 ":x86_macos": ["X86_NOT_WINDOWS"],
98 ":x86_windows": ["X86_WINDOWS"],
99 "//conditions:default": [],
100 }),
101 deps = [":zlib_common_headers"],
102)
103 
104cc_library(
105 name = "zlib_arm_crc32",
106 srcs = [
107 "crc32_simd.c",
108 "crc32_simd.h",
109 ],
110 # TODO(soon): The canonical GN build adds "-march=armv8-a+aes+crc" here. We could do the same,
111 # but this may reduce the target CPU level if the compiler defaults to e.g. armv8.5-a. We really
112 # should compile all of workerd with the same compile options. For now, just specify -mcrc which
113 # adds CRC instructions to whatever is already defined. The source file still compiles without
114 # explicitly enabling AES/PMULL extensions as these are used in inline assembly and only enabled
115 # if support for them is detected at runtime.
116 copts = ["-mcrc"],
117 local_defines = [
118 "CRC32_ARMV8_CRC32",
119 "ZLIB_IMPLEMENTATION",
120 ] + select({
121 "@platforms//os:linux": ["ARMV8_OS_LINUX"],
122 "@platforms//os:macos": ["ARMV8_OS_MACOS"],
123 "@platforms//os:windows": ["ARMV8_OS_WINDOWS"],
124 }),
125 target_compatible_with = select({
126 "@platforms//cpu:aarch64": [],
127 "//conditions:default": ["@platforms//:incompatible"],
128 }),
129 deps = [":zlib_common_headers"],
130)
131 
132cc_library(
133 name = "zlib_inflate_chunk_simd",
134 srcs = [
135 "contrib/optimizations/chunkcopy.h",
136 "contrib/optimizations/inffast_chunk.c",
137 "contrib/optimizations/inffast_chunk.h",
138 "contrib/optimizations/inflate.c",
139 ],
140 copts = zlib_warnings,
141 features = ["-parse_headers"],
142 local_defines = [
143 "INFLATE_CHUNK_READ_64LE",
144 "ZLIB_IMPLEMENTATION",
145 ] + select({
146 "@platforms//cpu:x86_64": [
147 "INFLATE_CHUNK_SIMD_SSE2",
148 ],
149 "@platforms//cpu:aarch64": [
150 "INFLATE_CHUNK_SIMD_NEON",
151 ],
152 }),
153 deps = [":zlib_common_headers"],
154)
155 
156cc_library(
157 name = "zlib_crc32_simd",
158 srcs = [
159 "crc32_simd.c",
160 "crc32_simd.h",
161 "crc_folding.c",
162 ],
163 copts = [
164 "-msse4.2",
165 "-mpclmul",
166 ],
167 local_defines = [
168 "CRC32_SIMD_SSE42_PCLMUL",
169 "ZLIB_IMPLEMENTATION",
170 ],
171 target_compatible_with = select({
172 "@platforms//cpu:x86_64": [],
173 "//conditions:default": ["@platforms//:incompatible"],
174 }),
175 deps = [":zlib_common_headers"],
176)
177 
178cc_library(
179 name = "zlib_slide_hash_simd",
180 srcs = [
181 "slide_hash_simd.h",
182 ],
183 # This target is header only so these are not needed to compile correctly, but still useful for
184 # header parsing (i.e. parse_headers feature).
185 local_defines = ["ZLIB_IMPLEMENTATION"] + select({
186 "@platforms//cpu:x86_64": ["DEFLATE_SLIDE_HASH_SSE2"],
187 "@platforms//cpu:aarch64": ["DEFLATE_SLIDE_HASH_NEON"],
188 }),
189 deps = [":zlib_common_headers"],
190)
191 
192filegroup(
193 name = "zlib_files",
194 srcs = [
195 "adler32.c",
196 "chromeconf.h",
197 "compress.c",
198 "contrib/optimizations/insert_string.h",
199 "cpu_features.c",
200 "cpu_features.h",
201 "crc32.c",
202 "crc32.h",
203 "deflate.c",
204 "deflate.h",
205 "gzclose.c",
206 "gzguts.h",
207 "gzlib.c",
208 "gzread.c",
209 "gzwrite.c",
210 "infback.c",
211 "inffast.c",
212 "inffast.h",
213 "inffixed.h",
214 "inflate.h",
215 "inftrees.c",
216 "inftrees.h",
217 "trees.c",
218 "trees.h",
219 "uncompr.c",
220 "zconf.h",
221 "zlib.h",
222 "zutil.c",
223 "zutil.h",
224 ],
225)
226 
227cc_library(
228 name = "zlib",
229 srcs = [":zlib_files"],
230 copts = zlib_warnings,
231 features = ["-parse_headers"],
232 # Duplicate definitions here since we want to avoid non-local defines, some of them are needed
233 # here. This excludes INFLATE_CHUNK_* which is only needed in zlib_inflate_chunk_simd.
234 local_defines = ["ZLIB_IMPLEMENTATION"] + select({
235 "@platforms//cpu:x86_64": [
236 "ADLER32_SIMD_SSSE3",
237 "CRC32_SIMD_SSE42_PCLMUL",
238 "DEFLATE_SLIDE_HASH_SSE2",
239 ],
240 "@platforms//cpu:aarch64": [
241 "ADLER32_SIMD_NEON",
242 "CRC32_ARMV8_CRC32",
243 "DEFLATE_SLIDE_HASH_NEON",
244 ],
245 }) + select({
246 ":x86_linux": ["X86_NOT_WINDOWS"],
247 ":x86_macos": ["X86_NOT_WINDOWS"],
248 ":x86_windows": ["X86_WINDOWS"],
249 ":arm64_linux": ["ARMV8_OS_LINUX"],
250 "arm64_macos": ["ARMV8_OS_MACOS"],
251 "arm64_windows": ["ARMV8_OS_WINDOWS"],
252 "//conditions:default": [],
253 }),
254 visibility = ["//visibility:public"],
255 deps = [
256 ":zlib_adler32_simd",
257 ":zlib_inflate_chunk_simd",
258 ":zlib_slide_hash_simd",
259 ] + select({
260 "@platforms//cpu:x86_64": [":zlib_crc32_simd"],
261 "@platforms//cpu:aarch64": [":zlib_arm_crc32"],
262 }),
263)
264 
265# Chromium zlib includes some custom compression utils that are not present in mainline zlib but
266# used by V8.
267cc_library(
268 name = "compression_utils_portable",
269 srcs = ["google/compression_utils_portable.cc"],
270 hdrs = ["google/compression_utils_portable.h"],
271 visibility = ["//visibility:public"],
272 deps = [":zlib"],
273)