| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright [2015-2024] EMBL-European Bioinformatics Institute |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
use strict; |
| 18 |
use warnings; |
| 19 |
|
| 20 |
use Module::Build; |
| 21 |
|
| 22 |
my $class = Module::Build->subclass( |
| 23 |
class => 'Module::Build::HTS', |
| 24 |
); |
| 25 |
|
| 26 |
my $build = $class->new( |
| 27 |
|
| 28 |
module_name => 'Bio::DB::HTS', |
| 29 |
dist_version_from => 'lib/Bio/DB/HTS.pm', |
| 30 |
dist_author => 'Alessandro Vullo', |
| 31 |
dist_abstract => 'Perl interface to HTS library for DNA sequencing', |
| 32 |
license => 'Apache_2_0', |
| 33 |
|
| 34 |
extra_compiler_flags => [ |
| 35 |
|
| 36 |
# must match DFLAGS in HTSlib Makefile |
| 37 |
'-D_IOLIB=2', '-D_FILE_OFFSET_BITS=64', |
| 38 |
# warnings not treated as errors |
| 39 |
'-Wno-error', |
| 40 |
# Don't care about unused results from function calls |
| 41 |
'-Wno-unused-result', ], |
| 42 |
|
| 43 |
build_requires => { 'ExtUtils::CBuilder' => 0, }, |
| 44 |
configure_requires => { 'Module::Build' => 0.38, }, |
| 45 |
requires => { 'perl' => '5.008', 'Bio::SeqFeature::Lite' => 0 }, |
| 46 |
meta_merge => { |
| 47 |
'resources' => { |
| 48 |
'repository' => 'https://github.com/Ensembl/Bio-DB-HTS', |
| 49 |
}, |
| 50 |
}, |
| 51 |
|
| 52 |
add_to_cleanup => [ |
| 53 |
't/data/*ai', |
| 54 |
't/data/*.gzi', |
| 55 |
't/data/write_test_07.cram', |
| 56 |
't/data/write_test_08.bam', |
| 57 |
], |
| 58 |
); |
| 59 |
|
| 60 |
$build->find_hts; |
| 61 |
$build->set_include_and_compiler_flags; |
| 62 |
$build->create_build_script; |
| 63 |
|
| 64 |
exit 0; |
| 65 |
|
| 66 |
|
| 67 |
package Module::Build::HTS; |
| 68 |
|
| 69 |
use Module::Load::Conditional qw(can_load); |
| 70 |
use base 'Module::Build'; |
| 71 |
|
| 72 |
sub find_hts { |
| 73 |
my ($self) = @_; |
| 74 |
|
| 75 |
# If either of these are set, we expect to find the htslib files there: |
| 76 |
# (They're explicitly set by the user, so we shouldn't fall back to |
| 77 |
# finding another copy somewhere else.) |
| 78 |
my $incdir = $self->args('htslib-includedir'); |
| 79 |
my $libdir = $self->args('htslib-libdir'); |
| 80 |
if ($incdir && $libdir) { |
| 81 |
return 1 if $self->find_hts_in_split_install_dirs($incdir, $libdir); |
| 82 |
$self->die_hts_not_found( |
| 83 |
"--htslib-includedir '$incdir' or --htslib-libdir '$libdir' command line parameters do not contain expected files\n" |
| 84 |
); |
| 85 |
} |
| 86 |
elsif (my $dir = $self->args('htslib')) { |
| 87 |
return 1 if $self->find_hts_in_build_dir($dir); |
| 88 |
return 1 if $self->find_hts_in_install_dir($dir); |
| 89 |
$self->die_hts_not_found( |
| 90 |
"--htslib '$dir' command line parameter does not contain expected files\n" |
| 91 |
); |
| 92 |
} |
| 93 |
elsif ($dir = $ENV{'HTSLIB_DIR'}) { |
| 94 |
return 1 if $self->find_hts_in_build_dir($dir); |
| 95 |
return 1 if $self->find_hts_in_install_dir($dir); |
| 96 |
$self->die_hts_not_found( |
| 97 |
"HTSLIB_DIR=$ENV{HTSLIB_DIR} environment variable does not contain expected files\n" |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
# Search through remaining possible (but not fatal) locations: |
| 102 |
my $found = 0; |
| 103 |
foreach my $dir ( |
| 104 |
$self->prefix, |
| 105 |
from_Alien(), |
| 106 |
scalar `pkg-config --variable=libdir htslib 2>/dev/null`, |
| 107 |
qw{ /usr /usr/local /usr/share /opt/local }, |
| 108 |
) { |
| 109 |
if ($dir and $self->find_hts_in_install_dir($dir)) { |
| 110 |
$found = 1; |
| 111 |
last; |
| 112 |
} |
| 113 |
} |
| 114 |
return 1 if $found; |
| 115 |
|
| 116 |
# Try pkgconfig again but this time trust whatever it returns, without extra verification |
| 117 |
return 1 if $self->find_hts_with_pkgconfig(); |
| 118 |
|
| 119 |
$self->die_hts_not_found(); |
| 120 |
} |
| 121 |
|
| 122 |
sub set_include_and_compiler_flags { |
| 123 |
my ($self) = @_; |
| 124 |
|
| 125 |
my $hts_include = $self->config_data('hts_include'); |
| 126 |
my $hts_lib = $self->config_data('hts_lib'); |
| 127 |
my $static = $self->args('static'); |
| 128 |
|
| 129 |
$self->include_dirs([$hts_include]) if $hts_include; |
| 130 |
|
| 131 |
my @linker_dirflags; |
| 132 |
if ($hts_lib) { |
| 133 |
push @linker_dirflags, "-L$hts_lib"; |
| 134 |
} |
| 135 |
$self->extra_linker_flags(@linker_dirflags, '-lhts', '-lpthread', '-lz'); |
| 136 |
} |
| 137 |
|
| 138 |
sub hts_dev_files_exist { |
| 139 |
my ($lib, $include) = @_; |
| 140 |
return (-f "$lib/libhts.a" || -f "$lib/libhts.so") && -f "$include/htslib/hts.h"; |
| 141 |
} |
| 142 |
|
| 143 |
# Look for the library and header in the location where htslib was compiled |
| 144 |
sub find_hts_in_build_dir { |
| 145 |
my ($self, $root) = @_; |
| 146 |
|
| 147 |
chomp($root); |
| 148 |
$root =~ s{/$}{}; |
| 149 |
$root =~ s{/(lib|include|include/htslib)$}{}; |
| 150 |
|
| 151 |
if (hts_dev_files_exist($root, $root)) { |
| 152 |
$self->config_data('hts_lib' => $root); |
| 153 |
$self->config_data('hts_include' => $root); |
| 154 |
return 1; |
| 155 |
} |
| 156 |
else { |
| 157 |
return 0; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
sub find_hts_in_install_dir { |
| 163 |
my ($self, $root) = @_; |
| 164 |
|
| 165 |
chomp($root); |
| 166 |
$root =~ s{/$}{}; |
| 167 |
$root =~ s{/(lib|include|include/htslib)$}{}; |
| 168 |
|
| 169 |
my $hts_lib = "$root/lib"; |
| 170 |
my $hts_include = "$root/include"; |
| 171 |
if (hts_dev_files_exist($hts_lib, $hts_include)) { |
| 172 |
$self->config_data('hts_lib' => $hts_lib); |
| 173 |
$self->config_data('hts_include' => $hts_include); |
| 174 |
return 1; |
| 175 |
} |
| 176 |
else { |
| 177 |
return 0; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
sub find_hts_in_split_install_dirs { |
| 182 |
my ($self, $hts_include, $hts_lib) = @_; |
| 183 |
|
| 184 |
chomp($hts_lib); |
| 185 |
chomp($hts_include); |
| 186 |
$hts_include =~ s{include/htslib$}{include}; |
| 187 |
|
| 188 |
if (hts_dev_files_exist($hts_lib, $hts_include)) { |
| 189 |
$self->config_data('hts_lib' => $hts_lib); |
| 190 |
$self->config_data('hts_include' => $hts_include); |
| 191 |
return 1; |
| 192 |
} |
| 193 |
else { |
| 194 |
return 0; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
sub find_hts_with_pkgconfig { |
| 199 |
my ($self) = @_; |
| 200 |
|
| 201 |
return 0 unless can_load( |
| 202 |
modules => { 'ExtUtils::PkgConfig' => undef } |
| 203 |
); |
| 204 |
my $pkg_name = 'htslib'; |
| 205 |
return 0 unless ExtUtils::PkgConfig->exists($pkg_name); |
| 206 |
|
| 207 |
if (my $libs_only_L = ExtUtils::PkgConfig->libs_only_L($pkg_name)) { |
| 208 |
# For compatibility with other htslib search methods. Note that this |
| 209 |
# assumes there will be at most one each of -I/-L directories, which |
| 210 |
# is true as of htslib-1.5 but might change in the future. |
| 211 |
$libs_only_L =~ s{^-L}{}; |
| 212 |
$self->config_data('hts_lib' => $libs_only_L); |
| 213 |
} |
| 214 |
if (my $cflags_only_I = ExtUtils::PkgConfig->cflags_only_I($pkg_name)) { |
| 215 |
# See above |
| 216 |
$cflags_only_I =~ s{^-I}{}; |
| 217 |
$self->config_data('hts_include' => $cflags_only_I); |
| 218 |
} |
| 219 |
|
| 220 |
return 1; |
| 221 |
} |
| 222 |
|
| 223 |
sub die_hts_not_found { |
| 224 |
my ($self, $msg) = @_; |
| 225 |
|
| 226 |
$msg ||= ''; |
| 227 |
die $msg, <<END; |
| 228 |
|
| 229 |
This module requires HTSlib (http://htslib.org/) |
| 230 |
Install it if you have not done so already. |
| 231 |
|
| 232 |
This script will attempt to locate HTSlib by looking for htslib/hts.h |
| 233 |
and libhts.a / libhts.so in: |
| 234 |
|
| 235 |
1. --htslib-includedir and --htslib-libdir command line arguments |
| 236 |
2. --htslib command line argument |
| 237 |
3. HTSLIB_DIR environment variable |
| 238 |
4. --prefix command line argument (which also sets installation location) |
| 239 |
5. Alien::HTSlib dependency resolver |
| 240 |
6. pkg-config (extra directories can be set in PKG_CONFIG_PATH environment variable) |
| 241 |
7. common library locations: /usr /usr/local, /usr/share, /opt/local |
| 242 |
|
| 243 |
If none of the above succeeds but htslib is registered with pkg-config, the script |
| 244 |
will try using pkg-config paths (via ExtUtils::PkgConfig) without checking if header |
| 245 |
and library files exist. |
| 246 |
|
| 247 |
END |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
sub from_Alien { |
| 252 |
can_load( |
| 253 |
modules => { 'Alien::HTSlib' => undef, 'File::ShareDir' => undef } |
| 254 |
) && File::ShareDir::dist_dir('Alien-HTSlib'); |
| 255 |
} |