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) || print STDERR "failed to execute '$cmd' (Reason: $! exitcode=$?)\n"; |
---|
45 | } |
---|
46 | else { |
---|
47 | print STDERR "failed to fork '$cmd' (Reason: $!)\n"; |
---|
48 | } |
---|
49 | |
---|
50 | if ($detectedCompiler eq 'unknown') { |
---|
51 | print STDERR "\n"; |
---|
52 | print STDERR "Problems detecting compiler type:\n"; |
---|
53 | print STDERR "dumpedVersion='$dumpedVersion'\n"; |
---|
54 | print STDERR "detailedVersion='$detailedVersion'\n"; |
---|
55 | } |
---|
56 | |
---|
57 | { |
---|
58 | my $dv_from_dumped = undef; |
---|
59 | my $dv_from_detail = undef; |
---|
60 | |
---|
61 | if ($dumpedVersion =~ /^([0-9]+(?:\.[0-9]+)+)$/) { $dv_from_dumped = $dumpedVersion; } |
---|
62 | if ($detailedVersion =~ /\s([0-9]+(?:\.[0-9]+)+)\s/) { $dv_from_detail = $1; } |
---|
63 | |
---|
64 | if (defined $dv_from_dumped) { |
---|
65 | $detectedVersion = |
---|
66 | ((defined $dv_from_detail) and (length($dv_from_detail)>length($dv_from_dumped))) |
---|
67 | ? $dv_from_detail |
---|
68 | : $dv_from_dumped; |
---|
69 | } |
---|
70 | else { |
---|
71 | if (defined $dv_from_detail) { $detectedVersion = $dv_from_detail; } |
---|
72 | else { |
---|
73 | print STDERR "\n"; |
---|
74 | print STDERR "Problems detecting compiler version:\n"; |
---|
75 | print STDERR "dumpedVersion='$dumpedVersion'\n"; |
---|
76 | print STDERR "detailedVersion='$detailedVersion'\n"; |
---|
77 | if (defined $dv_from_dumped) { print STDERR "dv_from_dumped='$dv_from_dumped'\n"; } |
---|
78 | if (defined $dv_from_detail) { print STDERR "dv_from_detail='$dv_from_detail'\n"; } |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | chomp($detectedVersion); |
---|
84 | chomp($detectedCompiler); |
---|
85 | |
---|
86 | if ($detectedCompiler eq 'gcc') { |
---|
87 | # SuSE uses fake-gcc-release numbers (e.g. gcc 7.4.1) |
---|
88 | # acceptSubVersions: |
---|
89 | if ($detectedVersion =~ /^([0-9]+)\.([0-9])\.([0-9])$/o) { |
---|
90 | my ($maj,$min,$sub) = ($1,$2,$3); |
---|
91 | if ($maj>=5) { |
---|
92 | # since gcc 5 series the release versions always have subversion==0 |
---|
93 | if ($sub>0) { |
---|
94 | my $baseVersion = "$maj.$min.0"; |
---|
95 | print STDERR "Detected compiler version >= 5 using non-zero subversion ($detectedVersion)\n"; |
---|
96 | print STDERR "Assuming this is a bugfix or vendor-specific release of version '$baseVersion'\n"; |
---|
97 | $detectedVersion = $baseVersion; |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | my $result = $detectedCompiler." ".$detectedVersion; |
---|
105 | print $result."\n"; |
---|
106 | } |
---|
107 | main(); |
---|