#! /usr/bin/perl
# This file is part of GNU Dico
# Copyright (C) 2014-2019 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 .
=head1 NAME
bootstrap - Bootstrap the Dico project
=head1 SYNOPSIS
B
[B<-ahmnv>]
[B<--add>]
[B<--dry-run>]
[B<--help>]
[B<--new>]
[B<--modules>]
[B<--skip-reconfig>]
[B<--verbose>]
[B<--quiet>]
[B<--skip-po>]
[I...]
=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 built
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 and F from stub files
F and F. The list of modules is obtained
from the contents of the F directory -- each subdirectory
is supposed to contain one module.
Additional F code for each module is obtained from
file F in each module directory. See the description of
this file in section B.
=item 4. Bootstraps the B submodule.
The F is run with arguments deduced from files
F and F.
=item 5. Creates the file F
This file is created from F using the information
obtained from F and F.
=item 6. Downloads new translations (F<*.po> files) into the F 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
stripped off any defines added by B. It is used by library
source files to avoid introducing dependency on B.
The name of the pristine header file is taken from the second argument
of the B statement in the F 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 from the command line it creates directory F>
and populates it with files from the directory F. To
this effect, it reads the list of file names from the section B
of the file F. This file is in Emacs outline format.
The B section lists file names relative to the F
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>
directory, creating eventual intermediate directories on the fly. E.g. the
file F is copied to
F/test/Makefile.am>. In the process some files undergo
I, 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, below.
Finally, the files F and F 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 and F
=item B<-n>, B<--dry-run>
Don't do anything, just print what would have been done.
=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. 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 bootstrap script
verbatim:
=over 4
=item B<--gnulib-srcdir=>I
Specifies the local directory where B 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. Requires that B<--gnulib-srcdir> point
to a correct gnulib snapshot.
=item B<--skip-po>
Do not download po files.
=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 is performed, and the resulting string is written to the
output file.
During macro expansion, each occurrence of B<< > >> is replaced with
the contents of the macro variable I, if it is defined. Expansion
of undefined variables leaves the text as is.
The construct B<< #I> >> is expanded if it appears on a line
alone, possibly preceded by any string of characters. It works similarly
to B<< > >>, except that if I expands to multiple lines, the
second and subsequent lines of expansion are prefixed with I on output.
If I 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 contains:
This file is generated automatically.
Please, do not edit.
See the docs for more info.
and the input file contains:
dnl
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
Produces the F file. Comment marker is B<##>.
The following variables are defined:
=over 8
=item B
B code for each module, obtained from the F
files.
=item B
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
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 or B at configure time, depending on whether
the module is enabled or not).
This is intended for use in B.
=item B
A list of assignments for module status variables, intended
for use in B.
=back
The following code illustrates the use of the latter two variables:
AC_CONFIG_COMMANDS([status],[
echo
],
[])
=item F
This file is optional. If present, it contains the B code for
that module, which is appended to the contents of the B variable
used for creating F.
No macro substitutions are performed for that file.
Comment marker is B<##>.
The following comments starting with B<## module> are I ones,
and are treated specially:
=over 8
=item B<## module name: I>
Use I 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).
=item B<## module description: I>
Use I as module description in B (see F).
=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
Produces the file F. Comment marker is B<##>.
Macro variables:
=over 8
=item B
Same as in F.
=item B
Produces B code for the B variable.
=item B
A whitespace-separated list of template files obtained from file
F.
=back
=item F
List of B modules to be imported to the package and additional
options to B.
=item maint/printflike
List of I programs for generating the C
variable in F.
=back
The following template files are processed when creating new modules:
=over 4
=item F
produces the file F/I.c>. Comment marker is B/>.
Macro variables:
=over 8
=item B
Expands to the name of the module.
=back
=item Any F files in the F directory
Comment marker is B<##>.
Macro variables:
=over 8
=item B
Expands to the name of the module.
=back
=back
The files F and
F follow the same rules as
F.
=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 $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} = <{$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 < '.',
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');
}
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
) 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;
foreach my $mod (@ARGV) {
create_module($mod);
}
modproc();
reconf();
} elsif ($modules) {
abend(EX_USAGE, "too many arguments") if @ARGV;
modproc();
reconf();
} elsif ($#ARGV >= 0) {
abend(EX_USAGE, "too many arguments; did you mean --new?");
} elsif (-d '.git') {
error("Initializing submodules", fd => \*STDOUT) unless $quiet;
run('git submodule init');
run('git submodule update');
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);