# Makefile for htslib, a C library for high-throughput sequencing data formats.
#
#    Copyright (C) 2013-2024 Genome Research Ltd.
#
#    Author: John Marshall <jm18@sanger.ac.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

CC     = gcc
AR     = ar
RANLIB = ranlib

# Default libraries to link if configure is not used
htslib_default_libs = -lz -lm -lbz2 -llzma -lcurl

CPPFLAGS =
# TODO: make the 64-bit support for VCF optional via configure, for now add -DVCF_ALLOW_INT64
#       to CFLAGS manually, here or in config.mk if the latter exists.
# TODO: probably update cram code to make it compile cleanly with -Wc++-compat
# For testing strict C99 support add -std=c99 -D_XOPEN_SOURCE=600
#CFLAGS   = -g -Wall -O2 -pedantic -std=c99 -D_XOPEN_SOURCE=600
CFLAGS   = -g -Wall -O2 -fvisibility=hidden
EXTRA_CFLAGS_PIC = -fpic
TARGET_CFLAGS =
LDFLAGS  = -fvisibility=hidden
VERSION_SCRIPT_LDFLAGS = -Wl,-version-script,$(srcprefix)htslib.map
LIBS     = $(htslib_default_libs)

prefix      = /usr/local
exec_prefix = $(prefix)
bindir      = $(exec_prefix)/bin
includedir  = $(prefix)/include
libdir      = $(exec_prefix)/lib
libexecdir  = $(exec_prefix)/libexec
datarootdir = $(prefix)/share
mandir      = $(datarootdir)/man
man1dir     = $(mandir)/man1
man5dir     = $(mandir)/man5
man7dir     = $(mandir)/man7
pkgconfigdir= $(libdir)/pkgconfig

MKDIR_P = mkdir -p
INSTALL = install -p
INSTALL_DATA    = $(INSTALL) -m 644
INSTALL_DIR     = $(MKDIR_P) -m 755
LIB_PERM        = 644
INSTALL_LIB     = $(INSTALL) -m $(LIB_PERM)
INSTALL_MAN     = $(INSTALL_DATA)
INSTALL_PROGRAM = $(INSTALL)

# Set by config.mk if plugins are enabled
plugindir =

BUILT_PROGRAMS = \
	annot-tsv \
	bgzip \
	htsfile \
	tabix

BUILT_TEST_PROGRAMS = 
#BUILT_TEST_PROGRAMS = \
#	test/hts_endian \
#	test/fieldarith \
#	test/hfile \
#	test/pileup \
#	test/pileup_mod \
#	test/plugins-dlhts \
#	test/sam \
#	test/test_bgzf \
#	test/test_expr \
#	test/test_faidx \
#	test/test_kfunc \
#	test/test_khash \
#	test/test_kstring \
#	test/test_mod \
#	test/test_nibbles \
#	test/test_realn \
#	test/test-regidx \
#	test/test_str2int \
#	test/test_time_funcs \
#	test/test_view \
#	test/test_index \
#	test/test-vcf-api \
#	test/test-vcf-sweep \
#	test/test-bcf-sr \
#	test/fuzz/hts_open_fuzzer.o \
#	test/test-bcf-translate \
#	test/test-parse-reg \
#	test/test_introspection \
#	test/test-bcf_set_variant_type

BUILT_THRASH_PROGRAMS = \
	test/thrash_threads1 \
	test/thrash_threads2 \
	test/thrash_threads3 \
	test/thrash_threads4 \
	test/thrash_threads5 \
	test/thrash_threads6 \
	test/thrash_threads7

all: lib-static lib-shared $(BUILT_PROGRAMS) plugins \
	$(BUILT_TEST_PROGRAMS) htslib_static.mk htslib-uninstalled.pc

# Report compiler and version
cc-version:
	-@$(CC) --version  2>/dev/null || true
	-@$(CC) --qversion 2>/dev/null || true
	-@$(CC) -V         2>/dev/null || true

ALL_CPPFLAGS = -I. $(CPPFLAGS)

# Usually htscodecs.mk is generated by running configure or config.status,
# but if those aren't used create a default here.
htscodecs.mk:
	echo '# Default htscodecs.mk generated by Makefile' > $@
	echo 'include $$(HTSPREFIX)htscodecs_bundled.mk' >> $@
	$(srcdir)/hts_probe_cc.sh '$(CC)' '$(CFLAGS) $(CPPFLAGS)' '$(LDFLAGS)' >> $@

srcdir = .
srcprefix =
HTSPREFIX =

# Flags for SIMD code
HTS_CFLAGS_AVX2 =
HTS_CFLAGS_AVX512 =
HTS_CFLAGS_SSE4 =

# Control building of SIMD code.  Not used if configure has been run.
HTS_BUILD_AVX2 =
HTS_BUILD_AVX512 =
HTS_BUILD_SSE4 =

include htslib_vars.mk
include htscodecs.mk

# If not using GNU make, you need to copy the version number from version.sh
# into here.
PACKAGE_VERSION := $(shell $(srcdir)/version.sh)

LIBHTS_SOVERSION = 3

# Version numbers for the Mac dynamic library.  Note that the leading 3
# is not strictly necessary and should be removed the next time
# LIBHTS_SOVERSION is bumped (see #1144 and
# https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html#//apple_ref/doc/uid/TP40002013-SW23)
MACH_O_COMPATIBILITY_VERSION = 3.1.21
MACH_O_CURRENT_VERSION = 3.1.21

# Force version.h to be remade if $(PACKAGE_VERSION) has changed.
version.h: $(if $(wildcard version.h),$(if $(findstring "$(PACKAGE_VERSION)",$(shell cat version.h)),,force))

version.h:
	echo '#define HTS_VERSION_TEXT "$(PACKAGE_VERSION)"' > $@

print-version:
	@echo $(PACKAGE_VERSION)

show-version:
	@echo PACKAGE_VERSION = $(PACKAGE_VERSION)

