-
Notifications
You must be signed in to change notification settings - Fork 4
/
remove_unused.pl
66 lines (54 loc) · 1.53 KB
/
remove_unused.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl
# Copyright (C) 2015-2021 Theo Niessink <[email protected]>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the LICENSE file for more details.
use strict;
use warnings;
use File::Find;
if($#ARGV != 0 or $ARGV[0] !~ /^--(dry-run|force)$/) {
$_ = $0; $_ = $& if(/[^\/\\]+$/);
print <<EOF;
Usage: $_ --dry-run | --force
Removes all files from libtommath/ and libtomcrypt/, except those C source
files (*.c, *.h) that are actually used in makefile.msvc.
EOF
exit 1;
}
my $dry_run = $1 ne 'force';
my %required = ();
open(FILE, '<makefile.msvc') or die "$!\n";
binmode(FILE);
while(<FILE>) {
tr/\r\n//d;
next unless(/^\s*(libtom(?:math|crypt)\S+\.(?:obj|h|c))/);
$_ = $1;
tr/\\/\//;
s/\.obj$/.c/;
$required{$_} = 1;
}
close(FILE);
my @required = keys %required;
undef %required;
push(@required, qw(libtommath/changes.txt libtommath/LICENSE));
push(@required, qw(libtomcrypt/changes libtomcrypt/LICENSE));
my @files = ();
my @dirs = ();
sub wanted {
if(-f) {
push @files, $File::Find::name;
} elsif(-d) {
push @dirs, $File::Find::name;
}
}
find(\&wanted, qw(libtommath libtomcrypt));
foreach my $file (@files) {
$file =~ tr/\\/\//;
if (!grep(/^\Q$file\E$/, @required)) {
print "$file\n" if($dry_run or unlink($file));
warn "[FAILED] $file\n" if(!$dry_run and -f $file);
}
}
foreach my $dir (sort { length($b) <=> length($a) } @dirs) {
rmdir($dir) unless($dry_run);
}