-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_targets
executable file
·283 lines (247 loc) · 6.37 KB
/
install_targets
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/perl
use FindBin;
use lib $FindBin::Bin;
use strict;
use warnings FATAL => 'all';
use File::Basename;
use File::Temp;
use _kxLab;
#
# Install file(s)
#
# usage:
# $0 [options] source[ source] destination hardware
#
# where:
# 'source' - the list of source files to be installed into dest directory
# 'destination' - is a destination directory for installation all source files
# 'hardware' - is a HARDWARE variant
#
# options:
# --preserve-source-dir=true - preserve source directory tree in the dest dir,
# for example, if source is foo/bar/file then we
# will have destination such as dest/foo/bar/file.
#
# --preserve-source-dir=one - preserve source directory depth is only 1. Its
# mean that if we have source like foo/bar/file,
# then destination will be dest/bar/file.
#
# Global variables
my $header_printed = 0;
my $cleanup = $ENV{DO_CREATE_DIST_FILES} ? 0 : 1;
my ($tempfd, $tempname);
sub usage
{
print <<EOF;
Usage: install_targets [options] source[ source]
Options:
--preserve-source-dir={true|one} - preserve source directory tree in the DEST dir.
true: If source is foo/bar/file then destination
file will be DEST/foo/bar/file.
one: depth is only one directory. If source is
fo/bar/file then destination will be
DEST/bar/file.
--destination=DEST - where DEST is a destination directory.
--toolchain=TOOLCHAIN - where TOOLCHAIN ia a toolchain name;
--hardware=HARDWARE - where HARDWARE ia a HARDWARE name;
--flavour=FLAVOUR - where FLAVOUR ia a FLAVOUR name.
EOF
exit;
}
# cleanpath( path )
sub cleanpath
{
my $path = shift;
$path =~ s!/{2,}!/!g;
return $path;
}
# dist( file )
sub dist
{
my $file = cleanpath(shift);
$file =~ s!^.*dist/!!;
print $tempfd "$file\n";
}
# newer_than( file1, file2 )
sub newer_than
{
my $file1 = shift;
my $file2 = shift;
_kxLab::error( "install_targets: Source file missing: $file1" ) if ( ! -f $file1 );
return( ! -f $file2 or -M $file1 < -M $file2 );
}
sub dir_is_empty
{
my ($path) = @_;
opendir DIR, $path;
while( my $entry = readdir DIR )
{
next if( $entry =~ /^\.\.?$/ );
closedir DIR;
return 0;
}
closedir DIR;
return 1;
}
# install_tree( install_dir, file, target, verbose )
sub install_tree
{
my $install_dir = cleanpath(shift);
my $file = shift;
my $target = shift;
my $verbose = shift;
opendir(DIR, "$target");
my @files = readdir(DIR);
closedir DIR;
foreach my $f ( @files )
{
next if ($f eq "." or $f eq "..");
if( -d "$target/$f" )
{
install_tree( "$install_dir/$f", "$file/$f", "$target/$f", $verbose );
}
elsif( newer_than( "$target/$f", "$install_dir/$f" ) )
{
if( !$header_printed )
{
print "\n======= Installing files =======\n";
$header_printed = 1;
}
print "Installing $f in $install_dir\n" if ( $verbose );
_kxLab::system( "mkdir -p \"$install_dir\"" );
_kxLab::system( "cp -fa \"$target/$f\" \"$install_dir\"" );
dist( "$install_dir/$f" );
}
}
}
# install( install_dir, preserve_source_dir, verbose, targets )
sub install
{
my $install_dir = cleanpath(shift);
my $preserve_source_dir = shift;
my $verbose = shift;
my $targets = shift;
foreach my $target ( @{$targets} )
{
my $file = basename($target);
my $path = "";
if( $preserve_source_dir eq "true" )
{
$path = dirname( $target );
}
elsif( $preserve_source_dir eq "one" )
{
$path = dirname( $target );
$path = basename( $path );
}
elsif( $preserve_source_dir eq "two" )
{
my ($first, $second);
$path = dirname( $target );
$second = basename( $path );
$path = dirname( $path );
$first = basename( $path );
$path = $first . "/" . $second;
}
if( -d $target )
{
if( dir_is_empty( $target ) )
{
_kxLab::system( "mkdir -p \"$install_dir/$path/$file\"" );
dist( "$install_dir/$path/$file" );
}
else
{
install_tree( "$install_dir/$path/$file", "$file", "$target", $verbose );
}
}
elsif( newer_than( $target, "$install_dir/$path/$file" ) )
{
if( !$header_printed )
{
print "\n======= Installing files =======\n" if ( $verbose );
$header_printed = 1;
}
print "Installing $file in $install_dir/$path\n" if ( $verbose );
_kxLab::system( "mkdir -p \"$install_dir/$path\"" );
_kxLab::system( "cp -fa \"$target\" \"$install_dir/$path\"" );
dist( "$install_dir/$path/$file" );
}
}
}
my $preserve_source_dir = "";
my $dest_dir;
my ($toolchain, $hardware, $flavour);
my $target_build_dir;
my @targets;
my $verbose = $ENV{VERBOSE};
my $curdir = $ENV{CWD};
my $fname = "";
foreach ( @ARGV )
{
if( /--preserve-source-dir=(\S*)/ )
{
$preserve_source_dir = $1;
}
elsif( /--destination=(\S*)/ )
{
$dest_dir = $1;
}
elsif( /--toolchain=(\S*)/ )
{
$toolchain = $1;
}
elsif( /--hardware=(\S*)/ )
{
$hardware = $1;
}
elsif( /--flavour=(\S*)/ )
{
$flavour = $1;
}
elsif( /--help/ )
{
usage;
}
elsif( /--/ )
{
while( <STDIN> )
{
#
# NOTE: arguments from STDIN should be splitted by '\n'
#
my $arg = $_;
chomp $arg; $arg =~ s/\\040/ /g;
push @targets, $arg;
}
last;
}
else
{
my $arg = $_;
chomp $arg; $arg =~ s/\\040/ /g;
push @targets, $arg;
}
}
if( ! defined $dest_dir or $dest_dir eq "" ) { usage; }
if( ! defined $toolchain or $toolchain eq "" ) { usage; }
if( ! defined $hardware or $hardware eq "" ) { usage; }
if( ! defined $flavour or $flavour eq "" )
{
$flavour = "";
$target_build_dir = "." . $toolchain . "/" . $hardware;
}
else
{
$target_build_dir = "." . $toolchain . "/" . $hardware . "/" . $flavour;
}
if ( $curdir )
{
$fname = "$curdir/" . $target_build_dir . "/.dist.XXXXXX";
}
else
{
$fname = $target_build_dir . "/.dist.XXXXXX";
}
($tempfd, $tempname) = File::Temp::tempfile( $fname, UNLINK => $cleanup );
install( $dest_dir, $preserve_source_dir, $verbose, \@targets );