Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / lib / site_perl / 5.8.0 / Midas_samy / Preprocess.pm
CommitLineData
86530b38
AT
1# -*- perl -*-
2
3package Midas::Preprocess;
4use strict;
5use warnings;
6
7use IO::File;
8use File::Temp 'tempfile';
9use File::Copy;
10
11use Midas::Command;
12use Midas::Paths;
13use Midas::Section;
14use Midas::MMU;
15use Midas::Configure;
16use Midas::State;
17use Midas::Globals;
18use Midas::Error;
19
20require Exporter;
21
22our @ISA = qw(Exporter);
23our @EXPORT = qw(preprocess);
24our @EXPORT_OK = qw();
25our %EXPORT_TAGS = (
26 all => [qw(
27 preprocess
28 run_cpp
29 run_m4
30 get_includes
31 get_cpp_includes
32 get_m4_includes
33 ),
34 ],
35 internals => [qw(
36 get_includes
37 get_cpp_includes
38 get_m4_includes
39 ),
40 ],
41 );
42
43Exporter::export_ok_tags('all', 'internals');
44
45##############################################################################
46
47# Perform both cpp and m4 preprocessing
48
49##############################################################################
50
51sub preprocess {
52 my $sfile = shift;
53 my $cppfile = shift;
54 my $m4file = shift;
55
56 banner "PREPROCESSING PHASE";
57
58 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
59
60 $m4file = path_to_build_file($CONFIG{local_m4}, $STATE)
61 unless defined $m4file;
62
63 if(exists $CONFIG{ttefmt} and defined $CONFIG{ttefmt}) {
64 my $ttefmt = uc $CONFIG{ttefmt};
65 add_cpp_defines(
66 "$ttefmt=1",
67 );
68 }
69
70 run_cpp($sfile, $cppfile);
71 run_m4($cppfile, $m4file);
72
73}
74
75##############################################################################
76
77sub get_includes {
78 my $include_hash = shift;
79
80 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
81
82 my %roothash = (
83 diagroot => $STATE->get_diag_root(),
84 startdir => $STATE->get_start_dir(),
85 builddir => $STATE->get_build_dir(),
86 abs => '/',
87 );
88
89 my @root_order = qw(builddir startdir diagroot abs);
90
91 my @incs;
92 foreach my $root (@root_order) {
93 next unless exists $include_hash->{$root};
94 fatal "Unknown include root $root in includes\n", M_DIR
95 unless exists $roothash{$root};
96
97 my $rootpath = $roothash{$root};
98 foreach my $dir (@{$include_hash->{$root}}) {
99 my $path = File::Spec->catdir($rootpath,
100 split /\//, $dir);
101 push @incs, $path;
102 }
103 }
104 return @incs;
105
106}
107
108##############################################################################
109
110sub get_cpp_includes {
111 return get_includes($CONFIG{cpp_includes});
112}
113
114##############################################################################
115
116sub get_m4_includes {
117 return get_includes($CONFIG{m4_includes});
118}
119
120##############################################################################
121
122# Use cpp to convert from assembly file to .cpp file
123
124##############################################################################
125
126sub run_cpp {
127 my $sfile = shift;
128 my $cppfile = shift;
129
130 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
131
132 $sfile = path_to_build_file($CONFIG{local_s}, $STATE)
133 unless defined $sfile;
134 $cppfile = path_to_build_file($CONFIG{local_cpp}, $STATE)
135 unless defined $cppfile;
136
137 local ($_);
138 # Don't compact path, b/c we want full path for standard includes
139 my @incs = map { "-I$_" } get_cpp_includes();
140 my @defs = map { "-D$_" } @{$CONFIG{'cpp_defines'}};
141
142 my $need_space = (scalar @incs and scalar @defs) ? ' ' : '';
143
144 my $mmu_argstring = '';
145 my @mmu_args = @{$STATE->get_mmu()->mmu_cpp_args()};
146 if(@mmu_args) {
147
148 $mmu_argstring .= ' ' . join ' ', @mmu_args;
149 }
150
151 my $arglist =
152 (join ' ', @{$CONFIG{cpp_opt}}) . ' ' .
153 (join ' ', @incs ) . $need_space .
154 (join ' ', @defs) . $mmu_argstring;
155 run_command("$CONFIG{cpp_cmd} $arglist $sfile > $cppfile",
156 -errcode => M_CPPFAIL);
157}
158
159##############################################################################
160
161# Take output of cpp and run through m4.
162
163##############################################################################
164
165sub run_m4 {
166 my $cppfile = shift;
167 my $m4file = shift;
168
169 my $pushd = Midas::Paths->pushd($STATE->get_build_dir);
170
171 $cppfile = path_to_build_file($CONFIG{local_cpp}, $STATE)
172 unless defined $cppfile;
173 $m4file = path_to_build_file($CONFIG{local_m4}, $STATE)
174 unless defined $m4file;
175
176 local ($_);
177 # Don't compact path, b/c we want full path for standard includes
178 my @incs = map { "--include=$_" } get_m4_includes();
179 my $arglist =
180 (join ' ', @{$CONFIG{m4_opt}}) . ' '.
181 (join ' ', @incs);
182 run_command("$CONFIG{m4_cmd} $arglist < $cppfile | $CONFIG{cat_cmd} --squeeze-blank > $m4file",
183 -errcode => M_M4FAIL
184 );
185
186}
187
188##############################################################################
1891;