xmake revision 810
830N/A#! /usr/perl5/bin/perl -w
830N/A
830N/A#
830N/A# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
830N/A# Use is subject to license terms.
830N/A#
830N/A# Permission is hereby granted, free of charge, to any person obtaining a
830N/A# copy of this software and associated documentation files (the
830N/A# "Software"), to deal in the Software without restriction, including
830N/A# without limitation the rights to use, copy, modify, merge, publish,
830N/A# distribute, and/or sell copies of the Software, and to permit persons
830N/A# to whom the Software is furnished to do so, provided that the above
830N/A# copyright notice(s) and this permission notice appear in all copies of
830N/A# the Software and that both the above copyright notice(s) and this
830N/A# permission notice appear in supporting documentation.
830N/A#
830N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
830N/A# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
830N/A# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
830N/A# OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
830N/A# HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
830N/A# INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
830N/A# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
830N/A# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
830N/A# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
830N/A#
830N/A# Except as contained in this notice, the name of a copyright holder
830N/A# shall not be used in advertising or otherwise to promote the sale, use
830N/A# or other dealings in this Software without prior written authorization
830N/A# of the copyright holder.
830N/A#
830N/A# ident "@(#)xmake 1.2 09/10/13 SMI"
830N/A#
830N/A
830N/Arequire 5.005; # minimal Perl version required
830N/Ause strict; #
830N/Ause diagnostics; #
830N/Ause File::Path;
830N/Ause File::Spec;
830N/Ause English qw( -nomatchvars );
830N/Ause POSIX qw(uname);
830N/A
830N/Amy $verbose = 0;
830N/Aif ((scalar(@ARGV) > 0) && ($ARGV[0] eq '-v')) {
830N/A shift @ARGV;
830N/A $verbose = 1;
830N/A}
830N/A
830N/A# Arguments: (envvar, defval)
830N/A# If environment variable 'envvar' is not set, set it to 'defval'
830N/Asub setenv_default {
830N/A my ($envvar, $defval) = @_;
830N/A
830N/A if (!exists $ENV{$envvar}) {
830N/A $ENV{$envvar} = $defval;
830N/A }
830N/A
830N/A if ($verbose > 0) {
830N/A print $envvar, '=', $ENV{$envvar}, "\n";
830N/A }
830N/A
830N/A return $ENV{$envvar};
830N/A}
830N/A
830N/A# save full path to current directory
830N/Amy $startdir = File::Spec->rel2abs(File::Spec->curdir());
830N/A
830N/A# climb the tree to find the open-src module directory we're in
830N/Amy @dirtree = File::Spec->splitdir($startdir);
830N/Amy $osdepth;
830N/A
830N/Afor my $n (1..$#dirtree) {
830N/A if ($dirtree[$n] eq 'open-src') {
830N/A if (-f File::Spec->catfile( @dirtree[0..$n], 'common/Makefile.inc')) {
830N/A $osdepth = $n;
830N/A last;
830N/A }
830N/A }
830N/A if ($dirtree[$n] eq 'closed-src') {
830N/A if (-f File::Spec->catfile( @dirtree[0..($n-1)],
830N/A 'open-src/common/Makefile.inc')) {
830N/A $osdepth = $n;
830N/A last;
830N/A }
830N/A }
830N/A}
830N/A
830N/Aif (!defined($osdepth)) {
830N/A die "$0: Cannot find path to open-src/common/Makefile.inc from here";
830N/A}
830N/A
830N/A
830N/A# Use dmake unless user environment overrides
830N/Amy $make_cmd = setenv_default('MAKE', 'dmake');
830N/A
830N/Aif ($make_cmd =~ m/dmake/) {
830N/A # Set dmake environment for parallel builds by default
830N/A setenv_default('DMAKE_MODE', 'parallel');
830N/A setenv_default('DMAKE_OUTPUT_MODE', 'TXT2');
830N/A
830N/A if (!exists $ENV{'DMAKE_MAX_JOBS'}) {
830N/A my $max_jobs;
830N/A my $machlist = join('/', $ENV{'HOME'}, '.make.machines');
830N/A if ( -f $machlist ) {
830N/A my $nodename = (POSIX::uname())[1];
830N/A if (open my $MACHLIST, '<', $machlist) {
830N/A while (my $m = <$MACHLIST>) {
830N/A my ($hostname, $vars) = split ' ', $m, 2;
830N/A
830N/A next if (!defined($hostname) || !defined($vars));
830N/A if ($hostname eq $nodename) {
830N/A my @varlist = split /\s+/, $vars;
830N/A foreach my $v (@varlist) {
830N/A my ($var, $val) = split /=/, $v;
830N/A if ($var eq 'max') {
830N/A $max_jobs = $val;
830N/A last;
830N/A }
830N/A }
830N/A last;
830N/A }
830N/A }
830N/A close $MACHLIST;
830N/A }
830N/A }
830N/A if (!defined($max_jobs)) {
830N/A $max_jobs = 0;
830N/A
830N/A if (open my $PSRINFO, '-|', '/usr/sbin/psrinfo') {
830N/A while (my $p = <$PSRINFO>) {
830N/A if ($p =~ m/on-line/) {
830N/A $max_jobs++;
830N/A }
830N/A }
830N/A close $PSRINFO;
830N/A }
830N/A }
830N/A setenv_default('DMAKE_MAX_JOBS', $max_jobs);
830N/A }
830N/A
830N/A my $dmake_odir =
830N/A setenv_default('DMAKE_ODIR', File::Spec->catfile(@dirtree[0..($osdepth-1)],
830N/A 'log', '.dmake'));
830N/A mkpath($dmake_odir);
830N/A}
830N/A
830N/A# if in top two levels, just run make
830N/Aif ($osdepth >= ($#dirtree - 2)) {
830N/A print join(' ', $make_cmd, @ARGV), "\n";
830N/A exec($make_cmd, @ARGV)
830N/A or die "$0: exec of $make_cmd failed: $OS_ERROR";
830N/A}
830N/A
830N/Amy $subdir_target = 'build-in-subdir';
830N/Afor my $f (@ARGV) {
830N/A if ($f =~ m{^install}ms) {
830N/A $subdir_target = 'install-in-subdir';
830N/A }
830N/A}
830N/A
830N/A# Otherwise get info from the module makefile
830N/Amy $moduledir = File::Spec->catdir( @dirtree[0..($osdepth+2)] );
830N/A
830N/Amy @makeargs = ($subdir_target, qq{subdir='$startdir'});
830N/Aif (scalar(@ARGV) > 0) {
830N/A push @makeargs, join(q{ }, q{subdir_cmd=}, @ARGV);
830N/A}
830N/A
830N/Aprint join(' ', "(cd $moduledir ;\\\n", $make_cmd, @makeargs), ")\n";
830N/Achdir $moduledir
830N/A or die "$0: Can't chdir $moduledir: $OS_ERROR";
830N/Aexec($make_cmd, @makeargs)
830N/A or die "$0: exec of $make_cmd failed: $OS_ERROR";
830N/A
830N/A__END__
830N/A
830N/A