Skip to content

Commit e8dafd0

Browse files
committed
Support the %load macro
We use the includecallback to get the macro file content.
1 parent 4c47a22 commit e8dafd0

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

Build.pm

+2-11
Original file line numberDiff line numberDiff line change
@@ -599,17 +599,8 @@ sub read_config {
599599
s/=$// for @{$config->{'substitute'}->{$l}};
600600
}
601601
# add rawmacros to our macro list
602-
if ($config->{'rawmacros'} ne '') {
603-
for my $rm (split("\n", $config->{'rawmacros'})) {
604-
if (@macros && $macros[-1] =~ /\\\z/s) {
605-
$macros[-1] = substr($macros[-1], 0, -1)."\n$rm";
606-
} elsif ($rm !~ /^%/) {
607-
push @macros, $rm;
608-
} else {
609-
push @macros, "%define ".substr($rm, 1);
610-
}
611-
}
612-
}
602+
Build::Rpm::add_macros($config, $config->{'rawmacros'}) if $config->{'rawmacros'} ne '';
603+
613604
# extract some helper hashes for the flags
614605
my %modules;
615606
for (@{$config->{'expandflags'} || []}) {

Build/Rpm.pm

+30
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,33 @@ sub builtinmacro {
340340
}
341341
return (exists($macros->{"with_$args[0]"}) ? 1 : 0) ^ ($macname eq 'without' ? 1 : 0) if $macname eq 'with' || $macname eq 'without';
342342
return (exists($macros->{$args[0]}) ? 1 : 0) ^ ($macname eq 'undefined' ? 1 : 0) if $macname eq 'defined' || $macname eq 'undefined';
343+
if ($macname eq 'load' && ref($macros->{'load'}) eq 'ARRAY') {
344+
my $l = $macros->{'load'};
345+
my $c = {};
346+
add_macros($c, $l->[0]->($args[0]));
347+
initmacros($c, $l->[1], $l->[2]);
348+
return '';
349+
}
343350
do_warn($config, "unsupported builtin macro $macname");
344351
return '';
345352
}
346353

354+
sub add_macros {
355+
my ($config, $rawmacros) = @_;
356+
$config->{'macros'} ||= [];
357+
return unless defined $rawmacros;
358+
my $macros = $config->{'macros'};
359+
for my $rm (split("\n", $rawmacros)) {
360+
if (@$macros && $macros->[-1] =~ /\\\z/s) {
361+
$macros->[-1] = substr($macros->[-1], 0, -1)."\n$rm";
362+
} elsif ($rm !~ /^%/) {
363+
push @$macros, $rm;
364+
} else {
365+
push @$macros, "%define ".substr($rm, 1);
366+
}
367+
}
368+
}
369+
347370
sub initmacros {
348371
my ($config, $macros, $macros_args) = @_;
349372
for my $line (@{$config->{'macros'} || []}) {
@@ -412,6 +435,7 @@ my %builtin_macros = (
412435
'dirname' => \&builtinmacro,
413436
'shrink' => \&builtinmacro,
414437
'suffix' => \&builtinmacro,
438+
'load' => \&builtinmacro,
415439

416440
'gsub' => \&luamacro,
417441
'len' => \&luamacro,
@@ -714,6 +738,12 @@ sub parse {
714738
my $multilinedefine;
715739
my $multilinecondition;
716740
my $substitute = $config->{'substitute'};
741+
742+
# setup load macro if we have an include callback
743+
if ($includecallback && !$config->{'parsing_config'}) {
744+
$macros{'load'} = [ $includecallback, \%macros, \%macros_args ];
745+
}
746+
717747
while (1) {
718748
my $line;
719749
my $doxspec = $xspec ? 1 : 0;

t/parse_spec.t

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/perl -w
22

33
use strict;
4-
use Test::More tests => 14;
4+
use Test::More tests => 15;
55

66
use Build;
77
use Build::Rpm;
@@ -396,3 +396,15 @@ $expected = {
396396
$result = Build::Rpm::parse($conf, [ split("\n", "%global foo 1\n%global bar 1\n$spec") ]);
397397
is_deeply($result, $expected, "multiline condition 4");
398398

399+
$Build::Rpm::includecallback = sub { $_[0] eq 'xxx' ? "%mac_foo mac_bar\n": undef };
400+
$spec = q[
401+
%{load:xxx}
402+
Name: %mac_foo
403+
];
404+
$expected = {
405+
'name' => 'mac_bar',
406+
'deps' => [],
407+
'subpacks' => ['mac_bar'],
408+
};
409+
$result = Build::Rpm::parse($conf, [ split("\n", $spec) ]);
410+
is_deeply($result, $expected, "multiline condition 4");

0 commit comments

Comments
 (0)