config_vars.h: override escape=$(subst ',\x27,$(subst ",\",$(subst \,\\,$(1))))
config_vars.h: override hts_cc_escaped=$(call escape,$(CC))
config_vars.h: override hts_cppflags_escaped=$(call escape,$(CPPFLAGS))
config_vars.h: override hts_cflags_escaped=$(call escape,$(CFLAGS))
config_vars.h: override hts_ldflags_escaped=$(call escape,$(LDFLAGS))
config_vars.h: override hts_libs_escaped=$(call escape,$(LIBS))

config_vars.h:
	printf '#define HTS_CC "%s"\n#define HTS_CPPFLAGS "%s"\n#define HTS_CFLAGS "%s"\n#define HTS_LDFLAGS "%s"\n#define HTS_LIBS "%s"\n' \
	'$(hts_cc_escaped)' \
	'$(hts_cppflags_escaped)' \
	'$(hts_cflags_escaped)' \
	'$(hts_ldflags_escaped)' \
	'$(hts_libs_escaped)' > $@

.SUFFIXES: .bundle .c .cygdll .dll .o .pico .so

.c.o:
	$(CC) $(CFLAGS) $(TARGET_CFLAGS) $(ALL_CPPFLAGS) -c -o $@ $<

.c.pico:
	$(CC) $(CFLAGS) $(TARGET_CFLAGS) $(ALL_CPPFLAGS) $(EXTRA_CFLAGS_PIC) -c -o $@ $<


LIBHTS_OBJS = \
	kfunc.o \
	kstring.o \
	bcf_sr_sort.o \
	bgzf.o \
	errmod.o \
	faidx.o \
	header.o \
	hfile.o \
	hts.o \
	hts_expr.o \
	hts_os.o\
	md5.o \
	multipart.o \
	probaln.o \
	realn.o \
	regidx.o \
	region.o \
	sam.o \
	sam_mods.o \
	simd.o \
	synced_bcf_reader.o \
	vcf_sweep.o \
	tbx.o \
	textutils.o \
	thread_pool.o \
	vcf.o \
	vcfutils.o \
	cram/cram_codecs.o \
	cram/cram_decode.o \
	cram/cram_encode.o \
	cram/cram_external.o \
	cram/cram_index.o \
	cram/cram_io.o \
	cram/cram_stats.o \
	cram/mFILE.o \
	cram/open_trace_file.o \
	cram/pooled_alloc.o \
	cram/string_alloc.o \
	$(HTSCODECS_OBJS) \
	$(NONCONFIGURE_OBJS)

# Without configure we wish to have a rich set of default figures,
# but we still need conditional inclusion as we wish to still
# support ./configure --disable-blah.
NONCONFIGURE_OBJS = hfile_libcurl.o

PLUGIN_EXT  =
PLUGIN_OBJS =

cram_h = cram/cram.h $(cram_samtools_h) $(header_h) $(cram_structs_h) $(cram_io_h) cram/cram_encode.h cram/cram_decode.h cram/cram_stats.h cram/cram_codecs.h cram/cram_index.h $(htslib_cram_h)
cram_io_h = cram/cram_io.h $(cram_misc_h)
cram_misc_h = cram/misc.h
cram_os_h = cram/os.h $(htslib_hts_endian_h)
cram_samtools_h = cram/cram_samtools.h $(htslib_sam_h)
cram_structs_h = cram/cram_structs.h $(htslib_thread_pool_h) $(htslib_cram_h) cram/string_alloc.h cram/mFILE.h $(htslib_khash_h)
cram_open_trace_file_h = cram/open_trace_file.h cram/mFILE.h
bcf_sr_sort_h = bcf_sr_sort.h $(htslib_synced_bcf_reader_h) $(htslib_kbitset_h)
fuzz_settings_h = fuzz_settings.h
header_h = header.h cram/string_alloc.h cram/pooled_alloc.h $(htslib_khash_h) $(htslib_kstring_h) $(htslib_sam_h)
hfile_internal_h = hfile_internal.h $(htslib_hts_defs_h) $(htslib_hfile_h) $(textutils_internal_h)
hts_internal_h = hts_internal.h $(htslib_hts_h) $(textutils_internal_h)
hts_time_funcs_h = hts_time_funcs.h
sam_internal_h = sam_internal.h $(htslib_sam_h)
textutils_internal_h = textutils_internal.h $(htslib_kstring_h)
thread_pool_internal_h = thread_pool_internal.h $(htslib_thread_pool_h)

# To be effective, config.mk needs to appear after most Makefile variables are
# set but before most rules appear, so that it can both use previously-set
# variables in its own rules' prerequisites and also update variables for use
# in later rules' prerequisites.

# If your make doesn't accept -include, change this to 'include' if you are
# using the configure script or just comment the line out if you are not.
-include config.mk

# Usually config.h is generated by running configure or config.status,
# but if those aren't used create a default config.h here.
config.h:
	echo '/* Default config.h generated by Makefile */' > $@
	echo '#ifndef _XOPEN_SOURCE' >> $@
	echo '#define _XOPEN_SOURCE 600' >> $@
	echo '#endif' >> $@
	echo '#define HAVE_LIBBZ2 1' >> $@
	echo '#define HAVE_LIBLZMA 1' >> $@
	echo '#ifndef __APPLE__' >> $@
	echo '#define HAVE_LZMA_H 1' >> $@
	echo '#endif' >> $@
	echo '#define HAVE_DRAND48 1' >> $@
	echo '#define HAVE_LIBCURL 1' >> $@
	if [ "x$(HTS_HAVE_CPUID)" != "x" ]; then \
	    echo '#define HAVE_DECL___CPUID_COUNT 1' >> $@ ; \
	    echo '#define HAVE_DECL___GET_CPUID_MAX 1' >> $@ ; \
	fi
	if [ "x$(HTS_BUILD_SSE4)" != "x" ]; then \
	    echo '#define HAVE_POPCNT 1' >> $@ ; \
	    echo '#define HAVE_SSE4_1 1' >> $@ ; \
	    echo '#define HAVE_SSSE3 1' >> $@ ; \
	    echo '#if defined(HTS_ALLOW_UNALIGNED) && HTS_ALLOW_UNALIGNED == 0' >> $@ ; \
	    echo '#define UBSAN 1' >> $@ ; \
	    echo '#endif' >> $@ ; \
	fi
	if [ "x$(HTS_BUILD_AVX2)" != "x" ] ; then \
	    echo '#define HAVE_AVX2 1' >> $@ ; \
	fi
	if [ "x$(HTS_BUILD_AVX512)" != "x" ] ; then \
	    echo '#define HAVE_AVX512 1' >> $@ ; \
	fi
	echo '#if defined __x86_64__ || defined __arm__ || defined __aarch64__' >> $@
	echo '#define HAVE_ATTRIBUTE_CONSTRUCTOR 1' >> $@
	echo '#endif' >> $@
	echo '#if (defined(__x86_64__) || defined(_M_X64))' >> $@
	echo '#define HAVE_ATTRIBUTE_TARGET 1' >> $@
	echo '#define HAVE_BUILTIN_CPU_SUPPORT_SSSE3 1' >> $@
	echo '#endif' >> $@

# And similarly for htslib.pc.tmp ("pkg-config template").  No dependency
# on htslib.pc.in listed, as if that file is newer the usual way to regenerate
# this target is via configure or config.status rather than this rule.
htslib.pc.tmp:
	sed -e '/^static_libs=/s/@static_LIBS@/$(htslib_default_libs)/;s#@[^-][^@]*@##g' $(srcprefix)htslib.pc.in > $@

# Create a makefile fragment listing the libraries and LDFLAGS needed for
# static linking.  This can be included by projects that want to build
# and link against the htslib source tree instead of an installed library.
htslib_static.mk: htslib.pc.tmp
	sed -n '/^static_libs=/s/[^=]*=/HTSLIB_static_LIBS = /p;/^static_ldflags=/s/[^=]*=/HTSLIB_static_LDFLAGS = /p' $< > $@


lib-static: libhts.a

# $(shell), :=, and ifeq/.../endif are GNU Make-specific.  If you don't have
# GNU Make, comment out the parts of these conditionals that don't apply.
ifneq "$(origin PLATFORM)" "file"
PLATFORM := $(shell uname -s)
endif
ifeq "$(PLATFORM)" "Darwin"
SHLIB_FLAVOUR = dylib
lib-shared: libhts.dylib
else ifeq "$(findstring CYGWIN,$(PLATFORM))" "CYGWIN"
SHLIB_FLAVOUR = cygdll
lib-shared: cyghts-$(LIBHTS_SOVERSION).dll
else ifeq "$(findstring MSYS,$(PLATFORM))" "MSYS"
SHLIB_FLAVOUR = dll
lib-shared: hts-$(LIBHTS_SOVERSION).dll hts-$(LIBHTS_SOVERSION).def hts-$(LIBHTS_SOVERSION).lib
else ifeq "$(findstring MINGW,$(PLATFORM))" "MINGW"
SHLIB_FLAVOUR = dll
lib-shared: hts-$(LIBHTS_SOVERSION).dll hts-$(LIBHTS_SOVERSION).def hts-$(LIBHTS_SOVERSION).lib
else
SHLIB_FLAVOUR = so
lib-shared: libhts.so
endif

BUILT_PLUGINS = $(PLUGIN_OBJS:.o=$(PLUGIN_EXT))

ifneq "$(BUILT_PLUGINS)" ""
plugins: lib-shared
endif
plugins: $(BUILT_PLUGINS)


libhts.a: $(LIBHTS_OBJS)
	@-rm -f $@
	$(AR) -rc $@ $(LIBHTS_OBJS)
	-$(RANLIB) $@

print-config:
	@echo HTS_CFLAGS_AVX2 = $(HTS_CFLAGS_AVX2)
	@echo HTS_CFLAGS_AVX512 = $(HTS_CFLAGS_AVX512)
	@echo HTS_CFLAGS_SSE4 = $(HTS_CFLAGS_SSE4)
	@echo LDFLAGS = $(LDFLAGS)
	@echo LIBHTS_OBJS = $(LIBHTS_OBJS)
	@echo LIBS = $(LIBS)
	@echo PLATFORM = $(PLATFORM)

# The target here is libhts.so, as that is the built file that other rules
# depend upon and that is used when -lhts appears in other program's recipes.
# As a byproduct invisible to make, libhts.so.NN is also created, as it is the
# file used at runtime (when $LD_LIBRARY_PATH includes the build directory).

libhts.so: $(LIBHTS_OBJS:.o=.pico)
	$(CC) -shared -Wl,-soname,libhts.so.$(LIBHTS_SOVERSION) $(VERSION_SCRIPT_LDFLAGS) $(LDFLAGS) -o $@ $(LIBHTS_OBJS:.o=.pico) $(LIBS) -lpthread
	ln -sf $@ libhts.so.$(LIBHTS_SOVERSION)

# Similarly this also creates libhts.NN.dylib as a byproduct, so that programs
# when run can find this uninstalled shared library (when $DYLD_LIBRARY_PATH
# includes this project's build directory).

libhts.dylib: $(LIBHTS_OBJS)
	$(CC) -dynamiclib -install_name $(libdir)/libhts.$(LIBHTS_SOVERSION).dylib -current_version $(MACH_O_CURRENT_VERSION) -compatibility_version $(MACH_O_COMPATIBILITY_VERSION) $(LDFLAGS) -o $@ $(LIBHTS_OBJS) $(LIBS)
	ln -sf $@ libhts.$(LIBHTS_SOVERSION).dylib

cyghts-$(LIBHTS_SOVERSION).dll libhts.dll.a: $(LIBHTS_OBJS)
	$(CC) -shared -Wl,--out-implib=libhts.dll.a -Wl,--enable-auto-import $(LDFLAGS) -o $@ -Wl,--whole-archive $(LIBHTS_OBJS) -Wl,--no-whole-archive $(LIBS) -lpthread

hts-$(LIBHTS_SOVERSION).dll hts.dll.a: $(LIBHTS_OBJS)
	$(CC) -shared -Wl,--out-implib=hts.dll.a -Wl,--enable-auto-import -Wl,--exclude-all-symbols $(LDFLAGS) -o $@ -Wl,--whole-archive $(LIBHTS_OBJS) -Wl,--no-whole-archive $(LIBS) -lpthread

hts-$(LIBHTS_SOVERSION).def: hts-$(LIBHTS_SOVERSION).dll
	gendef hts-$(LIBHTS_SOVERSION).dll

hts-$(LIBHTS_SOVERSION).lib: hts-$(LIBHTS_SOVERSION).def
	dlltool -m i386:x86-64 -d hts-$(LIBHTS_SOVERSION).def -l hts-$(LIBHTS_SOVERSION).lib

# Bundling libraries, binaries, dll dependencies, and licenses into a
# single directory.  NB: This is not needed for end-users, but a test bed
# for maintainers building binary distributions.
#
# NOTE: only tested on the supported MSYS2/MINGW64 environment.
dist-windows: DESTDIR=
dist-windows: prefix=dist-windows
dist-windows: install
	cp hts-$(LIBHTS_SOVERSION).def hts-$(LIBHTS_SOVERSION).lib dist-windows/lib
	cp `ldd hts-$(LIBHTS_SOVERSION).dll| awk '/mingw64/ {print $$3}'` dist-windows/bin
	mkdir -p dist-windows/share/licenses/htslib
	-cp -r /mingw64/share/licenses/mingw-w64-libraries \
	      /mingw64/share/licenses/brotli \
	      /mingw64/share/licenses/bzip2 \
	      /mingw64/share/licenses/gcc-libs \
	      /mingw64/share/licenses/libdeflate \
	      /mingw64/share/licenses/libpsl \
	      /mingw64/share/licenses/libtre \
	      /mingw64/share/licenses/libwinpthread \
	      /mingw64/share/licenses/openssl \
	      /mingw64/share/licenses/xz \
	      /mingw64/share/licenses/zlib \
	      /mingw64/share/licenses/zstd \
	      dist-windows/share/licenses/
	-cp -r /usr/share/licenses/curl \
	      dist-windows/share/licenses/
	cp LICENSE dist-windows/share/licenses/htslib/


# Target to allow htslib.mk to build all the object files before it
# links the shared and static libraries.
hts-object-files: $(LIBHTS_OBJS)
	touch $@

# On Unix dlopen("libhts.so.NN", RTLD_LAZY) may default to RTLD_LOCAL.
# Hence plugins need to link to (shared) libhts.so.NN themselves, as they
# may not be able to access libhts symbols via the main program's libhts
# if that was dynamically loaded without an explicit RTLD_GLOBAL.
%.so: %.pico libhts.so
	$(CC) -shared -Wl,-E $(LDFLAGS) -o $@ $< libhts.so $(LIBS) -lpthread

# For programs *statically* linked to libhts.a, on macOS loading a plugin
# linked to a shared libhts.NN.dylib would lead to conflicting duplicate
# symbols.  Fortunately macOS dlopen() defaults to RTLD_GLOBAL so there
# is less need for plugins to link back to libhts themselves.
%.bundle: %.o
	$(CC) -bundle -Wl,-undefined,dynamic_lookup $(LDFLAGS) -o $@ $< $(LIBS)

%.cygdll: %.o libhts.dll.a
	$(CC) -shared $(LDFLAGS) -o $@ $< libhts.dll.a $(LIBS)

%.dll: %.o hts.dll.a
	$(CC) -shared $(LDFLAGS) -o $@ $< hts.dll.a $(LIBS)


bgzf.o bgzf.pico: bgzf.c config.h $(htslib_hts_h) $(htslib_bgzf_h) $(htslib_hfile_h) $(htslib_thread_pool_h) $(htslib_hts_endian_h) cram/pooled_alloc.h $(hts_internal_h) $(htslib_khash_h)
errmod.o errmod.pico: errmod.c config.h $(htslib_hts_h) $(htslib_ksort_h) $(htslib_hts_os_h)
kstring.o kstring.pico: kstring.c config.h $(htslib_kstring_h)
header.o header.pico: header.c config.h $(textutils_internal_h) $(header_h)
hfile.o hfile.pico: hfile.c config.h $(htslib_hfile_h) $(hfile_internal_h) $(htslib_kstring_h) $(hts_internal_h) $(htslib_khash_h)
hfile_gcs.o hfile_gcs.pico: hfile_gcs.c config.h $(htslib_hts_h) $(htslib_kstring_h) $(hfile_internal_h)
hfile_libcurl.o hfile_libcurl.pico: hfile_libcurl.c config.h $(hfile_internal_h) $(htslib_hts_h) $(htslib_kstring_h) $(htslib_khash_h)
hfile_s3_write.o hfile_s3_write.pico: hfile_s3_write.c config.h $(hfile_internal_h) $(htslib_hts_h) $(htslib_kstring_h) $(htslib_khash_h)
hfile_s3.o hfile_s3.pico: hfile_s3.c config.h $(hfile_internal_h) $(htslib_hts_h) $(htslib_kstring_h) $(hts_time_funcs_h)
hts.o hts.pico: hts.c config.h os/lzma_stub.h $(htslib_hts_h) $(htslib_bgzf_h) $(cram_h) $(htslib_hfile_h) $(htslib_hts_endian_h) version.h config_vars.h $(hts_internal_h) $(hfile_internal_h) $(sam_internal_h) $(htslib_hts_expr_h) $(htslib_hts_os_h) $(htslib_khash_h) $(htslib_kseq_h) $(htslib_ksort_h) $(htslib_tbx_h) $(htscodecs_htscodecs_h)
hts_expr.o hts_expr.pico: hts_expr.c config.h $(htslib_hts_expr_h) $(htslib_hts_log_h) $(textutils_internal_h)
hts_os.o hts_os.pico: hts_os.c config.h $(htslib_hts_defs_h) os/rand.c
vcf.o vcf.pico: vcf.c config.h $(fuzz_settings_h) $(htslib_vcf_h) $(htslib_bgzf_h) $(htslib_tbx_h) $(htslib_hfile_h) $(hts_internal_h) $(htslib_khash_str2int_h) $(htslib_kstring_h) $(htslib_sam_h) $(htslib_khash_h) $(htslib_kseq_h) $(htslib_hts_endian_h)
sam.o sam.pico: sam.c config.h $(fuzz_settings_h) $(htslib_hts_defs_h) $(htslib_sam_h) $(htslib_bgzf_h) $(cram_h) $(hts_internal_h) $(sam_internal_h) $(htslib_hfile_h) $(htslib_hts_endian_h) $(htslib_hts_expr_h) $(header_h) $(htslib_khash_h) $(htslib_kseq_h) $(htslib_kstring_h)
sam_mods.o sam_mods.pico: sam_mods.c config.h $(htslib_sam_h) $(textutils_internal_h)
simd.o simd.pico: simd.c config.h $(htslib_sam_h) $(sam_internal_h)
tbx.o tbx.pico: tbx.c config.h $(htslib_tbx_h) $(htslib_bgzf_h) $(htslib_hts_endian_h) $(hts_internal_h) $(htslib_khash_h)
faidx.o faidx.pico: faidx.c config.h $(htslib_bgzf_h) $(htslib_faidx_h) $(htslib_hfile_h) $(htslib_khash_h) $(htslib_kstring_h) $(hts_internal_h)
bcf_sr_sort.o bcf_sr_sort.pico: bcf_sr_sort.c config.h $(bcf_sr_sort_h) $(htslib_khash_str2int_h) $(htslib_kbitset_h)
synced_bcf_reader.o synced_bcf_reader.pico: synced_bcf_reader.c config.h $(htslib_synced_bcf_reader_h) $(htslib_kseq_h) $(htslib_khash_str2int_h) $(htslib_bgzf_h) $(htslib_thread_pool_h) $(bcf_sr_sort_h)
vcf_sweep.o vcf_sweep.pico: vcf_sweep.c config.h $(htslib_vcf_sweep_h) $(htslib_bgzf_h)
vcfutils.o vcfutils.pico: vcfutils.c config.h $(htslib_vcfutils_h) $(htslib_kbitset_h)
kfunc.o kfunc.pico: kfunc.c config.h $(htslib_kfunc_h)
regidx.o regidx.pico: regidx.c config.h $(htslib_hts_h) $(htslib_kstring_h) $(htslib_kseq_h) $(htslib_khash_str2int_h) $(htslib_regidx_h) $(hts_internal_h)
region.o region.pico: region.c config.h $(htslib_hts_h) $(htslib_khash_h)
md5.o md5.pico: md5.c config.h $(htslib_hts_h) $(htslib_hts_endian_h)
multipart.o multipart.pico: multipart.c config.h $(htslib_kstring_h) $(hts_internal_h) $(hfile_internal_h)
plugin.o plugin.pico: plugin.c config.h $(hts_internal_h) $(htslib_kstring_h)
probaln.o probaln.pico: probaln.c config.h $(htslib_hts_h)
realn.o realn.pico: realn.c config.h $(htslib_hts_h) $(htslib_sam_h)
textutils.o textutils.pico: textutils.c config.h $(htslib_hfile_h) $(htslib_kstring_h) $(htslib_sam_h) $(hts_internal_h)

cram/cram_codecs.o cram/cram_codecs.pico: cram/cram_codecs.c config.h $(fuzz_settings_h) $(htslib_hts_endian_h) $(htscodecs_varint_h) $(htscodecs_pack_h) $(htscodecs_rle_h) $(cram_h)
cram/cram_decode.o cram/cram_decode.pico: cram/cram_decode.c config.h $(cram_h) $(cram_os_h) $(htslib_hts_h)
cram/cram_encode.o cram/cram_encode.pico: cram/cram_encode.c config.h $(cram_h) $(cram_os_h) $(sam_internal_h) $(htslib_hts_h) $(htslib_hts_endian_h) $(textutils_internal_h)
cram/cram_external.o cram/cram_external.pico: cram/cram_external.c config.h $(htscodecs_rANS_static4x16_h) $(htslib_hfile_h) $(cram_h)
cram/cram_index.o cram/cram_index.pico: cram/cram_index.c config.h $(htslib_bgzf_h) $(htslib_hfile_h) $(hts_internal_h) $(cram_h) $(cram_os_h)
cram/cram_io.o cram/cram_io.pico: cram/cram_io.c config.h os/lzma_stub.h $(fuzz_settings_h) $(cram_h) $(cram_os_h) $(htslib_hts_h) $(cram_open_trace_file_h) $(htscodecs_rANS_static_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_arith_dynamic_h) $(htscodecs_tokenise_name3_h) $(htscodecs_fqzcomp_qual_h) $(htscodecs_varint_h) $(htslib_hfile_h) $(htslib_bgzf_h) $(htslib_faidx_h) $(hts_internal_h)
cram/cram_stats.o cram/cram_stats.pico: cram/cram_stats.c config.h $(cram_h) $(cram_os_h)
cram/mFILE.o cram/mFILE.pico: cram/mFILE.c config.h $(htslib_hts_log_h) $(cram_os_h) cram/mFILE.h
cram/open_trace_file.o cram/open_trace_file.pico: cram/open_trace_file.c config.h $(cram_os_h) $(cram_open_trace_file_h) $(cram_misc_h) $(htslib_hfile_h) $(htslib_hts_log_h) $(htslib_hts_h)
cram/pooled_alloc.o cram/pooled_alloc.pico: cram/pooled_alloc.c config.h cram/pooled_alloc.h $(cram_misc_h)
cram/string_alloc.o cram/string_alloc.pico: cram/string_alloc.c config.h cram/string_alloc.h
thread_pool.o thread_pool.pico: thread_pool.c config.h $(thread_pool_internal_h) $(htslib_hts_log_h)

htscodecs/htscodecs/arith_dynamic.o htscodecs/htscodecs/arith_dynamic.pico: htscodecs/htscodecs/arith_dynamic.c config.h $(htscodecs_arith_dynamic_h) $(htscodecs_varint_h) $(htscodecs_pack_h) $(htscodecs_utils_h) $(htscodecs_c_simple_model_h)
htscodecs/htscodecs/fqzcomp_qual.o htscodecs/htscodecs/fqzcomp_qual.pico: htscodecs/htscodecs/fqzcomp_qual.c config.h $(htscodecs_fqzcomp_qual_h) $(htscodecs_varint_h) $(htscodecs_utils_h) $(htscodecs_c_simple_model_h)
htscodecs/htscodecs/htscodecs.o htscodecs/htscodecs/htscodecs.pico: htscodecs/htscodecs/htscodecs.c $(htscodecs_htscodecs_h) $(htscodecs_version_h)
htscodecs/htscodecs/pack.o htscodecs/htscodecs/pack.pico: htscodecs/htscodecs/pack.c config.h $(htscodecs_pack_h)
htscodecs/htscodecs/rANS_static32x16pr.o htscodecs/htscodecs/rANS_static32x16pr.pico: htscodecs/htscodecs/rANS_static32x16pr.c config.h $(htscodecs_rANS_word_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_rANS_static16_int_h) $(htscodecs_varint_h) $(htscodecs_utils_h)
htscodecs/htscodecs/rANS_static32x16pr_avx2.o htscodecs/htscodecs/rANS_static32x16pr_avx2.pico: htscodecs/htscodecs/rANS_static32x16pr_avx2.c config.h $(htscodecs_rANS_word_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_rANS_static16_int_h) $(htscodecs_varint_h) $(htscodecs_utils_h) $(htscodecs_permute_h)
htscodecs/htscodecs/rANS_static32x16pr_avx512.o htscodecs/htscodecs/rANS_static32x16pr_avx512.pico: htscodecs/htscodecs/rANS_static32x16pr_avx512.c config.h $(htscodecs_rANS_word_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_rANS_static16_int_h) $(htscodecs_varint_h) $(htscodecs_utils_h)
htscodecs/htscodecs/rANS_static32x16pr_neon.o htscodecs/htscodecs/rANS_static32x16pr_neon.pico: htscodecs/htscodecs/rANS_static32x16pr_neon.c config.h $(htscodecs_rANS_word_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_rANS_static16_int_h) $(htscodecs_varint_h) $(htscodecs_utils_h)
htscodecs/htscodecs/rANS_static32x16pr_sse4.o htscodecs/htscodecs/rANS_static32x16pr_sse4.pico: htscodecs/htscodecs/rANS_static32x16pr_sse4.c config.h $(htscodecs_rANS_word_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_rANS_static16_int_h) $(htscodecs_varint_h) $(htscodecs_utils_h)
htscodecs/htscodecs/rANS_static4x16pr.o htscodecs/htscodecs/rANS_static4x16pr.pico: htscodecs/htscodecs/rANS_static4x16pr.c config.h $(htscodecs_rANS_word_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_rANS_static16_int_h) $(htscodecs_pack_h) $(htscodecs_rle_h) $(htscodecs_utils_h) $(htscodecs_rANS_static32x16pr_h)
htscodecs/htscodecs/rANS_static.o htscodecs/htscodecs/rANS_static.pico: htscodecs/htscodecs/rANS_static.c config.h $(htscodecs_rANS_byte_h) $(htscodecs_utils_h) $(htscodecs_rANS_static_h)
htscodecs/htscodecs/rle.o htscodecs/htscodecs/rle.pico: htscodecs/htscodecs/rle.c config.h $(htscodecs_varint_h) $(htscodecs_rle_h)
htscodecs/htscodecs/tokenise_name3.o htscodecs/htscodecs/tokenise_name3.pico: htscodecs/htscodecs/tokenise_name3.c config.h $(htscodecs_pooled_alloc_h) $(htscodecs_arith_dynamic_h) $(htscodecs_rANS_static4x16_h) $(htscodecs_tokenise_name3_h) $(htscodecs_varint_h) $(htscodecs_utils_h)
htscodecs/htscodecs/utils.o htscodecs/htscodecs/utils.pico: htscodecs/htscodecs/utils.c config.h $(htscodecs_utils_h)

# Extra CFLAGS for specific files
htscodecs/htscodecs/rANS_static32x16pr_avx2.o htscodecs/htscodecs/rANS_static32x16pr_avx2.pico: TARGET_CFLAGS = $(HTS_CFLAGS_AVX2)
htscodecs/htscodecs/rANS_static32x16pr_avx512.o htscodecs/htscodecs/rANS_static32x16pr_avx512.pico: TARGET_CFLAGS = $(HTS_CFLAGS_AVX512)
htscodecs/htscodecs/rANS_static32x16pr_sse4.o htscodecs/htscodecs/rANS_static32x16pr_sse4.pico: TARGET_CFLAGS = $(HTS_CFLAGS_SSE4)

annot-tsv: annot-tsv.o libhts.a
	$(CC) $(LDFLAGS) -o $@ annot-tsv.o libhts.a $(LIBS) -lpthread

bgzip: bgzip.o libhts.a
	$(CC) $(LDFLAGS) -o $@ bgzip.o libhts.a $(LIBS) -lpthread

htsfile: htsfile.o libhts.a
	$(CC) $(LDFLAGS) -o $@ htsfile.o libhts.a $(LIBS) -lpthread

tabix: tabix.o libhts.a
	$(CC) $(LDFLAGS) -o $@ tabix.o libhts.a $(LIBS) -lpthread

annot-tsv.o: annot-tsv.c config.h $(htslib_hts_h) $(htslib_hts_defs_h) $(htslib_khash_str2int_h) $(htslib_kstring_h) $(htslib_kseq_h) $(htslib_bgzf_h) $(htslib_regidx_h) $(textutils_internal_h)
bgzip.o: bgzip.c config.h $(htslib_bgzf_h) $(htslib_hts_h) $(htslib_hfile_h)
htsfile.o: htsfile.c config.h $(htslib_hfile_h) $(htslib_hts_h) $(htslib_sam_h) $(htslib_vcf_h)
tabix.o: tabix.c config.h $(htslib_tbx_h) $(htslib_sam_h) $(htslib_vcf_h) $(htslib_kseq_h) $(htslib_bgzf_h) $(htslib_hts_h) $(htslib_regidx_h) $(htslib_hts_defs_h) $(htslib_hts_log_h) $(htslib_thread_pool_h)

# Runes to check that the htscodecs submodule is present
ifdef HTSCODECS_SOURCES
htscodecs/htscodecs/%.c: | htscodecs/htscodecs
	@if test -e htscodecs/.git && test ! -e "$@" ; then \
	  echo "Missing file '$@'" ; \
	  echo "  - Do you need to update the htscodecs submodule?" ; \
	  false ; \
	fi

htscodecs/htscodecs/%.h: | htscodecs/htscodecs
	@if test -e htscodecs/.git && test ! -e "$@" ; then \
	  echo "Missing file '$@'" ; \
	  echo "  - Do you need to update the htscodecs submodule?" ; \
	  false ; \
	fi

htscodecs/htscodecs:
	@if test -e .git ; then \
	printf "\\n\\nError: htscodecs submodule files not present for htslib.\\n\
	Try running: \\n\
	    git submodule update --init --recursive\\n\
	in the top-level htslib directory and then re-run make.\\n\\n\\n" ; \
	else \
	  printf "\\n\\nError: htscodecs submodule files not present and this is not a git checkout.\\n\
	  You have an incomplete distribution.  Please try downloading one of the\\n\
	  official releases from https://www.htslib.org/\\n" ; \
	fi
	@false

# Build the htscodecs/htscodecs/version.h file if necessary
htscodecs/htscodecs/version.h: force
	@if test -e $(srcdir)/htscodecs/.git && test -e $(srcdir)/htscodecs/configure.ac ; then \
	  vers=`cd $(srcdir)/htscodecs && git describe --always --dirty --match 'v[0-9]\.[0-9]*'` && \
	  case "$$vers" in \
	    v*) vers=$${vers#v} ;; \
	    *) iv=`awk '/^AC_INIT\(htscodecs,/ { match($$0, /[0-9]+(\.[0-9]+)*/); print substr($$0, RSTART, RLENGTH) }' $(srcdir)/htscodecs/configure.ac` ; vers="$$iv$${vers:+-g$$vers}" ;; \
	  esac ; \
	  if ! grep -s -q '"'"$$vers"'"' $@ ; then \
	    echo 'Updating $@ : #define HTSCODECS_VERSION_TEXT "'"$$vers"'"' ; \
	    echo '#define HTSCODECS_VERSION_TEXT "'"$$vers"'"' > $@ ; \
	  fi ; \
	fi
