• 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    // TODO: test and enable these.
49    // "-DRISCV_RVV",
50    // "-DADLER32_SIMD_RVV",
51]
52
53// The *host* x86 configuration (with *lower* CPU feature requirements).
54cflags_x86 = [
55    // See ARMV8_OS_LINUX above.
56    "-DX86_NOT_WINDOWS",
57    // Android's host CPU feature requirements are *lower* than the
58    // corresponding device CPU feature requirements, so it's easier to just
59    // say "no SIMD for you" rather than specificially disable SSSE3.
60    // We should have a conversation about that, but not until we at least have
61    // data on how many Studio users have CPUs that don't make the grade...
62    // https://issuetracker.google.com/171235570
63    "-DCPU_NO_SIMD",
64]
65cflags_x86_64 = cflags_x86 + ["-DINFLATE_CHUNK_READ_64LE"]
66
67// The additional *device* x86/x86_64 configuration. Devices have *higher* CPU
68// feature requirements than the host.
69cflags_android_x86 = [
70    // Android's x86 and x86-64 ABIs both include SSE2 and SSSE3.
71    "-UCPU_NO_SIMD",
72    "-DADLER32_SIMD_SSSE3",
73    // TODO: DINFLATE_CHUNK_SIMD_SSE2 causes `atest org.apache.harmony.tests.java.util.zip.DeflaterTest` failures.
74    // "-DINFLATE_CHUNK_SIMD_SSE2",
75]
76
77libz_srcs = [
78    "adler32.c",
79    "adler32_simd.c",
80    "compress.c",
81    "cpu_features.c",
82    "crc32.c",
83    "crc32_simd.c",
84    "crc_folding.c",
85    "deflate.c",
86    "gzclose.c",
87    "gzlib.c",
88    "gzread.c",
89    "gzwrite.c",
90    "infback.c",
91    "inffast.c",
92    "inflate.c",
93    "inftrees.c",
94    "trees.c",
95    "uncompr.c",
96    "zutil.c",
97
98    // Not-yet-enabled optimizations.
99    // See https://chromium-review.googlesource.com/749732.
100    // TODO: causes `atest org.apache.harmony.tests.java.util.zip.DeflaterTest` failures.
101    //    "contrib/optimizations/inffast_chunk.c",
102    //    "contrib/optimizations/inflate.c",
103]
104
105cc_defaults {
106    name: "libz_defaults",
107
108    cflags: cflags_shared,
109    stl: "none",
110    export_include_dirs: ["."],
111
112    host_supported: true,
113    native_bridge_supported: true,
114
115    vendor_available: true,
116    product_available: true,
117    ramdisk_available: true,
118    vendor_ramdisk_available: true,
119    recovery_available: true,
120
121    arch: {
122        arm: {
123            // TODO: This is to work around b/24465209. Remove after root cause
124            // is fixed.
125            pack_relocations: false,
126            ldflags: ["-Wl,--hash-style=both"],
127
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
180// TODO: Remove this when b/328163089 is fixed.
181// Thin lto will be enabled by default in the future.
182cc_defaults {
183    name: "libz_thin_lto_defaults",
184
185    target: {
186        android_arm64: {
187            lto: {
188                thin: true,
189            },
190        },
191    },
192}
193
194cc_library {
195    name: "libz",
196    defaults: [
197        "libz_defaults",
198        "libz_thin_lto_defaults",
199    ],
200
201    whole_static_libs: ["libz_static"],
202
203    unique_host_soname: true,
204    static_ndk_lib: true,
205
206    double_loadable: true,
207
208    stubs: {
209        versions: [
210            "29",
211            "30",
212        ],
213        symbol_file: "libz.map.txt",
214    },
215
216    // When used by Vendor/Product APEX,
217    // libz should be treated like non-stable module.
218    // (Hence, should be bundled in APEX).
219    target: {
220        product: {
221            no_stubs: true,
222        },
223        vendor: {
224            no_stubs: true,
225        },
226    },
227}
228
229cc_library {
230    name: "libz_static",
231    defaults: ["libz_defaults"],
232    visibility: ["//visibility:private"],
233
234    srcs: libz_srcs,
235
236    sdk_version: "minimum",
237    min_sdk_version: "apex_inherit",
238
239    apex_available: [
240        "com.android.art",
241        "com.android.art.debug",
242        "com.android.runtime",
243        "//apex_available:platform",
244    ],
245}
246
247// A build of libz with identical behavior between architectures.
248// Used by legacy OTA tools such as imgdiff and updater and their tests.
249// New code should not use this library, because new code should not make
250// assumptions about the _compressed_ bits, beyond the fact that they will
251// decompress to the same input bytes. The actual compressed byte sequences
252// can and do differ over time.
253cc_library {
254    name: "libz_stable",
255    defaults: ["libz_thin_lto_defaults"],
256    visibility: [
257        "//bootable/recovery/applypatch",
258        "//bootable/recovery/tests",
259        "//bootable/recovery/updater",
260        "//bootable/deprecated-ota/applypatch",
261        "//bootable/deprecated-ota/tests",
262        "//bootable/deprecated-ota/updater",
263    ],
264    // We only use the shared flags here; the whole point is that this
265    // library behaves the same on all different architectures.
266    cflags: cflags_shared,
267    stl: "none",
268    export_include_dirs: ["."],
269    srcs: libz_srcs,
270    host_supported: true,
271    vendor_available: true,
272    recovery_available: true,
273}
274
275cc_binary {
276    name: "zlib_bench",
277    srcs: ["contrib/bench/zlib_bench.cc"],
278    cflags: [
279        "-Wall",
280        "-Werror",
281        "-Wno-deprecated-non-prototype",
282        "-Wno-unused-parameter",
283    ],
284    host_supported: true,
285    shared_libs: ["libz"],
286    // We build zlib_bench32 and zlib_bench64 so it's easy to test LP32.
287    compile_multilib: "both",
288    multilib: {
289        lib32: {
290            suffix: "32",
291        },
292        lib64: {
293            suffix: "64",
294        },
295    },
296}
297
298cc_library {
299    name: "zlib_google_compression_utils_portable",
300    defaults: ["libz_defaults"],
301    srcs: [
302        "google/compression_utils_portable.cc",
303    ],
304    export_include_dirs: ["google"],
305    host_supported: true,
306    shared_libs: ["libz"],
307    sdk_version: "minimum",
308    visibility: ["//external/angle"],
309    apex_available: [
310        "com.android.runtime",
311        "//apex_available:platform",
312    ],
313}
314
315cc_test {
316    name: "zlib_tests",
317    srcs: [
318        "contrib/tests/infcover.cc",
319        "contrib/tests/utils_unittest.cc",
320    ],
321    cflags: [
322        "-DCMAKE_STANDALONE_UNITTESTS",
323        "-Wno-unused-parameter",
324    ],
325    include_dirs: [
326        // These tests include "gtest.h" rather than the usual "gtest/gtest.h".
327        "external/googletest/googletest/include/gtest/",
328    ],
329    shared_libs: ["libz"],
330    static_libs: ["zlib_google_compression_utils_portable"],
331    host_supported: true,
332    test_suites: ["device-tests"],
333}
334
335ndk_headers {
336    name: "libz_headers",
337    from: "",
338    to: "",
339    srcs: [
340        "zconf.h",
341        "zlib.h",
342    ],
343    license: "LICENSE",
344}
345
346ndk_library {
347    name: "libz",
348    symbol_file: "libz.map.txt",
349    first_version: "9",
350    unversioned_until: "current",
351    export_header_libs: [
352        "libz_headers",
353    ],
354}
355
356// Export zlib headers for inclusion in the musl sysroot.
357genrule {
358    name: "libc_musl_sysroot_zlib_headers",
359    visibility: ["//external/musl"],
360    srcs: [
361        "LICENSE",
362        "zconf.h",
363        "zlib.h",
364    ],
365    out: ["libc_musl_sysroot_zlib_headers.zip"],
366    tools: [
367        "soong_zip",
368        "zip2zip",
369    ],
370    cmd: "$(location soong_zip) -o $(genDir)/sysroot.zip -symlinks=false" +
371        // NOTICE
372        " -j -f $(location LICENSE) " +
373        // headers
374        " -j -P include " +
375        "  -f $(location zconf.h) " +
376        "  -f $(location zlib.h) " +
377        " && " +
378        "$(location zip2zip) -i $(genDir)/sysroot.zip -o $(out) " +
379        " include/**/*:include " +
380        " LICENSE:NOTICE.zlib",
381}
382
383cc_defaults {
384    name: "zlib_fuzz_defaults",
385    static_libs: ["libz"],
386    host_supported: true,
387}
388
389cc_fuzz {
390    name: "zlib_deflate_fuzzer",
391    defaults: ["zlib_fuzz_defaults"],
392    srcs: ["contrib/tests/fuzzers/deflate_fuzzer.cc"],
393}
394
395cc_fuzz {
396    name: "zlib_deflate_set_dictionary_fuzzer",
397    defaults: ["zlib_fuzz_defaults"],
398    srcs: ["contrib/tests/fuzzers/deflate_set_dictionary_fuzzer.cc"],
399}
400
401cc_fuzz {
402    name: "zlib_inflate_fuzzer",
403    defaults: ["zlib_fuzz_defaults"],
404    srcs: ["contrib/tests/fuzzers/inflate_fuzzer.cc"],
405}
406
407cc_fuzz {
408    name: "zlib_inflate_with_header_fuzzer",
409    defaults: ["zlib_fuzz_defaults"],
410    srcs: ["contrib/tests/fuzzers/inflate_with_header_fuzzer.cc"],
411}
412
413cc_fuzz {
414    name: "zlib_streaming_inflate_fuzzer",
415    defaults: ["zlib_fuzz_defaults"],
416    srcs: ["contrib/tests/fuzzers/streaming_inflate_fuzzer.cc"],
417    fuzz_config: {
418        libfuzzer_options: ["max_len=256000"],
419    },
420}
421
422cc_fuzz {
423    name: "zlib_uncompress_fuzzer",
424    defaults: ["zlib_fuzz_defaults"],
425    srcs: ["contrib/tests/fuzzers/uncompress_fuzzer.cc"],
426}
427