493N/A#! /usr/perl5/bin/perl -w
493N/A
493N/A#
1296N/A# Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
493N/A#
493N/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
919N/A# Software is furnished to do so, subject to the following conditions:
810N/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
919N/A# Software.
810N/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
919N/A# DEALINGS IN THE SOFTWARE.
493N/A#
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
1296N/Amy @pass_args; # arguments to pass on to the make we call
810N/Amy $verbose = 0;
1296N/Amy $silent = 0;
1296N/Amy $max_jobs;
1296N/A
1296N/Afor (my $i = 0; $i <= $#ARGV; $i++ ) {
1296N/A if ($ARGV[$i] eq '-s') {
1296N/A $silent = 1;
1296N/A $verbose = 0;
1296N/A goto pass_to_make;
1296N/A }
1296N/A elsif ($ARGV[$i] eq '-v') {
1296N/A $verbose = 1;
1296N/A next;
1296N/A }
1296N/A
1296N/A if ($i < $#ARGV) { # more than one arg left
1296N/A if ($ARGV[$i] eq '-j') {
1296N/A $max_jobs = $ARGV[++$i];
1296N/A next;
1296N/A }
1296N/A if ($ARGV[$i] eq '-C') {
1296N/A chdir($ARGV[++$i])
1296N/A or die "$0: Can't chdir $ARGV[$i]: $OS_ERROR";
1296N/A next;
1296N/A }
1296N/A }
1296N/A
1296N/A pass_to_make:
1296N/A # not used, pass to make command
1296N/A push @pass_args, $ARGV[$i];
810N/A}
810N/A
851N/Amy @makeargs = ();
851N/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
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}
851N/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) {
1003N/A if (-f File::Spec->catfile( @dirtree[0..$n],
1003N/A 'open-src/common/Makefile.inc')) {
1003N/A $osdepth = $n + 1;
1003N/A last;
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
851N/A if (!defined($max_jobs) && exists $ENV{'DMAKE_MAX_JOBS'}) {
851N/A $max_jobs = $ENV{'DMAKE_MAX_JOBS'};
851N/A }
851N/A
851N/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>) {
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 }
810N/A
851N/A push @makeargs, '-j', $max_jobs;
851N/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
967N/A# Set some make variables that our makefiles recognize
967N/Amy $topdir = File::Spec->catdir( @dirtree[0..($osdepth-1)] );
967N/Apush @makeargs, "TOP=$topdir", "V=$verbose";
967N/A
493N/A# if in top two levels, just run make
493N/Aif ($osdepth >= ($#dirtree - 2)) {
1296N/A if ($silent == 0) {
1296N/A print join(' ', $make_cmd, @makeargs, @pass_args), "\n";
1296N/A }
1296N/A exec_verbose($make_cmd, @makeargs, @pass_args);
493N/A}
493N/A
493N/Amy $subdir_target = 'build-in-subdir';
1296N/Afor my $f (@pass_args) {
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
851N/Apush @makeargs, $subdir_target, qq{subdir='$startdir'};
1296N/Aif (scalar(@pass_args) > 0) {
1296N/A push @makeargs, join(q{ }, q{subdir_cmd=}, @pass_args);
493N/A}
493N/A
1296N/Aif ($silent == 0) {
1296N/A print join(' ', "(cd $moduledir ;\\\n", $make_cmd, @makeargs), ")\n";
1296N/A}
493N/Achdir $moduledir
493N/A or die "$0: Can't chdir $moduledir: $OS_ERROR";
851N/Aexec_verbose($make_cmd, @makeargs);
493N/A
493N/A__END__
493N/A