endif

# Maintainer extra targets built
# - compile public headers as C++
# Maintainer source code checks
# - copyright boilerplate presence
# - tab and trailing space detection
maintainer-check: test/usepublic.o
	test/maintainer/check_copyright.pl .
	test/maintainer/check_spaces.pl .

# Look for untracked files in the git repository.
check-untracked:
	@if test -e .git && git status --porcelain | grep '^\?'; then \
	    echo 'Untracked files detected (see above). Please either clean up, add to .gitignore, or for test output files consider naming them to match *.tmp or *.tmp.*' ; \
	    false ; \
	fi

# Create a shorthand. We use $(SRC) or $(srcprefix) rather than $(srcdir)/
# for brevity in test and install rules, and so that build logs do not have
# ./ sprinkled throughout.
SRC = $(srcprefix)

# For tests that might use it, set $REF_PATH explicitly to use only reference
# areas within the test suite (or set it to ':' to use no reference areas).
#
# If using MSYS, avoid poor shell expansion via:
#    MSYS2_ARG_CONV_EXCL="*" make check
check test: all $(HTSCODECS_TEST_TARGETS)
	test/hts_endian
	test/test_expr
	test/test_kfunc
	test/test_khash
	test/test_kstring
	test/test_nibbles -v
	test/test_str2int
	test/test_time_funcs
	test/fieldarith test/fieldarith.sam
	test/hfile
	if test "x$(BUILT_PLUGINS)" != "x"; then \
	    HTS_PATH=. test/with-shlib.sh test/plugins-dlhts -g ./libhts.$(SHLIB_FLAVOUR); \
	fi
	if test "x$(BUILT_PLUGINS)" != "x"; then \
	  HTS_PATH=. test/with-shlib.sh test/plugins-dlhts -l ./libhts.$(SHLIB_FLAVOUR); \
	fi
	test/test_bgzf test/bgziptest.txt
	test/test-parse-reg -t test/colons.bam
	cd test/faidx && ./test-faidx.sh faidx.tst
	cd test/sam_filter && ./filter.sh filter.tst
	cd test/tabix && ./test-tabix.sh tabix.tst
	cd test/mpileup && ./test-pileup.sh mpileup.tst
	cd test/fastq && ./test-fastq.sh
	cd test/base_mods && ./base-mods.sh base-mods.tst
	REF_PATH=: test/sam test/ce.fa test/faidx/faidx.fa test/faidx/fastqs.fq
	test/test-regidx
	cd test && REF_PATH=: ./test.pl $${TEST_OPTS:-}

