+# from a -Dmksymlink target dir, I need to cd to the git-src tree to
+# use git (like script does). Presuming that's not unique, one fix is
+# to follow Configure's symlink-path to run git. Maybe GIT_DIR or
+# path-args can solve it, if so we should advise here, I tried only
+# very briefly ('cd -' works too).
+
+my ($subcd, $srcdir);
+our $opt_v = scalar grep $_ eq '-v', @ARGV;
+
+BEGIN {
+ my $root=".";
+ # test 1st to see if we're a -Dmksymlinks target dir
+ $subcd = '';
+ $srcdir = $root;
+ if (-l "./Configure") {
+ $srcdir = readlink("./Configure");
+ $srcdir =~ s/Configure//;
+ $subcd = "cd $srcdir &&"; # activate backtick fragment
+ }
+ while (!-e "$root/perl.c" and length($root)<100) {
+ if ($root eq '.') {
+ $root="..";
+ } else {
+ $root.="/..";
+ }
+ }
+ die "Can't find toplevel" if !-e "$root/perl.c";
+ sub path_to { "$root/$_[0]" } # use $_[0] if this'd be placed in toplevel.
+}
+
+sub read_file {
+ my $file = path_to(@_);
+ return "" unless -e $file;
+ open my $fh, '<', $file
+ or die "Failed to open for read '$file':$!";
+ return do { local $/; <$fh> };
+}
+
+sub write_file {
+ my ($file, $content) = @_;
+ $file= path_to($file);
+ open my $fh, '>', $file
+ or die "Failed to open for write '$file':$!";
+ print $fh $content;
+ close $fh;
+}
+
+sub backtick {
+ # only for git. If we're in a -Dmksymlinks build-dir, we need to
+ # cd to src so git will work . Probably a better way.
+ my $command = shift;
+ if (wantarray) {
+ my @result= `$subcd $command`;
+ warn "$subcd $command: \$?=$?\n" if $?;
+ print "#> $subcd $command ->\n @result\n" if !$? and $opt_v;
+ chomp @result;
+ return @result;
+ } else {
+ my $result= `$subcd $command`;
+ $result="" if ! defined $result;
+ warn "$subcd $command: \$?=$?\n" if $?;
+ print "#> $subcd $command ->\n $result\n" if !$? and $opt_v;
+ chomp $result;
+ return $result;
+ }
+}