-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.pl
executable file
·354 lines (300 loc) · 9.25 KB
/
build.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/perl
use Cwd qw(abs_path cwd);
use File::Spec;
use File::stat;
our $top_dir = (File::Spec->splitpath(File::Spec->rel2abs($0)))[1];
our $opts_file = "${top_dir}opts.pl";
is_file_newer("${top_dir}configure.pl", $opts_file) and die "Build options out of date. Please run configure.pl first.\n";
require $opts_file;
my $parallel = test_parallel();
my $targets_script = defined($ARGV[0]) ? $ARGV[0] : 'targets.pl';
unless (-f $targets_script) {
print "build.pl: target script '$targets_script' does not exist.\n";
exit 1;
}
do $targets_script;
1;
# test_parallel()
# Some systems have a conflicting parallel command. Run this to make sure
# we got the right one.
sub test_parallel {
my $parallel_exe = which('parallel');
if (! -x $parallel_exe) {
return undef;
}
my $manager;
my $found_token = 0;
if (!open($manager, "$parallel_exe -H 2 echo {} ::: hi |")) {
return undef;
}
my $line = <$manager>;
chomp $line;
$line eq "hi" and $found_token = 1;
if (!close($manager) || !$found_token) {
return undef;
}
print "build.pl: parallel detected at $parallel_exe\n";
return $parallel_exe;
}
# which($exe_name)
# Perl version to avoid using the system command that does not always exist.
sub which {
my @path = File::Spec->path();
# check the path for the executable
foreach my $i (@path) {
my $loc = "$i/$_[0]";
(-x $loc) and return abs_path($loc);
}
return undef;
}
# get_mtime($file)
sub get_mtime {
my $file = stat($_[0]);
return $file->mtime;
}
# is_file_newer($file, $than, \%mtime_cache)
# Checks if $file is newer than $than.
sub is_file_newer {
(-f $_[1]) or return 1;
defined($_[2]->{$_[0]}) or $_[2]->{$_[0]} = get_mtime($_[0]);
defined($_[2]->{$_[1]}) or $_[2]->{$_[1]} = get_mtime($_[1]);
return $_[2]->{$_[0]} >= $_[2]->{$_[1]};
}
# check_includes($file_to_check, $original_file, $obj, \@inclues, \%mtime_cache)
sub check_includes {
my $flag = 0;
my $latest_mtime = 0;
open (my $file, $_[0]) or return 0;
while (1) {
my $line = <$file>;
defined($line) or last;
my ($include) = $line =~ /#include\s+<([^>]+)>/;
if (defined($include)) {
foreach my $i (@{$_[3]}) {
my $attempt = "$i/$include";
if (-f $attempt) {
# recursion is disabled for now
#unless (defined($_[4]->{$i})) {
# my ($ret, $mtime) = check_includes($attempt, $_[1], $_[2], $_[3], $_[4]);
# $_[4]->{$attempt} = $mtime;
# $ret and $flag = 1;
#}
if (is_file_newer($attempt, $_[1], $_[4]) &&
is_file_newer($attempt, $_[2], $_[4])) {
$flag = 1;
}
$_[4]->{$attempt} > $latest_mtime and $latest_mtime = $_[4]->{$attempt};
last;
}
}
}
}
close ($file);
return ($flag, $latest_mtime);
}
# needs_building($file, $than, \@includes, \%mtime_cache)
# Checks if targets or build options changed in addition to checking
# whether the object file needs an update.
# C++ files check the immediate include files if they are newer too.
sub needs_building {
$_[0] =~ /.cpp$/ and (check_includes($_[0], $_[0], $_[1], $_[2], $_[3]))[0] and return 1;
return is_file_newer($_[0], $_[1]) ||
is_file_newer('targets.pl', $_[1]) ||
is_file_newer($opts_file, $_[1]);
}
sub get_asm_cmd {
return "jwasm $jwasm_args -zt1";
}
# get_cxx_cmd(\@includes, \@defines)
sub get_cxx_cmd {
my @cc_opts = ('g++', '-c');
# OWORLD currently miscompiles at -O2 on gcc 4.3.3.
#push (@cc_opts, $no_asm ? "-O2" : "-O1");
defined($ENV{CFLAGS}) and push (@cc_opts, $ENV{CFLAGS});
defined($ENV{CXXFLAGS}) and push (@cc_opts, $ENV{CXXFLAGS});
defined($debug) and $debug and push (@cc_opts, "-g");
push (@cc_opts, "-fpermissive");
defined($enable_multilib) and $enable_multilib and push (@cc_opts, "-m32");
push (@cc_opts, map { "-D$_" } @{$_[1]});
push (@cc_opts, map { "-I$_" } @{$_[0]});
return "@cc_opts";
}
sub get_wrc_cmd {
return $platform =~ /^linux/ ? 'wrc' : 'windres';
}
sub link_exe {
my ($exe, $obj_files, $libs, $lib_dirs) = @_;
defined($exe) or return 1; # No exe targets here
my $flag = 0;
foreach my $i (@$obj_files) {
unless (-f $i) {
print "build.pl: missing file '$i' for linking.\n";
exit 1;
}
# check modification times to see if we have to relink
if (-f $exe) {
is_file_newer($i, $exe) and $flag = 1;
} else {
$flag = 1;
}
}
if ($flag) {
my $linker = 'g++';
my @linker_opts;
if ($platform =~ /^linux/ && !$disable_wine) {
$linker = 'wineg++';
}
defined($ENV{LDFLAGS}) and push(@linker_opts, $ENV{LDFLAGS});
$debug and push(@linker_opts, '-g');
defined($enable_multilib) and $enable_multilib and push (@cc_opts, "-m32");
# windows based compiler options
unless ($disable_wine) {
push(@linker_opts, '-mno-cygwin');
$debug or push(@linker_opts, '-mwindows');
}
my $cmd = "$linker " .
join(' ', @linker_opts) . ' ' .
"@$obj_files " .
join(' ', map { "-l$_" } @$libs) . ' ' .
join(' ', map { "-L$_" } @$lib_dirs) . ' ' .
"-o $exe";
print $cmd,"\n";
if (system $cmd) {
print "build.pl: couldn't create executable '$exe'\n";
exit 1;
}
}
return 1;
}
# include_targets(@target_files)
#
# Usage: Use in your target files to include build stages found in
# other directories. This will change directory to where your
# target file is found in and execute the target script.
#
# Note that the new target file executed will have its own clean
# scope. This forces you to redefine what is necessary to run
# the building. It makes sense if you are logically grouping
# various targets by their purpose.
#
# Returns paths to the built targets.
#
sub include_targets {
my @built_targets;
my $orig_dir = cwd;
foreach my $i (@_) {
unless (-f $i) {
print "build.pl: target script '$i' does not exist.\n";
exit 1;
}
# change directory
my ($dir, $inc) = (File::Spec->splitpath($i))[1,2];
($dir) = $dir =~ /(.+?)\/?$/;
unless (-d $dir && chdir $dir) {
print "build.pl: unable to enter directory '$dir'.\n";
exit 1;
}
print "Entering '$dir'.\n";
# run script
push (@built_targets, map { "$dir/$_" } do $inc);
# go back to the original directory
print "Leaving '$dir'.\n";
unless (chdir $orig_dir) {
print "build.pl: original directory disappeared.\n";
exit 1;
}
}
return @built_targets;
}
sub break_extension {
my @parts = split('\.', $_[0]);
my $extension = pop(@parts);
my $name = join('.', @parts);
return ($name, $extension);
}
# build_targets(\@files_to_build, \@includes, \@defines)
#
# Usage: Called from target script. An array of files
# that are to be built is passed.
# i.e. ('AM.cpp','7k.ico', 'IB.asm')
# or qw(AM.cpp 7k.ico IB.asm)
#
# Pass the include paths and defines for the C++ compiler
# as needed for the source files to be built.
#
sub build_targets {
my %mtime_cache;
my @built_objects;
my %jobs;
# Read in input files, catagorize into jobs by extension
foreach my $i (@{$_[0]}) {
my ($name, $extension) = break_extension($i);
unless (defined($extension)) {
print "build.pl: no extension to figure out the file type of '$i'\n";
exit 1;
}
my $obj = "$name.o";
if (needs_building($i, $obj, $_[1], \%mtime_cache)) {
push (@{$jobs{$extension}}, $name);
}
push (@built_objects, $obj);
}
# execute the jobs
foreach my $i (keys %jobs) {
# Using the GNU parallel build system
if (defined($parallel)) {
my $cmd;
# get the command to build this type of file
if ($i eq 'cpp') {
my $cxx_cmd = get_cxx_cmd($_[1], $_[2]);
$cmd = "$cxx_cmd {}.cpp -o {}.o";
} elsif ($i eq 'asm') {
my $asm_cmd = get_asm_cmd();
$cmd = "$asm_cmd -Fo {}.o {}.asm";
} elsif ($i eq 'rc') {
my $wrc_cmd = get_wrc_cmd();
$cmd = "$wrc_cmd -i {}.rc -o {}.o";
} else {
print "build.pl: cannot identify how to build file type '$extension'\n";
exit 1;
}
my $manager;
if (!open($manager, "|-", "$parallel -H 2 --verbose '$cmd'")) {
print "build.pl: couldn't start the parallel build mananger.";
exit 1;
}
foreach my $i (@{$jobs{$i}}) {
print $manager "$i\n";
}
if (!close($manager)) {
print "build.pl: the build job failed. Stopping.\n";
exit 1;
}
# systems that cannot use GNU parallel
} else {
foreach my $j (@{$jobs{$i}}) {
my $cmd;
# get the command to build this type of file
if ($i eq 'cpp') {
my $cxx_cmd = get_cxx_cmd($_[1], $_[2]);
$cmd = "$cxx_cmd $j.cpp -o $j.o";
} elsif ($i eq 'asm') {
my $asm_cmd = get_asm_cmd();
$cmd = "$asm_cmd -Fo $j.o $j.asm";
} elsif ($i eq 'rc') {
my $wrc_cmd = get_wrc_cmd();
$cmd = "$wrc_cmd -i $j.rc -o $j.o";
} else {
print "build.pl: cannot identify how to build file type '$extension'\n";
exit 1;
}
print "$cmd\n";
if (system $cmd) {
print "build.pl: couldn't build '$i'. Stopping.\n";
exit 1;
}
}
}
}
return @built_objects;
}