Skip to content

feat: update htslib to 1.25 - #37

Open
tfenne wants to merge 3 commits into
rust-bio:mainfrom
tfenne:htslib-1.25
Open

tfenne wants to merge 3 commits into
rust-bio:mainfrom
tfenne:htslib-1.25

Conversation

@tfenne

@tfenne tfenne commented Nov 13, 2026

Copy link
Copy Markdown

Summary

Updates the bundled htslib from 1.23 to 1.25, and fixes the build so that lzma and bzip2 support are actually compiled in when their features are enabled. The 1.23 bump on main (#36) was never released, so for users the next release moves htslib from 1.19.1 (hts-sys 2.2.1) to 1.25.

A matching rust-htslib PR is ready and will be opened as soon as there is an hts-sys release it can reference.

Changes

  • htslib 1.23 → 1.25 (htscodecs 1.6.5 → 1.6.7). The library source list in the 1.25 Makefile (LIBHTS_OBJS) is unchanged, so build.rs's FILES needs no update.
  • HAVE___BUILTIN_PREFETCH added to the generated config.h, mirroring a new configure check in htslib 1.24+. Without it, hts_prefetch() (used in the pileup hot loop) falls back to a volatile read.
  • Fix: lzma support was never compiled in. The lzma block in build.rs defined HAVE_LIBBZ2 instead of HAVE_LIBLZMA, so with the lzma feature on, htslib could not decode CRAM blocks using LZMA ("Lzma compression is not compiled into this version") or identify xz-compressed files. It now defines HAVE_LIBLZMA and HAVE_LZMA_H. lzma-sys is built static, so it always provides lzma.h, including on macOS.
  • Fix: bzip2 support depended on that typo when using a system libbz2. bzip2-sys exports an include dir only when it builds bzip2 from source; when it links a system libbz2 found via pkg-config it exports nothing, so the bzip2 block never set HAVE_LIBBZ2, and bz2 support worked only because the lzma block set it by mistake. Observed on macOS: --no-default-features --features bzip2 had no bz2 support, and --features lzma alone claimed bz2. The same should apply on Linux whenever pkg-config finds libbz2 (e.g. with libbz2-dev installed), but that was not tested against the old build.rs. HAVE_LIBBZ2 is now set whenever the bzip2 feature is on, and the include dir is added when one is exported.
  • New tests/codec_support.rs: asserts via hts_features() that bz2/lzma are compiled in exactly when their features are enabled, and decodes an xz-compressed SAM header through hts_detect_format2 to exercise liblzma for real. These tests fail on the previous build.rs.
  • Prebuilt bindings regenerated with bindgen 0.72.1. linux_prebuilt_bindings.rs on main had been generated on macOS (it contained __darwin_va_list, with the _ link-name prefixes removed by hand); it is now generated on Linux, so libc declarations and va_list are correct for glibc. All htslib struct and union definitions are identical between the two files. osx_prebuilt_bindings.rs is generated on arm64; the 2.2.1 release's was x86_64. The only htslib-relevant difference is va_list (affects cram_set_voption and kvsprintf).
  • README: the binding-regeneration recipes shared the host target/ (so the Linux recipe could pick up macOS bindings) and globbed hts-sys-*/out, which current cargo (build/hts-sys/<hash>/out) no longer produces. Both recipes now use a dedicated target dir and reproduce the committed files byte for byte.

API changes since the last release (2.2.1 / htslib 1.19.1)

For htslib's own API this is purely additive: no htslib functions, constants, structs or type aliases removed, and no signatures or struct layouts changed. The only modified htslib items are parameter renames (kstrstr, kstrnstr, kmemmem, bcf_sr_remove_reader) and the HTS_VERSION value.

The system declarations bindgen sweeps in (libc, platform headers) vary with the generation host. Comparing the Linux files like for like against 2.2.1, nothing at all was removed, two glibc float128 aliases were added, and a few glibc version constants changed. The macOS file loses about 46 x86_64-only mcontext bitfield accessors because it is now generated on arm64.

Added since 1.19.1 include: bcf_itr_querys1, bcf_itr_regarray, bcf_itr_regions, tbx_itr_querys1, tbx_itr_regarray, tbx_itr_regions, tbx_itr_next1, sam_hdr_remove_tag_all, khgetline, kfgetline, bcf_sr_add_hreader, BCF_VL_P/LA/LG/LR/M, BAM_MAX_QNAME_LEN, CRAM_OPT_RM_UR, FASTQ_OPT_UMI/FASTQ_OPT_UMI_REGEX, BCF_SR_PAIR_ID, tbx_conf_gaf, and new bcf_sr_error values (bcf_sr_ok, bcf_sr_open_failed, bcf_sr_seek_error, bcf_sr_regions_error, bcf_sr_samples_error).

Behaviour changes users will see

From htslib's NEWS, 1.19.1 → 1.25:

  • GT phasing (1.22, extended to all VCF/BCF versions in 1.23): the reader sets the phase bit on the first GT allele when all other alleles are phased. E.g. 1|1 now decodes as both alleles phased; 1.19.1 left the first unphased. Code comparing decoded genotypes structurally will see different results.
  • CRAM output version (1.22): the default CRAM version written is now 3.1 instead of 3.0. htslib has been able to read 3.1 since 1.12, but older or third-party readers may not.
  • BGZF thread pools (1.24): a second bgzf_thread_pool() call on the same handle now returns -2 instead of silently replacing (and leaking) the first pool.
  • CRAM reference fetching (1.22): htslib no longer falls back to EBI's reference server when REF_PATH is unset.
  • SAM headers (1.22): @SQ lines with multiple differing LN values now fail to parse.
  • CRAM (1.22): range queries via SEEK_CUR were removed.
  • VCF Number= (1.23): VCFv4.4/4.5 Number=P/LA/LG/LR/M are now parsed as new BCF_VL_* values; exhaustive matches on BCF_VL_* downstream need new arms.
  • Synced reader (1.24): open failures set errnum to bcf_sr_open_failed (11). Previously they set open_failed, which is 0 and indistinguishable from success.
  • This PR: lzma support is now present when the lzma feature is enabled (it was silently absent before).

Versioning

This PR is titled feat:, so release-please will cut 2.3.0. If you would rather release it as 3.0.0, retitling to feat!: before merging is all that's needed. The evidence either way:

  • 2.3.0: the change to htslib's Rust API is purely additive (above).
  • 3.0.0: rust-htslib 1.x declares hts-sys = "2.2.0" (i.e. ^2.2), so a 2.3.0 release reaches every existing rust-htslib user on cargo update, bringing the behaviour changes above (notably GT phasing) without rust-htslib opting in. As 3.0.0, rust-htslib adopts it explicitly, and can pair it with its own pending breaking release.

The lzma/bzip2 fix is a separate fix: commit for easier review. Since PRs here are squash-merged, it won't get its own changelog entry; happy to split it into its own PR if you'd prefer that.

#24 (lzma-sys → liblzma-sys) doesn't touch build.rs and shouldn't conflict; the new codec tests will catch it if the swap ever stops lzma being compiled in.

Testing

  • macOS arm64: cargo test with default features, --no-default-features, --features bzip2, --features lzma, --features bzip2,lzma, and --all-features; plus cargo fmt --check and cargo clippy --all-targets. bzip2-sys used the system libbz2 here.
  • Linux arm64 (Docker): the same six configurations. All but --all-features (which enables bindgen) use the prebuilt Linux bindings. bzip2 was also built from source, with no system bzlib.h or pkg-config entry, to exercise the DEP_BZIP2_INCLUDE path.
  • New codec tests confirmed failing on the previous build.rs (macOS) and passing after the fix.
  • Coverage limits: lzma gets a real decode in the tests; bz2 is verified as compiled in and linked (libhts.a references BZ2_*) but no bz2 data is decoded.
  • rust-htslib's full test suite against this branch, via a [patch.crates-io] path override. Its adaptations to htslib's behaviour changes (e.g. the new Number= kinds) live on the rust-htslib side; nothing further was needed here.
    • Linux amd64, prebuilt bindings, default features (bzip2 + lzma + curl with static OpenSSL): 168 tests + 38 doctests pass.
    • Linux arm64, all features except bindgen: 170 tests + 38 doctests pass with both live bindgen and the prebuilt bindings.
    • macOS arm64, default, --all-features and --no-default-features: all pass. The prebuilt macOS bindings were also tested (157 tests + 38 doctests pass) before the lzma/bz2 fix, which does not change the bindings.

Bump the htslib submodule from 1.23 to 1.25 (htscodecs 1.6.5 -> 1.6.7).
The library source list in the 1.25 Makefile is unchanged.

- Define HAVE___BUILTIN_PREFETCH in the generated config.h, mirroring
  the configure check added in htslib 1.24.
- Regenerate the prebuilt bindings with bindgen 0.72.1. The Linux file
  is now generated on Linux; the previous one had been generated on
  macOS.
- Fix the README binding-regeneration recipes: use a dedicated target
  dir, and locate bindings.rs in both the old and new cargo build-dir
  layouts.
The lzma block in build.rs defined HAVE_LIBBZ2 instead of HAVE_LIBLZMA,
so lzma support was never compiled in. bzip2-sys only exports an include
dir when it builds bzip2 from source, so with a system libbz2 the bzip2
block never defined HAVE_LIBBZ2 either; bz2 support worked only through
the lzma block's typo.

Define HAVE_LIBLZMA/HAVE_LZMA_H whenever the lzma feature is enabled
(lzma-sys always ships lzma.h) and HAVE_LIBBZ2 whenever the bzip2
feature is enabled, and add tests asserting each codec is compiled in
exactly when its feature is on, including a real xz decode.
@tfenne

tfenne commented Nov 13, 2026

Copy link
Copy Markdown
Author

The numbers on the PR are large(!) at +3,656 / -4,819, but the vast majority of that is the rebuilt linux and mac bindings, which account for +3,535 / -4,794, so the non-bindings changes are actually just +121 / -25.

@johanneskoester johanneskoester left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@johanneskoester

Copy link
Copy Markdown
Contributor

Still failing in the CI.

Homebrew no longer recognises curl-openssl, so the macOS jobs failed in
the "Install htslib dependencies" step. The formula has been named curl
for years.
@tfenne

tfenne commented Nov 16, 2026

Copy link
Copy Markdown
Author

Thanks @johanneskoester - seems like the CI failure was an unrelated shift in the curl packages available via brew. I've fixed here but can make it a separate PR if that's preferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants