-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile.pl
121 lines (100 loc) · 3.13 KB
/
Makefile.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/perl
use warnings;
use strict;
use List::Util qw{max};
use Getopt::Long;
use IPC::Run3;
my $basefile='paper';
my $bibtex_db_check=1;
my $use_pdflatex=0;
my $force=0;
my $clean=0;
GetOptions(
'basefile=s'=>\$basefile,
'bibtex-check!'=>\$bibtex_db_check,
'pdflatex'=>\$use_pdflatex,
'force!'=>\$force,
'clean!'=>\$clean
);
if($clean or ($ARGV[0] and ($ARGV[0] eq 'clean'))){
my @delfiles= map {$basefile.'.'.$_} ('aux','bbl','blg','log','pdf','dvi','ps');
print "deleting ..\t",join (" ",@delfiles),"\n";
unlink(@delfiles);
exit 0;
}
my $progname=$use_pdflatex ? "pdflatex" : "latex";
my ($error_color,$def_color)=("\e[1;31m","\e[0m");
my ($input,$output)=($basefile.".tex",$basefile.".pdf");
my @files=glob("*tex");
my @bibfiles=glob("*bib");
if( (not $force) and (-e $output)){
my $inputfile_mtime = max(map {(stat($_))[9]} @files,@bibfiles);
my $outputfile_mtime = (stat($output))[9];
if($outputfile_mtime > $inputfile_mtime){
print "$output newer than all input files, exiting...\n" ;
exit ;
}
}
#Need to recreate output.pdf
run('');
my $log_file = readLog();
if($log_file =~ qr{^! LaTeX Error:(.*$)}m){
my $error = $1 || '';
print "There was an error:$error_color";
print $error,"$def_color\n";
print "please check $basefile.log for details\n";
cleanup();
}
if($log_file =~ qr{^LaTeX Warning: Citation.*undefined on input line \d*\.$}m){
print "Found some citations undefined, running bibtex...\n";
run3 ["bibtex",$basefile],\undef,\undef,\undef;
my $biblog=readLog('blg');
if($biblog =~ m/\(There were \d* error messages\)\Z/){
my $errormsg = '';
$errormsg = $error_color.'can\'t find '.$1.$def_color if $biblog =~ m/^\QI couldn't open database file \E(.*bib)$/m;
print "BibTex ran into errors: $errormsg \t check $basefile.blg\n";
cleanup();
}
my @missing=();
push @missing,$1 while($biblog =~ m{^Warning--I didn't find a database entry for ("[^"]+")$}mg);
if(@missing){
print "BiBTeX couldn't find entries for the following cites: ",join(" ",@missing),"\n";
if ($bibtex_db_check){
print "Exiting ... change \$bibtex_db_check if you want to continue inspite of these errors\n" ;
cleanup();
}
}
run("Again");
$log_file = readLog();
}
while(index($log_file,'Rerun to get cross-references right.' ) >= 0){
run("Fixing crossrefs by again");
$log_file = readLog();
}
if($log_file =~ m/Warning/){
print "I have ran pdflatex a few times, but it still warns,.. you should check $basefile.log\n";
}
unless($use_pdflatex){
print "Converting ps to pdf...\n";
run3 ["dvips","$basefile.dvi"], \undef,\undef,\undef;
run3 ["ps2pdf","$basefile.ps"], \undef,\undef,\undef;
unlink("$basefile.dvi","$basefile.ps");
}
print "done!\n";
sub readLog{
open my $fh,'<',($basefile.'.'.($_[0] || "log") )
or die "couldn't open log file";
local $/=undef;
my $str=<$fh>;
close($fh);
return $str;
}
sub cleanup{
unlink($basefile.'.dvi', $basefile.'.pdf', $basefile.'.ps');
exit 1;
}
sub run{
print $_[0], " running $progname...\n";
run3 [$progname,'-interaction=batchmode',$input],\undef,\undef,\undef;
return $? == -1 ? 0 : $?>>8;
}