xmake revision 919
493N/A#! /usr/perl5/bin/perl -w
493N/A
493N/A#
967N/A# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
493N/A# Use is subject to license terms.
493N/A#
919N/A# Permission is hereby granted, free of charge, to any person obtaining a
919N/A# copy of this software and associated documentation files (the "Software"),
919N/A# to deal in the Software without restriction, including without limitation
919N/A# the rights to use, copy, modify, merge, publish, distribute, sublicense,
919N/A# and/or sell copies of the Software, and to permit persons to whom the
810N/A# Software is furnished to do so, subject to the following conditions:
919N/A#
919N/A# The above copyright notice and this permission notice (including the next
919N/A# paragraph) shall be included in all copies or substantial portions of the
810N/A# Software.
919N/A#
919N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
919N/A# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
919N/A# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
919N/A# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
919N/A# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
919N/A# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
493N/A# DEALINGS IN THE SOFTWARE.
493N/A#
493N/A#
493N/A
493N/Arequire 5.005; # minimal Perl version required
493N/Ause strict; #
810N/Ause diagnostics; #
493N/Ause File::Path;
493N/Ause File::Spec;
810N/Ause English qw( -nomatchvars );
493N/Ause POSIX qw(uname);
810N/A
810N/Amy $verbose = 0;
810N/Aif ((scalar(@ARGV) > 0) && ($ARGV[0] eq '-v')) {
810N/A shift @ARGV;
810N/A $verbose = 1;
810N/A}
851N/A
851N/Amy @makeargs = ();
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};
493N/A}
851N/A
851N/Asub exec_verbose {
851N/A my $program = shift @_;
851N/A
851N/A if ($verbose > 0) {
851N/A print join(' ', $program, @_), "\n";
851N/A }
851N/A exec($program, @_)
851N/A or die "$0: exec of $program failed: $OS_ERROR";
851N/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
1003N/Afor my $n (1..$#dirtree) {
1003N/A if ($dirtree[$n] eq 'open-src') {
1003N/A if (-f File::Spec->catfile( @dirtree[0..$n], 'common/Makefile.inc')) {
1003N/A $osdepth = $n;
810N/A last;
493N/A }
493N/A }
493N/A if ($dirtree[$n] eq 'closed-src') {
493N/A if (-f File::Spec->catfile( @dirtree[0..($n-1)],
493N/A 'open-src/common/Makefile.inc')) {
493N/A $osdepth = $n;
810N/A last;
810N/A }
810N/A }
810N/A}
810N/A
810N/Aif (!defined($osdepth)) {
810N/A die "$0: Cannot find path to open-src/common/Makefile.inc from here";
810N/A}
810N/A
851N/A
851N/A# Use dmake unless user environment overrides
851N/Amy $make_cmd = setenv_default('MAKE', 'dmake');
851N/A
851N/Aif ($make_cmd =~ m/dmake/) {
851N/A # Set dmake environment for parallel builds by default
851N/A setenv_default('DMAKE_MODE', 'parallel');
851N/A setenv_default('DMAKE_OUTPUT_MODE', 'TXT2');
851N/A
851N/A my $max_jobs;
851N/A
851N/A foreach my $i ( 0..($#ARGV - 1) ) {
851N/A if ($ARGV[$i] eq '-j') {
851N/A $max_jobs = $ARGV[$i+1];
851N/A $ARGV[$i] = '';
810N/A $ARGV[$i+1] = '';
810N/A }
810N/A }
810N/A
810N/A if (!defined($max_jobs) && exists $ENV{'DMAKE_MAX_JOBS'}) {
810N/A $max_jobs = $ENV{'DMAKE_MAX_JOBS'};
810N/A }
810N/A
810N/A if (!defined($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>) {
851N/A if ($p =~ m/on-line/) {
851N/A $max_jobs++;
810N/A }
810N/A }
810N/A close $PSRINFO;
810N/A }
810N/A }
810N/A }
967N/A
967N/A push @makeargs, '-j', $max_jobs;
967N/A
967N/A my $dmake_odir =
493N/A setenv_default('DMAKE_ODIR', File::Spec->catfile(@dirtree[0..($osdepth-1)],
493N/A 'log', '.dmake'));
851N/A mkpath($dmake_odir);
851N/A}
493N/A
493N/A# if in top two levels, just run make
493N/Aif ($osdepth >= ($#dirtree - 2)) {
493N/A print join(' ', $make_cmd, @makeargs, @ARGV), "\n";
493N/A exec_verbose($make_cmd, @makeargs, @ARGV);
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 }
851N/A}
493N/A
810N/A# Otherwise get info from the module makefile
493N/Amy $moduledir = File::Spec->catdir( @dirtree[0..($osdepth+2)] );
493N/A
810N/Apush @makeargs, $subdir_target, qq{subdir='$startdir'};
493N/Aif (scalar(@ARGV) > 0) {
493N/A push @makeargs, join(q{ }, q{subdir_cmd=}, @ARGV);
851N/A}
493N/A
493N/Aprint join(' ', "(cd $moduledir ;\\\n", $make_cmd, @makeargs), ")\n";
493N/Achdir $moduledir
or die "$0: Can't chdir $moduledir: $OS_ERROR";
exec_verbose($make_cmd, @makeargs);
__END__