| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | sub main() { |
|---|
| 7 | my $detectedVersion = 'failed_to_detect_compiler'; |
|---|
| 8 | my $detectedCompiler = 'unknown'; |
|---|
| 9 | |
|---|
| 10 | my $args = scalar(@ARGV); |
|---|
| 11 | if ($args != 1) { |
|---|
| 12 | print STDERR 'Usage: arb_compiler_version.pl ${CXX}'."\n"; |
|---|
| 13 | $detectedVersion .= '__missing_arg_to__arb_compiler_version'; |
|---|
| 14 | } |
|---|
| 15 | else { |
|---|
| 16 | my $compiler = $ARGV[0]; |
|---|
| 17 | $detectedVersion = 'unknown_compiler_version'; |
|---|
| 18 | |
|---|
| 19 | my $dumpedVersion = undef; |
|---|
| 20 | my $detailedVersion = undef; |
|---|
| 21 | my $undetectable = 'undetectable (via arg="'.$compiler.'")'; |
|---|
| 22 | |
|---|
| 23 | $dumpedVersion = `$compiler -dumpversion`; |
|---|
| 24 | $detailedVersion = `$compiler --version`; |
|---|
| 25 | |
|---|
| 26 | if (not defined $dumpedVersion) { $dumpedVersion = $undetectable; } |
|---|
| 27 | if (not defined $detailedVersion) { $detailedVersion = $undetectable; } |
|---|
| 28 | |
|---|
| 29 | chomp($dumpedVersion); |
|---|
| 30 | chomp($detailedVersion); |
|---|
| 31 | |
|---|
| 32 | my $cmd = "$compiler -dM -E -x c /dev/null"; |
|---|
| 33 | if (open(CMD,$cmd.'|')) { |
|---|
| 34 | LINE: foreach (<CMD>) { |
|---|
| 35 | if (/__GNUC__/) { |
|---|
| 36 | $detectedCompiler = 'gcc'; |
|---|
| 37 | # clang also defines __GNUC__ so don't "last" here |
|---|
| 38 | } |
|---|
| 39 | elsif (/__clang__/) { |
|---|
| 40 | $detectedCompiler = 'clang'; |
|---|
| 41 | last LINE; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | close(CMD); |
|---|
| 45 | } |
|---|
| 46 | else { |
|---|
| 47 | print STDERR "failed to execute '$cmd'"; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | if ($detectedCompiler eq 'unknown') { |
|---|
| 51 | print STDERR "Problems detecting compiler type:\n"; |
|---|
| 52 | print STDERR "dumpedVersion='$dumpedVersion'\n"; |
|---|
| 53 | print STDERR "detailedVersion='$detailedVersion'\n"; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | { |
|---|
| 57 | my $dv_from_dumped = undef; |
|---|
| 58 | my $dv_from_detail = undef; |
|---|
| 59 | |
|---|
| 60 | if ($dumpedVersion =~ /^([0-9]+(?:\.[0-9]+)+)$/) { $dv_from_dumped = $dumpedVersion; } |
|---|
| 61 | if ($detailedVersion =~ /\s([0-9]+(?:\.[0-9]+)+)\s/) { $dv_from_detail = $1; } |
|---|
| 62 | |
|---|
| 63 | if (defined $dv_from_dumped) { |
|---|
| 64 | $detectedVersion = |
|---|
| 65 | ((defined $dv_from_detail) and (length($dv_from_detail)>length($dv_from_dumped))) |
|---|
| 66 | ? $dv_from_detail |
|---|
| 67 | : $dv_from_dumped; |
|---|
| 68 | } |
|---|
| 69 | else { |
|---|
| 70 | if (defined $dv_from_detail) { $detectedVersion = $dv_from_detail; } |
|---|
| 71 | else { |
|---|
| 72 | print STDERR "Problems detecting compiler version:\n"; |
|---|
| 73 | print STDERR "dumpedVersion='$dumpedVersion'\n"; |
|---|
| 74 | print STDERR "detailedVersion='$detailedVersion'\n"; |
|---|
| 75 | print STDERR "dv_from_dumped='$dv_from_dumped'\n"; |
|---|
| 76 | print STDERR "dv_from_detail='$dv_from_detail'\n"; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | chomp($detectedVersion); |
|---|
| 83 | chomp($detectedCompiler); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | my $result = $detectedCompiler." ".$detectedVersion; |
|---|
| 87 | print $result."\n"; |
|---|
| 88 | } |
|---|
| 89 | main(); |
|---|