5 kx #!/bin/env 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::Basename;
5 kx use File::Temp;
5 kx use _kxLab;
5 kx
5 kx
5 kx #
5 kx # Install file(s)
5 kx #
5 kx # usage:
5 kx # $0 [options] source[ source] destination hardware
5 kx #
5 kx # where:
5 kx # 'source' - the list of source files to be installed into dest directory
5 kx # 'destination' - is a destination directory for installation all source files
5 kx # 'hardware' - is a HARDWARE variant
5 kx #
5 kx # options:
5 kx # --preserve-source-dir=true - preserve source directory tree in the dest dir,
5 kx # for example, if source is foo/bar/file then we
5 kx # will have destination such as dest/foo/bar/file.
5 kx #
5 kx # --preserve-source-dir=one - preserve source directory depth is only 1. Its
5 kx # mean that if we have source like foo/bar/file,
5 kx # then destination will be dest/bar/file.
5 kx #
5 kx
5 kx # Global variables
5 kx my $header_printed = 0;
5 kx
5 kx my $cleanup = $ENV{DO_CREATE_DIST_FILES} ? 0 : 1;
5 kx my ($tempfd, $tempname);
5 kx
5 kx sub usage
5 kx {
5 kx print <<EOF;
5 kx
5 kx Usage: install_targets [options] source[ source]
5 kx Options:
5 kx --preserve-source-dir={true|one} - preserve source directory tree in the DEST dir.
5 kx true: If source is foo/bar/file then destination
5 kx file will be DEST/foo/bar/file.
5 kx one: depth is only one directory. If source is
5 kx fo/bar/file then destination will be
5 kx DEST/bar/file.
5 kx --destination=DEST - where DEST is a destination directory.
5 kx --toolchain=TOOLCHAIN - where TOOLCHAIN ia a toolchain name;
5 kx --hardware=HARDWARE - where HARDWARE ia a HARDWARE name;
5 kx --flavour=FLAVOUR - where FLAVOUR ia a FLAVOUR name.
5 kx
5 kx EOF
5 kx exit;
5 kx }
5 kx
5 kx
5 kx # cleanpath( path )
5 kx sub cleanpath
5 kx {
5 kx my $path = shift;
5 kx $path =~ s!/{2,}!/!g;
5 kx return $path;
5 kx }
5 kx
5 kx # dist( file )
5 kx sub dist
5 kx {
5 kx my $file = cleanpath(shift);
5 kx $file =~ s!^.*dist/!!;
5 kx print $tempfd "$file\n";
5 kx }
5 kx
5 kx # newer_than( file1, file2 )
5 kx sub newer_than
5 kx {
5 kx my $file1 = shift;
5 kx my $file2 = shift;
5 kx _kxLab::error( "install_targets: Source file missing: $file1" ) if ( ! -f $file1 );
5 kx return( ! -f $file2 or -M $file1 < -M $file2 );
5 kx }
5 kx
5 kx sub dir_is_empty
5 kx {
5 kx my ($path) = @_;
5 kx opendir DIR, $path;
5 kx while( my $entry = readdir DIR )
5 kx {
5 kx next if( $entry =~ /^\.\.?$/ );
5 kx closedir DIR;
5 kx return 0;
5 kx }
5 kx closedir DIR;
5 kx return 1;
5 kx }
5 kx
5 kx # install_tree( install_dir, file, target, verbose )
5 kx sub install_tree
5 kx {
5 kx my $install_dir = cleanpath(shift);
5 kx my $file = shift;
5 kx my $target = shift;
5 kx my $verbose = shift;
5 kx
5 kx opendir(DIR, "$target");
5 kx my @files = readdir(DIR);
5 kx closedir DIR;
5 kx foreach my $f ( @files )
5 kx {
5 kx next if ($f eq "." or $f eq "..");
5 kx if( -d "$target/$f" )
5 kx {
5 kx install_tree( "$install_dir/$f", "$file/$f", "$target/$f", $verbose );
5 kx }
5 kx elsif( newer_than( "$target/$f", "$install_dir/$f" ) )
5 kx {
5 kx if( !$header_printed )
5 kx {
5 kx print "\n======= Installing files =======\n";
5 kx $header_printed = 1;
5 kx }
5 kx print "Installing $f in $install_dir\n" if ( $verbose );
5 kx _kxLab::system( "mkdir -p \"$install_dir\"" );
5 kx _kxLab::system( "cp -fa \"$target/$f\" \"$install_dir\"" );
5 kx dist( "$install_dir/$f" );
5 kx }
5 kx }
5 kx }
5 kx
5 kx
5 kx
5 kx
5 kx # install( install_dir, preserve_source_dir, verbose, targets )
5 kx sub install
5 kx {
5 kx my $install_dir = cleanpath(shift);
5 kx my $preserve_source_dir = shift;
5 kx my $verbose = shift;
5 kx my $targets = shift;
5 kx
5 kx foreach my $target ( @{$targets} )
5 kx {
5 kx my $file = basename($target);
5 kx my $path = "";
5 kx if( $preserve_source_dir eq "true" )
5 kx {
5 kx $path = dirname( $target );
5 kx }
5 kx elsif( $preserve_source_dir eq "one" )
5 kx {
5 kx $path = dirname( $target );
5 kx $path = basename( $path );
5 kx }
5 kx elsif( $preserve_source_dir eq "two" )
5 kx {
5 kx my ($first, $second);
5 kx
5 kx $path = dirname( $target );
5 kx $second = basename( $path );
5 kx $path = dirname( $path );
5 kx $first = basename( $path );
5 kx $path = $first . "/" . $second;
5 kx }
5 kx
5 kx if( -d $target )
5 kx {
5 kx if( dir_is_empty( $target ) )
5 kx {
5 kx _kxLab::system( "mkdir -p \"$install_dir/$path/$file\"" );
5 kx dist( "$install_dir/$path/$file" );
5 kx }
5 kx else
5 kx {
5 kx install_tree( "$install_dir/$path/$file", "$file", "$target", $verbose );
5 kx }
5 kx }
5 kx elsif( newer_than( $target, "$install_dir/$path/$file" ) )
5 kx {
5 kx if( !$header_printed )
5 kx {
5 kx print "\n======= Installing files =======\n" if ( $verbose );
5 kx $header_printed = 1;
5 kx }
5 kx print "Installing $file in $install_dir/$path\n" if ( $verbose );
5 kx _kxLab::system( "mkdir -p \"$install_dir/$path\"" );
5 kx _kxLab::system( "cp -fa \"$target\" \"$install_dir/$path\"" );
5 kx dist( "$install_dir/$path/$file" );
5 kx }
5 kx }
5 kx }
5 kx
5 kx my $preserve_source_dir = "";
5 kx my $dest_dir;
5 kx my ($toolchain, $hardware, $flavour);
5 kx my $target_build_dir;
5 kx my @targets;
5 kx my $verbose = $ENV{VERBOSE};
5 kx my $curdir = $ENV{CWD};
5 kx my $fname = "";
5 kx
5 kx
5 kx foreach ( @ARGV )
5 kx {
5 kx if( /--preserve-source-dir=(\S*)/ )
5 kx {
5 kx $preserve_source_dir = $1;
5 kx }
5 kx elsif( /--destination=(\S*)/ )
5 kx {
5 kx $dest_dir = $1;
5 kx }
5 kx elsif( /--toolchain=(\S*)/ )
5 kx {
5 kx $toolchain = $1;
5 kx }
5 kx elsif( /--hardware=(\S*)/ )
5 kx {
5 kx $hardware = $1;
5 kx }
5 kx elsif( /--flavour=(\S*)/ )
5 kx {
5 kx $flavour = $1;
5 kx }
5 kx elsif( /--help/ )
5 kx {
5 kx usage;
5 kx }
5 kx elsif( /--/ )
5 kx {
5 kx while( <STDIN> )
5 kx {
5 kx #
5 kx # NOTE: arguments from STDIN should be splitted by '\n'
5 kx #
5 kx my $arg = $_;
5 kx
5 kx chomp $arg; $arg =~ s/\\040/ /g;
5 kx
5 kx push @targets, $arg;
5 kx }
5 kx
5 kx last;
5 kx }
5 kx else
5 kx {
5 kx my $arg = $_;
5 kx
5 kx chomp $arg; $arg =~ s/\\040/ /g;
5 kx
5 kx push @targets, $arg;
5 kx }
5 kx }
5 kx
5 kx if( ! defined $dest_dir or $dest_dir eq "" ) { usage; }
5 kx if( ! defined $toolchain or $toolchain eq "" ) { usage; }
5 kx if( ! defined $hardware or $hardware eq "" ) { usage; }
5 kx if( ! defined $flavour or $flavour eq "" )
5 kx {
5 kx $flavour = "";
5 kx $target_build_dir = "." . $toolchain . "/" . $hardware;
5 kx }
5 kx else
5 kx {
5 kx $target_build_dir = "." . $toolchain . "/" . $hardware . "/" . $flavour;
5 kx }
5 kx
5 kx if ( $curdir )
5 kx {
5 kx $fname = "$curdir/" . $target_build_dir . "/.dist.XXXXXX";
5 kx }
5 kx else
5 kx {
5 kx $fname = $target_build_dir . "/.dist.XXXXXX";
5 kx }
5 kx
5 kx ($tempfd, $tempname) = File::Temp::tempfile( $fname, UNLINK => $cleanup );
5 kx
5 kx install( $dest_dir, $preserve_source_dir, $verbose, \@targets );