# This config closely follows the original GN build file # Ref: https://chromium.googlesource.com/chromium/src/third_party/zlib.git/+/refs/heads/main/BUILD.gn # The main difference is that we don't set ZLIB_DLL because it messes things up on Windows. # Other aspects that are not ported to make this easier to maintain: Support for architectures other # than x86_64 or arm64 load("@bazel_skylib//lib:selects.bzl", "selects") load("@rules_cc//cc:cc_library.bzl", "cc_library") zlib_warnings = [ "-Wno-incompatible-pointer-types", "-Wunused-variable", ] selects.config_setting_group( name = "x86_linux", match_all = [ "@platforms//cpu:x86_64", "@platforms//os:linux", ], ) selects.config_setting_group( name = "x86_macos", match_all = [ "@platforms//cpu:x86_64", "@platforms//os:macos", ], ) selects.config_setting_group( name = "x86_windows", match_all = [ "@platforms//cpu:x86_64", "@platforms//os:windows", ], ) selects.config_setting_group( name = "arm64_linux", match_all = [ "@platforms//cpu:aarch64", "@platforms//os:linux", ], ) selects.config_setting_group( name = "arm64_macos", match_all = [ "@platforms//cpu:aarch64", "@platforms//os:macos", ], ) selects.config_setting_group( name = "arm64_windows", match_all = [ "@platforms//cpu:aarch64", "@platforms//os:windows", ], ) cc_library( name = "zlib_common_headers", hdrs = [ "chromeconf.h", "deflate.h", "inffast.h", "inffixed.h", "inflate.h", "inftrees.h", "zconf.h", "zlib.h", "zutil.h", ], features = ["-parse_headers"], # For zlib to be found using before system zlib, use this flag for the include to also be added using isystem. includes = ["."], local_defines = ["ZLIB_IMPLEMENTATION"], ) cc_library( name = "zlib_adler32_simd", srcs = [ "adler32_simd.c", "adler32_simd.h", ], copts = select({ "@platforms//cpu:x86_64": ["-mssse3"], "//conditions:default": [], }), local_defines = ["ZLIB_IMPLEMENTATION"] + select({ "@platforms//cpu:x86_64": ["ADLER32_SIMD_SSSE3"], "@platforms//cpu:aarch64": ["ADLER32_SIMD_NEON"], }) + select({ ":x86_linux": ["X86_NOT_WINDOWS"], ":x86_macos": ["X86_NOT_WINDOWS"], ":x86_windows": ["X86_WINDOWS"], "//conditions:default": [], }), deps = [":zlib_common_headers"], ) cc_library( name = "zlib_arm_crc32", srcs = [ "crc32_simd.c", "crc32_simd.h", ], # TODO(soon): The canonical GN build adds "-march=armv8-a+aes+crc" here. We could do the same, # but this may reduce the target CPU level if the compiler defaults to e.g. armv8.5-a. We really # should compile all of workerd with the same compile options. For now, just specify -mcrc which # adds CRC instructions to whatever is already defined. The source file still compiles without # explicitly enabling AES/PMULL extensions as these are used in inline assembly and only enabled # if support for them is detected at runtime. copts = ["-mcrc"], local_defines = [ "CRC32_ARMV8_CRC32", "ZLIB_IMPLEMENTATION", ] + select({ "@platforms//os:linux": ["ARMV8_OS_LINUX"], "@platforms//os:macos": ["ARMV8_OS_MACOS"], "@platforms//os:windows": ["ARMV8_OS_WINDOWS"], }), target_compatible_with = select({ "@platforms//cpu:aarch64": [], "//conditions:default": ["@platforms//:incompatible"], }), deps = [":zlib_common_headers"], ) cc_library( name = "zlib_inflate_chunk_simd", srcs = [ "contrib/optimizations/chunkcopy.h", "contrib/optimizations/inffast_chunk.c", "contrib/optimizations/inffast_chunk.h", "contrib/optimizations/inflate.c", ], copts = zlib_warnings, features = ["-parse_headers"], local_defines = [ "INFLATE_CHUNK_READ_64LE", "ZLIB_IMPLEMENTATION", ] + select({ "@platforms//cpu:x86_64": [ "INFLATE_CHUNK_SIMD_SSE2", ], "@platforms//cpu:aarch64": [ "INFLATE_CHUNK_SIMD_NEON", ], }), deps = [":zlib_common_headers"], ) cc_library( name = "zlib_crc32_simd", srcs = [ "crc32_simd.c", "crc32_simd.h", "crc_folding.c", ], copts = [ "-msse4.2", "-mpclmul", ], local_defines = [ "CRC32_SIMD_SSE42_PCLMUL", "ZLIB_IMPLEMENTATION", ], target_compatible_with = select({ "@platforms//cpu:x86_64": [], "//conditions:default": ["@platforms//:incompatible"], }), deps = [":zlib_common_headers"], ) cc_library( name = "zlib_slide_hash_simd", srcs = [ "slide_hash_simd.h", ], # This target is header only so these are not needed to compile correctly, but still useful for # header parsing (i.e. parse_headers feature). local_defines = ["ZLIB_IMPLEMENTATION"] + select({ "@platforms//cpu:x86_64": ["DEFLATE_SLIDE_HASH_SSE2"], "@platforms//cpu:aarch64": ["DEFLATE_SLIDE_HASH_NEON"], }), deps = [":zlib_common_headers"], ) filegroup( name = "zlib_files", srcs = [ "adler32.c", "chromeconf.h", "compress.c", "contrib/optimizations/insert_string.h", "cpu_features.c", "cpu_features.h", "crc32.c", "crc32.h", "deflate.c", "deflate.h", "gzclose.c", "gzguts.h", "gzlib.c", "gzread.c", "gzwrite.c", "infback.c", "inffast.c", "inffast.h", "inffixed.h", "inflate.h", "inftrees.c", "inftrees.h", "trees.c", "trees.h", "uncompr.c", "zconf.h", "zlib.h", "zutil.c", "zutil.h", ], ) cc_library( name = "zlib", srcs = [":zlib_files"], copts = zlib_warnings, features = ["-parse_headers"], # Duplicate definitions here since we want to avoid non-local defines, some of them are needed # here. This excludes INFLATE_CHUNK_* which is only needed in zlib_inflate_chunk_simd. local_defines = ["ZLIB_IMPLEMENTATION"] + select({ "@platforms//cpu:x86_64": [ "ADLER32_SIMD_SSSE3", "CRC32_SIMD_SSE42_PCLMUL", "DEFLATE_SLIDE_HASH_SSE2", ], "@platforms//cpu:aarch64": [ "ADLER32_SIMD_NEON", "CRC32_ARMV8_CRC32", "DEFLATE_SLIDE_HASH_NEON", ], }) + select({ ":x86_linux": ["X86_NOT_WINDOWS"], ":x86_macos": ["X86_NOT_WINDOWS"], ":x86_windows": ["X86_WINDOWS"], ":arm64_linux": ["ARMV8_OS_LINUX"], "arm64_macos": ["ARMV8_OS_MACOS"], "arm64_windows": ["ARMV8_OS_WINDOWS"], "//conditions:default": [], }), visibility = ["//visibility:public"], deps = [ ":zlib_adler32_simd", ":zlib_inflate_chunk_simd", ":zlib_slide_hash_simd", ] + select({ "@platforms//cpu:x86_64": [":zlib_crc32_simd"], "@platforms//cpu:aarch64": [":zlib_arm_crc32"], }), ) # Chromium zlib includes some custom compression utils that are not present in mainline zlib but # used by V8. cc_library( name = "compression_utils_portable", srcs = ["google/compression_utils_portable.cc"], hdrs = ["google/compression_utils_portable.h"], visibility = ["//visibility:public"], deps = [":zlib"], )