Andreas Wacknitz
2024-04-04 7587efba57fedc8f0e46216bba4a49da6e4c4625
commit | author | age
f1b729 1 #!/usr/perl5/bin/perl
NJ 2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22 # Copyright (c) 2010, Oracle and/or it's affiliates.  All rights reserved.
23 #
24 #
25 # build-watch.pl - a simple utility to watch a process and it's children under
26 #            dtrace and process the results for dependency information.
27 #
28 # Ex:
29 #    $ export WS_TOP=/top/of/your/workspace
30 #    $ cd $(WS_TOP)/components/{component}
31 #    $ $(WS_TOP)/tools/build-watch.pl -c "gmake install"
32 #
33
34 use File::Temp qw/tempfile/;
35 use Getopt::Long;
36
37 my $verbose = 0;
38
39 my @ignore = (
40     '^[^/\.].*',        # ignore paths that don't begin with / or .    
41     '^/dev/',        # ignore devices
42     '^/etc/',        # ignore config files
43     '^/proc/',        # ignore /proc
44     '^/tmp/',        # ignore temp files
45     '^/var/',        # ignore more temp/volatile files
46     '^/usr/lib/locale/',    # ignore locale support
47     '^/usr/share/lib/make/',    # ignore make bits
48     '^/usr/share/lib/zoneinfo/',    # ignore timezone info
49     '^/ws/',        # nothing in /ws can be interesting
50     '^\.[/\.]{0,1}$'    # ignore ., .., and ./
51 );
52
53 sub match
54 {
55     local($string, @expressions) = @_;
56
57     foreach (@expressions) {
58         ($string =~ m{$_}) && return (1);
59     }
60     return (0);
61 }
62
63 sub process_dtrace_results
64 {
65     local ($filename) = @_;
66     my (%tools, %files, $fh) = ();
67
68     open($fh, "<$filename") || die "open($filename): $!\n";
69     while (<$fh>) {
70         if (/^TOOL:\s+(\S+) = (\S+)$/) {
71             $tools{$2} = $1;
72         } elsif ((/^FILE:\s+(\S+)\s*$/) && (match($1, @ignore) == 0) &&
73              (-e $1)) {
74             $files{$1} = $1;
75         }
76     }
77     close($fh);
78
79     return (\%tools, \%files);
80 }
81
82 sub generate_package_requirements
83 {
84     local (*tools, *files) = @_;
85     my ($count, %pkgs, @search_strings, $search_string) = (0);
86
87     # create a set of search strings to query the package DB
88     foreach (sort (keys %tools, keys %files)) {
89         if ($count++ % 100 == 0) {
90             defined($search_string) && \
91                 push(@search_strings, $search_string);
92             $search_string = $_;
93         } else {
94             $search_string .= " OR $_";
95         }
96     }
97     push(@search_strings, $search_string);
98
99     # walk through the search strings and query the package DB
100     foreach (@search_strings) {
101         my $IFH;
102
103         open($IFH, "pkg search -l -H -o path,pkg.name '$_'|");
104         while (<$IFH>) {
105                    (/^(\S+)\s+(\S+)$/) && ($pkgs{$1} = $2);
106         }
107         close($IFH);
108     }
109
110     return (\%pkgs);
111 }
112
113 #
114 # Main execution begins here
115 #
116 GetOptions("c|command=s" => \$cmd, "i|input-file=s" => \@file,
a325d4 117        "p|pkg" => \$pkg_flag, "v|verbose" => \$verbose);
f1b729 118
NJ 119 if (defined($cmd)) {
120     $file = (tempfile(UNLINK => 1))[1];
121
122     if (!defined($ENV{'WS_TOP'})) {
123         print("WS_TOP must be set in the calling environment\n");
124         exit(1);
125     }
126     ($verbose == 1) && print("*** Executing '$cmd' under dtrace...\n");
127     system($ENV{'WS_TOP'}."/tools/build-watch.d", "-o", $file, "-c", $cmd);
128 }
129
130 ($verbose == 1) && printf("*** Processing results...\n");
131 my ($tools, $files) = process_dtrace_results($file);
132
a325d4 133 if (defined($pkg_flag)) {
NJ 134     ($verbose == 1) && printf("*** Generating package requirements...\n");
135     my ($pkgs) = generate_package_requirements($tools, $files);
136 }
f1b729 137
NJ 138 if (defined($tools)) {
3bb285 139     print "\n";
NJ 140     print "REQUIRED_TOOL +=\t$_\n" for (sort keys %$tools);
f1b729 141 }
NJ 142
143 if (defined($files)) {
3bb285 144     print "\n";
NJ 145     print "REQUIRED_FILE +=\t$_\n" for (sort keys %$files);
f1b729 146 }
NJ 147
148 if (defined($pkgs)) {
149     @unique{values %$pkgs} = ();
3bb285 150     print "\n";
NJ 151     print "REQUIRED_PKG +=\t$_\n" for (sort keys %unique);
f1b729 152 }
NJ 153
154 exit(0);