• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1package {
2    default_applicable_licenses: ["external_zlib_license"],
3}
4
5license {
6    name: "external_zlib_license",
7    visibility: [":__subpackages__"],
8    license_kinds: [
9        "SPDX-license-identifier-BSD",
10        "SPDX-license-identifier-Zlib",
11    ],
12    license_text: [
13        "LICENSE",
14    ],
15}
16
17// These cflags are shared --- not only between all architectures,
18// but between libz and libz_stable too.
19cflags_shared = [
20    // Our compiler does support hidden visibility.
21    "-DHAVE_HIDDEN",
22    // Our compiler does support const.
23    "-DZLIB_CONST",
24    // Use the traditional Rabin-Karp rolling hash to match zlib DEFLATE output exactly.
25    "-DCHROMIUM_ZLIB_NO_CASTAGNOLI",
26    // Enable -O3 for everyone, as chromium's BUILD.gn does.
27    "-O3",
28    "-Wall",
29    "-Werror",
30    "-Wno-deprecated-non-prototype",
31    "-Wno-unused",
32    "-Wno-unused-parameter",
33]
34
35cflags_arm = [
36    // Even the NDK dropped non-neon support in r24.
37    "-DADLER32_SIMD_NEON",
38    // HWCAP_CRC32 is checked at runtime, so it's okay to enable crc32
39    // acceleration for both 64-bit and 32-bit (which may be armv7, at
40    // least for NDK users).
41    "-DCRC32_ARMV8_CRC32",
42    // TODO: DINFLATE_CHUNK_SIMD_NEON causes `atest org.apache.harmony.tests.java.util.zip.DeflaterTest` failures.
43    // "-DINFLATE_CHUNK_SIMD_NEON",
44]
45cflags_arm64 = cflags_arm + ["-DINFLATE_CHUNK_READ_64LE"]
46
47cflags_riscv64 = [
48    "-DRISCV_RVV",
49    "-DADLER32_SIMD_RVV",
50    "-DDEFLATE_SLIDE_HASH_RVV",
51    "-DINFLATE_CHUNK_GENERIC",
52    "-DINFLATE_CHUNK_READ_64LE",
53]
54
55// The *host* x86 configuration (with *lower* CPU feature requirements).
56cflags_x86 = [
57    // See ARMV8_OS_LINUX above.
58    "-DX86_NOT_WINDOWS",
59    // Android's host CPU feature requirements are *lower* than the
60    // corresponding device CPU feature requirements, so it's easier to just
61    // say "no SIMD for you" rather than specificially disable SSSE3.
62    // We should have a conversation about that, but not until we at least have
63    // data on how many Studio users have CPUs that don't make the grade...
64    // https://issuetracker.google.com/171235570
65    "-DCPU_NO_SIMD",
66]
67cflags_x86_64 = cflags_x86 + ["-DINFLATE_CHUNK_READ_64LE"]
68
69// The additional *device* x86/x86_64 configuration. Devices have *higher* CPU
70// feature requirements than the host.
71cflags_android_x86 = [
72    // Android's x86 and x86-64 ABIs both include SSE2 and SSSE3.
73    "-UCPU_NO_SIMD",
74    "-DADLER32_SIMD_SSSE3",
75    // TODO: DINFLATE_CHUNK_SIMD_SSE2 causes `atest org.apache.harmony.tests.java.util.zip.DeflaterTest` failures.
76    // "-DINFLATE_CHUNK_SIMD_SSE2",
77]
78
79libz_srcs = [
80    "adler32.c",
81    "adler32_simd.c",
82    "compress.c",
83    "cpu_features.c",
84    "crc32.c",
85    "crc32_simd.c",
86    "crc_folding.c",
87    "deflate.c",
88    "gzclose.c",
89    "gzlib.c",
90    "gzread.c",
91    "gzwrite.c",
92    "infback.c",
93    "inffast.c",
94    "inflate.c",
95    "inftrees.c",
96    "trees.c",
97    "uncompr.c",
98    "zutil.c",
99
100    // Not-yet-enabled optimizations.
101    // See https://chromium-review.googlesource.com/749732.
102    // TODO: causes `atest org.apache.harmony.tests.java.util.zip.DeflaterTest` failures.
103    //    "contrib/optimizations/inffast_chunk.c",
104    //    "contrib/optimizations/inflate.c",
105]
106
107cc_defaults {
108    name: "libz_defaults",
109    defaults: [
110        "bug_24465209_workaround",
111    ],
112
113    cflags: cflags_shared,
114    stl: "none",
115    export_include_dirs: ["."],
116
117    host_supported: true,
118    native_bridge_supported: true,
119
120    vendor_available: true,
121    product_available: true,
122    ramdisk_available: true,
123    vendor_ramdisk_available: true,
124    recovery_available: true,
125
126    arch: {
127        arm: {
128            cflags: cflags_arm,
129        },
130        arm64: {
131            cflags: cflags_arm64,
132        },
133        riscv64: {
134            cflags: cflags_riscv64,
135        },
136        x86: {
137            cflags: cflags_x86,
138        },
139        x86_64: {
140            cflags: cflags_x86_64,
141        },
142    },
143    target: {
144        android_arm: {
145            cflags: [
146                // Since we're building for the platform, we claim to be Linux rather than
147                // Android so we use getauxval() directly instead of the NDK
148                // android_getCpuFeatures which isn't available to us anyway.
149                "-DARMV8_OS_LINUX",
150            ],
151        },
152        android_x86: {
153            cflags: cflags_android_x86,
154        },
155        android_x86_64: {
156            cflags: cflags_android_x86,
157        },
158        darwin_arm64: {
159            cflags: [
160                "-DARMV8_OS_MACOS",
161            ],
162        },
163        linux_bionic: {
164            enabled: true,
165        },
166        linux_arm64: {
167            cflags: [
168                // Since we're building for the platform, we claim to be Linux rather than
169                // Android so we use getauxval() directly instead of the NDK
170                // android_getCpuFeatures which isn't available to us anyway.
171                "-DARMV8_OS_LINUX",
172            ],
173        },
174        windows: {
175            enabled: true,
176        },
177    },
178}
179
180cc_library {
181    name: "libz",
182    defaults: ["libz_defaults"],
183
184    srcs: libz_srcs,
185
186    unique_host_soname: true,
187    static_ndk_lib: true,
188
189    double_loadable: true,
190
191    stubs: {
192        versions: [
193            "29",
194            "30",
195        ],
196        symbol_file: "libz.map.txt",
197    },
198
199    // linker/linker64 statically link zlib.
200    static: {
201        apex_available: [
202            "com.android.runtime",
203            "com.android.appsearch",
204        ],
205    },
206
207    // When used by Vendor/Product APEX,
208    // libz should be treated like non-stable module.
209    // (Hence, should be bundled in APEX).
210    target: {
211        product: {
212            no_stubs: true,
213        },
214        vendor: {
215            no_stubs: true,
216        },
217    },
218
219    afdo: true,
220    // TODO(b/390639586): Remove this line once the blocker is resolved.
221    min_sdk_version: "apex_inherit",
222}
223
224// A build of libz with identical behavior between architectures.
225// Used by legacy OTA tools such as imgdiff and updater and their tests.
226// New code should not use this library, because new code should not make
227// assumptions about the _compressed_ bits, beyond the fact that they will
228// decompress to the same input bytes. The actual compressed byte sequences
229// can and do differ over time.
230cc_library {
231    name: "libz_stable",
232    visibility: [
233        "//bootable/recovery/applypatch",
234        "//bootable/recovery/tests",
235        "//bootable/recovery/updater",
236        "//bootable/deprecated-ota/applypatch",
237        "//bootable/deprecated-ota/tests",
238        "//bootable/deprecated-ota/updater",
239    ],
240    // We only use the shared flags here; the whole point is that this
241    // library behaves the same on all different architectures.
242    cflags: cflags_shared,
243    stl: "none",
244    export_include_dirs: ["."],
245    srcs: libz_srcs,
246    host_supported: true,
247    vendor_available: true,
248    recovery_available: true,
249}
250
251cc_binary {
252    name: "zlib_bench",
253    srcs: ["contrib/bench/zlib_bench.cc"],
254    cflags: [
255        "-Wall",
256        "-Werror",
257        "-Wno-deprecated-non-prototype",
258        "-Wno-unused-parameter",
259    ],
260    host_supported: true,
261    shared_libs: ["libz"],
262    // We build zlib_bench32 and zlib_bench64 so it's easy to test LP32.
263    compile_multilib: "both",
264    multilib: {
265        lib32: {
266            suffix: "32",
267        },
268        lib64: {
269            suffix: "64",
270        },
271    },
272}
273
274cc_library {
275    name: "zlib_google_compression_utils_portable",
276    defaults: ["libz_defaults"],
277    srcs: [
278        "google/compression_utils_portable.cc",
279    ],
280    export_include_dirs: ["google"],
281    host_supported: true,
282    shared_libs: ["libz"],
283    sdk_version: "minimum",
284    visibility: ["//external/angle"],
285    apex_available: [
286        "com.android.runtime",
287        "//apex_available:platform",
288    ],
289}
290
291cc_library_static {
292    name: "tflite_support_libz",
293    defaults: ["libz_defaults"],
294    srcs: [
295        "contrib/minizip/ioapi.c",
296        "contrib/minizip/unzip.c",
297    ],
298    sdk_version: "current",
299    // TODO: switch this to "apex_inherit".
300    min_sdk_version: "30",
301    apex_available: [
302        "//apex_available:platform",
303        "com.android.adservices",
304        "com.android.extservices",
305    ],
306}
307
308cc_test {
309    name: "zlib_tests",
310    srcs: [
311        "contrib/tests/infcover.cc",
312        "contrib/tests/utils_unittest.cc",
313    ],
314    cflags: [
315        "-DCMAKE_STANDALONE_UNITTESTS",
316        "-Wno-unused-parameter",
317    ],
318    include_dirs: [
319        // These tests include "gtest.h" rather than the usual "gtest/gtest.h".
320        "external/googletest/googletest/include/gtest/",
321    ],
322    shared_libs: ["libz"],
323    static_libs: ["zlib_google_compression_utils_portable"],
324    host_supported: true,
325    test_suites: ["device-tests"],
326}
327
328ndk_headers {
329    name: "libz_headers",
330    from: "",
331    to: "",
332    srcs: [
333        "zconf.h",
334        "zlib.h",
335    ],
336    license: "LICENSE",
337}
338
339ndk_library {
340    name: "libz",
341    symbol_file: "libz.map.txt",
342    first_version: "9",
343    unversioned_until: "current",
344}
345
346// Export zlib headers for inclusion in the musl sysroot.
347genrule {
348    name: "libc_musl_sysroot_zlib_headers",
349    visibility: ["//external/musl"],
350    srcs: [
351        "LICENSE",
352        "zconf.h",
353        "zlib.h",
354    ],
355    out: ["libc_musl_sysroot_zlib_headers.zip"],
356    tools: [
357        "soong_zip",
358        "zip2zip",
359    ],
360    cmd: "$(location soong_zip) -o $(genDir)/sysroot.zip -symlinks=false" +
361        // NOTICE
362        " -j -f $(location LICENSE) " +
363        // headers
364        " -j -P include " +
365        "  -f $(location zconf.h) " +
366        "  -f $(location zlib.h) " +
367        " && " +
368        "$(location zip2zip) -i $(genDir)/sysroot.zip -o $(out) " +
369        " include/**/*:include " +
370        " LICENSE:NOTICE.zlib",
371}
372
373cc_defaults {
374    name: "zlib_fuzz_defaults",
375    static_libs: ["libz"],
376    host_supported: true,
377}
378
379cc_fuzz {
380    name: "zlib_deflate_fuzzer",
381    defaults: ["zlib_fuzz_defaults"],
382    srcs: ["contrib/tests/fuzzers/deflate_fuzzer.cc"],
383}
384
385cc_fuzz {
386    name: "zlib_deflate_set_dictionary_fuzzer",
387    defaults: ["zlib_fuzz_defaults"],
388    srcs: ["contrib/tests/fuzzers/deflate_set_dictionary_fuzzer.cc"],
389}
390
391cc_fuzz {
392    name: "zlib_inflate_fuzzer",
393    defaults: ["zlib_fuzz_defaults"],
394    srcs: ["contrib/tests/fuzzers/inflate_fuzzer.cc"],
395}
396
397cc_fuzz {
398    name: "zlib_inflate_with_header_fuzzer",
399    defaults: ["zlib_fuzz_defaults"],
400    srcs: ["contrib/tests/fuzzers/inflate_with_header_fuzzer.cc"],
401}
402
403cc_fuzz {
404    name: "zlib_streaming_inflate_fuzzer",
405    defaults: ["zlib_fuzz_defaults"],
406    srcs: ["contrib/tests/fuzzers/streaming_inflate_fuzzer.cc"],
407    fuzz_config: {
408        libfuzzer_options: ["max_len=256000"],
409    },
410}
411
412cc_fuzz {
413    name: "zlib_uncompress_fuzzer",
414    defaults: ["zlib_fuzz_defaults"],
415    srcs: ["contrib/tests/fuzzers/uncompress_fuzzer.cc"],
416}
417