5 kx #!/usr/bin/perl
5 kx
5 kx use FindBin;
5 kx use lib $FindBin::Bin;
5 kx
5 kx use strict;
5 kx use warnings FATAL => 'all';
5 kx
5 kx use File::Find;
5 kx use _kxLab;
5 kx
5 kx
5 kx # Global variables
5 kx my $header_printed = 0;
5 kx
5 kx my $top_dir;
5 kx my @targets;
5 kx my $verbose = $ENV{VERBOSE};
5 kx
5 kx my %seen;
5 kx my ( @dist_clean_dirs, @clean_dirs, @dist_clean_dirs_all, @clean_dirs_all );
5 kx my ( $dist_clean_count, $clean_count );
5 kx
5 kx sub do_clean
5 kx {
5 kx unlink "$top_dir/.makefile";
5 kx
5 kx foreach my $d ( @dist_clean_dirs )
5 kx {
5 kx my $printed_d;
5 kx
5 kx $printed_d = $d;
5 kx $printed_d =~ s/^$top_dir\///;
5 kx
5 kx print "\n===\n" if ( $verbose );
5 kx print "=== Dist cleaning in $printed_d...\n" if ( $verbose );
5 kx print "===\n" if ( $verbose );
5 kx _kxLab::system( "make -C $d dist_clean" );
5 kx }
5 kx
5 kx foreach my $d ( @clean_dirs )
5 kx {
5 kx my $printed_d;
5 kx
5 kx $printed_d = $d;
5 kx $printed_d =~ s/^$top_dir\///;
5 kx
5 kx print "\n===\n" if ( $verbose );
5 kx print "=== Cleaning in $printed_d...\n" if ( $verbose );
5 kx print "===\n" if ( $verbose );
5 kx _kxLab::system( "make -C $d local_clean" );
5 kx unlink "$d/.makefile";
5 kx }
5 kx }
5 kx
5 kx sub do_clean_list
5 kx {
5 kx my $dir = shift;
5 kx my $cwd = `pwd`;
5 kx
5 kx chomp $cwd;
5 kx
5 kx return if( ! -f "$cwd/Makefile" );
5 kx return if( $dir =~ m/sources/ );
5 kx
5 kx # needs dist clean:
5 kx if( -f "$cwd/.dist" ) { push @dist_clean_dirs_all, $cwd; }
5 kx
5 kx # needs clean:
5 kx push @clean_dirs_all, $cwd;
5 kx }
5 kx
5 kx sub process_clean
5 kx {
5 kx # add directory which contains '.makefile' too.
5 kx if( $_ eq ".makefile" ) { do_clean_list( $File::Find::dir ); }
5 kx
5 kx return if( ! -d $_ );
5 kx
5 kx return if( $File::Find::name =~ m/build-system/ );
5 kx return if( $File::Find::name =~ m/dist/ );
5 kx return if( $File::Find::name =~ m/sources/ );
5 kx
5 kx foreach my $d ( @targets )
5 kx {
5 kx if( $d eq $_ ) { do_clean_list( $File::Find::dir ); }
5 kx }
5 kx }
5 kx
5 kx
5 kx foreach ( @ARGV )
5 kx {
5 kx push @targets, $_;
5 kx }
5 kx $top_dir = pop @targets;
5 kx
5 kx if( ! -d $top_dir )
5 kx {
5 kx die "\nTop: $top_dir: is not a directory\n\n";
5 kx }
5 kx if( ! $top_dir =~ m/^\// )
5 kx {
5 kx die "\nTop: $top_dir: is not absolute path\n\n";
5 kx }
5 kx
5 kx find( \&process_clean, "$top_dir" );
5 kx
5 kx # get unique names:
5 kx %seen = ();
5 kx @dist_clean_dirs = grep { ! $seen{ $_ }++ } @dist_clean_dirs_all;
5 kx # delete subdirs which already contains TARGET name:
5 kx foreach my $target ( @targets )
5 kx {
5 kx my @match = ();
5 kx @match = grep { /$target/ } keys %seen;
5 kx foreach my $subdir ( @match ) { delete( $seen{"$subdir"} ); }
5 kx }
5 kx @dist_clean_dirs = reverse( keys %seen );
5 kx
5 kx # get unique names:
5 kx %seen = ();
5 kx @clean_dirs = grep { ! $seen{ $_ }++ } @clean_dirs_all;
5 kx foreach my $target ( @targets )
5 kx {
5 kx my @match = ();
5 kx @match = grep { /$target/ } keys %seen;
5 kx foreach my $subdir ( @match ) { delete( $seen{"$subdir"} ); }
5 kx }
5 kx @clean_dirs = reverse( keys %seen );
5 kx
5 kx push @clean_dirs, "$top_dir/build-system";
5 kx
5 kx $dist_clean_count = @dist_clean_dirs;
5 kx $clean_count = @clean_dirs;
5 kx
5 kx if( $dist_clean_count != 0 || $clean_count != 0 )
5 kx {
5 kx if( !$header_printed )
5 kx {
5 kx print "\n======= Cleaning build tree =======\n";
5 kx $header_printed = 1;
5 kx }
5 kx
5 kx do_clean();
5 kx
5 kx print "\n";
5 kx }
5 kx else
5 kx {
5 kx print "Cleaning... (nothing to be done).\n";
5 kx }
5 kx