|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub fn build(b: *std.Build) void { |
| 4 | + const upstream = b.dependency("zstd", .{}); |
| 5 | + const target = b.standardTargetOptions(.{}); |
| 6 | + const optimize = b.standardOptimizeOption(.{}); |
| 7 | + |
| 8 | + const strip = b.option(bool, "strip", "Omit debug information"); |
| 9 | + const pic = b.option(bool, "pie", "Produce Position Independent Code"); |
| 10 | + |
| 11 | + const compression = b.option(bool, "compression", "build compression module") orelse true; |
| 12 | + const decompression = b.option(bool, "decompression", "build decompression module") orelse true; |
| 13 | + const dictbuilder = b.option(bool, "dictbuilder", "build dictbuilder module") orelse compression; |
| 14 | + const deprecated = b.option(bool, "deprecated", "build deprecated module") orelse false; |
| 15 | + |
| 16 | + const minify = b.option(bool, "minify", "Configures a bunch of other options to space-optimized defaults") orelse false; |
| 17 | + const legacy_support = b.option(usize, "legacy-support", "makes it possible to decompress legacy zstd formats") orelse @as(usize, if (minify) 0 else 5); |
| 18 | + // For example, `-Dlegacy-support=0` means: no support for legacy formats |
| 19 | + // For example, `-Dlegacy-support=2` means: support legacy formats >= v0.2.0 |
| 20 | + std.debug.assert(legacy_support < 8); |
| 21 | + |
| 22 | + const disable_assembly = b.option(bool, "disable-assembly", "Assembly support") orelse false; |
| 23 | + const huf_force_decompress_x1 = b.option(bool, "huf-force-decompress-x1", "") orelse minify; |
| 24 | + const huf_force_decompress_x2 = b.option(bool, "huf-force-decompress-x2", "") orelse false; |
| 25 | + const force_decompress_sequences_short = b.option(bool, "force-decompress-sequences-short", "") orelse minify; |
| 26 | + const force_decompress_sequences_long = b.option(bool, "force-decompress-sequences-long", "") orelse false; |
| 27 | + const no_inline = b.option(bool, "no-inline", "Disable Inlining") orelse minify; |
| 28 | + const strip_error_strings = b.option(bool, "strip-error-strings", "removes the error messages that are otherwise returned by `ZSTD_getErrorName` (implied by `-Dminify`)") orelse minify; |
| 29 | + const exclude_compressors_dfast_and_up = b.option(bool, "exclude-compressors-dfast-and-up", "") orelse false; |
| 30 | + const exclude_compressors_greedy_and_up = b.option(bool, "exclude-compressors-greedy-and-up", "") orelse false; |
| 31 | + |
| 32 | + const zstd = b.addStaticLibrary(.{ |
| 33 | + .name = "zstd", |
| 34 | + .target = target, |
| 35 | + .optimize = optimize, |
| 36 | + .strip = strip, |
| 37 | + .pic = pic, |
| 38 | + .link_libc = true, |
| 39 | + }); |
| 40 | + b.installArtifact(zstd); |
| 41 | + zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = common_sources }); |
| 42 | + // zstd does not install into its own subdirectory. :( |
| 43 | + zstd.installHeader(upstream.path("lib/zstd.h"), "zstd.h"); |
| 44 | + zstd.installHeader(upstream.path("lib/zdict.h"), "zdict.h"); |
| 45 | + zstd.installHeader(upstream.path("lib/zstd_errors.h"), "zstd_errors.h"); |
| 46 | + if (compression) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = compression_sources }); |
| 47 | + if (decompression) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = decompress_sources }); |
| 48 | + if (dictbuilder) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = dict_builder_sources }); |
| 49 | + if (deprecated) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = deprecated_sources }); |
| 50 | + if (legacy_support != 0) { |
| 51 | + for (legacy_support..8) |i| zstd.addCSourceFile(.{ .file = upstream.path(b.fmt("lib/legacy/zstd_v0{d}.c", .{i})) }); |
| 52 | + } |
| 53 | + |
| 54 | + if (target.result.cpu.arch == .x86_64) { |
| 55 | + if (decompression) { |
| 56 | + zstd.addAssemblyFile(upstream.path("lib/decompress/huf_decompress_amd64.S")); |
| 57 | + } |
| 58 | + } else { |
| 59 | + zstd.defineCMacro("ZSTD_DISABLE_ASM", null); |
| 60 | + } |
| 61 | + |
| 62 | + zstd.defineCMacro("ZSTD_LEGACY_SUPPORT", b.fmt("{d}", .{legacy_support})); |
| 63 | + if (disable_assembly) zstd.defineCMacro("ZSTD_DISABLE_ASM", null); |
| 64 | + if (huf_force_decompress_x1) zstd.defineCMacro("HUF_FORCE_DECOMPRESS_X1", null); |
| 65 | + if (huf_force_decompress_x2) zstd.defineCMacro("HUF_FORCE_DECOMPRESS_X2", null); |
| 66 | + if (force_decompress_sequences_short) zstd.defineCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT", null); |
| 67 | + if (force_decompress_sequences_long) zstd.defineCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG", null); |
| 68 | + if (no_inline) zstd.defineCMacro("ZSTD_NO_INLINE", null); |
| 69 | + if (strip_error_strings) zstd.defineCMacro("ZSTD_STRIP_ERROR_STRINGS", null); |
| 70 | + if (exclude_compressors_dfast_and_up) { |
| 71 | + zstd.defineCMacro("ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR", null); |
| 72 | + zstd.defineCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", null); |
| 73 | + zstd.defineCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", null); |
| 74 | + zstd.defineCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", null); |
| 75 | + zstd.defineCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", null); |
| 76 | + zstd.defineCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", null); |
| 77 | + } |
| 78 | + if (exclude_compressors_greedy_and_up) { |
| 79 | + zstd.defineCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", null); |
| 80 | + zstd.defineCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", null); |
| 81 | + zstd.defineCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", null); |
| 82 | + zstd.defineCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", null); |
| 83 | + zstd.defineCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", null); |
| 84 | + } |
| 85 | + |
| 86 | + { |
| 87 | + const examples: []const []const u8 = &.{ |
| 88 | + "simple_compression", |
| 89 | + "simple_decompression", |
| 90 | + "multiple_simple_compression", |
| 91 | + "dictionary_compression", |
| 92 | + "dictionary_decompression", |
| 93 | + "streaming_compression", |
| 94 | + "streaming_decompression", |
| 95 | + "multiple_streaming_compression", |
| 96 | + "streaming_memory_usage", |
| 97 | + }; |
| 98 | + |
| 99 | + for (examples) |name| { |
| 100 | + const exe = b.addExecutable(.{ |
| 101 | + .name = name, |
| 102 | + .target = target, |
| 103 | + .optimize = optimize, |
| 104 | + }); |
| 105 | + exe.addCSourceFile(.{ .file = upstream.path(b.fmt("examples/{s}.c", .{name})) }); |
| 106 | + exe.addIncludePath(upstream.path("examples/common.c")); |
| 107 | + exe.linkLibrary(zstd); |
| 108 | + b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "examples" } } }).step); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +const common_sources: []const []const u8 = &.{ |
| 114 | + "common/debug.c", |
| 115 | + "common/zstd_common.c", |
| 116 | + "common/threading.c", |
| 117 | + "common/entropy_common.c", |
| 118 | + "common/fse_decompress.c", |
| 119 | + "common/xxhash.c", |
| 120 | + "common/error_private.c", |
| 121 | + "common/pool.c", |
| 122 | +}; |
| 123 | + |
| 124 | +const compression_sources: []const []const u8 = &.{ |
| 125 | + "compress/fse_compress.c", |
| 126 | + "compress/huf_compress.c", |
| 127 | + "compress/zstd_double_fast.c", |
| 128 | + "compress/zstd_compress_literals.c", |
| 129 | + "compress/zstdmt_compress.c", |
| 130 | + "compress/zstd_compress_superblock.c", |
| 131 | + "compress/zstd_opt.c", |
| 132 | + "compress/zstd_compress.c", |
| 133 | + "compress/zstd_compress_sequences.c", |
| 134 | + "compress/hist.c", |
| 135 | + "compress/zstd_ldm.c", |
| 136 | + "compress/zstd_lazy.c", |
| 137 | + "compress/zstd_fast.c", |
| 138 | +}; |
| 139 | + |
| 140 | +const decompress_sources: []const []const u8 = &.{ |
| 141 | + "decompress/zstd_decompress.c", |
| 142 | + "decompress/huf_decompress.c", |
| 143 | + "decompress/zstd_decompress_block.c", |
| 144 | + "decompress/zstd_ddict.c", |
| 145 | +}; |
| 146 | + |
| 147 | +const dict_builder_sources: []const []const u8 = &.{ |
| 148 | + "dictBuilder/divsufsort.c", |
| 149 | + "dictBuilder/zdict.c", |
| 150 | + "dictBuilder/cover.c", |
| 151 | + "dictBuilder/fastcover.c", |
| 152 | +}; |
| 153 | + |
| 154 | +const deprecated_sources: []const []const u8 = &.{ |
| 155 | + "deprecated/zbuff_decompress.c", |
| 156 | + "deprecated/zbuff_common.c", |
| 157 | + "deprecated/zbuff_compress.c", |
| 158 | +}; |
| 159 | + |
| 160 | +const legacy_sources: []const []const u8 = &.{ |
| 161 | + "legacy/zstd_v01.c", |
| 162 | + "legacy/zstd_v02.c", |
| 163 | + "legacy/zstd_v03.c", |
| 164 | + "legacy/zstd_v04.c", |
| 165 | + "legacy/zstd_v05.c", |
| 166 | + "legacy/zstd_v06.c", |
| 167 | + "legacy/zstd_v07.c", |
| 168 | +}; |
0 commit comments