Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | # -*- perl -*- |
2 | ||
3 | package Midas::Preprocess; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | use IO::File; | |
8 | use File::Temp 'tempfile'; | |
9 | use File::Copy; | |
10 | ||
11 | use Midas::Command; | |
12 | use Midas::Paths; | |
13 | use Midas::Section; | |
14 | use Midas::MMU; | |
15 | use Midas::Configure; | |
16 | use Midas::State; | |
17 | use Midas::Globals; | |
18 | use Midas::Error; | |
19 | ||
20 | require Exporter; | |
21 | ||
22 | our @ISA = qw(Exporter); | |
23 | our @EXPORT = qw(preprocess); | |
24 | our @EXPORT_OK = qw(); | |
25 | our %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 | ||
43 | Exporter::export_ok_tags('all', 'internals'); | |
44 | ||
45 | ############################################################################## | |
46 | ||
47 | # Perform both cpp and m4 preprocessing | |
48 | ||
49 | ############################################################################## | |
50 | ||
51 | sub 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 | ||
77 | sub 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 | ||
110 | sub get_cpp_includes { | |
111 | return get_includes($CONFIG{cpp_includes}); | |
112 | } | |
113 | ||
114 | ############################################################################## | |
115 | ||
116 | sub 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 | ||
126 | sub 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 | ||
165 | sub 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 | ############################################################################## | |
189 | 1; |