+# CONSTANTS/VARIABLES
+
+# Regex to catch compiler commands.
+my $cc_regex = qr/
+ (?<!\.)(?:cc|gcc|g\+\+|c\+\+)
+ (?:-[\d.]+)?
+ /x;
+# Full regex which matches the complete compiler name. Used in a few places to
+# prevent false negatives.
+my $cc_regex_full = qr/
+ (?:[a-z0-9_]+-(?:linux-|kfreebsd-)?gnu(?:eabi|eabihf)?-)?
+ $cc_regex
+ /x;
+# Regex to catch (GCC) compiler warnings.
+my $warning_regex = qr/^(.+?):([0-9]+):[0-9]+: warning: (.+?) \[(.+?)\]$/;
+
+# List of source file extensions which require preprocessing.
+my @source_preprocess_compile_cpp = (
+ # C++
+ qw( cc cp cxx cpp CPP c++ C ),
+ # Objective-C++
+ qw( mm Mr),
+);
+my @source_preprocess_compile = (
+ # C
+ qw( c ),
+ # Objective-C
+ qw( m ),
+ # (Objective-)C++
+ @source_preprocess_compile_cpp,
+ # Fortran
+ qw( F FOR fpp FPP FTN F90 F95 F03 F08 ),
+);
+my @source_preprocess_no_compile = (
+ # Assembly
+ qw( s ),
+);
+my @source_preprocess = (
+ @source_preprocess_compile,
+ @source_preprocess_no_compile,
+);
+# List of source file extensions which don't require preprocessing.
+my @source_no_preprocess_compile_cpp = (
+ # C++
+ qw( ii ),
+ # Objective-C++
+ qw( mii ),
+);
+my @source_no_preprocess_compile = (
+ # C
+ qw( i ),
+ # (Objective-)C++
+ @source_no_preprocess_compile_cpp,
+ # Objective-C
+ qw( mi ),
+ # Fortran
+ qw( f for ftn f90 f95 f03 f08 ),
+);
+my @source_no_preprocess_no_compile = (
+ # Assembly
+ qw( S sx ),
+);
+my @source_no_preprocess = (
+ @source_no_preprocess_compile,
+ @source_no_preprocess_no_compile,
+);
+# List of header file extensions which require preprocessing.
+my @header_preprocess = (
+ # C, C++, Objective-C, Objective-C++
+ qw( h ),
+ # C++
+ qw( hh H hp hxx hpp HPP h++ tcc ),
+);
+
+# Hashes for fast extensions lookup to check if a file falls in one of these
+# categories.
+my %extensions_no_preprocess = map { $_ => 1 } (
+ @source_no_preprocess,
+);
+my %extensions_preprocess = map { $_ => 1 } (
+ @header_preprocess,
+ @source_preprocess,
+);
+my %extensions_compile_link = map { $_ => 1 } (
+ @source_preprocess,
+ @source_no_preprocess,
+);
+my %extensions_compile = map { $_ => 1 } (
+ @source_preprocess_compile,
+ @source_no_preprocess_compile,
+);
+my %extensions_no_compile = map { $_ => 1 } (
+ @source_preprocess_no_compile,
+ @source_no_preprocess_no_compile,
+);
+my %extensions_compile_cpp = map { $_ => 1 } (
+ @source_preprocess_compile_cpp,
+ @source_no_preprocess_compile_cpp,
+);
+my %extension = map { $_ => 1 } (
+ @source_no_preprocess,
+ @source_no_preprocess_compile,
+ @source_no_preprocess_compile_cpp,
+ @source_no_preprocess_no_compile,
+ @header_preprocess,
+ @source_preprocess,
+ @source_preprocess_compile,
+ @source_preprocess_compile_cpp,
+ @source_preprocess_no_compile,
+);
+
+# Regexp to match file extensions.
+my $file_extension_regex = qr/
+ \s
+ \S+ # Filename without extension.
+ \.
+ ([^\\.\s]+) # File extension.
+ (?=\s|\\) # At end of word. Can't use \b because some files have non
+ # word characters at the end and because \b matches double
+ # extensions (like .cpp.o). Works always as all lines are
+ # terminated with "\n".
+ /x;
+
+# Expected (hardening) flags. All flags are used as regexps.
+my @def_cflags = (
+ '-g',
+ '-O(?:2|3)',
+);
+my @def_cflags_format = (
+ '-Wformat',
+ '-Wformat-security',
+ '-Werror=format-security',
+);
+my @def_cflags_fortify = (
+ # fortify needs at least -O1, but -O2 is recommended anyway
+);
+my @def_cflags_stack = (
+ '-fstack-protector',
+ '--param=ssp-buffer-size=4',
+);
+my @def_cflags_pie = (
+ '-fPIE',
+);
+my @def_cxxflags = (
+ @def_cflags,
+);
+# @def_cxxflags_* is the same as @def_cflags_*.
+my @def_cppflags = ();
+my @def_cppflags_fortify = (
+ '-D_FORTIFY_SOURCE=2',
+);
+my @def_ldflags = ();
+my @def_ldflags_relro = (
+ '-Wl,(-z,)?relro',
+);
+my @def_ldflags_bindnow = (
+ '-Wl,(-z,)?now',
+);
+my @def_ldflags_pie = (
+ '-fPIE',
+ '-pie',
+);
+my @def_ldflags_pic = (
+ '-fPIC',
+ '-fpic',
+);
+# Renaming rules for the output so the regex parts are not visible. Also
+# stores string values of flag regexps above, see compile_flag_regexp().
+my %flag_renames = (
+ '-O(?:2|3)' => '-O2',
+ '-Wl,(-z,)?relro' => '-Wl,-z,relro',
+ '-Wl,(-z,)?now' => '-Wl,-z,now',
+);
+
+# Statistics of missing flags and non-verbose build commands. Used for
+# $option_buildd.
+my %statistics = (
+ preprocess => 0,
+ preprocess_missing => 0,
+ compile => 0,
+ compile_missing => 0,
+ compile_cpp => 0,
+ compile_cpp_missing => 0,
+ link => 0,
+ link_missing => 0,
+ commands => 0,
+ commands_nonverbose => 0,
+);
+
+# Use colored (ANSI) output?
+my $option_color;
+
+