user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/3] gcf2: switch build phase to use autodie
Date: Tue, 12 Sep 2023 10:48:30 +0000	[thread overview]
Message-ID: <20230912104830.1773716-4-e@80x24.org> (raw)
In-Reply-To: <20230912104830.1773716-1-e@80x24.org>

This simplifies much of our code since much of it is
error-handling.
---
 lib/PublicInbox/Gcf2.pm | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/lib/PublicInbox/Gcf2.pm b/lib/PublicInbox/Gcf2.pm
index 9ca99d00..0f4d2bf0 100644
--- a/lib/PublicInbox/Gcf2.pm
+++ b/lib/PublicInbox/Gcf2.pm
@@ -12,29 +12,28 @@ use IO::Handle; # autoflush
 use PublicInbox::Git;
 
 BEGIN {
+	use autodie;
 	my (%CFG, $c_src);
 	# PublicInbox::Spawn will set PERL_INLINE_DIRECTORY
 	# to ~/.cache/public-inbox/inline-c if it exists and Inline::C works
 	my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //
 		die 'PERL_INLINE_DIRECTORY not defined';
-	my $f = "$inline_dir/.public-inbox.lock";
-	open my $fh, '+>', $f or die "open($f): $!";
+	open my $fh, '+>', "$inline_dir/.public-inbox.lock";
 
 	# CentOS 7.x ships Inline 0.53, 0.64+ has built-in locking
-	flock($fh, LOCK_EX) or die "LOCK_EX($f): $!\n";
+	flock($fh, LOCK_EX);
 
 	my $pc = which($ENV{PKG_CONFIG} // 'pkg-config') //
 		die "pkg-config missing for libgit2";
 	my ($dir) = (__FILE__ =~ m!\A(.+?)/[^/]+\z!);
-	my $ef = "$inline_dir/.public-inbox.pkg-config.err";
-	open my $err, '+>', $ef or die "open($ef): $!";
+	open my $err, '+>', "$inline_dir/.public-inbox.pkg-config.err";
 	my $vals = {};
 	my $rdr = { 2 => $err };
 	my @switches = qw(modversion cflags libs);
 	for my $k (@switches) {
 		my $rd = popen_rd([$pc, "--$k", 'libgit2'], undef, $rdr);
 		chomp(my $val = do { local $/; <$rd> });
-		close($rd) or last; # checks for error and sets $?
+		CORE::close($rd) or last; # checks for error and sets $?
 		$vals->{$k} = $val;
 	}
 	if (!$?) {
@@ -42,9 +41,9 @@ BEGIN {
 		# ExtUtils::MakeMaker from automatically trying to
 		# build them.
 		my $f = "$dir/gcf2_libgit2.h";
-		open(my $src, '<', $f) or die "E: open($f): $!";
+		open my $src, '<', $f;
 		local $/;
-		defined($c_src = <$src>) or die "read $f: $!";
+		$c_src = <$src> // die "read $f: $!";
 	}
 	unless ($c_src) {
 		seek($err, 0, SEEK_SET);
@@ -55,23 +54,23 @@ BEGIN {
 	# can rebuild if there's changes (it doesn't seem to detect
 	# $CFG{CCFLAGSEX} nor $CFG{CPPFLAGS} changes)
 	$c_src .= "/* $pc --$_ libgit2 => $vals->{$_} */\n" for @switches;
-	open my $oldout, '>&', \*STDOUT or die "dup(1): $!";
-	open my $olderr, '>&', \*STDERR or die "dup(2): $!";
-	open STDOUT, '>&', $fh or die "1>$f: $!";
-	open STDERR, '>&', $fh or die "2>$f: $!";
+	open my $oldout, '>&', \*STDOUT;
+	open my $olderr, '>&', \*STDERR;
+	open STDOUT, '>&', $fh;
+	open STDERR, '>&', $fh;
 	STDERR->autoflush(1);
 	STDOUT->autoflush(1);
 	$CFG{CCFLAGSEX} = $vals->{cflags};
 	$CFG{LIBS} = $vals->{libs};
 
 	# we use Capitalized and ALLCAPS for compatibility with old Inline::C
-	eval <<'EOM';
+	CORE::eval <<'EOM';
 use Inline C => Config => %CFG, BOOT => q[git_libgit2_init();];
 use Inline C => $c_src, BUILD_NOISY => 1;
 EOM
 	$err = $@;
-	open(STDERR, '>&', $olderr) or warn "restore stderr: $!";
-	open(STDOUT, '>&', $oldout) or warn "restore stdout: $!";
+	open(STDERR, '>&', $olderr);
+	open(STDOUT, '>&', $oldout);
 	if ($err) {
 		seek($fh, 0, SEEK_SET);
 		my @msg = <$fh>;

      parent reply	other threads:[~2023-09-12 10:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-12 10:48 [PATCH 0/3] just-ahead-of-time build improvements Eric Wong
2023-09-12 10:48 ` [PATCH 1/3] gcf2: detect libgit2 version changes Eric Wong
2023-09-12 10:48 ` [PATCH 2/3] xap_helper_cxx: detect libxapian " Eric Wong
2023-09-12 10:48 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230912104830.1773716-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).