xmake revision 919
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay#! /usr/perl5/bin/perl -w
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay#
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# Use is subject to license terms.
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay#
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# Permission is hereby granted, free of charge, to any person obtaining a
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# copy of this software and associated documentation files (the "Software"),
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# to deal in the Software without restriction, including without limitation
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# the rights to use, copy, modify, merge, publish, distribute, sublicense,
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# and/or sell copies of the Software, and to permit persons to whom the
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# Software is furnished to do so, subject to the following conditions:
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay#
088fa5d9eaa83bf4b3d59a64c0519f42a143aaa9Alin Brici# The above copyright notice and this permission notice (including the next
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# paragraph) shall be included in all copies or substantial portions of the
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# Software.
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay#
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmiller# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
088fa5d9eaa83bf4b3d59a64c0519f42a143aaa9Alin Brici# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
088fa5d9eaa83bf4b3d59a64c0519f42a143aaa9Alin Brici# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# DEALINGS IN THE SOFTWARE.
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay#
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay#
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayrequire 5.005; # minimal Perl version required
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayuse strict; #
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayuse diagnostics; #
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayuse File::Path;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayuse File::Spec;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayuse English qw( -nomatchvars );
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayuse POSIX qw(uname);
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemaymy $verbose = 0;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayif ((scalar(@ARGV) > 0) && ($ARGV[0] eq '-v')) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay shift @ARGV;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay $verbose = 1;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay}
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmillermy @makeargs = ();
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmiller
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# Arguments: (envvar, defval)
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmiller# If environment variable 'envvar' is not set, set it to 'defval'
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmillersub setenv_default {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay my ($envvar, $defval) = @_;
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmiller
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay if (!exists $ENV{$envvar}) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay $ENV{$envvar} = $defval;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay }
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay if ($verbose > 0) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay print $envvar, '=', $ENV{$envvar}, "\n";
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay }
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay return $ENV{$envvar};
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay}
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemaysub exec_verbose {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay my $program = shift @_;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay if ($verbose > 0) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay print join(' ', $program, @_), "\n";
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay }
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay exec($program, @_)
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay or die "$0: exec of $program failed: $OS_ERROR";
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay}
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# save full path to current directory
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemaymy $startdir = File::Spec->rel2abs(File::Spec->curdir());
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# climb the tree to find the open-src module directory we're in
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemaymy @dirtree = File::Spec->splitdir($startdir);
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmillermy $osdepth;
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmiller
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayfor my $n (1..$#dirtree) {
4d154727feb7fbe69dd1451fa5462f558e7165c7Brendan Mmiller if ($dirtree[$n] eq 'open-src') {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay if (-f File::Spec->catfile( @dirtree[0..$n], 'common/Makefile.inc')) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay $osdepth = $n;
3d67e448e54b5acfa464c365cd5a563d0e4ef29aJason Lemay last;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay }
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay }
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay if ($dirtree[$n] eq 'closed-src') {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay if (-f File::Spec->catfile( @dirtree[0..($n-1)],
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay 'open-src/common/Makefile.inc')) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay $osdepth = $n;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay last;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay }
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay }
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay}
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayif (!defined($osdepth)) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay die "$0: Cannot find path to open-src/common/Makefile.inc from here";
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay}
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay# Use dmake unless user environment overrides
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemaymy $make_cmd = setenv_default('MAKE', 'dmake');
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemayif ($make_cmd =~ m/dmake/) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay # Set dmake environment for parallel builds by default
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay setenv_default('DMAKE_MODE', 'parallel');
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay setenv_default('DMAKE_OUTPUT_MODE', 'TXT2');
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay my $max_jobs;
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay foreach my $i ( 0..($#ARGV - 1) ) {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay if ($ARGV[$i] eq '-j') {
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay $max_jobs = $ARGV[$i+1];
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay $ARGV[$i] = '';
102cd62c1b6b55d0dee627930d058a8cc36e2b4fJason Lemay $ARGV[$i+1] = '';
}
}
if (!defined($max_jobs) && exists $ENV{'DMAKE_MAX_JOBS'}) {
$max_jobs = $ENV{'DMAKE_MAX_JOBS'};
}
if (!defined($max_jobs)) {
my $machlist = join('/', $ENV{'HOME'}, '.make.machines');
if ( -f $machlist ) {
my $nodename = (POSIX::uname())[1];
if (open my $MACHLIST, '<', $machlist) {
while (my $m = <$MACHLIST>) {
my ($hostname, $vars) = split ' ', $m, 2;
next if (!defined($hostname) || !defined($vars));
if ($hostname eq $nodename) {
my @varlist = split /\s+/, $vars;
foreach my $v (@varlist) {
my ($var, $val) = split /=/, $v;
if ($var eq 'max') {
$max_jobs = $val;
last;
}
}
last;
}
}
close $MACHLIST;
}
}
if (!defined($max_jobs)) {
$max_jobs = 0;
if (open my $PSRINFO, '-|', '/usr/sbin/psrinfo') {
while (my $p = <$PSRINFO>) {
if ($p =~ m/on-line/) {
$max_jobs++;
}
}
close $PSRINFO;
}
}
}
push @makeargs, '-j', $max_jobs;
my $dmake_odir =
setenv_default('DMAKE_ODIR', File::Spec->catfile(@dirtree[0..($osdepth-1)],
'log', '.dmake'));
mkpath($dmake_odir);
}
# if in top two levels, just run make
if ($osdepth >= ($#dirtree - 2)) {
print join(' ', $make_cmd, @makeargs, @ARGV), "\n";
exec_verbose($make_cmd, @makeargs, @ARGV);
}
my $subdir_target = 'build-in-subdir';
for my $f (@ARGV) {
if ($f =~ m{^install}ms) {
$subdir_target = 'install-in-subdir';
}
}
# Otherwise get info from the module makefile
my $moduledir = File::Spec->catdir( @dirtree[0..($osdepth+2)] );
push @makeargs, $subdir_target, qq{subdir='$startdir'};
if (scalar(@ARGV) > 0) {
push @makeargs, join(q{ }, q{subdir_cmd=}, @ARGV);
}
print join(' ', "(cd $moduledir ;\\\n", $make_cmd, @makeargs), ")\n";
chdir $moduledir
or die "$0: Can't chdir $moduledir: $OS_ERROR";
exec_verbose($make_cmd, @makeargs);
__END__