-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake-single.pl
executable file
·81 lines (76 loc) · 2.51 KB
/
make-single.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
#!/home/ben/software/install/bin/perl
# This turns the C files including "text-fuzzy.c" and the others
# generated from templates into a single file "text-fuzzy-single.c"
# which is then included into "Fuzzy.xs".
# The benefit of making all these small files into a single one is
# that there are far fewer small C files in the distribution, and a
# possible improvement in optimisation by the compiler compared to
# keeping them as small single files.
# This script should be run by "build.pl" via "makeitfile" as part of
# the normal build process. The outputs are also cleaned up via "make
# -f makeitfile clean"
use warnings;
use strict;
use utf8;
use FindBin '$Bin';
use File::Slurper qw!read_text write_text!;
use Convert::Moji 'make_regex';
use Deploy 'do_system';
# Switch on debugging messages.
my $verbose;
chdir $Bin or die $!;
# The redirection of output is to stop a small annoying message
# "text-fuzzy.c is already up to date".
do_system ("./make-edit-distance-c.pl;make -f makeitfile text-fuzzy.c > /dev/null", $verbose);
# These files should already have been generated by
# ./make-edit-distance-c.pl.
my @cfiles = qw!ed-trans-char.c ed-trans-int.c
edit-distance-char.c edit-distance-int.c!;
my $tfbase = "$Bin/text-fuzzy";
my $tffile = "$tfbase.c";
my $tfhfile = "$tfbase.h";
# This file is also incorporated into "text-fuzzy-single.c".
my $cfgfile = "$Bin/config.h";
for my $file ($tffile, @cfiles) {
if (! -f $file) {
warn "No file $file";
}
}
my %ctexts;
for my $file (@cfiles) {
my $txt = read_text ($file);
# Blank all the includes.
$txt =~ s!(#include.*)!/* $1 */!g;
# Make all the functions static.
$txt =~ s!^int!static int!gsm;
my $base = $file;
$base =~ s/\.c$//;
$ctexts{$base} = $txt;
}
my $regex = make_regex (keys %ctexts);
my $tftext = read_text ($tffile);
my $tfh = read_text ($tfhfile);
my $cfgh = read_text ($cfgfile);
$tftext =~ s/#include\s*"text-fuzzy.h"/#line 1 "$tfhfile"\n$tfh/;
$tftext =~ s/#include\s*"config.h"/#line 1 "$cfgfile"\n$cfgh/;
$tftext =~ s/#include\s*"($regex).h"/#line 1 "$1.c"\n$ctexts{$1}/g;
# The name of the output file.
my $tfout = "$tfbase-single.c";
if (-f $tfout) {
chmod 0644, $tfout or die $!;
}
# Print a message so that it's easier to work out how the file was built.
my $cfiles = join ', ', @cfiles;
my $head = <<EOF;
/*
This file was generated by $0
from $tffile,
$cfiles, and
$tfhfile.
*/
#line 1 "text-fuzzy.c"
EOF
$tftext = $head . $tftext;
write_text ($tfout, $tftext);
# Prevent accidental editing of the file
chmod 0444, $tfout or die $!;