Skip to content

Commit

Permalink
Refactor macro initialization
Browse files Browse the repository at this point in the history
We now have a dedicated function to set the macro hashes.

The commit also adds a gettargetarchos method so that we limit
the code that accesses the macro arrays. The idea is that we can
switch from '%define foo bar' syntax to '%foo bar' syntax in
the future.
  • Loading branch information
mlschroe committed Jan 29, 2020
1 parent 7e5c477 commit 8a09643
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
17 changes: 13 additions & 4 deletions Build.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ sub unify {
return grep(delete($h{$_}), @_);
}

sub define($)
{
my $def = shift;
$extra_macros .= '%define '.$def."\n";
sub define {
my ($def) = @_;
$extra_macros .= "%define $def\n";
}

sub init_helper_hashes {
Expand Down Expand Up @@ -451,6 +450,16 @@ sub read_config {
return $config;
}

sub gettargetarchos {
my ($config) = @_;
my ($arch, $os);
for (@{$config->{'macros'} || []}) {
$arch = $1 if /^%define _target_cpu (\S+)/;
$os = $1 if /^%define _target_os (\S+)/;
}
return ($arch, $os);
}

sub do_subst {
my ($config, @deps) = @_;
my @res;
Expand Down
5 changes: 1 addition & 4 deletions Build/Arch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ sub parse {
$ret->{'deps'} = [];
push @{$ret->{'deps'}}, @{$vars{$_} || []} for qw{makedepends checkdepends depends};
# get arch from macros
my $arch;
for (@{$config->{'macros'} || []}) {
$arch = $1 if /^%define _target_cpu (\S+)/;
}
my ($arch) = Build::gettargetarchos($config);
# map to arch linux name and add arch dependent
$arch = 'i686' if $arch =~ /^i[345]86$/;
push @{$ret->{'deps'}}, @{$vars{"${_}_$arch"} || []} for qw{makedepends checkdepends depends};
Expand Down
8 changes: 2 additions & 6 deletions Build/Deb.pm
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ sub parse {
my @control;

# get arch and os from macros
my ($arch, $os);
for (@{$bconf->{'macros'} || []}) {
$arch = $1 if /^%define _target_cpu (\S+)/;
$os = $1 if /^%define _target_os (\S+)/;
}
my ($arch, $os) = Build::gettargetarchos($bconf);
# map to debian names
$os = 'linux' if !defined($os);
$os = 'linux' unless defined $os;
$arch = basearch($arch);

if (ref($fn) eq 'ARRAY') {
Expand Down
24 changes: 18 additions & 6 deletions Build/Rpm.pm
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,29 @@ sub grabargs {
return \%m;
}

sub initmacros {
my ($config, $macros, $macros_args) = @_;
for my $line (@{$config->{'macros'} || []}) {
next unless $line =~ /^%define\s*([0-9a-zA-Z_]+)(?:\(([^\)]*)\))?\s*(.*?)$/;
my $macname = $1;
my $macargs = $2;
my $macbody = $3;
if (defined $macargs) {
$macros_args->{$macname} = $macargs;
} else {
delete $macros_args->{$macname};
}
$macros->{$macname} = $macbody;
}
}

sub expandmacros {
my ($config, $line, $lineno, $macros, $macros_args) = @_;

if (!$macros) {
$macros = {};
$macros_args = {};
for my $macline (@{$config->{'macros'} || []}) {
expandmacros($config, $macline, undef, $macros, $macros_args);
}
initmacros($config, $macros, $macros_args);
}
my $expandedline = '';
my $tries = 0;
Expand Down Expand Up @@ -403,9 +417,7 @@ sub parse {
return $ret;
}

for my $line (@{$config->{'macros'} || []}) {
expandmacros($config, $line, undef, \%macros, \%macros_args);
}
initmacros($config, \%macros, \%macros_args);
my $skip = 0;
my $main_preamble = 1;
my $preamble = 1;
Expand Down

0 comments on commit 8a09643

Please sign in to comment.