diff --git a/Build.pm b/Build.pm index 9ab1b05d6..321db5878 100644 --- a/Build.pm +++ b/Build.pm @@ -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 { @@ -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; diff --git a/Build/Arch.pm b/Build/Arch.pm index 3d25f46eb..074aaab7a 100644 --- a/Build/Arch.pm +++ b/Build/Arch.pm @@ -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}; diff --git a/Build/Deb.pm b/Build/Deb.pm index 16b81e58a..cf1abf2d5 100644 --- a/Build/Deb.pm +++ b/Build/Deb.pm @@ -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') { diff --git a/Build/Rpm.pm b/Build/Rpm.pm index a6ebada7f..89bab63f6 100644 --- a/Build/Rpm.pm +++ b/Build/Rpm.pm @@ -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; @@ -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;