]> ruderich.org/simon Gitweb - blhc/blhc.git/blobdiff - bin/blhc
Detect compile type (preprocess, compile, link) correctly.
[blhc/blhc.git] / bin / blhc
index bf5f8ff58dc1edddd4208c703ee8c702d808266a..3d5f73c054229f45146713f3a9a871142f79b047 100755 (executable)
--- a/bin/blhc
+++ b/bin/blhc
@@ -36,6 +36,81 @@ my $cc_regex = qr/(?:[a-z0-9_]+-(?:linux|kfreebsd)-gnu(?:eabi|eabihf)?-)?
 # Regex to catch (GCC) compiler warnings.
 my $warning_regex = qr/^(.+?):([0-9]+):[0-9]+: warning: (.+?) \[(.+?)\]$/;
 
+# Regex for source files which require preprocessing.
+my $source_preprocess_compile_regex = qr/
+    # C
+      c
+    # Objective-C
+    | m
+    # C++
+    | cc | cp | cxx | cpp | CPP | c\+\+ | C
+    # Objective-C++
+    | mm | M
+    # Fortran
+    | F | FOR | fpp | FPP | FTN | F90 | F95 | F03 | F08
+    /x;
+my $source_preprocess_no_compile_regex = qr/
+    # Assembly
+      s
+    /x;
+my $source_preprocess_regex = qr/
+      $source_preprocess_compile_regex
+    | $source_preprocess_no_compile_regex
+    /x;
+# Regex for source files which don't require preprocessing.
+my $source_no_preprocess_compile_regex = qr/
+    # C
+      i
+    # C++
+    | ii
+    # Objective-C
+    | mi
+    # Objective-C++
+    | mii
+    # Fortran
+    | f | for | ftn | f90 | f95 | f03 | f08
+    /x;
+my $source_no_preprocess_no_compile_regex = qr/
+    # Assembly
+      S | sx
+    /x;
+my $source_no_preprocess_regex = qr/
+      $source_no_preprocess_compile_regex
+    | $source_no_preprocess_no_compile_regex
+    /x;
+# Regex for header files which require preprocessing.
+my $header_preprocess_regex = qr/
+    # C, C++, Objective-C, Objective-C++
+      h
+    # C++
+    | hh | H | hp | hxx | hpp | HPP | h\+\+ | tcc
+    /x;
+# Regexps to match files with the given characteristics.
+my $file_no_preprocess_regex = qr/
+    $cc_regex.+?
+    \.(?: $source_no_preprocess_regex)\b
+    /x;
+my $file_preprocess_regex = qr/
+    $cc_regex.+?
+    \.(?: $header_preprocess_regex
+        | $source_preprocess_regex)\b
+    /x;
+my $file_compile_link_regex = qr/
+    $cc_regex.+?
+    \.(?: $source_preprocess_regex
+        | $source_no_preprocess_regex)\b
+    /x;
+my $file_compile_regex = qr/
+    $cc_regex.+?
+    \.(?: $source_preprocess_compile_regex
+        | $source_no_preprocess_compile_regex)\b
+    /x;
+my $file_no_compile_regex = qr/
+    $cc_regex.+
+    \.(?: $source_preprocess_no_compile_regex
+        | $source_no_preprocess_no_compile_regex)\b
+    /x;
+
 # Expected (hardening) flags. All flags are used as regexps.
 my @cflags = (
     '-g',
@@ -443,47 +518,60 @@ for (my $i = 0; $i < scalar @input; $i++) {
     # Even if it's a verbose build, we might have to skip this line.
     next if $skip;
 
+    # Skip unnecessary tests when only preprocessing.
+    my $flag_preprocess = 0;
+
+    my $preprocess = 0;
+    my $compile    = 0;
+    my $link       = 0;
 
-    # Is this a compiler or linker command?
-    my $compiler = 1;
-    my $linker   = 0;
-
-    # Linker commands.
-    if ($line =~ m{\s-o                        # -o
-                   [\s\\]*\s+                  # possible line continuation
-                   (?:[/.A-Za-z0-9~_-]+/)?     # path to file
-                        [A-Za-z0-9~_-]+        # binary name (no dots!)
-                   (?:[0-9.]*\.so[0-9.]*[a-z]? # library (including version)
-                    |\.la
-                    |\.cgi)?                   # CGI binary
-                   (?:\s|\\|$)                 # end of file name
-                  }x
-            or $line =~ /^libtool: link: /
-            or $line =~ m{\s*/bin/bash .+?libtool\s+(.+?\s+)?--mode=(re)?link}) {
-        $compiler = 0;
-        $linker   = 1;
+    # Preprocess, compile, assemble.
+    if ($line =~ /$cc_regex.*?\s(-E|-S|-c)\b/) {
+        $preprocess      = 1;
+        $flag_preprocess = 1 if $1 eq '-E';
+        $compile         = 1 if $1 eq '-S' or $1 eq '-c';
+    # Otherwise assume we are linking.
+    } else {
+        $link = 1;
+    }
+
+    # These file types don't require preprocessing.
+    if ($line =~ /$file_no_preprocess_regex/) {
+        $preprocess = 0;
+    }
+    # These file types require preprocessing.
+    if ($line =~ /$file_preprocess_regex/) {
+        $preprocess = 1;
     }
 
     # If there are source files then it's compiling/linking in one step and we
-    # must check both.
-    if ($line =~ /\.(?:c|cc|cpp)\b/) {
-        $compiler = 1;
+    # must check both. We only check for source files here, because header
+    # files cause too many false positives.
+    if (not $flag_preprocess and $line =~ /$file_compile_link_regex/) {
+        # Assembly files don't need CFLAGS.
+        if (not $line =~ /$file_compile_regex/
+                and $line =~ /$file_no_compile_regex/) {
+            $compile = 0;
+        # But the rest does.
+        } else {
+            $compile = 1;
+        }
     }
 
     # Check hardening flags.
     my @missing;
-    if ($compiler and not all_flags_used($line, \@missing, @cflags)
+    if ($compile and not all_flags_used($line, \@missing, @cflags)
             # Libraries linked with -fPIC don't have to (and can't) be linked
             # with -fPIE as well. It's no error if only PIE flags are missing.
             and not pic_pie_conflict($line, $harden_pie, \@missing, @cflags_pie)) {
         error_flags('CFLAGS missing', \@missing, \%flag_renames, $line);
         $exit |= 1 << 3;
     }
-    if ($compiler and not all_flags_used($line, \@missing, @cppflags)) {
+    if ($preprocess and not all_flags_used($line, \@missing, @cppflags)) {
         error_flags('CPPFLAGS missing', \@missing, \%flag_renames, $line);
         $exit |= 1 << 3;
     }
-    if ($linker and not all_flags_used($line, \@missing, @ldflags)
+    if ($link and not all_flags_used($line, \@missing, @ldflags)
             # Same here, -fPIC conflicts with -fPIE.
             and not pic_pie_conflict($line, $harden_pie, \@missing, @ldflags_pie)) {
         error_flags('LDFLAGS missing', \@missing, \%flag_renames, $line);