1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
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;
8 use PublicInbox::Spawn qw(which popen_rd);
10 my (%CFG, $c_src, $lockfh);
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!);
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> };
26 my $c = popen_rd([$pc, '--cflags', $x], undef, $rdr);
27 $c = do { local $/; <$c> };
30 # note: we name C source files .h to prevent
31 # ExtUtils::MakeMaker from automatically trying to
33 my $f = "$dir/gcf2_$x.h";
34 if (open(my $fh, '<', $f)) {
45 die "E: libgit2 not installed\n" unless $c_src;
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";
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;