Skip to content

Instantly share code, notes, and snippets.

@jmarshall
Created September 20, 2019 10:18
Show Gist options
  • Save jmarshall/d23a7dfae881a0f293fa0a70deaff430 to your computer and use it in GitHub Desktop.
Save jmarshall/d23a7dfae881a0f293fa0a70deaff430 to your computer and use it in GitHub Desktop.
Hacky Perl script for updating HTSlib/samtools/bcftools Makefile dependencies
#!/usr/bin/perl
use strict;
use warnings;
my $curfname = "";
my $pkg = "";
my %var = (
# htslib
"cram/cram.h" => "cram_h",
"cram/cram_io.h" => "cram_io_h",
"cram/misc.h" => "cram_misc_h",
"cram/os.h" => "cram_os_h",
"cram/sam_header.h" => "cram_sam_header_h",
"cram/cram_samtools.h" => "cram_samtools_h",
"cram/cram_structs.h" => "cram_structs_h",
"cram/open_trace_file.h" => "cram_open_trace_file_h",
"bcf_sr_sort.h" => "bcf_sr_sort_h",
"header.h" => "header_h",
"hfile_internal.h" => "hfile_internal_h",
"hts_internal.h" => "hts_internal_h",
"sam_internal.h" => "sam_internal_h",
"textutils_internal.h" => "textutils_internal_h",
"thread_pool_internal.h" => "thread_pool_internal_h",
# samtools
"bam.h" => "bam_h",
"bam2bcf.h" => "bam2bcf_h",
"bam_lpileup.h" => "bam_lpileup_h",
"bam_plbuf.h" => "bam_plbuf_h",
"bam_tview.h" => "bam_tview_h",
"bedidx.h" => "bedidx_h",
"sam.h" => "sam_h",
"sam_opts.h" => "sam_opts_h",
"sample.h" => "sample_h",
"samtools.h" => "samtools_h",
"stats_isize.h" => "stats_isize_h",
"test.h" => "test_test_h", "../test.h" => "test_test_h",
"tmp_file.h" => "tmp_file_h",
# bcftools
"bcftools.h" => "bcftools_h",
#"bin.h" => "bin_h",
"call.h" => "call_h",
"convert.h" => "convert_h",
"tsv2vcf.h" => "tsv2vcf_h",
"filter.h" => "filter_h",
"gvcf.h" => "gvcf_h",
"khash_str2str.h" => "khash_str2str_h",
"ploidy.h" => "ploidy_h",
"prob1.h" => "prob1_h",
#roh_h = HMM.h #!!!
#cnv_h = HMM.h #!!!
"smpl_ilist.h" => "smpl_ilist_h",
"vcfbuf.h" => "vcfbuf_h",
"bam2bcf.h" => "bam2bcf_h",
"bam_sample.h" => "bam_sample_h",
"variantkey.h" => "variantkey_h",
);
sub mkdep {
my ($fname) = @_;
my $deps = $fname;
my $ifdef = 0;
local $_;
open F, $fname or warn "$0: can't open $fname: $!\n";
while (<F>) {
chomp;
$ifdef++ if /^#\s*ifdef\s+ENABLE_/;
$ifdef-- if $ifdef > 0 && /^#\s*endif/;
next unless /^\s*#\s*include\s*([<"][^">]*[>"])?/;
if (! defined $1) { print STDERR "$fname:$.: unexpected \"$_\"\n"; next }
$_ = $1;
next if m{<(arpa/inet|assert|byteswap|bzlib|CommonCrypto/CommonHMAC|ctype|curl/curl|[nx]?cursesw?|ncursesw?/curses|dirent|dlfcn|errno|EXTERN|fcntl|float|getopt|gsl/gsl_(vector|multifit_nlin|version)|inttypes|libdeflate|limits|lzma|math|netdb|openssl/(hmac|md5|sha)|perl|pthread|pwd|regex|signal|std(arg|bool|def|int|io|lib)|strings?|sys/(ioctl|mman|select|socket|stat|syscall|time|types)|time|unistd|windows|winsock2|wordexp|zlib).h>};
next if m{<(rc(Connect|Misc)|dataObj(Close|Lseek|Open|Read|Write)).h>};
next if m{"version.h"} && $ifdef > 0;
s{[.][.]/}{} if m{"../version.h"};
if (0 && m{[<"]htslib/(k\w+).h[>"]} && $pkg ne 'htslib') { $_ = "\$(HTSDIR)/htslib/$1.h" }
elsif (m{[<"]htslib/(\w+).h[>"]}) { $_ = "\$(htslib_$1_h)" }
elsif (m{[<"]([./\w]+.[ch])[>"]} && exists $var{$1}) { $_ = "\$($var{$1})" }
elsif (m{"(\.\./)+(\w+).c"}) { $_ = "$2.o" }
else { s/[<">]//g }
$deps .= " $_";
}
close F;
return $deps;
}
sub canonical {
return join(' ', sort split /\s+/, $_[0]);
}
while (<>) {
my $mklineno = $.;
chomp;
$pkg = $1 if /^# Makefile for (\w+)/;
if (m{^([\w/-]+\.o(\s+[\w/-]+\.pico)?):\s*([\w/-]+\.c)}) {
my $orig = $_;
$_ = "$1: " . mkdep($3);
if ($_ eq $orig) { }
elsif (canonical($_) eq canonical($orig)) {
print STDERR "$ARGV:$mklineno: ignored out-of-order rule for $1\n";
$_ = $orig;
}
else {
print STDERR "$ARGV:$mklineno: changed rule for $1\n";
}
}
print "$_\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment