Perl Script To Remove Unnecessary Headers
Here is the perl script that I wrote to scan through our source code and remove unnecessary headers. You call it with the parameters solutionfile (path to .sln file to build), configname (name of a config, like Debug|Win32), and rootpath (root directory containing .c files to modify). It will then iterate over everything in the rootpath and comment out any headers that are not required. If you’re not using IncrediBuild, you’ll have to modify the path and the command line arguments that it calls for the build checking step.
#!perl
use strict;
use File::Find;
#
# Description: Attempts to fix headers in source files by commenting
out unused ones
#
my $solution = $ARGV[0];
my $config = $ARGV[1];
my $basedir = $ARGV[2];
my $buildconsoleexe;
my $ignoreheader = "//IGNORED_HEADER ";
my $checkedheader = " //CHECKED_HEADER";
my @buildconsolepaths = ("C:/Program Files/Xoreax/Incredibuild/BuildConsole.exe",
"C:/Program Files (X86)/Xoreax/Incredibuild/BuildConsole.exe",
"D:/Program Files/Xoreax/Incredibuild/BuildConsole.exe");
if (!@ARGV or $#ARGV != 2)
{
die "Not enough arguments\nFORMAT FixHeaders solutionfile configname rootdirectory\n";
}
foreach my $path (@buildconsolepaths)
{
if (-e $path)
{
$buildconsoleexe = $path;
last;
}
}
if (!$buildconsoleexe)
{
die "BuildConsole is not installed!\n";
}
sub wanted
{
my $old = $File::Find::name;
my $new = "tempfile.c";
my $lastline = -1;
if (not $old =~ /AutoGen/ and $old =~ /(\w+)\.c$/ )
{
my $basename = $1;
print "Processing $old, basename $basename\n";
while (1)
{
my $currentline = 0;
open(OLD, "< $old") or die "can't open $old: $!";
open(NEW, "> $new") or die "can't open $new: $!";
while (<OLD>)
{
if ($currentline == $lastline)
{
s/$ignoreheader//;
chomp;
print NEW $_, $checkedheader, "\n";
$lastline = -1;
}
elsif (/^\#include.*\"(.*)\"/ and not /$checkedheader/ and $lastline == -1)
{
my $included = $1;
if ($included =~ /$basename\.h/ or $included =~ /\.c$/)
{
print NEW $_;
}
else
{
print NEW $ignoreheader, $_;
$lastline = $currentline;
}
}
else
{
print NEW $_;
}
$currentline++;
}
close(OLD) or die "can't close $old: $!";
close(NEW) or die "can't close $new: $!";
rename($old, "$old.orig") or die "can't rename $old to $old.orig: $!";
rename($new, $old) or die "can't rename $new to $old: $!";
if ($lastline != -1)
{
if ((system $buildconsoleexe, "$solution","/build","/cfg=\"$config\"") == 0)
{
# it succeeded, let it stay ignored
$lastline = -1;
}
}
else
{
last;
}
}
}
}
find( {wanted=> \&wanted, no_chdir => 1}, $basedir );
Optimizing Build Times for Large C Projects « Double Buffered said
[...] Perl Script To Remove Unnecessary Headers [...]
Michael said
Your algorithm is slightly off. You can have something like
#include “a.h”
#include “b.h”
#include “c.h”
where b depends on a, and c depends on b, and nothing depends on c. You should really start at the bottom and work your way up, rather than at the top and work your way down.