#!/bin/env perl
use FindBin;
use lib $FindBin::Bin;
use strict;
use warnings FATAL => 'all';
use File::Find;
use _kxLab;
# Global variables
my $header_printed = 0;
my $top_dir;
my @targets;
my $verbose = $ENV{VERBOSE};
my %seen;
my ( @clean_dirs, @clean_dirs_all );
my ( $clean_count );
sub do_clean
{
unlink "$top_dir/.makefile";
foreach my $d ( @clean_dirs )
{
my $printed_d;
$printed_d = $d;
$printed_d =~ s/^$top_dir\///;
print "\n=======\n" if ( $verbose );
print "======= Cleaning in $printed_d...\n" if ( $verbose );
print "=======\n" if ( $verbose );
_kxLab::system( "make -C $d local_clean" );
# remove requires and .makefile:
unlink "$d/.makefile";
unlink <$d/.*_requires*>;
# remove lists of files installed into $top_dir/dist/products and $top_dir/dist/rootfs:
unlink <$d/.*.rootfs>;
unlink <$d/.*.dist*>;
}
}
sub find_directories
{
# find all directories which has Makefile and hiden build results
my $shell_output = <<`SHELL`;
dirs=`find $top_dir -name ".[a-zA-Z0-9_-+.]*" \\
! -path "$top_dir/doc/*" \\
! -path "$top_dir/dist/*" \\
! -path "$top_dir/sources/*" \\
! -path "$top_dir/build-system/3pp/sources/*" \\
-prune -exec dirname {} \\; | sort -u`
for dir in \$dirs ; do
if [ -f "\$dir/Makefile" ] ; then
echo "\$dir"
fi
done
SHELL
@clean_dirs = split /\n/, $shell_output;
@clean_dirs = grep { $_ ne $top_dir } @clean_dirs;
}
foreach ( @ARGV )
{
push @targets, $_;
}
$top_dir = pop @targets;
if( ! -d $top_dir )
{
die "\nTop: $top_dir: is not a directory\n\n";
}
if( ! $top_dir =~ m/^\// )
{
die "\nTop: $top_dir: is not absolute path\n\n";
}
find_directories();
$clean_count = $#clean_dirs;
if( $clean_count != 0 )
{
if( !$header_printed )
{
print "\n======= Cleaning build tree =======\n\n" if ( $verbose );
$header_printed = 1;
}
do_clean();
# remove DEST_DIR_ABS
_kxLab::system( "rm -rf $top_dir/dist" );
print "\n";
}
else
{
print "\nCleaning... (nothing to be done).\n\n";
}