test/hts_endian: test/hts_endian.o
	$(CC) $(LDFLAGS) -o $@ test/hts_endian.o $(LIBS)

# To build the fuzzer, try:
# make  CC="clang16 -fsanitize=address,undefined,fuzzer" \
#     CFLAGS="-g -O3 -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" \
#     test/fuzz/hts_open_fuzzer
test/fuzz/hts_open_fuzzer: test/fuzz/hts_open_fuzzer.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/fuzz/hts_open_fuzzer.o libhts.a $(LIBS) -lpthread

test/fieldarith: test/fieldarith.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/fieldarith.o libhts.a $(LIBS) -lpthread

test/hfile: test/hfile.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/hfile.o libhts.a $(LIBS) -lpthread

test/pileup: test/pileup.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/pileup.o libhts.a $(LIBS) -lpthread

test/pileup_mod: test/pileup_mod.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/pileup_mod.o libhts.a $(LIBS) -lpthread

test/plugins-dlhts: test/plugins-dlhts.o
	$(CC) $(LDFLAGS) -o $@ test/plugins-dlhts.o $(LIBS)

test/sam: test/sam.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/sam.o libhts.a $(LIBS) -lpthread

test/test_bgzf: test/test_bgzf.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_bgzf.o libhts.a $(LIBS) -lpthread

