8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / scripts / genoffsets.pl
blobc68c6184a1b7abf6297069acd66428137c0acabb
1 #!/bin/perl
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License, Version 1.0 only
7 # (the "License"). You may not use this file except in compliance
8 # with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
21 # CDDL HEADER END
24 # Copyright 2004 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
27 # ident "%Z%%M% %I% %E% SMI"
31 # ctfstabs requires an object file with CTF data, and a file containing
32 # directives which indicate the types and members for which offsets are to
33 # be generated. The developer provides a single input file containing both
34 # the #include directives used to generate the object file as well as the
35 # offset directives. This script automates the splitting of the master file,
36 # the generation of the object file, the invocation of ctfstabs, and cleanup.
39 use strict;
40 use warnings;
41 use File::Basename;
42 use Getopt::Std;
43 use POSIX qw(:sys_wait_h);
45 # Globals.
46 our $PROGNAME = basename($0);
47 our ($CTmp, $OTmp, $GenTmp, $GenPPTmp, $Keep, $Verbose);
49 sub usage {
50 print STDERR "Usage: $PROGNAME [-k] [-s ctfstabs] [-r ctfconvert] ",
51 "compiler [options]\n";
52 print STDERR " NOTE: compiler options must enable stabs or DWARF as ",
53 "appropriate\n";
54 exit(2);
57 sub cleanup {
58 return if ($Keep);
60 unlink($CTmp) if (-f $CTmp);
61 unlink($OTmp) if (-f $OTmp);
62 unlink($GenTmp) if (-f $GenTmp);
63 unlink($GenPPTmp) if (-f $GenPPTmp);
66 sub bail {
67 print STDERR "$PROGNAME: ", join(" ", @_), "\n";
68 cleanup();
69 exit(1);
73 sub findprog {
74 my ($arg, $name, $default) = @_;
76 if (defined $arg) {
77 return ($arg);
78 } elsif (defined $ENV{$name}) {
79 return ($ENV{$name});
80 } else {
81 return ($default);
85 sub runit {
86 my (@argv) = @_;
87 my $rc;
89 if ($Verbose) {
90 print STDERR "+ @argv\n";
92 if ((my $rc = system(@argv)) == -1) {
93 bail("Failed to execute $argv[0]: $!");
94 } elsif (WIFEXITED($rc)) {
95 $_ = WEXITSTATUS($rc);
96 if ($_ == 0) {
97 return;
98 } else {
99 bail("$argv[0] failed with status $_");
101 } elsif (WSIGNALLED($rc)) {
102 $_ = WTERMSIG($rc);
103 # WCOREDUMP isn't a POSIX macro, do it the non-portable way.
104 if ($rc & 0x80) {
105 bail("$argv[0] failed with signal $_ (core dumped)");
106 } else {
107 bail("$argv[0] failed with signal $_");
113 # Main.
116 my %opts;
117 getopts("kr:s:v", \%opts) || usage();
118 usage() if (@ARGV < 1);
120 my $ctfstabs = findprog($opts{"s"}, "CTFSTABS", "ctfstabs");
121 my $ctfconvert = findprog($opts{"r"}, "CTFCONVERT", "ctfconvert");
123 $Keep = $opts{k};
124 $Verbose = $opts{k} || $opts{v};
125 my ($cc, @cflags) = @ARGV;
127 $CTmp = "ctfstabs.tmp.$$.c"; # The C file used to generate CTF
128 $OTmp = "ctfstabs.tmp.$$.o"; # Object file with CTF
129 $GenTmp = "ctfstabs.tmp.$$.gen.c"; # genassym directives
130 $GenPPTmp = "ctfstabs.tmp.$$.genpp"; # Post-processed genassym directives
132 my ($cfile, $genfile);
133 open($cfile, '>', $CTmp) || bail("failed to create $CTmp: $!");
134 open($genfile, '>', $GenTmp) || bail("failed to create $GenTmp: $!");
136 if ($Verbose) {
137 print STDERR "Splitting from stdin to $CTmp and $GenTmp\n";
140 while (<STDIN>) {
141 # #includes go to the C file. All other preprocessor directives
142 # go to both the C file and the offsets input file. Anything
143 # that's not a preprocessor directive goes into the offsets input
144 # file. Also strip comments from the genfile, as they can confuse
145 # the preprocessor.
146 if (/^#include/) {
147 print $cfile $_;
148 } elsif (/^#/) {
149 print $cfile $_;
150 print $genfile $_;
151 } elsif (/^\\#/) {
152 print $genfile $_;
153 } elsif (!/^\\/) {
154 print $genfile $_;
157 close($cfile) || bail("can't close $CTmp: $!");
158 close($genfile) || bail("can't close $GenTmp: $!");
160 # Compile the C file.
161 runit($cc, @cflags, '-c', '-o', $OTmp, $CTmp);
163 # Convert the debugging information to CTF.
164 runit($ctfconvert, '-l', 'ctfstabs', $OTmp);
166 # Run ctfstabs on the resulting mess.
167 runit($cc, @cflags, "-P", "-o", "$GenPPTmp", $GenTmp);
168 runit($ctfstabs, "-t", "genassym", "-i", $GenPPTmp, $OTmp);
170 cleanup();
172 exit (0);