From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <e@80x24.org>
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net
X-Spam-Level: 
X-Spam-ASN:  
X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00,
	DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,
	T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no
	version=3.4.6
Received: from localhost (dcvr.yhbt.net [127.0.0.1])
	by dcvr.yhbt.net (Postfix) with ESMTP id 3D9E31F487
	for <meta@public-inbox.org>; Fri,  8 Dec 2023 03:54:39 +0000 (UTC)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org;
	s=selector1; t=1702007679;
	bh=rjvHdI1jpRycF7DpQfY7SpnoHFOmFF3P9CgW4MRM+Xg=;
	h=From:To:Subject:Date:In-Reply-To:References:From;
	b=E55zvPv2QD/qCnkb4r3sy2zgtlQZPS8JhWrBQNKCNhrmFwg1d8V6msDbqEJAAI53p
	 psyQ7lxZdb/6oXnJQy+ouuLsaWr3T86TQFxW8d5O1MQ3VkOxdcJljmVcv4hz6cqq8/
	 QxBdkDFJ0/1R9s1Y3K0au/qmBgQ/8fSR8R81Sz4M=
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/6] xap_helper_cxx: drop chdir usage in build
Date: Fri,  8 Dec 2023 03:54:35 +0000
Message-ID: <20231208035438.3710696-4-e@80x24.org>
In-Reply-To: <20231208035438.3710696-1-e@80x24.org>
References: <20231208035438.3710696-1-e@80x24.org>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
List-Id: <meta.public-inbox.org>

While chdir simplifies path manipulation on our end, its use
falls over when PERL5LIB/@INC contains relative paths which need
to be made absolute.  It's fewer lines of code to get eliminate
chdir usage than it is to keep using relative paths in most
places.
---
 lib/PublicInbox/XapHelperCxx.pm | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/lib/PublicInbox/XapHelperCxx.pm b/lib/PublicInbox/XapHelperCxx.pm
index 1aa75f2a..17f988ee 100644
--- a/lib/PublicInbox/XapHelperCxx.pm
+++ b/lib/PublicInbox/XapHelperCxx.pm
@@ -60,17 +60,12 @@ sub build () {
 	}
 	require PublicInbox::CodeSearch;
 	require PublicInbox::Lock;
-	require PublicInbox::OnDestroy;
 	my ($prog) = ($bin =~ m!/([^/]+)\z!);
 	my $lk = PublicInbox::Lock->new("$dir/$prog.lock")->lock_for_scope;
 	write_file '>', "$dir/$prog.cpp", qq{#include "xap_helper.h"\n},
 			PublicInbox::Search::generate_cxx(),
 			PublicInbox::CodeSearch::generate_cxx();
 
-	opendir my $dh, '.';
-	my $restore = PublicInbox::OnDestroy->new(\&chdir, $dh);
-	chdir $dir;
-
 	# xap_modversion may be set by needs_rebuild
 	$xap_modversion //= xap_cfg('--modversion');
 	my $fl = xap_cfg(qw(--libs --cflags));
@@ -83,15 +78,15 @@ sub build () {
 	$^O eq 'netbsd' and $fl =~ s/(\A|[ \t])\-L([^ \t]+)([ \t]|\z)/
 				"$1-L$2 -Wl,-rpath=$2$3"/egsx;
 	my @xflags = split(' ', "$fl $xflags"); # ' ' awk-mode eats leading WS
-	my @cflags = grep(!/\A-(?:Wl|l|L)/, @xflags);
-	run_die([$cxx, '-c', "$prog.cpp", '-I', $srcpfx, @cflags]);
-	run_die([$cxx, '-o', "$prog.tmp", "$prog.o", @xflags]);
-	unlink "$prog.cpp", "$prog.o";
-	write_file '>', 'XFLAGS.tmp', $xflags, "\n";
-	write_file '>', 'xap_modversion.tmp', $xap_modversion, "\n";
+	my @cflags = ('-I', $srcpfx, grep(!/\A-(?:Wl|l|L)/, @xflags));
+	run_die([$cxx, '-o', "$dir/$prog.o", '-c', "$dir/$prog.cpp", @cflags]);
+	run_die([$cxx, '-o', "$dir/$prog.tmp", "$dir/$prog.o", @xflags]);
+	unlink "$dir/$prog.cpp", "$dir/$prog.o";
+	write_file '>', "$dir/XFLAGS.tmp", $xflags, "\n";
+	write_file '>', "$dir/xap_modversion.tmp", $xap_modversion, "\n";
 	undef $xap_modversion; # do we ever build() twice?
 	# not quite atomic, but close enough :P
-	rename("$_.tmp", $_) for ($prog, qw(XFLAGS xap_modversion));
+	rename("$dir/$_.tmp", "$dir/$_") for ($prog, qw(XFLAGS xap_modversion));
 }
 
 sub check_build () {