test/test_expr: test/test_expr.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_expr.o libhts.a $(LIBS) -lpthread

test/test_faidx: test/test_faidx.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_faidx.o libhts.a $(LIBS) -lpthread

test/test_kfunc: test/test_kfunc.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_kfunc.o libhts.a $(LIBS) -lpthread

test/test_khash: test/test_khash.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_khash.o libhts.a $(LIBS) -lpthread

test/test_kstring: test/test_kstring.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_kstring.o libhts.a $(LIBS) -lpthread

test/test_mod: test/test_mod.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_mod.o libhts.a $(LIBS) -lpthread

test/test_nibbles: test/test_nibbles.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_nibbles.o libhts.a $(LIBS) -lpthread

test/test_realn: test/test_realn.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_realn.o libhts.a $(LIBS) -lpthread

test/test-regidx: test/test-regidx.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test-regidx.o libhts.a $(LIBS) -lpthread

test/test-parse-reg: test/test-parse-reg.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test-parse-reg.o libhts.a $(LIBS) -lpthread

test/test_str2int: test/test_str2int.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_str2int.o libhts.a $(LIBS) -lpthread

test/test_time_funcs: test/test_time_funcs.o
	$(CC) $(LDFLAGS) -o $@ test/test_time_funcs.o

test/test_view: test/test_view.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_view.o libhts.a $(LIBS) -lpthread

