forked from wxWidgets/wxWidgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupmake
executable file
·1549 lines (1216 loc) · 51.3 KB
/
upmake
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
# This chunk of stuff was generated by App::FatPacker. To find the original
# file's code, look for the end of this BEGIN block or the string 'FATPACK'
BEGIN {
my %fatpacked;
$fatpacked{"Makefile/Update.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'MAKEFILE_UPDATE';
package Makefile::Update;
# ABSTRACT: Update make files.
use strict;
use warnings;
use autodie;
use Exporter qw(import);
our @EXPORT = qw(read_files_list upmake);
# VERSION
=head1 SYNOPSIS
use Makefile::Update;
my $vars = read_files_list('files.lst');
upmake('foo.vcxproj', $vars->{sources}, $vars->{headers});
=cut
=func read_files_list
Reads the file containing the file lists definitions and returns a hash ref
with variable names as keys and refs to arrays of the file names as values.
Takes an (open) file handle as argument.
The file contents is supposed to have the following very simple format:
# Comments are allowed and ignored.
#
# The variable definitions must always be in the format shown below,
# i.e. whitespace is significant and there should always be a single
# file per line.
sources =
file1.cpp
file2.cpp
headers =
file1.h
file2.h
# It is also possible to define variables in terms of other variables
# defined before it in the file (no forward references):
everything =
$sources
$headers
=cut
sub read_files_list
{
my ($fh) = @_;
my ($var, %vars);
while (<$fh>) {
chomp;
s/#.*$//;
s/^\s+//;
s/\s+$//;
next if !$_;
if (/^(\w+)\s*=$/) {
$var = $1;
} else {
die "Unexpected contents outside variable definition at line $.\n"
unless defined $var;
if (/^\$(\w+)$/) {
my $name = $1;
die qq{Reference to undefined variable "$name" in the } .
qq{assignment to "$var" at line $.\n}
unless exists $vars{$name};
my $value = $vars{$name};
push @{$vars{$var}}, $_ for @$value;
} else {
push @{$vars{$var}}, $_;
}
}
}
return \%vars;
}
=func upmake
Update a file in place using the specified function and passing it the rest of
the arguments.
The first parameter is either just the file path or a hash reference which may
contain the following keys:
=over
=item C<file>
The path to the file to be updated, required.
=item C<verbose>
If true, give more messages about what is being done.
=item C<quiet>
If true, don't output any non-error messages.
=item C<dryrun>
If true, don't really update the file but just output whether it would have
been updated or not. If C<verbose> is also true, also output the diff of the
changes that would have been done.
=back
This is meant to be used with C<update_xxx()> defined in different
Makefile::Update::Xxx modules.
Returns 1 if the file was changed or 0 otherwise.
=cut
sub upmake
{
my $file_or_options = shift;
my ($updater, @args) = @_;
my ($fname, $verbose, $quiet, $dryrun);
if (ref $file_or_options eq 'HASH') {
$fname = $file_or_options->{file};
$verbose = $file_or_options->{verbose};
$quiet = $file_or_options->{quiet};
$dryrun = $file_or_options->{dryrun};
} else {
$fname = $file_or_options;
$verbose =
$quiet =
$dryrun = 0;
}
if ($dryrun) {
my $old = do {
local $/;
open my $f, '<', $fname;
<$f>
};
my $new = '';
open my $in, '<', \$old;
open my $out, '>', \$new;
if ($updater->($in, $out, @args)) {
print qq{Would update "$fname"};
if ($verbose) {
if (eval { require Text::Diff; }) {
print " with the following changes:\n";
print Text::Diff::diff(\$old, \$new, {
FILENAME_A => $fname,
FILENAME_B => "$fname.new"
});
} else {
print ".\n";
warn qq{Can't display diff of the changes, please install Text::Diff module.\n};
}
} else {
print ".\n";
}
} else {
print qq{Wouldn't change the file "$fname".\n};
}
return 0;
}
my $fname_new = "$fname.upmake.new"; # TODO make it more unique
open my $in, '<', $fname;
open my $out, '>', $fname_new;
my $changed = $updater->($in, $out, @args);
close $in;
close $out;
if ($changed) {
rename $fname_new, $fname;
} else {
unlink $fname_new;
}
if ($changed) {
print qq{File "$fname" successfully updated.\n} unless $quiet;
return 1;
} else {
print qq{No changes in the file "$fname".\n} if $verbose;
return 0;
}
}
1;
MAKEFILE_UPDATE
$fatpacked{"Makefile/Update/Bakefile0.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'MAKEFILE_UPDATE_BAKEFILE0';
package Makefile::Update::Bakefile0;
# ABSTRACT: Update bakefile-0.x files list.
use Exporter qw(import);
our @EXPORT = qw(update_bakefile_0);
use strict;
use warnings;
# VERSION
=head1 SYNOPSIS
This is used exclusively to update wxWidgets C<files.bkl> and is probably not
useful outside of wxWidgets project.
use Makefile::Update::Bakefile0;
Makefile::Update::upmake('bakefiles/files.bkl', \&update_bakefile_0, $vars);
=head1 SEE ALSO
Makefile::Update
=cut
=func update_bakefile_0
Update file with variable definitions in bakefile-0 format with the data
from the hash ref containing all the file lists.
Takes the (open) file handles of the files to read and to write and the file
lists hash ref as arguments.
Returns 1 if any changes were made.
=cut
sub update_bakefile_0
{
my ($in, $out, $vars) = @_;
# Variable whose contents is being currently replaced.
my $var;
# Hash with files defined for the specified variable as keys and 0 or 1
# depending on whether we have seen them in the input file as values.
my %files;
# <if>-related state.
my ($seen_any_files, $wrapped_in_if, $if_nesting_level, $add_new_files);
# Set to 1 if we made any changes.
my $changed = 0;
while (<$in>) {
chomp;
if (/<set var="(\w+)" hints="files">/ && exists $vars->{$1}) {
$var = $1;
%files = map { $_ => 0 } @{$vars->{$var}};
$seen_any_files = 0;
$if_nesting_level = 0;
$add_new_files = 0;
} elsif (defined $var) {
local $_ = $_;
s/<!-- .* -->//;
s/^\s+//;
s/\s+$//;
# We need to handle <if>...</if> inside the files list if only
# because we need to insert any newly added files before the final
# </if>.
if (m{<if [^>]+>}) {
if (!$seen_any_files) {
# Remember that the closing tag will be </if>, not </set>.
$wrapped_in_if = 1
}
$if_nesting_level++;
} elsif (m{</if>}) {
if (!--$if_nesting_level && $wrapped_in_if) {
# We need to add any new files here, before the last
# </if> as otherwise they would end up outside of it.
$add_new_files = 1;
}
} elsif (m{</set>}) {
# Note that if we're in the $wrapped_in_if case, then this had
# already been done and $var was undefined, so we don't do it
# twice.
$add_new_files = 1
} elsif ($_) {
if (not exists $files{$_}) {
# This file was removed.
$changed = 1;
next;
}
if ($files{$_}) {
warn qq{Duplicate file "$_" in the definition of the } .
qq{variable "$var" at line $.\n}
} else {
$files{$_} = 1;
}
}
}
if ($add_new_files) {
# Check if we have any new files.
#
# TODO Insert them in alphabetical order.
while (my ($file, $seen) = each(%files)) {
if (!$seen) {
# This file was wasn't present in the input, add it.
# TODO Use proper indentation.
print $out " $file\n";
$changed = 1;
}
}
undef $var;
$add_new_files = 0
}
print $out "$_\n";
}
$changed
}
MAKEFILE_UPDATE_BAKEFILE0
$fatpacked{"Makefile/Update/CMakefile.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'MAKEFILE_UPDATE_CMAKEFILE';
package Makefile::Update::CMakefile;
# ABSTRACT: Update lists of files in CMake variables.
use Exporter qw(import);
our @EXPORT = qw(update_cmakefile);
use strict;
use warnings;
# VERSION
=head1 SYNOPSIS
This can be used to update the contents of a variable containing a list of
files in a CMake file.
use Makefile::Update::CMakefile;
Makefile::Update::upmake('CMakeLists.txt', \&update_cmakefile, $vars);
=head1 SEE ALSO
Makefile::Update
=cut
# Variables in our input files use make-like $(var) syntax while CMake uses
# shell-like ${var}, so convert to the target format.
sub _var_to_cmake
{
my ($var) = @_;
$var =~ s/\((\w+)\)/{$1}/g;
$var;
}
=func update_cmakefile
Update variable definitions in a CMake file with the data from the hash
ref containing all the file lists.
The variables are supposed to be defined in the following format:
set(var
foo
bar
baz
)
Notably, each file has to be on its own line, including the first one.
Takes the (open) file handles of the files to read and to write and the file
lists hash ref as arguments.
Returns 1 if any changes were made.
=cut
sub update_cmakefile
{
my ($in, $out, $vars) = @_;
# Variable whose contents is being currently replaced.
my $var;
# Hash with files defined for the specified variable as keys and 0 or 1
# depending on whether we have seen them in the input file as values.
my %files;
# Set to 1 if we made any changes.
my $changed = 0;
while (<$in>) {
# Preserve the original line to be able to output it with any comments
# that we strip below.
my $line_orig = $_;
# Get rid of white space and comments.
chomp;
s/^\s+//;
s/\s+$//;
s/ *#.*$//;
# Are we inside a variable definition?
if (defined $var) {
if (/^\)$/) {
# End of variable definition, check if we have any new files.
#
# TODO Insert them in alphabetical order.
while (my ($file, $seen) = each(%files)) {
if (!$seen) {
# This file wasn't present in the input, add it.
# TODO Use proper indentation.
print $out " $file\n";
$changed = 1;
}
}
undef $var;
} elsif ($_) {
# We're inside a variable definition.
if (not exists $files{$_}) {
# This file was removed.
$changed = 1;
next;
}
if ($files{$_}) {
warn qq{Duplicate file "$_" in the definition of the } .
qq{variable "$var" at line $.\n}
} else {
$files{$_} = 1;
}
}
} elsif (/^set *\( *(\w+)$/ && exists $vars->{$1}) {
# Start of a new variable definition.
$var = $1;
%files = map { _var_to_cmake($_) => 0 } @{$vars->{$var}};
}
print $out $line_orig;
}
$changed
}
MAKEFILE_UPDATE_CMAKEFILE
$fatpacked{"Makefile/Update/MSBuild.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'MAKEFILE_UPDATE_MSBUILD';
package Makefile::Update::MSBuild;
# ABSTRACT: Update list of sources and headers in MSBuild projects.
use Exporter qw(import);
our @EXPORT = qw(update_msbuild_project update_msbuild update_msbuild_filters);
use strict;
use warnings;
# VERSION
=head1 SYNOPSIS
Given an MSBuild project C<project.vcxproj> and its associated filters file
C<projects.vcxproj.filters>, the functions in this module can be used to update
the list of files in them to correspond to the given ones.
use Makefile::Update::MSBuild;
upmake_msbuild_project('project.vcxproj', \@sources, \@headers);
=head1 SEE ALSO
Makefile::Update, Makefile::Update::VCProj
=cut
=func update_msbuild_project
Update sources and headers in an MSBuild project and filter files.
Pass the path of the project to update or a hash with the same keys as used by
C<Makefile::Update::upmake> as the first parameter and the references to the
sources and headers arrays as the subsequent ones.
Returns 1 if any changes were made, either to the project itself or to its
associated C<.filters> file.
=cut
sub update_msbuild_project
{
my ($file_or_options, $sources, $headers) = @_;
use Makefile::Update;
if (!Makefile::Update::upmake($file_or_options,
\&update_msbuild, $sources, $headers
)) {
return 0;
}
my $args;
if (ref $file_or_options eq 'HASH') {
# Need to make a copy to avoid modifying the callers hash.
$args = { %$file_or_options };
$args->{file} .= ".filters"
} else {
$args = "$file_or_options.filters"
}
return Makefile::Update::upmake($args,
\&update_msbuild_filters, $sources, $headers
);
}
=func update_msbuild
Update sources and headers in an MSBuild project.
Parameters: input and output file handles and array references to the sources
and the headers to be used in this project.
Returns 1 if any changes were made.
=cut
sub update_msbuild
{
my ($in, $out, $sources, $headers) = @_;
# Hashes mapping the sources/headers names to 1 if they have been seen in
# the project or 0 otherwise.
my %sources = map { $_ => 0 } @$sources;
my %headers = map { $_ => 0 } @$headers;
# Reference to the hash corresponding to the files currently being
# processed.
my $files;
# Set to 1 when we are inside any <ItemGroup> tag.
my $in_group = 0;
# Set to 1 when we are inside an item group containing sources or headers
# respectively.
my ($in_sources, $in_headers) = 0;
# Set to 1 if we made any changes.
my $changed = 0;
while (my $line_with_eol = <$in>) {
(my $line = $line_with_eol) =~ s/\r?\n?$//;
if ($line =~ /^\s*<ItemGroup>$/) {
$in_group = 1;
} elsif ($line =~ m{^\s*</ItemGroup>$}) {
if (defined $files) {
my $kind = $in_sources ? 'Compile' : 'Include';
# Check if we have any new files.
#
# TODO Insert them in alphabetical order.
while (my ($file, $seen) = each(%$files)) {
if (!$seen) {
# Convert path separator to the one used by MSBuild.
$file =~ s@/@\\@g;
print $out qq{ <Cl$kind Include="$file" />\r\n};
$changed = 1;
}
}
$in_sources = $in_headers = 0;
$files = undef;
}
$in_group = 0;
} elsif ($in_group) {
if ($line =~ m{^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>$}) {
my $kind = $+{kind};
if ($kind eq 'Compile') {
warn "Mix of sources and headers at line $.\n" if $in_headers;
$in_sources = 1;
$files = \%sources;
} else {
warn "Mix of headers and sources at line $.\n" if $in_sources;
$in_headers = 1;
$files = \%headers;
}
my $closed_tag = defined $+{slash};
# Normalize the path separator, we always use Unix ones but the
# project files use Windows one.
my $file = $+{file};
$file =~ s@\\@/@g;
if (not exists $files->{$file}) {
# This file was removed.
$changed = 1;
if (!$closed_tag) {
# We have just the opening <ClCompile> or <ClInclude>
# tag, ignore everything until the matching closing one.
my $tag = "Cl$kind";
while (<$in>) {
last if m{^\s*</$tag>\r?\n$};
}
}
# In any case skip either this line containing the full
# <ClCompile/> tag or the line with the closing tag.
next;
} else {
if ($files->{$file}) {
warn qq{Duplicate file "$file" in the project at line $.\n};
} else {
$files->{$file} = 1;
}
}
}
}
print $out $line_with_eol;
}
$changed
}
=func update_msbuild_filters
Update sources and headers in an MSBuild filters file.
Parameters: input and output file handles, array references to the sources
and the headers to be used in this project and a callback used to determine
the filter for the new files.
Returns 1 if any changes were made.
=cut
sub update_msbuild_filters
{
my ($in, $out, $sources, $headers, $filter_cb) = @_;
# Use standard/default classifier for the files if none is explicitly
# specified.
if (!defined $filter_cb) {
$filter_cb = sub {
my ($file) = @_;
return 'Source Files' if $file =~ q{\.c(c|pp|xx|\+\+)?$};
return 'Header Files' if $file =~ q{\.h(h|pp|xx|\+\+)?$};
warn qq{No filter defined for the file "$file".\n};
undef
}
}
# Hashes mapping the sources/headers names to the text representing them in
# the input file if they have been seen in it or nothing otherwise.
my %sources = map { $_ => undef } @$sources;
my %headers = map { $_ => undef } @$headers;
# Reference to the hash corresponding to the files currently being
# processed.
my $files;
# Set to 1 when we are inside any <ItemGroup> tag.
my $in_group = 0;
# Set to 1 when we are inside an item group containing sources or headers
# respectively.
my ($in_sources, $in_headers) = 0;
# Set to 1 if we made any changes.
my $changed = 0;
while (my $line_with_eol = <$in>) {
(my $line = $line_with_eol) =~ s/\r?\n?$//;
if ($line =~ /^\s*<ItemGroup>?$/) {
$in_group = 1;
} elsif ($line =~ m{^\s*</ItemGroup>?$}) {
if (defined $files) {
# Output the group contents now, all at once, inserting any new
# files: we must do it like this to ensure that they are
# inserted in alphabetical order.
my $kind = $in_sources ? 'Compile' : 'Include';
foreach my $file (sort keys %$files) {
if (defined $files->{$file}) {
print $out $files->{$file};
} else {
my $filter = $filter_cb->($file);
# Convert path separator to the one used by MSBuild.
$file =~ s@/@\\@g;
my $indent = ' ' x 2;
print $out qq{$indent$indent<Cl$kind Include="$file"};
if (defined $filter) {
print $out ">\r\n$indent$indent$indent<Filter>$filter</Filter>\r\n$indent$indent</Cl$kind>\r\n";
} else {
print $out " />\r\n";
}
$changed = 1;
}
}
$in_sources = $in_headers = 0;
$files = undef;
}
$in_group = 0;
} elsif ($in_group &&
$line =~ m{^\s*<Cl(?<kind>Compile|Include) Include="(?<file>[^"]+)"\s*(?<slash>/)?>?$}) {
my $kind = $+{kind};
if ($kind eq 'Compile') {
warn "Mix of sources and headers at line $.\n" if $in_headers;
$in_sources = 1;
$files = \%sources;
} else {
warn "Mix of headers and sources at line $.\n" if $in_sources;
$in_headers = 1;
$files = \%headers;
}
my $closed_tag = defined $+{slash};
# Normalize the path separator, we always use Unix ones but the
# project files use Windows one.
my $file = $+{file};
$file =~ s@\\@/@g;
my $text = $line_with_eol;
if (!$closed_tag) {
# We have just the opening <ClCompile> tag, get everything
# until the next </ClCompile>.
while (<$in>) {
$text .= $_;
last if m{^\s*</Cl$kind>\r?\n?$};
}
}
if (not exists $files->{$file}) {
# This file was removed.
$changed = 1;
} else {
if ($files->{$file}) {
warn qq{Duplicate file "$file" in the project at line $.\n};
} else {
$files->{$file} = $text;
}
}
# Don't output this line yet, wait until the end of the group.
next
}
print $out $line_with_eol;
}
$changed
}
MAKEFILE_UPDATE_MSBUILD
$fatpacked{"Makefile/Update/Makefile.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'MAKEFILE_UPDATE_MAKEFILE';
package Makefile::Update::Makefile;
# ABSTRACT: Update lists of files in makefile variables.
use Exporter qw(import);
our @EXPORT = qw(update_makefile);
use strict;
use warnings;
# VERSION
=head1 SYNOPSIS
This can be used to update the contents of a variable containing a list of
files in a makefile.
use Makefile::Update::Makefile;
Makefile::Update::upmake('GNUmakefile', \&update_makefile, $vars);
=head1 SEE ALSO
Makefile::Update
=cut
=func update_makefile
Update variable definitions in a makefile format with the data from the hash
ref containing all the file lists.
Only most straightforward cases of variable or target definitions are
recognized here, i.e. just "var := value", "var = value" or "target: value".
In particular we don't support any GNU make extensions such as "export" or
"override" without speaking of anything more complex.
On top of it, currently the value should contain a single file per line with
none at all on the first line (but this restriction could be relaxed later if
needed), i.e. the only supported case is
var = \
foo \
bar \
baz
and it must be followed by an empty line, too.
Notice that if any of the "files" in the variable value looks like a makefile
variable, i.e. has "$(foo)" form, it is ignored by this function, i.e. not
removed even if it doesn't appear in the list of files (which will never be
the case normally).
Takes the (open) file handles of the files to read and to write and the file
lists hash ref as arguments.
Returns 1 if any changes were made.
=cut
sub update_makefile
{
my ($in, $out, $vars) = @_;
# Variable whose contents is being currently replaced and its original
# name in the makefile.
my ($var, $makevar);
# Hash with files defined for the specified variable as keys and 0 or 1
# depending on whether we have seen them in the input file as values.
my %files;
# Array of lines in the existing makefile.
my @values;
# True if the values are in alphabetical order: we use this to add new
# entries in alphabetical order too if the existing ones use it, otherwise
# we just append them at the end.
my $sorted = 1;
# Extensions of the files in the files list (they're keys of this hash,
# the values are not used), there can be more than one (e.g. ".c" and
# ".cpp").
my %src_exts;
# Extension of the files in the makefiles: here there can also be more
# than one, but in this case we just give up and don't perform any
# extensions translation because we don't have enough information to do it
# (e.g. which extension should be used for the new files in the makefile?).
# Such case is indicated by make_ext being empty (as opposed to its
# initial undefined value).
my $make_ext;
# Helper to get the extension. Note that the "extension" may be a make
# variable, e.g. the file could be something like "foo.$(obj)", so don't
# restrict it to just word characters.
sub _get_ext { $_[0] =~ /(\.\S+)$/ ? $1 : undef }
# Indent and the part after the value (typically some amount of spaces and
# a backslash) for normal lines and, separately, for the last one, as it
# may or not have backslash after it.
my ($indent, $tail, $last_tail);
# We can't use the usual check for EOF inside while itself because this
# wouldn't work for files with no new line after the last line, so check
# for the EOF manually.
my $eof = 0;
# Set to 1 if we made any changes.
my $changed = 0;
while (1) {
my $line = <$in>;
if (defined $line) {
chomp $line;
} else {
$line = '';
$eof = 1;
}
# If we're inside the variable definition, parse the current line as
# another file name,
if (defined $var) {
if ($line =~ /^(?<indent>\s*)(?<file>[^ ]+)(?<tail>\s*\\?)$/) {
if (defined $indent) {
warn qq{Inconsistent indent at line $. in the } .
qq{definition of the variable "$makevar".\n}
if $+{indent} ne $indent;
} else {
$indent = $+{indent};
}
$last_tail = $+{tail};
my $file_orig = $+{file};
$tail = $last_tail if !defined $tail;
# Check if we have something with the correct extension and
# preserve unchanged all the rest -- we don't want to remove
# expansions of other makefile variables from this one, for
# example, but such expansions would never be in the files
# list as they don't make sense for the other formats.
my $file = $file_orig;
if (defined (my $file_ext = _get_ext($file))) {
if (defined $make_ext) {
if ($file_ext ne $make_ext) {
# As explained in the comment before make_ext
# definition, just don't do anything in this case.
$make_ext = '';
}
} else {
$make_ext = $file_ext;
}
# We need to try this file with all of the source
# extensions we have as it can correspond to any of them.
for my $src_ext (keys %src_exts) {
if ($file_ext ne $src_ext) {
(my $file_try = $file) =~ s/\Q$file_ext\E$/$src_ext/;
if (exists $files{$file_try}) {
$file = $file_try;
last
}
}
}
if (!exists $files{$file}) {
# This file was removed.
$changed = 1;
# Don't store this line in @values below.
next;
}
}
if (exists $files{$file}) {
if ($files{$file}) {
warn qq{Duplicate file "$file" in the definition of the } .
qq{variable "$makevar" at line $.\n}
} else {
$files{$file} = 1;
}
}
# Are we still sorted?
if (@values && lc $line lt $values[-1]) {
$sorted = 0;
}
push @values, $line;
next;
}
# If the last line had a continuation character, the file list
# should only end if there is nothing else on the following line.
if ($last_tail =~ /\\$/ && $line =~ /\S/) {
warn qq{Expected blank line at line $..\n};
}
# End of variable definition, add new lines.
# We can only map the extensions if we have a single extension to
# map them to (i.e. make_ext is not empty) and we only need to do
# it if are using more than one extension in the source files list
# or the single extension that we use is different from make_ext.
if (defined $make_ext) {
if ($make_ext eq '' ||
(keys %src_exts == 1 && exists $src_exts{$make_ext})) {
undef $make_ext
}
}
my $new_files = 0;
while (my ($file, $seen) = each(%files)) {
next if $seen;
# This file was wasn't present in the input, add it.
# If this is the first file we add, ensure that the last line