]> Dogcows Code - chaz/p5-Alien-ZMQ/blob - inc/My/Build.pm
8c7d987b4c8410762c6b202227998ededb701e70
[chaz/p5-Alien-ZMQ] / inc / My / Build.pm
1 package My::Build;
2
3 use v5.10;
4 use warnings FATAL => 'all';
5 use strict;
6 use utf8;
7
8 use Archive::Tar;
9 use Cwd qw/realpath/;
10 use Digest::SHA qw/sha1_hex/;
11 use File::Path qw/remove_tree/;
12 use File::Spec::Functions qw/catdir catfile/;
13 use IPC::Cmd qw/can_run run/;
14 use LWP::Simple qw/getstore RC_OK/;
15 use Module::Build;
16
17 use base 'Module::Build';
18
19 sub ACTION_code {
20 my $self = shift;
21
22 $self->SUPER::ACTION_code;
23
24 return if -e 'build-zeromq';
25 $self->add_to_cleanup('build-zeromq');
26
27 my %args = $self->args;
28 my %vars;
29
30 $self->have_c_compiler or die "C compiler not found";
31
32 unless (exists $args{'zmq-skip-probe'}) {
33 say "Probing...";
34 %vars = $self->probe_zeromq;
35 }
36
37 if ($vars{inc_version} && $vars{lib_version}) {
38 say "Found ØMQ $vars{lib_version}; skipping installation";
39 } else {
40 say "ØMQ not found; building from source...";
41 %vars = $self->install_zeromq;
42 }
43
44 # write vars to ZMQ.pm
45 my $module = catfile qw/blib lib Alien ZMQ.pm/;
46 open my $LIB, "<$module" or die "Cannot read module";
47 my $lib = do { local $/; <$LIB> };
48 close $LIB;
49 $lib =~ s/^sub inc_dir.*$/sub inc_dir { "$vars{inc_dir}" }/m;
50 $lib =~ s/^sub lib_dir.*$/sub lib_dir { "$vars{lib_dir}" }/m;
51 $lib =~ s/^sub inc_version.*$/sub inc_version { v$vars{inc_version} }/m;
52 $lib =~ s/^sub lib_version.*$/sub lib_version { v$vars{lib_version} }/m;
53 my @stats = stat $module;
54 chmod 0644, $module;
55 open $LIB, ">$module" or die "Cannot write config to module";
56 print $LIB $lib;
57 close $LIB;
58 chmod $stats[2], $module;
59
60 open my $TARGET, ">build-zeromq";
61 print $TARGET time, "\n";
62 close $TARGET;
63 }
64
65 sub probe_zeromq {
66 my $self = shift;
67 my $cb = $self->cbuilder;
68
69 my $src = "test-$$.c";
70 open my $SRC, ">$src";
71 print $SRC <<END;
72 #include <stdio.h>
73 #include <zmq.h>
74 int main(int argc, char* argv[]) {
75 int major, minor, patch;
76 zmq_version(&major, &minor, &patch);
77 printf("%d.%d.%d %d.%d.%d",
78 ZMQ_VERSION_MAJOR, ZMQ_VERSION_MINOR, ZMQ_VERSION_PATCH,
79 major, minor, patch);
80 return 0;
81 }
82 END
83 close $SRC;
84
85 my @inc_search;
86 my @lib_search;
87
88 my $cflags = $self->args('zmq-cflags');
89 my $libs = $self->args('zmq-libs');
90
91 my $pkg_version;
92
93 my $pkg_config = $ENV{PKG_CONFIG_COMMAND} || "pkg-config";
94 for my $pkg (qw/libzmq zeromq3/) {
95 $pkg_version = `$pkg_config $pkg --modversion`;
96 chomp $pkg_version;
97 next unless $pkg_version;
98
99 $cflags ||= `$pkg_config $pkg --cflags`;
100 $libs ||= `$pkg_config $pkg --libs`;
101
102 # use -I and -L flag arguments as extra search directories
103 my $inc = `$pkg_config $pkg --cflags-only-I`;
104 push @inc_search, map { s/^-I//; $_ } split(/\s+/, $inc);
105 my $lib = `$pkg_config $pkg --libs-only-L`;
106 push @lib_search, map { s/^-L//; $_ } split(/\s+/, $lib);
107
108 last;
109 }
110
111 my $obj = eval {
112 $cb->compile(source => $src, include_dirs => [@inc_search], extra_compiler_flags => $cflags);
113 };
114 unlink $src;
115 return unless $obj;
116
117 my $exe = eval {
118 $cb->link_executable(objects => $obj, extra_linker_flags => $libs);
119 };
120 unlink $obj;
121 return unless $exe;
122
123 my $out = `./$exe`;
124 unlink $exe;
125 my ($inc_version, $lib_version) = $out =~ /(\d\.\d\.\d) (\d\.\d\.\d)/;
126
127 # query the compiler for include and library search paths
128 my $cc = $ENV{CC} || "cc";
129 push @lib_search, map {
130 my $path = $_;
131 $path =~ s/^.+ =?//;
132 $path =~ s/\n.*$//;
133 -d $path ? realpath($path) : ();
134 } split /:/, `$cc -print-search-dirs`;
135 push @inc_search, map {
136 my $path = $_;
137 $path =~ s/lib(32|64)?$/include/;
138 $path;
139 } @lib_search;
140
141 # search for the header and library files
142 my ($inc_dir) = grep { -f catfile($_, "zmq.h") } @inc_search;
143 my ($lib_dir) = grep {
144 -f catfile($_, "libzmq.so") ||
145 -f catfile($_, "libzmq.dylib") ||
146 -f catfile($_, "libzmq.dll")
147 } @lib_search;
148
149 (
150 inc_version => $inc_version,
151 lib_version => $lib_version,
152 pkg_version => $pkg_version,
153 inc_dir => $inc_dir,
154 lib_dir => $lib_dir,
155 );
156 }
157
158 sub install_zeromq {
159 my $self = shift;
160
161 can_run("libtool") or die "The libtool command cannot be found";
162
163 my $version = $self->notes('zmq-version');
164 my $sha1 = $self->notes('zmq-sha1');
165 my $archive = "zeromq-$version.tar.gz";
166
167 say "Downloading ØMQ $version source archive from download.zeromq.org...";
168 getstore("http://download.zeromq.org/$archive", $archive) == RC_OK
169 or die "Failed to download ØMQ source archive";
170
171 say "Verifying...";
172 my $sha1sum = Digest::SHA->new;
173 open my $ARCHIVE, "<$archive";
174 binmode $ARCHIVE;
175 $sha1sum->addfile($ARCHIVE);
176 close $ARCHIVE;
177 $sha1sum->hexdigest eq $sha1 or die "Source archive checksum mismatch";
178
179 say "Extracting...";
180 Archive::Tar->new($archive)->extract;
181 unlink $archive;
182
183 my $prefix = catdir($self->install_destination("lib"), qw/auto share dist Alien-ZMQ/);
184 my $basedir = $self->base_dir;
185 my $datadir = catdir($basedir, "share");
186 my $srcdir = catdir($basedir, "zeromq-$version");
187
188 say "Configuring...";
189 my @config = split(/\s/, $self->args('zmq-config') || "");
190 chdir $srcdir;
191 run(command => ["./configure", "--prefix=$prefix", @config])
192 or die "Failed to configure ØMQ";
193
194 say "Compiling...";
195 run(command => ['make']) or die "Failed to make ØMQ";
196
197 say "Installing...";
198 run(command => [qw|make install prefix=/|, "DESTDIR=$datadir"])
199 or die "Failed to install ØMQ";
200
201 chdir $basedir;
202 remove_tree($srcdir);
203
204 (
205 inc_version => $version,
206 lib_version => $version,
207 inc_dir => catdir($prefix, "include"),
208 lib_dir => catdir($prefix, "lib"),
209 );
210 }
211
212 1;
This page took 0.046718 seconds and 3 git commands to generate.