test/test_index: test/test_index.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_index.o libhts.a $(LIBS) -lpthread

test/test-vcf-api: test/test-vcf-api.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test-vcf-api.o libhts.a $(LIBS) -lpthread

test/test-vcf-sweep: test/test-vcf-sweep.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test-vcf-sweep.o libhts.a $(LIBS) -lpthread

test/test-bcf-sr: test/test-bcf-sr.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test-bcf-sr.o libhts.a $(LIBS) -lpthread

test/test-bcf-translate: test/test-bcf-translate.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test-bcf-translate.o libhts.a $(LIBS) -lpthread

test/test_introspection: test/test_introspection.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test_introspection.o libhts.a $(LIBS) -lpthread

test/test-bcf_set_variant_type: test/test-bcf_set_variant_type.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/test-bcf_set_variant_type.o libhts.a $(LIBS) -lpthread

# Extra tests for bundled htscodecs
test_htscodecs_rans4x8: htscodecs/tests/rans4x8
	cd htscodecs/tests && srcdir=. && export srcdir && ./rans4x8.test

test_htscodecs_rans4x16: htscodecs/tests/rans4x16pr
	cd htscodecs/tests && srcdir=. && export srcdir && ./rans4x16.test

test_htscodecs_arith: htscodecs/tests/arith_dynamic
	cd htscodecs/tests && srcdir=. && export srcdir && ./arith.test

test_htscodecs_tok3: htscodecs/tests/tokenise_name3
	cd htscodecs/tests && srcdir=. && export srcdir && ./tok3.test

test_htscodecs_fqzcomp: htscodecs/tests/fqzcomp_qual
	cd htscodecs/tests && srcdir=. && export srcdir && ./fqzcomp.test

test_htscodecs_varint: htscodecs/tests/varint
	cd htscodecs/tests && ./varint

htscodecs/tests/arith_dynamic: htscodecs/tests/arith_dynamic_test.o $(HTSCODECS_OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) -lm -lpthread

htscodecs/tests/fqzcomp_qual: htscodecs/tests/fqzcomp_qual_test.o $(HTSCODECS_OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) -lm -lpthread

htscodecs/tests/rans4x16pr: htscodecs/tests/rANS_static4x16pr_test.o $(HTSCODECS_OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) -lm -lpthread

htscodecs/tests/rans4x8: htscodecs/tests/rANS_static_test.o $(HTSCODECS_OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) -lm -lpthread

htscodecs/tests/tokenise_name3: htscodecs/tests/tokenise_name3_test.o $(HTSCODECS_OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) -lm -lpthread

htscodecs/tests/varint: htscodecs/tests/varint_test.o $(HTSCODECS_OBJS)
	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS) -lm -lpthread

htscodecs/tests/arith_dynamic_test.o: CPPFLAGS += -Ihtscodecs
htscodecs/tests/arith_dynamic_test.o: htscodecs/tests/arith_dynamic_test.c config.h $(htscodecs_arith_dynamic_h)
htscodecs/tests/fqzcomp_qual_test.o: CPPFLAGS += -Ihtscodecs
htscodecs/tests/fqzcomp_qual_test.o: htscodecs/tests/fqzcomp_qual_test.c config.h $(htscodecs_fqzcomp_qual_h) $(htscodecs_varint_h)
htscodecs/tests/rANS_static4x16pr_test.o: CPPFLAGS += -Ihtscodecs
htscodecs/tests/rANS_static4x16pr_test.o: htscodecs/tests/rANS_static4x16pr_test.c config.h $(htscodecs_rANS_static4x16_h)
htscodecs/tests/rANS_static_test.o: CPPFLAGS += -Ihtscodecs
htscodecs/tests/rANS_static_test.o: htscodecs/tests/rANS_static_test.c config.h $(htscodecs_rANS_static_h)
htscodecs/tests/tokenise_name3_test.o: CPPFLAGS += -Ihtscodecs
htscodecs/tests/tokenise_name3_test.o: htscodecs/tests/tokenise_name3_test.c config.h $(htscodecs_tokenise_name3_h)
htscodecs/tests/varint_test.o: CPPFLAGS += -Ihtscodecs
htscodecs/tests/varint_test.o: htscodecs/tests/varint_test.c config.h $(htscodecs_varint_h)

