10N/A#!/usr/perl5/bin/perl
10N/A#
10N/A# CDDL HEADER START
10N/A#
10N/A# The contents of this file are subject to the terms of the
10N/A# Common Development and Distribution License (the "License").
10N/A# You may not use this file except in compliance with the License.
10N/A#
10N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10N/A# or http://www.opensolaris.org/os/licensing.
10N/A# See the License for the specific language governing permissions
10N/A# and limitations under the License.
10N/A#
10N/A# When distributing Covered Code, include this CDDL HEADER in each
10N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
10N/A# If applicable, add the following below this CDDL HEADER, with the
10N/A# fields enclosed by brackets "[]" replaced with your own identifying
10N/A# information: Portions Copyright [yyyy] [name of copyright owner]
10N/A#
10N/A# CDDL HEADER END
10N/A#
5680N/A
5680N/A#
3770N/A# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
10N/A#
10N/A#
10N/A# build-watch.pl - a simple utility to watch a process and it's children under
10N/A# dtrace and process the results for dependency information.
10N/A#
10N/A# Ex:
10N/A# $ export WS_TOP=/top/of/your/workspace
10N/A# $ cd $(WS_TOP)/components/{component}
10N/A# $ $(WS_TOP)/tools/build-watch.pl -c "gmake install"
10N/A#
10N/A
10N/Ause File::Temp qw/tempfile/;
10N/Ause Getopt::Long;
10N/A
10N/Amy $verbose = 0;
10N/A
10N/Amy @ignore = (
10N/A '^[^/\.].*', # ignore paths that don't begin with / or .
10N/A '^/dev/', # ignore devices
10N/A '^/etc/', # ignore config files
10N/A '^/proc/', # ignore /proc
10N/A '^/tmp/', # ignore temp files
10N/A '^/var/', # ignore more temp/volatile files
10N/A '^/usr/lib/locale/', # ignore locale support
10N/A '^/usr/share/lib/make/', # ignore make bits
10N/A '^/usr/share/lib/zoneinfo/', # ignore timezone info
10N/A '/SUNWspro/', # ignore compiler bits
10N/A '/sunstudio12.1/', # ignore more compiler bits
10N/A '^/ws/', # nothing in /ws can be interesting
10N/A '^\.[/\.]{0,1}$' # ignore ., .., and ./
10N/A);
10N/A
10N/Asub match
10N/A{
10N/A local($string, @expressions) = @_;
10N/A
10N/A foreach (@expressions) {
10N/A ($string =~ m{$_}) && return (1);
10N/A }
10N/A return (0);
10N/A}
10N/A
10N/Asub process_dtrace_results
10N/A{
10N/A local ($filename) = @_;
10N/A my (%tools, %files, $fh) = ();
10N/A
10N/A open($fh, "<$filename") || die "open($filename): $!\n";
10N/A while (<$fh>) {
10N/A if (/^TOOL:\s+(\S+) = (\S+)$/) {
10N/A $tools{$2} = $1;
10N/A } elsif ((/^FILE:\s+(\S+)\s*$/) && (match($1, @ignore) == 0) &&
10N/A (-e $1)) {
10N/A $files{$1} = $1;
10N/A }
10N/A }
10N/A close($fh);
10N/A
10N/A return (\%tools, \%files);
10N/A}
10N/A
10N/Asub generate_package_requirements
10N/A{
10N/A local (*tools, *files) = @_;
10N/A my ($count, %pkgs, @search_strings, $search_string) = (0);
10N/A
10N/A # create a set of search strings to query the package DB
10N/A foreach (sort (keys %tools, keys %files)) {
10N/A if ($count++ % 100 == 0) {
10N/A defined($search_string) && \
10N/A push(@search_strings, $search_string);
10N/A $search_string = $_;
10N/A } else {
10N/A $search_string .= " OR $_";
10N/A }
10N/A }
10N/A push(@search_strings, $search_string);
10N/A
10N/A # walk through the search strings and query the package DB
10N/A foreach (@search_strings) {
10N/A my $IFH;
10N/A
10N/A open($IFH, "pkg search -l -H -o path,pkg.name '$_'|");
10N/A while (<$IFH>) {
10N/A (/^(\S+)\s+(\S+)$/) && ($pkgs{$1} = $2);
10N/A }
10N/A close($IFH);
10N/A }
10N/A
10N/A return (\%pkgs);
10N/A}
10N/A
10N/A#
10N/A# Main execution begins here
10N/A#
10N/AGetOptions("c|command=s" => \$cmd, "i|input-file=s" => \@file,
32N/A "p|pkg" => \$pkg_flag, "v|verbose" => \$verbose);
10N/A
10N/Aif (defined($cmd)) {
10N/A $file = (tempfile(UNLINK => 1))[1];
10N/A
10N/A if (!defined($ENV{'WS_TOP'})) {
10N/A print("WS_TOP must be set in the calling environment\n");
10N/A exit(1);
10N/A }
10N/A ($verbose == 1) && print("*** Executing '$cmd' under dtrace...\n");
10N/A system($ENV{'WS_TOP'}."/tools/build-watch.d", "-o", $file, "-c", $cmd);
10N/A}
10N/A
10N/A($verbose == 1) && printf("*** Processing results...\n");
10N/Amy ($tools, $files) = process_dtrace_results($file);
10N/A
32N/Aif (defined($pkg_flag)) {
32N/A ($verbose == 1) && printf("*** Generating package requirements...\n");
32N/A my ($pkgs) = generate_package_requirements($tools, $files);
32N/A}
10N/A
10N/Aif (defined($tools)) {
17N/A print "\n";
17N/A print "REQUIRED_TOOL +=\t$_\n" for (sort keys %$tools);
10N/A}
10N/A
10N/Aif (defined($files)) {
17N/A print "\n";
17N/A print "REQUIRED_FILE +=\t$_\n" for (sort keys %$files);
10N/A}
10N/A
10N/Aif (defined($pkgs)) {
10N/A @unique{values %$pkgs} = ();
17N/A print "\n";
17N/A print "REQUIRED_PKG +=\t$_\n" for (sort keys %unique);
10N/A}
10N/A
10N/Aexit(0);