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