test/hts_endian.o: test/hts_endian.c config.h $(htslib_hts_endian_h)
test/fuzz/hts_open_fuzzer.o: test/fuzz/hts_open_fuzzer.c config.h $(htslib_hfile_h) $(htslib_hts_h) $(htslib_sam_h) $(htslib_vcf_h)
test/fieldarith.o: test/fieldarith.c config.h $(htslib_sam_h)
test/hfile.o: test/hfile.c config.h $(htslib_hfile_h) $(htslib_hts_defs_h) $(htslib_kstring_h)
test/pileup.o: test/pileup.c config.h $(htslib_sam_h) $(htslib_kstring_h)
test/pileup_mod.o: test/pileup_mod.c config.h $(htslib_sam_h)
test/plugins-dlhts.o: test/plugins-dlhts.c config.h
test/sam.o: test/sam.c config.h $(htslib_hts_defs_h) $(htslib_sam_h) $(htslib_faidx_h) $(htslib_khash_h) $(htslib_hts_log_h)
test/test_bgzf.o: test/test_bgzf.c config.h $(htslib_bgzf_h) $(htslib_hfile_h) $(htslib_hts_log_h) $(hfile_internal_h)
test/test_expr.o: test/test_expr.c config.h $(htslib_hts_expr_h)
test/test_kfunc.o: test/test_kfunc.c config.h $(htslib_kfunc_h)
test/test_khash.o: test/test_khash.c config.h $(htslib_khash_h) $(htslib_kroundup_h)
test/test_kstring.o: test/test_kstring.c config.h $(htslib_kstring_h)
test/test_mod.o: test/test_mod.c config.h $(htslib_sam_h)
test/test_nibbles.o: test/test_nibbles.c config.h $(htslib_sam_h) $(sam_internal_h)
test/test-parse-reg.o: test/test-parse-reg.c config.h $(htslib_hts_h) $(htslib_sam_h)
test/test_realn.o: test/test_realn.c config.h $(htslib_hts_h) $(htslib_sam_h) $(htslib_faidx_h)
test/test-regidx.o: test/test-regidx.c config.h $(htslib_kstring_h) $(htslib_regidx_h) $(htslib_hts_defs_h) $(textutils_internal_h)
test/test_str2int.o: test/test_str2int.c config.h $(textutils_internal_h)
test/test_time_funcs.o: test/test_time_funcs.c config.h $(hts_time_funcs_h)
test/test_view.o: test/test_view.c config.h $(cram_h) $(htslib_sam_h) $(htslib_vcf_h) $(htslib_hts_log_h)
test/test_faidx.o: test/test_faidx.c config.h $(htslib_faidx_h)
test/test_index.o: test/test_index.c config.h $(htslib_sam_h) $(htslib_vcf_h)
test/test-vcf-api.o: test/test-vcf-api.c config.h $(htslib_hts_h) $(htslib_vcf_h) $(htslib_kstring_h) $(htslib_kseq_h)
test/test-vcf-sweep.o: test/test-vcf-sweep.c config.h $(htslib_vcf_sweep_h)
test/test-bcf-sr.o: test/test-bcf-sr.c config.h $(htslib_synced_bcf_reader_h) $(htslib_hts_h) $(htslib_vcf_h)
test/test-bcf-translate.o: test/test-bcf-translate.c config.h $(htslib_vcf_h)
test/test_introspection.o: test/test_introspection.c config.h $(htslib_hts_h) $(htslib_hfile_h)
test/test-bcf_set_variant_type.o: test/test-bcf_set_variant_type.c config.h $(htslib_hts_h) vcf.c

# Standalone target not added to $(BUILT_TEST_PROGRAMS) as some may not
# have a compiler that compiles as C++ when given a .cpp source file.
test/usepublic.o: test/usepublic.cpp config.h $(htslib_bgzf_h) $(htslib_cram_h) $(htslib_faidx_h) $(htslib_hfile_h) $(htslib_hts_h) $(htslib_hts_defs_h) $(htslib_hts_endian_h) $(htslib_hts_expr_h) $(htslib_hts_log_h) $(htslib_hts_os_h) $(htslib_kbitset_h) $(htslib_kfunc_h) $(htslib_khash_h) $(htslib_khash_str2int_h) $(htslib_klist_h) $(HTSPREFIX)htslib/knetfile.h $(htslib_kroundup_h) $(htslib_kseq_h) $(htslib_ksort_h) $(htslib_kstring_h) $(htslib_regidx_h) $(htslib_sam_h) $(htslib_synced_bcf_reader_h) $(htslib_tbx_h) $(htslib_thread_pool_h) $(htslib_vcf_h) $(htslib_vcf_sweep_h) $(htslib_vcfutils_h)
	$(CC) $(CFLAGS) $(TARGET_CFLAGS) $(ALL_CPPFLAGS) -c -o $@ test/usepublic.cpp


test/thrash_threads1: test/thrash_threads1.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/thrash_threads1.o libhts.a $(LIBS) -lpthread

test/thrash_threads2: test/thrash_threads2.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/thrash_threads2.o libhts.a $(LIBS) -lpthread

test/thrash_threads3: test/thrash_threads3.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/thrash_threads3.o libhts.a $(LIBS) -lpthread

test/thrash_threads4: test/thrash_threads4.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/thrash_threads4.o libhts.a $(LIBS) -lpthread

test/thrash_threads5: test/thrash_threads5.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/thrash_threads5.o libhts.a $(LIBS) -lpthread

test/thrash_threads6: test/thrash_threads6.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/thrash_threads6.o libhts.a $(LIBS) -lpthread

test/thrash_threads7: test/thrash_threads7.o libhts.a
	$(CC) $(LDFLAGS) -o $@ test/thrash_threads7.o libhts.a $(LIBS) -lpthread

test_thrash: $(BUILT_THRASH_PROGRAMS)

# Test to ensure the functions in the header files are exported by the shared
# library.  This currently works by comparing the output from ctags on
# the headers with the list of functions exported by the shared library.
# Note that functions marked as exported in the .c files and not the public
# headers will be missed by this test.
test-shlib-exports: header-exports.txt shlib-exports-$(SHLIB_FLAVOUR).txt
	@echo "Checking shared library exports"
	@if test ! -s header-exports.txt ; then echo "Error: header-exports.txt empty" ; false ; fi
	@if test ! -s shlib-exports-$(SHLIB_FLAVOUR).txt ; then echo "Error: shlib-exports-$(SHLIB_FLAVOUR).txt empty" ; false ; fi
	@! comm -23 header-exports.txt shlib-exports-$(SHLIB_FLAVOUR).txt | grep . || \
	( echo "Error: Found unexported symbols (listed above)" ; false )

