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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
|
#! /usr/bin/perl
# This file is part of GNU Dico
# Copyright (C) 2014-2024 Sergey Poznyakoff
#
# GNU Dico is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Dico is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Dico. If not, see <http://www.gnu.org/licenses/>.
=head1 NAME
bootstrap - Bootstrap the Dico project
=head1 SYNOPSIS
B<bootstrap>
[B<-ahmnv>]
[B<--add>]
[B<--dry-run>]
[B<--force>]
[B<--help>]
[B<--new>]
[B<--modules>]
[B<--skip-reconfig>]
[B<--verbose>]
[B<--quiet>]
[B<--skip-po>]
[B<--update-po>]
[I<NAME>...]
=head1 DESCRIPTION
Bootstrap the Dico project. This is the first command that should be run
after cloning the project from the repository in order to be able to build
it.
When run without arguments performs the following actions:
=over 4
=item 1. Pulls in git submodules.
=item 2. Creates directories and placeholders for automatically generated files
=item 3. Creates autoconf/automake sources for building Dico modules.
Recreates F<configure.ac> and F<modules/Makefile.am> from stub files
F<configure.boot> and F<modules/stub.am>. The list of modules is obtained
from the contents of the F<modules> directory -- each subdirectory
is supposed to contain one module.
Additional F<configure.ac> code for each module is obtained from
file F<module.ac> in each module directory. See the description of
this file in section B<FILES>.
=item 4. Bootstraps the B<gnulib> submodule.
The F<gnulib-tool> is run with arguments deduced from files
F<configure.ac> and F<maint/gnulib.modules>.
=item 5. Creates the file F<po/Makevars>
This file is created from F<po/Makevsrs.template> using the information
obtained from F<configure.ac> and F<maint/printflike>.
=item 6. Downloads new translations (F<*.po> files) into the F<po> directory.
This step can be skipped using the B<--skip-po> option.
=item 7. Creates a pristine configuration header file
The pristine configuration header file is a version of F<config.h>
stripped off any defines added by B<gnulib>. It is used by library
source files to avoid introducing dependency on B<gnulib>.
The name of the pristine header file is taken from the second argument
of the B<AC_CONFIG_HEADERS> statement in the F<configure.boot> file.
=item 8. Reconfigures the project.
=back
The program must be run from the Dico source tree topmost directory.
When given the B<-m> (B<--modules>) option, only the second step is
performed.
When the B<-a> (B<--add>, B<--new>) option is given, the program creates
basic infrastructure for modules named in the command line. For each
I<NAME> from the command line it creates directory F<modules/I<NAME>>
and populates it with files from the directory F<modules/template>. To
this effect, it reads the list of file names from the section B<FILE LIST>
of the file F<modules/template/README>. This file is in Emacs outline format.
The B<FILE LIST> section lists file names relative to the F<modules/template>
directory, one file per line. File names must start at column 1. A
whitespace character (space or tab) starts a comment that spans to the
physical end of line. Empty lines are ignored.
Each file name from that list is copied to the newly created F<modules/I<NAME>>
directory, creating eventual intermediate directories on the fly. E.g. the
file F<modules/template/test/Makefile.am> is copied to
F<modules/I<NAME>/test/Makefile.am>. In the process some files undergo
I<macro expansion>, in which macro variables appearing in the source file
are replaced with their actual values in the destination file. This process
is discussed in detail in section B<FILES>, below.
Finally, the files F<configure.ac> and F<modules/Makefile.am> are recreated
to take into account the newly created modules.
By default, the program prints a succint report of what is being done.
This can be changed using the B<-q> (B<--quiet>) and B<-v> (or B<--verbose>)
options. The B<-q> (B<--quiet>) option instructs the program to suppress
any output excepting error messages. The B<-v> (or B<--verbose>) option,
on the contrary, instructs it to also print whatever output that was
generated when running auxiliary commands.
The B<-n> (B<--dry-run>) option instructs the tool to print what would have
been done without actually doing it.
=head1 OPTIONS
=over 4
=item B<-a>, B<--add>, B<--new>
Create basic infrastructure for modules named in the command line and
add them to F<configure.ac> and F<modules/Makefile.am>
=item B<-n>, B<--dry-run>
Don't do anything, just print what would have been done.
=item B<--force>
Force bootstrapping even in the absence of F<.git> directory. Use this
option B<dico> is used as a git submodule.
=item B<-m>, B<--modules>
Run only the second step: creation of autoconf/automake sources for
Dico modules.
=item B<--skip-po>
Don't synchronize localization files with the Translation Project.
=item B<-s>, B<--skip-reconfigure>
Don't run B<autoreconf>. Takes effect only with B<--add> (B<--new>) or
B<--modules>.
=item B<-v>, B<--verbose>
Increase output verbosity. Multiple options accumulate.
=back
The following options are passed to the B<gnulib> bootstrap script
verbatim:
=over 4
=item B<--gnulib-srcdir=>I<DIRNAME>
Specifies the local directory where B<gnulib> sources reside. Use this if
you already have gnulib sources on your machine, and do not want to waste
your bandwidth downloading them again.
=item B<--no-git>
Do not use git to update B<gnulib>. Requires that B<--gnulib-srcdir> point
to a correct gnulib snapshot.
=item B<--skip-po>
Do not download po files.
=item B<--update-po>
Update only the translation files in the F<po> directory.
=back
The following options cause the program to display informative text and
exit:
=over 4
=item B<-h>
Show a short usage summary.
=item B<--help>
Show man page.
=item B<--usage>
Show a concise command line syntax reminder.
=back
=head1 FILES
The following files are used as templates to create output files. When
creating output file, each line from the corresponding template is read,
I<macro expansion> is performed, and the resulting string is written to the
output file.
During macro expansion, each occurrence of B<< <I<NAME>> >> is replaced with
the contents of the macro variable I<NAME>, if it is defined. Expansion
of undefined variables leaves the text as is.
The construct B<< <I<NAME>#I<TEXT>> >> is expanded if it appears on a line
alone, possibly preceded by any string of characters. It works similarly
to B<< <I<NAME>> >>, except that if I<NAME> expands to multiple lines, the
second and subsequent lines of expansion are prefixed with I<TEXT> on output.
If I<TEXT> is empty, the content of the source line immediately preceding the
construct is used instead. This makes it possible to use expansions after a
comment character. E.g. if the variable B<HEADING> contains:
This file is generated automatically.
Please, do not edit.
See the docs for more info.
and the input file contains:
dnl <HEADING#>
Then, the resulting expansion will be:
dnl This file is generated automatically.
dnl Please, do not edit.
dnl See the docs for more info.
The macro variables are specific for each file, and are described below.
For each file, a special comment sequence is defined. Lines beginning
with that sequence are omitted from the output.
=over 4
=item F<configure.boot>
Produces the F<configure.ac> file. Comment marker is B<##>.
The following variables are defined:
=over 8
=item B<MODULES>
B<Autoconf> code for each module, obtained from the F<module.ac>
files.
=item B<HEADING>
Generates an initial header text, warning the reader that the file is
generated automatically and informing him about the ways to recreate
that file.
=item B<STATUS_OUTPUT>
A list of printable description for modules that can be disabled. Each
module is represented with a line
MODNAME ................ STATUS
where MODNAME is the module name and STATUS is the module status variable
(which produces B<yes> or B<no> at configure time, depending on whether
the module is enabled or not).
This is intended for use in B<AC_CONFIG_COMMANDS>.
=item B<STATUS_DEFN>
A list of assignments for module status variables, intended
for use in B<AC_CONFIG_COMMANDS>.
=back
The following code illustrates the use of the latter two variables:
AC_CONFIG_COMMANDS([status],[
echo<STATUS_OUTPUT>
],
[<STATUS_DEFN>])
=item F<modules/*/module.ac>
This file is optional. If present, it contains the B<autoconf> code for
that module, which is appended to the contents of the B<MODULES> variable
used for creating F<configure.ac>.
No macro substitutions are performed for that file.
Comment marker is B<##>.
The following comments starting with B<## module> are I<pragmatic> ones,
and are treated specially:
=over 8
=item B<## module name: I<NAME>>
Use I<NAME> as module name when creating variable names for that module. This
is useful when the module name contains symbols that cannot be used in variable
names (as in, e.g. B<dict.org>).
=item B<## module description: I<TEXT>>
Use I<TEXT> as module description in B<STATUS_OUTPUT> (see F<configure.boot>).
=item B<## module category:>
Reserved for future use.
=back
As well as other B<##> comments, these comments do not appear in the output.
=item F<modules/stub.am>
Produces the file F<modules/Makefile.am>. Comment marker is B<##>.
Macro variables:
=over 8
=item B<HEADING>
Same as in F<configure.boot>.
=item B<MODULES>
Produces B<Makefille.am> code for the B<SUBDIRS> variable.
=item B<TEMPLATE>
A whitespace-separated list of template files obtained from file
F<modules/template/README>.
=back
=item F<maint/gnulib.modules>
List of B<gnulib> modules to be imported to the package and additional
options to B<gnulib-tool>.
=item maint/printflike
List of I<printf-like> programs for generating the C<XGETTEXT_OPTIONS>
variable in F<po/Makevars>.
=back
The following template files are processed when creating new modules:
=over 4
=item F<modules/template/module.c>
produces the file F<modules/I<NAME>/I<NAME>.c>. Comment marker is B<//>.
Macro variables:
=over 8
=item B<MODNAME>
Expands to the name of the module.
=back
=item Any F<Makefile.am> files in the F<modules/template> directory
Comment marker is B<##>.
Macro variables:
=over 8
=item B<MODNAME>
Expands to the name of the module.
=back
=back
The files F<modules/template/tests/atlocal.in> and
F<modules/template/tests/testsuite.at> follow the same rules as
F<Makefile.am>.
=head1 EXIT CODES
=over 4
=item B<0>
Successful termination.
=item B<64>
Command line usage error.
=item B<66>
Can't open input file.
=item B<73>
Can't create output file.
=back
=head1 BUGS
Error handling can be made better.
=cut
use strict;
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use Pod::Usage;
use Pod::Man;
use File::Temp qw(tempfile);
use File::Basename;
use File::Path qw(make_path);
use Data::Dumper;
use IPC::Open3;
use Symbol 'gensym';
use constant EX_OK => 0;
use constant EX_USAGE => 64; # command line usage error
use constant EX_DATAERR => 65; # data format error
use constant EX_NOINPUT => 66; # cannot open input file
use constant EX_SOFTWARE => 70; # internal software error (not used yet)
use constant EX_CANTCREAT => 73; # can't create (user) output file
my $progname = basename($0);
my $progdescr = "Recreate Dico module autoconf structure";
my $addmod;
my $dry_run;
my $verbose;
my $quiet;
my $modules;
my $skip_reconf;
my $skip_po;
my $update_po;
my $force;
my $autoconf = "module.ac";
my $config_stub = "configure.boot";
my $makefile_stub = "modules/stub.am";
my $makefile = "modules/Makefile.am";
my %outfile;
my %pkg_info;
my %categories; # not used now
sub error {
my $msg = shift;
local %_ = @_;
my $fd = defined($_{fd}) ? $_{fd} : \*STDERR;
print $fd "$progname: " if defined($progname);
print $fd "$_{prefix}: " if defined($_{prefix});
print $fd "$msg\n"
}
sub abend {
my $code = shift;
print STDERR "$progname: " if defined($progname);
print STDERR "@_\n";
exit $code;
}
my $status = 0;
sub rewrite_configure_ac {
my $ref = shift;
my %vartab = (
'overwrite' => 1,
'tempfile' => 1,
'ignorecomm' => '##',
'HEADING' => "Do not edit. -*- mode: autoconf; buffer-read-only: t; -*- vi: set ro:\
This file is created automatically from $config_stub.\
To recreate, run $progname from the top of Dico source tree.
",
'MODULES' => sub {
my $s;
foreach my $name (sort keys %{$ref}) {
my $h = $ref->{$name};
$s .= "# Module $name\n";
if (defined($h->{autoconf})) {
open(my $afd, "<", $h->{autoconf})
or abend(EX_NOINPUT,
"can't open $h->{autoconf} for reading: $!");
while (<$afd>) {
next if /^##/;
$s .= $_;
}
close $afd;
}
$s .= "\n";
$s .= "DICO_TESTS($h->{dir})\n" if ($h->{tests});
$s .= "AC_CONFIG_FILES([$h->{dir}/Makefile])\n";
$s .= "\n# End of module $name\n\n";
}
return $s;
},
'STATUS_OUTPUT' => sub {
return join "\n",
map {
my $n;
if (defined($ref->{$_}{descr})) {
$n = $ref->{$_}{descr};
} else {
$n = $_;
$n =~ s/\b(\w)/\U$1/g;
}
my $l = 34 - length($n) - 2;
"$n " .
(($l > 0) ? '.' x $l : '') .
" \$status_$ref->{$_}{modname}";
}
grep { $ref->{$_}{status} }
sort keys %{$ref};
},
'STATUS_DEFN' => sub {
return join "\n",
map { "status_$ref->{$_}{modname}=\$status_$ref->{$_}{modname}" }
grep { defined($ref->{$_}{status}) }
sort keys %{$ref};
}
);
make_file($config_stub, "configure.ac", \%vartab);
}
sub rewrite_makefile {
my $ref = shift;
my %vartab = (
'overwrite' => 1,
'tempfile' => 1,
'ignorecomm' => '##',
'HEADING' => "Do not edit. -*- buffer-read-only: t; -*- vi: set ro:\
This file is created automatically from $makefile_stub.\
To recreate, run $progname from the top of Dico source tree.
",
'MODULES' => sub {
my $s;
foreach my $am (map { $ref->{$_}{am} }
grep { defined($ref->{$_}{am}) }
sort keys %{$ref}) {
$s .= "$am\n";
}
$s .= "SUBDIRS=";
foreach my $dir (map { $ref->{$_}{SUBDIRS} } sort keys %{$ref}) {
$s .= "\\\n $dir";
}
$s .= "\n";
return $s;
},
'TEMPLATE' => sub {
return join(' ', map { "template/$_" } read_file_list())
}
);
make_file($makefile_stub, $makefile, \%vartab);
}
sub add_module {
my ($name,$ref) = @_;
my $dir = "modules/$name";
error("adding module $name", prefix=>'DEBUG') if ($verbose>1);
my $acname = uc($name);
$ref->{$name}{dir} = $dir;
$ref->{$name}{SUBDIRS} = $name;
$ref->{$name}{modname} = $name;
if (-r "$dir/$autoconf") {
$ref->{$name}{autoconf} = "$dir/$autoconf";
open(my $fd, "<", "$dir/$autoconf")
or abend(EX_NOINPUT,
"can't open $dir/$autoconf for reading: $!");
while (<$fd>) {
chomp;
s/\s+$//;
if (/^##\s*module\s+name\s*:\s*([^\s]+).*$/) {
$ref->{$name}{modname} = $1;
} elsif (/^##\s*module\s+description\s*:\s*(.+).*$/) {
$ref->{$name}{descr} = $1;
} elsif (/^##\s*module\s+category\s*:\s*(.+).*$/) {
$ref->{$name}{category} = $1;
$categories{$1}++;
}
s/#.*//;
next if ($_ eq "");
if (/AM_CONDITIONAL\s*\(\s*\[${acname}_COND\]/) {
$ref->{$name}{am} = <<EOT;
if ${acname}_COND
${acname}_DIR=$name
endif
EOT
;
$ref->{$name}{SUBDIRS} = "\$(${acname}_DIR)";
}
$ref->{$name}{status} = 1
if (!$ref->{$name}{status} and /status_$ref->{$name}{modname}=/);
}
close $fd;
}
$ref->{$name}{tests} = (-d "$dir/tests"
and -f "$dir/tests/Makefile.am"
and -f "$dir/tests/testsuite.at");
if (-f "$dir/Makefile.am") {
open(my $fd, "<", "$dir/Makefile.am")
or abend(EX_NOINPUT,
"can't open $dir/$autoconf for reading: $!");
my %checks = ('mod' => "mod_LTLIBRARIES does not contain $ref->{$name}{modname}.la",
'SOURCES' => "$ref->{$name}{modname}_la_SOURCES not defined");
while (<$fd>) {
chomp;
s/\s+$//;
s/#.*//;
next if ($_ eq "");
if (/mod_LTLIBRARIES\s*=\s*$ref->{$name}{modname}.la/) {
delete $checks{mod};
} elsif (/$ref->{$name}{modname}_la_SOURCES\s*=/) {
delete $checks{SOURCES};
}
}
close $fd;
if (keys(%checks)) {
while (my ($k,$v) = each %checks) {
error("$dir/Makefile.am: $v");
}
$status = EX_DATAERR;
return;
}
}
if ($verbose>2) {
print STDERR "module $name:\n";
print STDERR Dumper($ref->{$name});
}
}
sub replace_macro {
my $name = shift;
my $ref = shift;
my $s;
if (defined($ref->{$name})) {
if (ref($ref->{$name}) eq 'CODE') {
$s = &{$ref->{$name}};
} else {
$s = $ref->{$name};
}
} else {
$s = "<$name>";
}
return $s;
}
sub make_file {
my $infile = shift;
my $ofile = shift;
my $ref = shift;
my $dir = dirname($ofile);
unless (-d $dir) {
error("creating directory $dir", prefix => 'DEBUG') if ($verbose>1);
unless ($dry_run) {
make_path($dir, {error=>\my $err});
if (@$err) {
for my $diag (@$err) {
my ($file, $message) = %$diag;
$file = $dir if ($file eq '');
error("error creating $file: $message");
}
exit(EX_CANTCREAT);
}
}
}
error("creating $ofile from $infile", prefix => 'DEBUG') if ($verbose>1);
return if $dry_run;
if (!$ref->{overwrite} and -e $ofile) {
error("$ofile already exists", prefix => 'warning');
} else {
open(my $ifd, "<", $infile)
or abend(EX_DATAERR, "can't open $infile for reading: $!");
my $ofd;
if (defined($ref->{tempfile})) {
my $tempname;
($ofd, $tempname) = tempfile(DIR => dirname($infile))
or abend(EX_CANTCREAT,
"can't open temporary file for creating $ofile: $!");
$outfile{$tempname} = $ofile;
} else {
open($ofd, ">", $ofile)
or abend(EX_CANTCREAT, "can't open $ofile for writing: $!");
}
while (<$ifd>) {
next if (defined($ref->{ignorecomm}) and /^$ref->{ignorecomm}/);
if (/^(.*)<([A-Z][A-Z0-9_]+)#([^>]*)>\s*$/) {
my $pfx = $3 || $1;
$_ = $1 . replace_macro($2, $ref);
chomp;
s/\n/\n$pfx/g;
$_ .= "\n";
} else {
s/<([A-Z][A-Z0-9_]+)>/replace_macro($1, $ref)/gex;
}
print $ofd $_;
}
close($ifd);
close($ofd);
}
}
sub read_file_list {
open(my $fd, '<', 'modules/template/README') or die;
my $found;
while (<$fd>) {
chomp;
if (/^\*\s+FILE\s+LIST/) {
$found = 1;
last;
}
}
my @ret;
if ($found) {
while (<$fd>) {
chomp;
last if /^\*/ or /Local Variables:/;
next if /^\s/;
s/[[:space:]].*$//;
next if /^$/;
push @ret, $_;
}
}
close($fd);
return @ret;
}
sub create_module {
my $modname = shift;
error("Creating framework for module $modname", fd => \*STDOUT)
unless $quiet;
my %variables = (
'./module.c' => { rename => sub { s/module/${modname}/; },
ignorecomm => '//',
MODNAME => $modname },
'Makefile.am' => { ignorecomm => '##',
MODNAME => $modname },
'./tests/atlocal.in' => { ref => 'Makefile.am' },
'./tests/testsuite.at' => { ref => 'Makefile.am' },
);
my $dir = "modules/$modname";
if (-d $dir) {
error("$dir already exists", prefix => 'warning');
} else {
error("creating $dir", prefix => 'DEBUG') if ($verbose>1);
mkdir($dir) unless $dry_run;
}
my $template_dir = 'modules/template';
foreach my $file (read_file_list) {
# my $subdir = $dir . '/' . dirname($file);
# unless (-d $subdir) {
# error("creating $subdir", prefix => 'DEBUG')
# if ($verbose>1);
# mkdir($subdir) unless $dry_run;
# }
my $vartab;
my $key = './' . $file;
VARTAB:
do {
if (exists($variables{$key})
|| exists($variables{$key = basename($file)})) {
$vartab = $variables{$key};
}
if ($vartab && exists($vartab->{ref})) {
$key = $vartab->{ref};
goto VARTAB;
}
};
my $outfile = $file;
if ($vartab && exists($vartab->{rename})) {
local $_ = $outfile;
&{$vartab->{rename}};
$outfile = $_;
}
make_file("$template_dir/$file", "$dir/$outfile", $vartab);
}
}
sub cleanup {
foreach my $f (keys %outfile) {
unlink $f if (-e $f);
}
}
sub checksrc {
return 0 unless -e $config_stub;
open(my $fd, '<', $config_stub)
or abend(EX_NOINPUT, "can't open $config_stub: $!");
my $anchor;
while (<$fd>) {
chomp;
s/^\s+//;
if (/^AC_CONFIG_SRCDIR\(\[(.*)\]\)/) {
$anchor = $1;
last;
}
}
close $fd;
abend(EX_DATAERR, "AC_CONFIG_SRCDIR not found in $config_stub")
unless defined $anchor;
return -e $anchor;
}
sub modproc {
my %modtab;
my @modnames = map { s#modules/##; $_; } grep { $_ ne 'modules/template' && -d $_; } glob "modules/*";
error("Bootstrapping dico modules", fd => \*STDOUT)
unless $quiet;
foreach my $m (@modnames) {
add_module($m, \%modtab);
}
rewrite_configure_ac(\%modtab);
rewrite_makefile(\%modtab);
if ($status == 0 and !$dry_run) {
while (my ($from, $to) = each %outfile) {
rename $from, $to
or abend(EX_CANTCREAT, "can't rename $from to $to: $!");
}
}
}
sub get_command_name {
my $cmd = shift;
if ($cmd =~ /\s/) {
unshift @_, split /\s+/, $cmd;
$cmd = shift;
}
if ($cmd =~ /^(sh|perl)$/) {
$cmd = shift;
}
return $cmd;
}
sub run {
my $id = get_command_name(@_);
error("running @_", prefix=>'DEBUG') if $verbose>2;
return if $dry_run;
my $out = gensym;
my $err = gensym;
open(my $err, ">&", \*STDERR) or die "can't dup STDERR: $!";
open(my $in, "<&", \*STDIN) or die "can't dup STDIN: $!";
my $pid = open3($in, $out, $err, @_);
my $logpid;
if ($verbose) {
$logpid = fork();
if ($logpid == 0) {
while (<$out>) {
chomp;
error($_, fd => \*STDOUT, prefix => "[$id]");
}
exit;
}
}
while (<$err>) {
chomp;
next if /^(libtoolize|autoreconf):/ && !$verbose;
error($_, prefix => "[$id]");
}
waitpid($pid, 0);
my $ret = $?;
if ($? == -1) {
abend(EX_CANTCREAT, "failed to run @_");
} elsif ($? & 127) {
abend(EX_CANTCREAT, "@_ exited on signal " . ($? & 127));
} elsif (my $e = ($? >> 8)) {
abend(EX_CANTCREAT, "@_ exited with status $e");
}
waitpid($logpid, 0) if defined $logpid;
}
sub gnulib_modlist {
my @options;
my @include;
my @exclude;
open(my $fd, '<', 'maint/gnulib.modules')
or abend(EX_NOINPUT, "can't open maint/gnulib.modules: $!");
while (<$fd>) {
chomp;
s/\s+$//;
s/^\s+//;
next if /^#/ or /^$/;
if (/^(--.+?)\s*(?:=\s*(.+))/) {
push @options, "$1".($2 ? "=$2" : '');
} elsif (/^[!](.+)/) {
push @exclude, $1;
} else {
push @include, $_;
}
}
close $fd;
return (
@options,
(map { "--avoid=$_" } @exclude),
@include
);
}
sub gnulib_init {
error("Bootstrapping gnulib", fd => \*STDOUT) unless $quiet;
my @import_options = qw(
--import
--local-dir=gl
--m4-base=m4
--doc-base=doc
--tests-base=tests
--aux-dir=build-aux
--makefile-name=gnulib.am
--no-conditional-dependencies
--macro-prefix=gl);
push @import_options, '--verbose' if $verbose;
run("sh", "gnulib/gnulib-tool",
@import_options,
gnulib_modlist);
}
sub po_makevars_template {
my $file = shift;
return if -f $file;
open(my $fd, '>', $file)
or abend(EX_CANTCREAT, "cannot create $file: $!");
print $fd <<'EOT';
# Makefile variables for PO directory in any package using GNU gettext.
# Usually the message domain is the same as the package name.
DOMAIN = $(PACKAGE)
# These two variables depend on the location of this directory.
subdir = po
top_builddir = ..
# These options get passed to xgettext.
XGETTEXT_OPTIONS = --keyword=_ --keyword=N_
# This is the copyright holder that gets inserted into the header of the
# $(DOMAIN).pot file. Set this to the copyright holder of the surrounding
# package. (Note that the msgstr strings, extracted from the package's
# sources, belong to the copyright holder of the package.) Translators are
# expected to transfer the copyright for their translations to this person
# or entity, or to disclaim their copyright. The empty string stands for
# the public domain; in this case the translators are expected to disclaim
# their copyright.
COPYRIGHT_HOLDER = Free Software Foundation, Inc.
# This is the email address or URL to which the translators shall report
# bugs in the untranslated strings:
# - Strings which are not entire sentences, see the maintainer guidelines
# in the GNU gettext documentation, section 'Preparing Strings'.
# - Strings which use unclear terms or require additional context to be
# understood.
# - Strings which make invalid assumptions about notation of date, time or
# money.
# - Pluralisation problems.
# - Incorrect English spelling.
# - Incorrect formatting.
# It can be your email address, or a mailing list address where translators
# can write to without being subscribed, or the URL of a web page through
# which the translators can contact you.
MSGID_BUGS_ADDRESS =
# This is the list of locale categories, beyond LC_MESSAGES, for which the
# message catalogs shall be used. It is usually empty.
EXTRA_LOCALE_CATEGORIES =
EOT
}
sub make_tree {
error("Creating placeholders", fd => \*STDOUT) unless $quiet;
return if $dry_run;
my %dirs;
$dirs{$pkg_info{AC_CONFIG_MACRO_DIR}} = 1
if exists $pkg_info{AC_CONFIG_MACRO_DIR};
if (exists($pkg_info{AC_CONFIG_HEADERS})) {
my @d = map { dirname($_) } @{$pkg_info{AC_CONFIG_HEADERS}};
@dirs{@d} = (1) x @d;
}
foreach my $dir (keys %dirs) {
unless (-d $dir) {
mkdir $dir
or abend(EX_CANTCREAT, "cannot create directory $dir: $!");
}
}
foreach my $cf (map { "$_.in" } @{$pkg_info{AC_CONFIG_HEADERS}}) {
unless (-f $cf) {
open(my $fd, '>', $cf)
or abend(EX_CANTCREAT, "can't create placeholder file $cf: $!");
close($fd);
}
}
# FIXME include/lib/config.h
po_makevars_template('po/Makevars.template');
unless (-f 'ChangeLog') {
open(my $fd, '>', 'ChangeLog')
or abend(EX_CANTCREAT, "cannot create ChangeLog: $!");
print $fd <<EOT;
This file is a placeholder. It will be replaced with the actual ChangeLog
by make dist. Run make ChangeLog if you wish to create it earlier.
EOT
}
}
sub make_pristine_conf {
my $cfname;
my ($ofd, $tempname) = tempfile(DIR => '.',
TEMPLATE => 'configure.XXXXXX');
open(my $ifd, '<', 'configure.ac')
or abend(EX_DATAERR, "can't open configure.ac for reading: $!");
while (<$ifd>) {
next if /^gl_/;
if (s/^AC_CONFIG_HEADERS\(\[?(.+?)\]?\).*/$1/) {
my @cf = split;
if (@cf > 1) {
$cfname = $cf[1];
print $ofd "AC_CONFIG_HEADERS($cfname)\n";
next;
}
}
print $ofd $_;
}
close $ofd;
close $ifd;
if ($cfname) {
error("Generating pristine config header in ${cfname}.in",
fd => \*STDOUT) unless $quiet;
run("autoheader --force $tempname");
}
unlink $tempname;
}
sub reconf {
return if $status || $skip_reconf;
error("Reconfiguring", fd => \*STDOUT) unless $quiet;
run("autoreconf -i -s -f");
make_pristine_conf();
}
sub get_pkg_info {
my $m4_prog = q{divert(-1)
changequote([,])
define([X_PRINT],[divert(0)$1divert(-1)])
define([AC_INIT],[X_PRINT([PACKAGE_NAME:$1
PACKAGE_BUGREPORT:$3
])])
define([AC_CONFIG_HEADERS],
[X_PRINT([[$0]:$1
])])
define([AC_CONFIG_MACRO_DIR],[X_PRINT([[$0]:$1
])])
};
my $tmp = File::Temp->new();
print $tmp $m4_prog;
close $tmp;
open(my $fh, '-|', 'm4', $tmp->filename, 'configure.boot')
or abend(EX_NOINPUT, "Can't run m4: $!");
while (<$fh>) {
chomp;
my ($k,$v) = split /:/, $_, 2;
$pkg_info{$k} = $v;
}
close $fh;
($pkg_info{PACKAGE_TARNAME} = $pkg_info{PACKAGE_NAME}) =~ s/^GNU\s+//;
$pkg_info{PACKAGE_BUGREPORT} =
"bug-$pkg_info{PACKAGE_TARNAME}\@gnu.org.ua"
unless $pkg_info{PACKAGE_BUGREPORT};
if (exists($pkg_info{AC_CONFIG_HEADERS})) {
$pkg_info{AC_CONFIG_HEADERS} = [split /\s+/, $pkg_info{AC_CONFIG_HEADERS}]
}
}
my %po_var = (
COPYRIGHT_HOLDER => "Sergey Poznyakoff", # FIXME
);
sub fill_xgettext_options {
my $file = shift;
open(my $ifd, '<', $file)
or abend(EX_NOINPUT, "Can't open $file: $!");
$po_var{XGETTEXT_OPTIONS} = [map {
chomp;
s/\s+$//;
s/^\s+//;
if (/^(.+?):(\d)$/) {
"--flag=$1:$2:pass-c-format"
} else {
()
}
} <$ifd>];
close $ifd;
}
sub po_init {
my $template = "po/Makevars.template";
error("Creating po/Makevars", fd => \*STDOUT) unless $quiet;
return if $dry_run;
$po_var{MSGID_BUGS_ADDRESS} = $pkg_info{PACKAGE_BUGREPORT};
fill_xgettext_options('maint/printflike') if -f 'maint/printflike';
open(my $ifd, '<', $template)
or abend(EX_NOINPUT, "Can't open $template: $!");
open(my $ofd, '>', 'po/Makevars')
or abend(EX_CANTCREAT, "Can't create po/Makevars: $!");
while (<$ifd>) {
chomp;
if (/^\s*([A-Za-z_][A-Za-z_0-9]+)\s*=/) {
if (exists($po_var{$1})) {
if (ref($po_var{$1}) eq 'ARRAY') {
print $ofd join("\\\n ", $_, @{$po_var{$1}})
. "\n";
} else {
print $ofd "$1 = $po_var{$1}\n";
}
next;
}
}
print $ofd "$_\n";
}
close $ifd;
close $ofd;
po_sync($pkg_info{PACKAGE_TARNAME}) unless $skip_po
}
sub po_sync {
my $domain = shift;
error("Updating po files", fd => \*STDOUT) unless $quiet;
my @rsync_options = qw(
--copy-links
--recursive
--times
--compress);
push @rsync_options, '--verbose' if $verbose;
run('rsync',
@rsync_options,
"rsync://translationproject.org/tp/latest/$domain/", 'po');
my @linguas = sort map { my ($name) = fileparse($_, ".po"); $name }
glob "po/*.po";
open(my $fh, '>', 'po/LINGUAS')
or abend(EX_CANTCREAT, "can't open po/LINGUAS for writing: $!");
print $fh join("\n", @linguas), "\n";
close $fh;
}
END {
cleanup;
}
$SIG{HUP} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = \&cleanup;
#
GetOptions("h" => sub {
pod2usage(-message => "$progname: $progdescr",
-exitstatus => 0);
},
"help" => sub {
pod2usage(-exitstatus => EX_OK, -verbose => 2);
},
"usage" => sub {
pod2usage(-exitstatus => EX_OK, -verbose => 0);
},
"add|new|a" => \$addmod,
"dry-run|n" => \$dry_run,
"verbose|v+" => \$verbose,
"quiet|q" => \$quiet,
"modules|m" => \$modules,
"skip-reconfigure|s" => \$skip_reconf,
"skip-po" => \$skip_po,
"update-po" => \$update_po,
"force" => \$force
) or exit(EX_USAGE);
abend(EX_USAGE, "must run from the Dico source tree topmost directory")
unless checksrc();
++$verbose if $dry_run;
$verbose = 0 if $quiet;
get_pkg_info();
if ($addmod) {
abend(EX_USAGE, "not enough arguments") unless @ARGV;
abend(EX_USAGE, "--update-po conflicts with --add or --new") if $update_po;
foreach my $mod (@ARGV) {
create_module($mod);
}
modproc();
reconf();
} elsif ($modules) {
abend(EX_USAGE, "too many arguments") if @ARGV;
abend(EX_USAGE, "--update-po conflicts with --modules") if $update_po;
modproc();
reconf();
} elsif ($#ARGV >= 0) {
abend(EX_USAGE, "too many arguments; did you mean --new?");
} elsif ($update_po) {
po_sync($pkg_info{PACKAGE_TARNAME});
} elsif (-d '.git' || $force) {
error("Initializing submodules", fd => \*STDOUT) unless $quiet;
run('git submodule update --init --recursive') if -d '.git';
make_tree();
modproc();
gnulib_init();
po_init();
reconf();
} else {
error("this does not look like a clone of the Dico repository");
error("did you mean --modules?");
abend(EX_USAGE, "If not, try $progname --help for detailed info");
}
exit($status);
|