Skip to content

Instantly share code, notes, and snippets.

@Zaxuhe
Created July 18, 2017 19:39
Show Gist options
  • Save Zaxuhe/12611b8453b735ebc54a1fd20601773f to your computer and use it in GitHub Desktop.
Save Zaxuhe/12611b8453b735ebc54a1fd20601773f to your computer and use it in GitHub Desktop.
Remove unused includes
#!/bin/bash
# prune include files one at a time, recompile, and put them back if it doesn't compile
# arguments are list of files to check
removeinclude() {
file=$1
header=$2
perl -i -p -e 's+([ \t]*#include[ \t][ \t]*[\"\<]'$2'[\"\>])+//REMOVEINCLUDE $1+' $1
}
replaceinclude() {
file=$1
perl -i -p -e 's+//REMOVEINCLUDE ++' $1
}
for file in `find ./src -name "*"`
do
includes=`grep "^[ \t]*#include" $file | awk '{print $2;}' | sed 's/[\"\<\>]//g'`
echo $includes
for i in $includes
do
touch $file # just to be sure it recompiles
removeinclude $file $i
make clean >/dev/null 2>&1
if make -j10 >/dev/null 2>&1;
then
grep -v REMOVEINCLUDE $file > tmp && mv tmp $file
echo removed $i from $file
else
replaceinclude $file
echo $i was needed in $file
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment