-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake-c-file.pl
executable file
·213 lines (188 loc) · 4.66 KB
/
make-c-file.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/home/ben/software/install/bin/perl
# Make the single C file from the files
use warnings;
use strict;
use utf8;
use FindBin '$Bin';
use File::Slurper qw!read_text write_text!;
use C::Tokenize ':all';
use JSON::Create ':all';
use File::Copy 'copy';
use Getopt::Long;
# Turn on or off debugging messages.
my $ok = GetOptions (
verbose => \my $verbose,
);
if (! $ok) {
print <<EOF;
--verbose - debugging messages
EOF
exit;
}
# Version of the library
my %version;
my $dir = "$Bin/../../software/libdeflate/libdeflate-1.23";
if (! -d $dir) {
die "No $dir";
}
my $lib = "$dir/lib";
my $common = "$dir/common";
my @cfiles;
push @cfiles, <$common/*.c>;
push @cfiles, <$lib/*.c>;
push @cfiles, <$lib/arm/*.c>;
push @cfiles, <$lib/x86/*.c>;
my @hfiles;
push @hfiles, <$dir/*.h>;
push @hfiles, <$common/*.h>;
push @hfiles, <$lib/*.h>;
my @x86 = <$lib/x86/*.h>;
for (@x86) {
s!x86/!x86-!;
}
push @hfiles, @x86;
my @arm = <$lib/arm/*.h>;
for (@arm) {
s!arm/!arm-!;
}
push @hfiles, @arm;
my %includes;
for my $file (@hfiles) {
my $bfile = $file;
my $hfile = $file;
$bfile =~ s!.*/!!;
if ($verbose) {
print "Processing header file '$bfile':\n";
}
$hfile =~ s!(x86|arm)-!$1/!g;
my $text = read_text ($hfile);
if ($text =~ m!#define\s+(LIBDEFLATE_VERSION_STRING)\s+"(.*?)"!) {
if ($verbose) {
print "Found $1 '$2' in $hfile\n";
}
$version{$1} = $2;
}
if ($hfile =~ m!(arm|x86)!) {
my $type = $1;
$text =~ s!#\s*include\s*"(.*?)"!#include "$type-$1"!g;
$text =~ s!$type-\.\./!!;
}
$text =~ s!$comment_re!!g;
$includes{$bfile} = $text;
}
if ($verbose) {
print "Done files.\n";
}
# Copy the copyright of libdeflate into this directory
copy ("$dir/COPYING", "$Bin/libd-copyr") or die $!;
# The text contents of the output file.
my $c = '';
my $copyright = read_text ("$Bin/libd-copyr");
$c .= "/*\n$copyright\n*/\n";
my $saw_bitbuf_t;
for my $cfile (@cfiles) {
if (-d $cfile) {
next;
}
my $text = read_text ($cfile);
$text =~ s!$comment_re!!g;
if ($cfile =~ m!(arm|x86)!) {
my $type = $1;
$text =~ s!#\s*include\s*"(.*?)"!#include "$type-$1"!g;
$text =~ s!$type-\.\./!!;
}
if ($cfile =~ m!(adler32|crc32)!) {
my $type = $1;
$text =~ s!\bdispatch!${type}_dispatch!g;
}
if ($cfile =~ m!deflate_((?:de)?compress)!) {
my $what = uc $1;
$text =~ s!(BITBUF_NBITS)!${what}_$1!g;
}
# http://www.cpantesters.org/cpan/report/648fc304-6d58-11eb-8344-d38a1f24ea8f
# http://www.cpantesters.org/cpan/report/6ff92c12-6d58-11eb-8344-d38a1f24ea8f
# http://www.cpantesters.org/cpan/report/6e732488-6d58-11eb-8344-d38a1f24ea8f
if ($text =~ m!(typedef.*bitbuf_t;)!) {
if ($saw_bitbuf_t) {
$text =~ s!(typedef.*bitbuf_t;)!/* $1 */!g;
}
$saw_bitbuf_t = 1;
}
$c .= "/* $cfile */\n";
$c .= $text;
}
while ($c =~ m!
(
\#\s*include\s*[<"]
.*? # Allow for directory paths
(
(?:common_defs
|compiler_(?:gcc|msc)
|lib_common
|libdeflate
|(?:arm|x86)-
(cpu_features
|crc32_pclmul_template
# http://www.cpantesters.org/cpan/report/239faa2c-49ba-11ed-afcc-a473647750dd
|crc32_pmull_helpers
|crc32_pmull_wide
|adler32_template
)
|(?:x86|arm)/.*?
|adler32_vec_template
|crc32_vec_template
|crc32_table
|crc32_tables
|crc32_multipliers
|deflate_compress
|deflate_constants
|unaligned
|hc_matchfinder
|bt_matchfinder
|ht_matchfinder
|matchfinder_common
|decompress_template
|gzip_constants
|zlib_constants
|cpu_features_common
)\.h
)
[">]
)!gx) {
my $include = $1;
my $hfile = $2;
$hfile =~ s!(x86|arm)/!$1-!;
my $nohash = $include;
# Prevent the commented #include from matching the above big
# regex.
$nohash =~ s!#!hashhashhash!;
if ($includes{$hfile}) {
# lib/cpu_features_common.h has no include guard so we must
# not include it twice.
if ($hfile =~ /cpu_features_common\.h/) {
$c =~ s!$include!/* $nohash */\n$includes{$hfile}!;
$c =~ s!$include!/* $nohash - no include guard */!g;
}
else {
$c =~ s!$include!/* $nohash */\n$includes{$hfile}!g;
}
}
else {
warn "$hfile not found";
}
}
# Solaris doesn't like len_t:
# http://www.cpantesters.org/cpan/report/14fb7626-6e2f-11eb-84bc-edd243e66a77
$c =~ s!\blen_t\b!libdeflate_len_t!g;
# Restore #
$c =~ s!hashhashhash!#!g;
write_text ("$Bin/libdeflate-one.c", $c);
# Test compilation
system ("cc -c $Bin/libdeflate-one.c");
# Remove the o file, we are going to include the C file in our thing.
unlink "$Bin/libdeflate-one.o" or die $!;
if (! keys %version) {
die "No version information was found";
}
write_json ("$Bin/version.json", \%version);
exit;