]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Gcf2.pm
gcf2*: more descriptive package descriptions
[public-inbox.git] / lib / PublicInbox / Gcf2.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # backend for public-inbox-gcf2(1) (git-cat-file based on libgit2,
5 # other libgit2 stuff may go here, too)
6 package PublicInbox::Gcf2;
7 use strict;
8 use PublicInbox::Spawn qw(which popen_rd);
9 use Fcntl qw(LOCK_EX);
10 my (%CFG, $c_src, $lockfh);
11 BEGIN {
12         # PublicInbox::Spawn will set PERL_INLINE_DIRECTORY
13         # to ~/.cache/public-inbox/inline-c if it exists
14         my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //
15                 die 'PERL_INLINE_DIRECTORY not defined';
16         my $f = "$inline_dir/.public-inbox.lock";
17         open $lockfh, '>', $f or die "failed to open $f: $!\n";
18         my $pc = which($ENV{PKG_CONFIG} // 'pkg-config');
19         my ($dir) = (__FILE__ =~ m!\A(.+?)/[^/]+\z!);
20         my $rdr = {};
21         open $rdr->{2}, '>', '/dev/null' or die "open /dev/null: $!";
22         for my $x (qw(libgit2)) {
23                 my $l = popen_rd([$pc, '--libs', $x], undef, $rdr);
24                 $l = do { local $/; <$l> };
25                 next if $?;
26                 my $c = popen_rd([$pc, '--cflags', $x], undef, $rdr);
27                 $c = do { local $/; <$c> };
28                 next if $?;
29
30                 # note: we name C source files .h to prevent
31                 # ExtUtils::MakeMaker from automatically trying to
32                 # build them.
33                 my $f = "$dir/gcf2_$x.h";
34                 if (open(my $fh, '<', $f)) {
35                         chomp($l, $c);
36                         local $/;
37                         $c_src = <$fh>;
38                         $CFG{LIBS} = $l;
39                         $CFG{CCFLAGSEX} = $c;
40                         last;
41                 } else {
42                         die "E: $f: $!\n";
43                 }
44         }
45         die "E: libgit2 not installed\n" unless $c_src;
46
47         # CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
48         flock($lockfh, LOCK_EX) or die "LOCK_EX failed on $f: $!\n";
49 }
50
51 # we use Capitalized and ALLCAPS for compatibility with old Inline::C
52 use Inline C => Config => %CFG, BOOT => 'git_libgit2_init();';
53 use Inline C => $c_src;
54 undef $c_src;
55 undef %CFG;
56 undef $lockfh;
57 1;