| 1 | # -*- perl -*- |
| 2 | |
| 3 | package Midas::Source; |
| 4 | |
| 5 | use Midas::Command; |
| 6 | use Midas::Globals; |
| 7 | use Midas::Paths; |
| 8 | use Midas::Error; |
| 9 | |
| 10 | use File::Spec; |
| 11 | use File::Copy; |
| 12 | use File::Basename; |
| 13 | |
| 14 | |
| 15 | use strict; |
| 16 | |
| 17 | use fields qw(ofile args fullsource); |
| 18 | |
| 19 | ############################################################################### |
| 20 | |
| 21 | sub new { |
| 22 | my $class = shift; |
| 23 | my %args = @_; |
| 24 | |
| 25 | my $this = fields::new($class); |
| 26 | foreach my $key (keys %args) { |
| 27 | $this->{$key} = $args{$key}; |
| 28 | } |
| 29 | |
| 30 | return $this; |
| 31 | } |
| 32 | |
| 33 | ############################################################################### |
| 34 | |
| 35 | sub set_defaults { |
| 36 | my $this = shift; |
| 37 | |
| 38 | } |
| 39 | |
| 40 | ############################################################################### |
| 41 | |
| 42 | sub get_full_source_file { |
| 43 | my $this = shift; |
| 44 | return $this->{fullsource}; |
| 45 | } |
| 46 | |
| 47 | ############################################################################### |
| 48 | |
| 49 | sub get_source_file { |
| 50 | my $this = shift; |
| 51 | return basename $this->get_full_source_file(); |
| 52 | } |
| 53 | |
| 54 | ############################################################################### |
| 55 | |
| 56 | sub get_object_file { |
| 57 | my $this = shift; |
| 58 | return $this->{ofile}; |
| 59 | } |
| 60 | |
| 61 | ############################################################################### |
| 62 | |
| 63 | sub is_library { |
| 64 | my $this = shift; |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | ############################################################################### |
| 69 | |
| 70 | sub from_diag_source { |
| 71 | my $this = shift; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | ############################################################################### |
| 76 | |
| 77 | sub copy_to_build_dir { |
| 78 | my $this = shift; |
| 79 | |
| 80 | fatal "Cannot copy_to_build_dir of file generated from diag source\n", M_FILE |
| 81 | if $this->from_diag_source(); |
| 82 | |
| 83 | my @search = $this->get_search_paths(); |
| 84 | my $full_source = $this->get_full_source_file(); |
| 85 | my $local_source = path_to_build_file($this->get_source_file(), $STATE); |
| 86 | |
| 87 | foreach my $testdir (@search) { |
| 88 | my $testfile = File::Spec->catfile($testdir, $full_source); |
| 89 | if(-e $testfile) { |
| 90 | chat "Copying $testfile to $local_source\n"; |
| 91 | copy($testfile, $local_source); |
| 92 | last; |
| 93 | } |
| 94 | } |
| 95 | if(not -e $local_source) { |
| 96 | fatal "Could not find \"$full_source\" in search path: @search\n", M_FILE; |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
| 101 | ############################################################################### |
| 102 | |
| 103 | sub get_search_paths { |
| 104 | my $this = shift; |
| 105 | fatal "Cannot get_search_paths of abstract superclass\n", M_CODE; |
| 106 | } |
| 107 | |
| 108 | ############################################################################### |
| 109 | |
| 110 | sub process_source { |
| 111 | my $this = shift; |
| 112 | } |
| 113 | |
| 114 | ############################################################################### |
| 115 | |
| 116 | sub build { |
| 117 | my $this = shift; |
| 118 | fatal "Cannot build source file in abstract superclass\n", M_CODE; |
| 119 | } |
| 120 | |
| 121 | ############################################################################### |
| 122 | |
| 123 | sub debug_string { |
| 124 | my $this = shift; |
| 125 | |
| 126 | my $str = "O=$this->{ofile} "; |
| 127 | $str .= "ARGS=\"$this->{args}\" " if defined $this->{args}; |
| 128 | |
| 129 | return $str; |
| 130 | } |
| 131 | ############################################################################### |
| 132 | ############################################################################### |
| 133 | |
| 134 | package Midas::Source::Assembly; |
| 135 | |
| 136 | use strict; |
| 137 | |
| 138 | use Midas::Paths; |
| 139 | use Midas::Globals; |
| 140 | use Midas::Command; |
| 141 | use Midas::Error; |
| 142 | |
| 143 | use base 'Midas::Source'; |
| 144 | use fields qw(sfile); |
| 145 | |
| 146 | ############################################################################### |
| 147 | |
| 148 | sub new { |
| 149 | my $class = shift; |
| 150 | my %args = @_; |
| 151 | |
| 152 | my $this = fields::new($class); |
| 153 | foreach my $key (keys %args) { |
| 154 | $this->{$key} = $args{$key}; |
| 155 | } |
| 156 | |
| 157 | return $this; |
| 158 | } |
| 159 | |
| 160 | ############################################################################### |
| 161 | |
| 162 | sub set_defaults { |
| 163 | my $this = shift; |
| 164 | $this->SUPER::set_defaults(); |
| 165 | } |
| 166 | |
| 167 | ############################################################################### |
| 168 | |
| 169 | sub get_search_paths { |
| 170 | my $this = shift; |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | ############################################################################### |
| 175 | |
| 176 | sub from_diag_source { |
| 177 | my $this = shift; |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | ############################################################################### |
| 182 | |
| 183 | sub debug_string { |
| 184 | my $this = shift; |
| 185 | |
| 186 | my $str = "ASM: S=$this->{sfile} "; |
| 187 | $str .= $this->SUPER::debug_string(); |
| 188 | return $str; |
| 189 | } |
| 190 | |
| 191 | ############################################################################### |
| 192 | |
| 193 | # This function may be called from a child thread, sot it is important |
| 194 | # that it doesn't write any state (i.e., have any side-effects) |
| 195 | sub build { |
| 196 | my $this = shift; |
| 197 | my $pushd = Midas::Paths->pushd($STATE->get_build_dir); |
| 198 | |
| 199 | my $source = $this->get_source_file(); |
| 200 | my $cmd = "$CONFIG{as_cmd} "; |
| 201 | $cmd .= "$this->{args} " if defined $this->{args}; |
| 202 | $cmd .= join ' ', @{$CONFIG{as_opt}}; |
| 203 | $cmd .= " $source -o $this->{ofile}"; |
| 204 | |
| 205 | run_command($cmd, -errcode => M_ASMFAIL); |
| 206 | } |
| 207 | |
| 208 | ############################################################################### |
| 209 | ############################################################################### |
| 210 | |
| 211 | package Midas::Source::C; |
| 212 | |
| 213 | use strict; |
| 214 | |
| 215 | use Midas::Globals; |
| 216 | use Midas::Preprocess ':all'; |
| 217 | use Midas::Command; |
| 218 | use Midas::Paths; |
| 219 | use Midas::Error; |
| 220 | |
| 221 | |
| 222 | use base 'Midas::Source'; |
| 223 | use fields qw(cfile sfile); |
| 224 | |
| 225 | ############################################################################### |
| 226 | |
| 227 | sub new { |
| 228 | my $class = shift; |
| 229 | my %args = @_; |
| 230 | |
| 231 | my $this = fields::new($class); |
| 232 | foreach my $key (keys %args) { |
| 233 | $this->{$key} = $args{$key}; |
| 234 | } |
| 235 | |
| 236 | return $this; |
| 237 | } |
| 238 | |
| 239 | ############################################################################### |
| 240 | |
| 241 | sub set_defaults { |
| 242 | my $this = shift; |
| 243 | |
| 244 | $this->SUPER::set_defaults(); |
| 245 | } |
| 246 | |
| 247 | ############################################################################### |
| 248 | |
| 249 | sub get_search_paths { |
| 250 | my $this = shift; |
| 251 | return get_includes($CONFIG{csrc_includes}); |
| 252 | } |
| 253 | |
| 254 | ############################################################################### |
| 255 | |
| 256 | sub process_source { |
| 257 | my $this = shift; |
| 258 | # Run gcc -S if assembly |
| 259 | |
| 260 | if(defined $this->{sfile}) { |
| 261 | my $cmd = $CONFIG{gcc_cmd}; |
| 262 | my $gccopt = join ' ', @{$CONFIG{gcc_opt}}; |
| 263 | |
| 264 | my @incs = map { "-I$_" } get_includes($CONFIG{c_includes}); |
| 265 | |
| 266 | # Don't use cfile directly, since it may contain path seperator. |
| 267 | my $c_file = $this->get_source_file(); |
| 268 | |
| 269 | $cmd .= " $gccopt @incs " if defined $gccopt; |
| 270 | $cmd .= " $this->{args} " if defined $this->{args}; |
| 271 | $cmd .= " $c_file -o $this->{sfile}"; |
| 272 | |
| 273 | $cmd =~ s/\s+/ /g; |
| 274 | |
| 275 | chat "Compiling C code $c_file to generate $this->{sfile}\n"; |
| 276 | run_command($cmd, -errcode => M_CCFAIL); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | ############################################################################### |
| 281 | |
| 282 | # This function may be called from a child thread, sot it is important |
| 283 | # that it doesn't write any state (i.e., have any side-effects) |
| 284 | sub build { |
| 285 | my $this = shift; |
| 286 | my $pushd = Midas::Paths->pushd($STATE->get_build_dir); |
| 287 | |
| 288 | if(defined $this->{sfile}) { |
| 289 | # Already compiled to assembly - now just assemble |
| 290 | |
| 291 | my $args = join ' ', @{$CONFIG{as_opt}}; |
| 292 | run_command("$CONFIG{as_cmd} $args $this->{sfile} -o $this->{ofile}", |
| 293 | -errcode => M_ASMFAIL); |
| 294 | |
| 295 | } else { |
| 296 | # Need to compile from .c |
| 297 | |
| 298 | my $cfile = $this->get_source_file(); |
| 299 | |
| 300 | my $cmd = $CONFIG{gcc_cmd}; |
| 301 | my $gccopt = join ' ', @{$CONFIG{gcc_opt}}; |
| 302 | my @incs = map { "-I$_" } get_includes($CONFIG{c_includes}); |
| 303 | $cmd .= " $gccopt @incs " if defined $gccopt; |
| 304 | $cmd .= " -c $cfile -o $this->{ofile}"; |
| 305 | $cmd =~ s/\s+/ /g; |
| 306 | run_command($cmd, -errcode => M_CCFAIL); |
| 307 | } |
| 308 | |
| 309 | } |
| 310 | |
| 311 | ############################################################################### |
| 312 | |
| 313 | sub debug_string { |
| 314 | my $this = shift; |
| 315 | |
| 316 | my $str = "C: C=$this->{cfile} "; |
| 317 | $str .= "S=$this->{sfile} " if defined $this->{sfile}; |
| 318 | $str .= $this->SUPER::debug_string(); |
| 319 | return $str; |
| 320 | } |
| 321 | |
| 322 | ############################################################################### |
| 323 | ############################################################################### |
| 324 | |
| 325 | package Midas::Source::Object; |
| 326 | |
| 327 | use strict; |
| 328 | |
| 329 | use Midas::Globals; |
| 330 | use Midas::Preprocess ':all'; |
| 331 | |
| 332 | |
| 333 | use base 'Midas::Source'; |
| 334 | use fields qw(); |
| 335 | |
| 336 | ############################################################################### |
| 337 | |
| 338 | sub new { |
| 339 | my $class = shift; |
| 340 | my %args = @_; |
| 341 | |
| 342 | my $this = fields::new($class); |
| 343 | foreach my $key (keys %args) { |
| 344 | $this->{$key} = $args{$key}; |
| 345 | } |
| 346 | |
| 347 | return $this; |
| 348 | } |
| 349 | |
| 350 | ############################################################################### |
| 351 | |
| 352 | sub set_defaults { |
| 353 | my $this = shift; |
| 354 | |
| 355 | $this->SUPER::set_defaults(); |
| 356 | } |
| 357 | |
| 358 | ############################################################################### |
| 359 | |
| 360 | sub build { |
| 361 | my $this = shift; |
| 362 | } |
| 363 | |
| 364 | ############################################################################### |
| 365 | |
| 366 | sub get_search_paths { |
| 367 | my $this = shift; |
| 368 | return get_includes($CONFIG{link_paths}); |
| 369 | } |
| 370 | |
| 371 | ############################################################################### |
| 372 | |
| 373 | sub debug_string { |
| 374 | my $this = shift; |
| 375 | my $str = "O: "; |
| 376 | $str .= $this->SUPER::debug_string(); |
| 377 | return $str; |
| 378 | } |
| 379 | |
| 380 | ############################################################################### |
| 381 | ############################################################################### |
| 382 | |
| 383 | package Midas::Source::Library; |
| 384 | |
| 385 | use strict; |
| 386 | |
| 387 | use Midas::Globals; |
| 388 | use Midas::Preprocess ':all'; |
| 389 | |
| 390 | |
| 391 | use base 'Midas::Source'; |
| 392 | use fields qw(); |
| 393 | |
| 394 | ############################################################################### |
| 395 | |
| 396 | sub new { |
| 397 | my $class = shift; |
| 398 | my %args = @_; |
| 399 | |
| 400 | my $this = fields::new($class); |
| 401 | foreach my $key (keys %args) { |
| 402 | $this->{$key} = $args{$key}; |
| 403 | } |
| 404 | |
| 405 | return $this; |
| 406 | } |
| 407 | |
| 408 | ############################################################################### |
| 409 | |
| 410 | sub set_defaults { |
| 411 | my $this = shift; |
| 412 | |
| 413 | $this->SUPER::set_defaults(); |
| 414 | } |
| 415 | |
| 416 | ############################################################################### |
| 417 | |
| 418 | sub build { |
| 419 | my $this = shift; |
| 420 | } |
| 421 | |
| 422 | ############################################################################### |
| 423 | |
| 424 | sub get_search_paths { |
| 425 | my $this = shift; |
| 426 | |
| 427 | return get_includes($CONFIG{link_paths}); |
| 428 | } |
| 429 | |
| 430 | ############################################################################### |
| 431 | |
| 432 | sub debug_string { |
| 433 | my $this = shift; |
| 434 | my $str = "L: "; |
| 435 | $str .= $this->SUPER::debug_string(); |
| 436 | return $str; |
| 437 | } |
| 438 | |
| 439 | ############################################################################### |
| 440 | |
| 441 | sub is_library { |
| 442 | my $this = shift; |
| 443 | return 1; # libraries need to link after everything else |
| 444 | } |
| 445 | |
| 446 | ############################################################################### |
| 447 | ############################################################################### |
| 448 | 1; |