# Extract symbols that should be exported from public headers using ctags
# Filter out macros in htslib/hts_defs.h.
header-exports.txt: test/header_syms.pl htslib/*.h
	test/header_syms.pl htslib/*.h | sort -u -o $@

shlib-exports-so.txt: libhts.so
	nm -D -g libhts.so | awk '$$2 == "T" { sub("@.*", "", $$3); print $$3 }' | sort -u -o $@

shlib-exports-dylib.txt: libhts.dylib
	nm -Ug libhts.dylib | awk '$$2 == "T" { sub("^_", "", $$3); print $$3 }' | sort -u -o $@

shlib-exports-dll.txt: hts.dll.a
	nm -g hts.dll.a | awk '$$2 == "T" { print $$3 }' | sort -u -o $@

$(srcprefix)htslib.map: libhts.so
	LC_ALL=C ; export LC_ALL; \
	curr_vers=`expr 'X$(PACKAGE_VERSION)' : 'X\([0-9]*\.[0-9.]*\)'` ; \
	last_vers=`awk '/^HTSLIB_[0-9](\.[0-9]+)+/ { lv = $$1 } END { print lv }' htslib.map` ; \
	if test "x$$curr_vers" = 'x' || test "x$$last_vers" = 'x' ; then \
	    echo "Version check failed : $$curr_vers / $$las_vers" 1>&2 ; \
	    exit 1 ; \
	fi && \
	if test "HTSLIB_$$curr_vers" = "$$last_vers" ; then \
	    echo "Refusing to update $@ - HTSlib version not changed" 1>&2 ; \
	    exit 1 ; \
	fi && \
	nm --with-symbol-versions -D -g libhts.so | awk '$$2 ~ /^[DGRT]$$/ && $$3 ~ /@@Base$$/ && $$3 !~ /^(_init|_fini|_edata)@@/ { sub(/@@Base$$/, ";", $$3); print "    " $$3 }' > $@.tmp && \
	if [ -s $@.tmp ] ; then \
	    cat $@ > $@.new.tmp && \
	    printf '\n%s {\n' "HTSLIB_$$curr_vers" >> $@.new.tmp && \
	    cat $@.tmp >> $@.new.tmp && \
	    printf '} %s;\n' "$$last_vers" >> $@.new.tmp && \
	        rm -f $@.tmp && \
	        mv $@.new.tmp $@ ; \
	    fi ; \
	else \
	    rm -f $@.tmp ; \
	fi

install: libhts.a $(BUILT_PROGRAMS) $(BUILT_PLUGINS) installdirs install-$(SHLIB_FLAVOUR) install-pkgconfig
	$(INSTALL_PROGRAM) $(BUILT_PROGRAMS) $(DESTDIR)$(bindir)
	if test -n "$(BUILT_PLUGINS)"; then $(INSTALL_PROGRAM) $(BUILT_PLUGINS) $(DESTDIR)$(plugindir); fi
	$(INSTALL_DATA) $(SRC)htslib/*.h $(DESTDIR)$(includedir)/htslib
	$(INSTALL_DATA) libhts.a $(DESTDIR)$(libdir)/libhts.a
	$(INSTALL_MAN) $(SRC)annot-tsv.1 $(SRC)bgzip.1 $(SRC)htsfile.1 $(SRC)tabix.1 $(DESTDIR)$(man1dir)
	$(INSTALL_MAN) $(SRC)faidx.5 $(SRC)sam.5 $(SRC)vcf.5 $(DESTDIR)$(man5dir)
	$(INSTALL_MAN) $(SRC)htslib-s3-plugin.7 $(DESTDIR)$(man7dir)

installdirs:
	$(INSTALL_DIR) $(DESTDIR)$(bindir) $(DESTDIR)$(includedir) $(DESTDIR)$(includedir)/htslib $(DESTDIR)$(libdir) $(DESTDIR)$(man1dir) $(DESTDIR)$(man5dir) $(DESTDIR)$(man7dir) $(DESTDIR)$(pkgconfigdir)
	if test -n "$(plugindir)"; then $(INSTALL_DIR) $(DESTDIR)$(plugindir); fi

# After installation, the real file in $(libdir) will be libhts.so.X.Y.Z,
# with symlinks libhts.so (used via -lhts during linking of client programs)
# and libhts.so.NN (used by client executables at runtime).

install-so: libhts.so installdirs
	$(INSTALL_LIB) libhts.so $(DESTDIR)$(libdir)/libhts.so.$(PACKAGE_VERSION)
	ln -sf libhts.so.$(PACKAGE_VERSION) $(DESTDIR)$(libdir)/libhts.so
	ln -sf libhts.so.$(PACKAGE_VERSION) $(DESTDIR)$(libdir)/libhts.so.$(LIBHTS_SOVERSION)

install-cygdll: cyghts-$(LIBHTS_SOVERSION).dll installdirs
	$(INSTALL_PROGRAM) cyghts-$(LIBHTS_SOVERSION).dll $(DESTDIR)$(bindir)/cyghts-$(LIBHTS_SOVERSION).dll
	$(INSTALL_PROGRAM) libhts.dll.a $(DESTDIR)$(libdir)/libhts.dll.a

install-dll: hts-$(LIBHTS_SOVERSION).dll installdirs
	$(INSTALL_PROGRAM) hts-$(LIBHTS_SOVERSION).dll $(DESTDIR)$(bindir)/hts-$(LIBHTS_SOVERSION).dll
	$(INSTALL_PROGRAM) hts.dll.a $(DESTDIR)$(libdir)/hts.dll.a

install-dylib: libhts.dylib installdirs
	$(INSTALL_PROGRAM) libhts.dylib $(DESTDIR)$(libdir)/libhts.$(PACKAGE_VERSION).dylib
	ln -sf libhts.$(PACKAGE_VERSION).dylib $(DESTDIR)$(libdir)/libhts.dylib
	ln -sf libhts.$(PACKAGE_VERSION).dylib $(DESTDIR)$(libdir)/libhts.$(LIBHTS_SOVERSION).dylib

# Substitute these pseudo-autoconf variables only at install time
# so that "make install prefix=/prefix/path" etc continue to work.
install-pkgconfig: htslib.pc.tmp installdirs
	sed -e 's#@-includedir@#$(includedir)#g;s#@-libdir@#$(libdir)#g;s#@-PACKAGE_VERSION@#$(PACKAGE_VERSION)#g' htslib.pc.tmp > $(DESTDIR)$(pkgconfigdir)/htslib.pc
	chmod 644 $(DESTDIR)$(pkgconfigdir)/htslib.pc

# A pkg-config file (suitable for copying to $PKG_CONFIG_PATH) that provides
# flags for building against the uninstalled library in this build directory.
htslib-uninstalled.pc: htslib.pc.tmp
	sed -e 's#@-includedir@#'`pwd`'#g;s#@-libdir@#'`pwd`'#g' htslib.pc.tmp > $@


testclean:
	-rm -f test/*.tmp test/*.tmp.* test/faidx/*.tmp* \
               test/longrefs/*.tmp.* test/tabix/*.tmp.* \
               test/bgzf_boundaries/*.tmp.* test/*/FAIL* \
               header-exports.txt shlib-exports-$(SHLIB_FLAVOUR).txt
	-rm -rf htscodecs/tests/test.out

# Only remove this in git checkouts
DEL_HTSCODECS_VERSION := $(if $(wildcard htscodecs/.git),htscodecs/htscodecs/version.h)

mostlyclean: testclean
	-rm -f *.o *.pico cram/*.o cram/*.pico test/*.o test/*.dSYM config_vars.h version.h
	-rm -f htscodecs/htscodecs/*.o htscodecs/htscodecs/*.pico $(DEL_HTSCODECS_VERSION)
	-rm -f hts-object-files
	-rm -f htscodecs/tests/*.o

clean: mostlyclean clean-$(SHLIB_FLAVOUR)
	-rm -f libhts.a $(BUILT_PROGRAMS) $(BUILT_PLUGINS) $(BUILT_TEST_PROGRAMS) $(BUILT_THRASH_PROGRAMS)
	-rm -f htscodecs/tests/rans4x8 htscodecs/tests/rans4x16pr htscodecs/tests/arith_dynamic htscodecs/tests/tokenise_name3 htscodecs/tests/fqzcomp_qual htscodecs/tests/varint

distclean maintainer-clean: clean
	-rm -f config.cache config.h config.log config.mk config.status
	-rm -f TAGS *.pc.tmp *-uninstalled.pc htslib_static.mk htscodecs.mk
	-rm -rf autom4te.cache

clean-so:
	-rm -f libhts.so libhts.so.*

clean-cygdll:
	-rm -f cyghts-*.dll libhts.dll.a

clean-dll:
	-rm -f hts-*.dll hts.dll.a

clean-dylib:
	-rm -f libhts.dylib libhts.*.dylib


tags TAGS:
	ctags -f TAGS *.[ch] cram/*.[ch] htslib/*.h

# We recommend libhts-using programs be built against a separate htslib
# installation.  However if you feel that you must bundle htslib source
# code with your program, this hook enables Automake-style "make dist"
# for this subdirectory.  If you do bundle an htslib snapshot, please
# add identifying information to $(PACKAGE_VERSION) as appropriate.
# (The wildcards attempt to omit non-exported files (.git*, README.md,
# etc) and other detritus that might be in the top-level directory.)
distdir:
	@if [ -z "$(distdir)" ]; then echo "Please supply a distdir=DIR argument."; false; fi
	tar -c *.[ch15] [ILMNRchtv]*[ELSbcekmnth] | (cd $(distdir) && tar -x)
	+cd $(distdir) && $(MAKE) distclean

force:


.PHONY: all check check-untracked clean distclean distdir force
.PHONY: install install-pkgconfig installdirs lib-shared lib-static
.PHONY: maintainer-check maintainer-clean mostlyclean plugins
.PHONY: print-config print-version show-version tags
.PHONY: test test-shlib-exports test_thrash testclean
.PHONY: clean-so install-so
.PHONY: clean-cygdll install-cygdll
.PHONY: clean-dll install-dll
.PHONY: clean-dylib install-dylib
.PHONY: test_htscodecs_rans4x8 test_htscodecs_rans4x16 test_htscodecs_arith
.PHONY: test_htscodecs_tok3 test_htscodecs_fqzcomp test_htscodecs_varint
.PHONY: cc-version
