1#!/usr/bin/perl
2
3use strict;
4
5# exec make, but first run make with a target of no64.  If it outputs YES,
6# then remove 64-bit arches from RC_CFLAGS and RC_ARCHS, remover RC_xxx,
7# where xxx is a 64-bit architecture.  If there are no archs left, just
8# return success.
9
10my $dir = '.';
11
12for(0...$#ARGV) {
13    if($ARGV[$_] eq '-C') {
14	$dir = $ARGV[$_ + 1];
15	last;
16    }
17}
18
19my $no64 = `make -C $dir no64`;
20chomp($no64);
21
22if($no64 eq 'YES') {
23    my @archs;
24    my @arch64;
25    my @cflags;
26    my $arch = 0;
27    for(split(" ", $ENV{RC_CFLAGS})) {
28	if($arch) {
29	    if(/64/) {
30		push(@arch64, $_);
31	    } else {
32		push(@cflags, '-arch', $_);
33		push(@archs, $_);
34	    }
35	    $arch = 0;
36	    next;
37	}
38	if($_ eq '-arch') {
39	    $arch = 1;
40	    next;
41	}
42	push(@cflags, $_);
43    }
44    unless(scalar(@archs) > 0) {
45	print "Not building:\tmake @ARGV\n";
46	exit 0;
47    }
48    $ENV{RC_CFLAGS} = join(' ', @cflags);
49    $ENV{RC_ARCHS} = join(' ', @archs);
50    push(@ARGV, "RC_CFLAGS=$ENV{RC_CFLAGS}", "RC_ARCHS=$ENV{RC_ARCHS}");
51    for(@arch64) {
52	delete($ENV{"RC_$_"});
53	push(@ARGV, "RC_$_=");
54    }
55}
56print "make @ARGV\n";
57exec {'make'} 'make', @ARGV;
58