Debian Codegraph @

Build.PL

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 push @linker_dirflags, "-Wl,-rpath,$hts_lib" unless $static;
135 }
136 $self->extra_linker_flags(@linker_dirflags, '-lhts', '-lpthread', '-lz');
137 }
138
139 sub hts_dev_files_exist {
140 my ($lib, $include) = @_;
141 return (-f "$lib/libhts.a" || -f "$lib/libhts.so") && -f "$include/htslib/hts.h";
142 }
143
144 # Look for the library and header in the location where htslib was compiled
145 sub find_hts_in_build_dir {
146 my ($self, $root) = @_;
147
148 chomp($root);
149 $root =~ s{/$}{};
150 $root =~ s{/(lib|include|include/htslib)$}{};
151
152 if (hts_dev_files_exist($root, $root)) {
153 $self->config_data('hts_lib' => $root);
154 $self->config_data('hts_include' => $root);
155 return 1;
156 }
157 else {
158 return 0;
159 }
160 }
161
162
163 sub find_hts_in_install_dir {
164 my ($self, $root) = @_;
165
166 chomp($root);
167 $root =~ s{/$}{};
168 $root =~ s{/(lib|include|include/htslib)$}{};
169
170 my $hts_lib = "$root/lib";
171 my $hts_include = "$root/include";
172 if (hts_dev_files_exist($hts_lib, $hts_include)) {
173 $self->config_data('hts_lib' => $hts_lib);
174 $self->config_data('hts_include' => $hts_include);
175 return 1;
176 }
177 else {
178 return 0;
179 }
180 }
181
182 sub find_hts_in_split_install_dirs {
183 my ($self, $hts_include, $hts_lib) = @_;
184
185 chomp($hts_lib);
186 chomp($hts_include);
187 $hts_include =~ s{include/htslib$}{include};
188
189 if (hts_dev_files_exist($hts_lib, $hts_include)) {
190 $self->config_data('hts_lib' => $hts_lib);
191 $self->config_data('hts_include' => $hts_include);
192 return 1;
193 }
194 else {
195 return 0;
196 }
197 }
198
199 sub find_hts_with_pkgconfig {
200 my ($self) = @_;
201
202 return 0 unless can_load(
203 modules => { 'ExtUtils::PkgConfig' => undef }
204 );
205 my $pkg_name = 'htslib';
206 return 0 unless ExtUtils::PkgConfig->exists($pkg_name);
207
208 if (my $libs_only_L = ExtUtils::PkgConfig->libs_only_L($pkg_name)) {
209 # For compatibility with other htslib search methods. Note that this
210 # assumes there will be at most one each of -I/-L directories, which
211 # is true as of htslib-1.5 but might change in the future.
212 $libs_only_L =~ s{^-L}{};
213 $self->config_data('hts_lib' => $libs_only_L);
214 }
215 if (my $cflags_only_I = ExtUtils::PkgConfig->cflags_only_I($pkg_name)) {
216 # See above
217 $cflags_only_I =~ s{^-I}{};
218 $self->config_data('hts_include' => $cflags_only_I);
219 }
220
221 return 1;
222 }
223
224 sub die_hts_not_found {
225 my ($self, $msg) = @_;
226
227 $msg ||= '';
228 die $msg, <<END;
229
230 This module requires HTSlib (http://htslib.org/)
231 Install it if you have not done so already.
232
233 This script will attempt to locate HTSlib by looking for htslib/hts.h
234 and libhts.a / libhts.so in:
235
236 1. --htslib-includedir and --htslib-libdir command line arguments
237 2. --htslib command line argument
238 3. HTSLIB_DIR environment variable
239 4. --prefix command line argument (which also sets installation location)
240 5. Alien::HTSlib dependency resolver
241 6. pkg-config (extra directories can be set in PKG_CONFIG_PATH environment variable)
242 7. common library locations: /usr /usr/local, /usr/share, /opt/local
243
244 If none of the above succeeds but htslib is registered with pkg-config, the script
245 will try using pkg-config paths (via ExtUtils::PkgConfig) without checking if header
246 and library files exist.
247
248 END
249
250 }
251
252 sub from_Alien {
253 can_load(
254 modules => { 'Alien::HTSlib' => undef, 'File::ShareDir' => undef }
255 ) && File::ShareDir::dist_dir('Alien-HTSlib');
256 }