Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perlmod / Midas / 3.32 / lib / site_perl / 5.8.0 / Midas / Preprocess.pm
CommitLineData
86530b38
AT
1# ========== Copyright Header Begin ==========================================
2#
3# OpenSPARC T2 Processor File: Preprocess.pm
4# Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved
5# 4150 Network Circle, Santa Clara, California 95054, U.S.A.
6#
7# * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; version 2 of the License.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21#
22# For the avoidance of doubt, and except that if any non-GPL license
23# choice is available it will apply instead, Sun elects to use only
24# the General Public License version 2 (GPLv2) at this time for any
25# software where a choice of GPL license versions is made
26# available with the language indicating that GPLv2 or any later version
27# may be used, or where a choice of which version of the GPL is applied is
28# otherwise unspecified.
29#
30# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
31# CA 95054 USA or visit www.sun.com if you need additional information or
32# have any questions.
33#
34# ========== Copyright Header End ============================================
35# -*- perl -*-
36
37package Midas::Preprocess;
38use strict;
39use warnings;
40
41use IO::File;
42use File::Temp 'tempfile';
43use File::Copy;
44
45use Midas::Command;
46use Midas::Paths;
47use Midas::Section;
48use Midas::MMU;
49use Midas::Configure;
50use Midas::State;
51use Midas::Globals;
52use Midas::Error;
53
54require Exporter;
55
56our @ISA = qw(Exporter);
57our @EXPORT = qw(preprocess);
58our @EXPORT_OK = qw();
59our %EXPORT_TAGS = (
60 all => [qw(
61 preprocess
62 run_cpp
63 run_m4
64 get_includes
65 get_cpp_includes
66 get_m4_includes
67 ),
68 ],
69 internals => [qw(
70 get_includes
71 get_cpp_includes
72 get_m4_includes
73 ),
74 ],
75 );
76
77Exporter::export_ok_tags('all', 'internals');
78
79##############################################################################
80
81# Perform both cpp and m4 preprocessing
82
83##############################################################################
84
85sub preprocess {
86 my $sfile = shift;
87 my $cppfile = shift;
88 my $m4file = shift;
89
90 banner "PREPROCESSING PHASE";
91
92 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
93
94 $m4file = path_to_build_file($CONFIG{local_m4}, $STATE)
95 unless defined $m4file;
96
97 if(exists $CONFIG{ttefmt} and defined $CONFIG{ttefmt}) {
98 my $ttefmt = uc $CONFIG{ttefmt};
99 add_cpp_defines(
100 "$ttefmt=1",
101 );
102 }
103
104 run_cpp($sfile, $cppfile);
105 run_m4($cppfile, $m4file);
106
107}
108
109##############################################################################
110
111sub get_includes {
112 my $include_hash = shift;
113
114 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
115
116 my %roothash = (
117 diagroot => $STATE->get_diag_root(),
118 startdir => $STATE->get_start_dir(),
119 builddir => $STATE->get_build_dir(),
120 abs => '/',
121 );
122
123 my @root_order = qw(builddir startdir diagroot abs);
124
125 my @incs;
126 foreach my $root (@root_order) {
127 next unless exists $include_hash->{$root};
128 fatal "Unknown include root $root in includes\n", M_DIR
129 unless exists $roothash{$root};
130
131 my $rootpath = $roothash{$root};
132 foreach my $dir (@{$include_hash->{$root}}) {
133 my $path = File::Spec->catdir($rootpath,
134 split /\//, $dir);
135 push @incs, $path;
136 }
137 }
138 return @incs;
139
140}
141
142##############################################################################
143
144sub get_cpp_includes {
145 return get_includes($CONFIG{cpp_includes});
146}
147
148##############################################################################
149
150sub get_m4_includes {
151 return get_includes($CONFIG{m4_includes});
152}
153
154##############################################################################
155
156# Use cpp to convert from assembly file to .cpp file
157
158##############################################################################
159
160sub run_cpp {
161 my $sfile = shift;
162 my $cppfile = shift;
163
164 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
165
166 $sfile = path_to_build_file($CONFIG{local_s}, $STATE)
167 unless defined $sfile;
168 $cppfile = path_to_build_file($CONFIG{local_cpp}, $STATE)
169 unless defined $cppfile;
170
171 local ($_);
172 # Don't compact path, b/c we want full path for standard includes
173 my @incs = map { "-I$_" } get_cpp_includes();
174 my @defs = map { "-D$_" } @{$CONFIG{'cpp_defines'}};
175
176 my $need_space = (scalar @incs and scalar @defs) ? ' ' : '';
177
178 my $mmu_argstring = '';
179 my @mmu_args = @{$STATE->get_mmu()->mmu_cpp_args()};
180 if(@mmu_args) {
181
182 $mmu_argstring .= ' ' . join ' ', @mmu_args;
183 }
184
185 my $arglist =
186 (join ' ', @{$CONFIG{cpp_opt}}) . ' ' .
187 (join ' ', @incs ) . $need_space .
188 (join ' ', @defs) . $mmu_argstring;
189 run_command("$CONFIG{cpp_cmd} $arglist $sfile > $cppfile",
190 -errcode => M_CPPFAIL);
191}
192
193##############################################################################
194
195# Take output of cpp and run through m4.
196
197##############################################################################
198
199sub run_m4 {
200 my $cppfile = shift;
201 my $m4file = shift;
202
203 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
204
205 $cppfile = path_to_build_file($CONFIG{local_cpp}, $STATE)
206 unless defined $cppfile;
207 $m4file = path_to_build_file($CONFIG{local_m4}, $STATE)
208 unless defined $m4file;
209
210 local ($_);
211 # Don't compact path, b/c we want full path for standard includes
212 my @incs = map { "--include=$_" } get_m4_includes();
213 my $arglist =
214 (join ' ', @{$CONFIG{m4_opt}}) . ' '.
215 (join ' ', @incs);
216 run_command("$CONFIG{m4_cmd} $arglist < $cppfile | $CONFIG{cat_cmd} --squeeze-blank > $m4file",
217 -errcode => M_M4FAIL
218 );
219
220}
221
222##############################################################################
2231;