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