This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-app-carta
1985 lines (1816 loc) · 78.8 KB
/
make-app-carta
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
### !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
### !!!! RHEL5 ships with perl 5.8.8 which contains a version of !!!!
### !!!! File::Path which is too old... after we retire RHEL5 we !!!!
### !!!! should switch the startup back to #!/usr/bin/perl !!!!
### !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
###
### osx: make-app -v -ni ws=trunk arch=darwin out=/tmp
### linux: make-app ws=trunk version=4.5.0 out=tmp type=test
###
$longest_line = 0;
use File::Basename;
use File::Find;
use File::Path 2.07 qw( make_path remove_tree );
use File::Spec;
use Cwd qw(abs_path getcwd);
use List::Util qw(max);
use POSIX qw(uname);
use File::Copy;
use Term::ANSIColor;
use feature 'state';
$osname = lc((uname( ))[0]);
$spf="%6d";
$workspace='';
$arch=$osname;
%types=( );
$quiet='';
$quiet='';
$non_interactive='';
$local_template_path='';
$local_data_path='';
$match='';
$output_dir= '';
$library_destination = '';
$data_destination = '';
$data_url = 'https://svn.cv.nrao.edu/svn/casa-data/distro';
$ignore_missing_dep='';
$template_url = '';
$distro_name = 'carta';
$branch='release';
@prefix = ( );
$version = '';
$reltype = '';
#$qtpath="/Users/rpmbuild/Qt5.7.0/5.7/clang_64/lib";
$qtpath=$ENV{'qtpath'}."/lib";
print "make-app-carta use qt at:$qtpath\n";
%exclude = ( );
## unbuffer output
$| = 1;
# Trim
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
foreach ( @ARGV ) {
m|^version=(\d+\.\d+\.\d+(?:-\d+)?)| && ( $version = $1 );
### the "test" and "prerelease" distros are generated from a clean build
### the "stable" and "release" distros are repackaged "test" and "prerelease" distros
### "DEV" is a developer created distro...
m@^type=(prerelease|test|DEV)@ && ( $reltype = $1 );
m|^ws=(\S+)|i && -d "$1" && ( $workspace = $1, next );
m|^arch=(\S+)|i && ( $arch = $1, next );
m|^match=(.+)| && ( $match = $1, next );
m|^out=(.+)| && ( $output_dir = $1, next );
m|^template=(.+)| && ( $local_template_path = $1, next );
m|^data=(.+)| && ( $local_data_path = $1, next );
m|^url=(.+)| && ( $template_url = $1, next );
m|^branch=(.+)| && ( $branch = $1, next );
m|^-ignore-missing-dep|i && ( $ignore_missing_dep = 'yes', next );
m|^-q$|i && ( $quiet = 'yes', $verbose = '', next );
m|^-v$|i && ( $verbose = 'yes', $quiet = '', next );
m|^-ni$|i && ( $non_interactive = 'yes', next );
m|^-help$| && ( usage( ), exit );
}
if ( ! $workspace ) {
if ( $ENV{'CASAPATH'} =~ m|^(\S+)\s+\S+| ) {
$workspace = $1 if -d $1;
}
}
if ( ! $arch ) {
$ENV{'CASAPATH'} =~ m|\S+\s+(\S+)| && ( $arch = $1 );
}
###
### set up the directory where the binary distribution will be created...
###
unless( $template_url ) { $template_url = 'https://open-bitbucket.nrao.edu/scm/casa/casa-pkg.git' }
#unless( $template_url ) { $template_url = 'http://gitlab.nrao.edu/casa/development_tools.git' }
$distribution_directory='';
if ( $osname eq "linux" ) {
#unless( $template_url ) { $template_url = 'https://svn.cv.nrao.edu/svn/casa/development_tools/packaging/template/linux/casa-distro' }
unless( $template_path ) { $template_path = 'casa-pkg/packaging/template/linux/carta-distro' }
if ( $template_url =~ m|/(\w+)-distro$| ) { $distro_name = $1 }
die "please supply a version string with 'version=<VER>'" unless $version;
$distribution_directory = ($bldtype ? "$output_dir/$distro_name\-$bldtype-$version" : "$output_dir/$distro_name\-$version");
print "Distribution directory: $distribution_directory\n";
print "Distro name: $distro_name\n";
print "Version: $version\n";
$version_file = "$distribution_directory/lib/casa/VERSION";
%exclude = map { $_ => 1 } (
"libXrandr", "librt", "libXi", "libatlas", "libX11", "libexpat", "libz", "libkeyutils", "libdl",
"libresolv", "libnsl", "libXext", "libXfixes", "libgdbm", "libSM", "libsqlite3", "libcblas",
"libfontconfig", "libICE", "libXinerama", "libcom_err", "libkrb5support", "libbz2",
"libutil", "libkrb5", "libxcb", "libfreetype", "libgobject-2.0", "libxml2",
"libgssapi_krb5", "libgthread-2.0", "libXcursor", "libselinux",
"libf77blas", "libc", "libXau", "libk5crypto", "libpthread", "libstdc++", "libXrender",
"libm", "libuuid", "libglib-2.0", "libXft", "libfreebl3.so", "libdb-4.7.so", "libgcc_s",
"libcrypt", "libnuma" );
} elsif ( $osname eq "darwin" ) {
#unless( $template_url ) { $template_url = 'https://svn.cv.nrao.edu/svn/casa/development_tools/packaging/template/osx/CASA.app' }
unless( $template_path ) { $template_path = 'casa-pkg/packaging/template/osx/Carta.app' }
$distribution_directory = "$output_dir/Carta.app";
$version_file = "$distribution_directory/Contents/Resources/VERSION"
} else {
die "unknown os environment: $osname";
}
if ( $output_dir ) {
die "output directory, $output_dir, does not exist..." unless -d $output_dir;
} else {
die "please supply an output directory with 'out=<PATH>'";
}
if ( $local_template_path ) {
die "template source directory ($local_template_path) does not exist" unless -d $local_template_path;
if ( $local_template_path !~ m|^/| ) {
$local_template_path = getcwd( ) . "/$local_template_path";
}
}
if ( $local_data_path ) {
die "data source directory ($local_data_path) does not exist" unless -d $local_data_path;
if ( $local_data_path !~ m|^/| ) {
$local_data_path = getcwd( ) . "/$local_data_path";
}
}
##
## where the otool discovered dependencies end up...
##
if ( $osname eq "linux" ) {
$library_destination = "$distribution_directory/lib";
$data_destination = "$distribution_directory";
} else {
$library_destination = "$distribution_directory/Contents/Frameworks";
$data_destination = "$distribution_directory/Contents/Resources";
}
usage("could not find workspace... please specify one with 'ws=<PATH>'") unless $workspace && -d $workspace;
#usage("could not find architecture... please specify one with 'arch=<STR>'") unless $arch && -d "$workspace/$arch";
$workspace = abs_path($workspace);
## turn on auto-flushing... for carriage return output...
if ( $verbose ) { $| = 1 }
## fetch casa app template...
unless ( $quiet ) { print "fetching template...\n"; }
#fetch_template( $output_dir );
print "output_dir: $output_dir\n";
#fetch_svn( $output_dir, $template_url, $local_template_path, ($osname eq "linux" ? "$distro_name\-$reltype-$version" : '') );
## fill in workspace references...
print "Initial fetch\n";
print "Template path: $template_path\n";
if ($osname eq "linux") {
fetch_git( $output_dir, $template_url, $template_path, $local_template_path,
($osname eq "linux" ? "$distro_name\-$version" : '') );
}
else {
$macappdir = ( split '/', $template_path )[ -1 ];
print "Mac app dir: $macappdir\n";
fetch_git( $output_dir, $template_url, $template_path, $local_template_path, $macappdir);
}
$app_dir = $distribution_directory;
print "app_dir: $app_dir\n";
fill_stubs( $app_dir, \©_file, "filling explicit stubs" );
## scan for dependencies and discover prefix...
scan_application( $app_dir, "initial scan of application..." );
unless ( $quiet ) { print "\nfound load-time dependencies in ", scalar(keys %libraries_dirs)," directories...\n"; }
@prefix = prefix( );
die "could not find prefix path..." unless scalar(@prefix) > 0;
unless ( $quiet ) { print "using library paths " . join(' ',@prefix) . "\n" }
# now that we have the prefix, scan the application again and
# fill in the stubs that depend on the prefix...
fill_stubs( $app_dir, \©_file, "filling prefix relative stubs..." );
###
### check to see if more than one version of python is referenced...
###
$python_count = 0;
foreach ( keys %libraries_dirs ) {
print "Key: $_";
my $ref = $libraries_dirs{$_};
foreach ( keys %$ref ) {
if ( m|Python\.framework| ) { $python_count += 1; }
}
}
if ( $python_count > 1 ) { die "application is linked against more than one version of python"; }
## scan for dependencies and discover prefix...
$scan_count = 0;
unless ( $quiet ) { print "\nbeginning multiple passes at resolving remaining dependencies...\n" }
do {
@scan_copies = ( );
++$scan_count;
scan_application( $app_dir, "rescanning application (pass $scan_count)...", 1 ); ##真的有rescan
unless ( $quiet ) { print "\nfound load-time dependencies in ", scalar(keys %libraries_dirs)," directories...\n"; }
$nnn = scalar(keys %libraries_dirs);
###
### print out dependencies for user to check...
###
unless ( $quiet ) {
print_dependencies( );
unless ( $non_interactive ) {
my $cont = '';
do {
print "OK to continue [y/n]? ";
$cont = <STDIN>;
chomp $cont;
$cont =~ tr|YN|yn|;
die "user aborted application generation" if $cont eq 'n';
} while ( $cont ne 'y' );
}
}
unless( $quiet ) { print "copying dependencies to $library_destination ...\n" }
foreach $libdir ( keys %libraries_dirs ) {
my $ref = $libraries_dirs{$libdir};
###
### throw away dependencies to obvious system areas...
###
next if m|^/System| || m|^/usr/lib |;
###
### copy over each dependency...
###
foreach $dep ( keys %$ref ) {
my $destfile = $dep;
###
### stored frameworks look like:
###
### QtUiTools.framework/Versions/4/QtUiTools QtXml.framework/Versions/4/QtXml
### QtCore.framework/Versions/4/QtCore QtDBus.framework/Versions/4/QtDBus
### Python.framework/Versions/2.7/Python QtGui.framework/Versions/4/QtGui
###
if ( $dep =~ m|^(\S+\.framework)| ) {
$destfile = $1;
}
my ($v, $d, $f) = File::Spec->splitpath($destfile);
unless ( -e "$library_destination/$f" ) {
push( @scan_copies, $f );
}
if ( ! -e "$libdir/$destfile" ) {
if ( $ignore_missing_dep ) {
unless ( $quiet ) { print "warning: dependency $libdir/$destfile does NOT exist\n" }
} else {
# way1
# 3. There is an exception case, Qt5.7 will produce a libwcs.dylib
# (before, always "libwcs.5.15.dylib) and its the result of "otool -L" will include libwcs.5.15.dylib, therefore some error happen, so I just workaround as below
# if ($combine == "libwcs.5.15.dylib"){
# print "ignore libwcs.5.15.dylib/";
# grimmer copy, dir:libwcs.5.15.dylib;dest:
# dependency libwcs.5.15.dylib/ does NOT exist... at ./make-app-carta line 297. die
# way2: just comment
# die "dependency $libdir/$destfile does NOT exist... $initial";
}
} else {
print "Copying: $libdir/$destfile to $library_destination\n" ;
copy_file( "$libdir/$destfile", $library_destination );
}
}
}
} until ( scalar(@scan_copies) == 0 );
unless( $quiet ) { print "resolving links to outside of application directory...\n" }
resolve_external_links( "$distribution_directory" );
adjust_runtime_paths( "$distribution_directory" );
# I don't think any of these are needed for Carta
#adjust_version_file( "$distribution_directory", fetch_revision( $branch ) );
#osx_specific_adjustments( "$distribution_directory" );
linux_specific_adjustments( "$distribution_directory" );
#create_revstate( $workspace, ($osname eq 'darwin' ? "$distribution_directory/Contents/Resources/revstate" : "$distribution_directory/.revstate" ) );
print "application creation complete\n";
exit 0;
##
## for automated builds, this should fetch version information
## from: https://svn.cv.nrao.edu/cgi-bin/casa-version
## for example, with the linux RPM build script 'casa-version'
## however, when building from a developer workspace, we need
## to somehow get the version number to use from the workspace
##
sub fetch_version {
unless ( $version ) {
open( my $ver, "< $workspace/code/VERSION" );
foreach ( <$ver> ) {
m|^(\d+\.\d+\.\d+)| && ( $version = $1, last );
}
close($ver);
}
return $version;
}
sub create_revstate {
my $workspace = shift(@_);
my $output_file = shift(@_);
my $orig_dir = getcwd( );
chdir($workspace) or die "could not change to workspace ($workspace): $!";
if ( ! -d '.svn' && -d 'code/.svn' ) { chdir( "code" ) or die "could not change to code after failing to find a subversion tree in $worksapce" }
my $url = '';
my $revision = '';
my $version = '';
open( my $svninfo, "svn info |" );
foreach ( <$svninfo> ) {
m|^URL:\s+(.*)| && ( chomp( $url = $1 ), next );
m|^Last Changed Rev:\s+(\d+)| && ( $revision = $1, next );
}
close( $svninfo );
unless( $url && $revision ) { die "could not find subversion information" }
$version = fetch_version( );
unless( $version ) { die "could not find version number" }
open( my $out, "> $output_file" );
print $out "url=\"$url\"\n";
print $out "revision=\"$revision\"\n";
print $out "version=\"$version\"\n";
close( $out );
chdir($orig_dir);
}
sub expand {
state $output_dir_expanded;
unless ( $output_dir_expanded ) { $output_dir_expanded = abs_path($output_dir) }
my $path = shift(@_);
# if ( -l $path && -d readlink($path) ) {
if ( -l $path ) {
my $result = abs_path($path);
$result =~ s|^$output_dir_expanded|$output_dir|;
return $result;
} else {
my $dir = dirname($path);
my $file = basename($path);
my $result = abs_path($dir);
$result =~ s|^$output_dir_expanded|$output_dir|;
return "$result/$file";
}
}
sub translate_subdir {
my $primary = shift(@_);
my $secondary = shift(@_);
my @primary = split('/',$primary);
my @secondary = split('/',$secondary);
my $symlinks_found = { };
for ( $pi = $#primary; $pi > 0; --$pi ) {
for ( $si = 0; $si < $#secondary; ++$si ) {
my $path = join('/',@primary[0 .. $pi]) . '/' . join('/',@secondary[$si .. $#secondary]);
if ( -e $path ) {
$symlinks_found->{$path} = 1;
last;
}
}
}
return $symlinks_found;
}
sub change_runtime_path {
my $exe = shift(@_);
$exe = expand($exe);
my $libref = shift(@_);
my $libraries = expand(shift(@_));
### this exception causes many python plugins to avoid load path substitution
#*# if ( $exe =~ m|Plugins| ) { return }
###
my $common = common_prefix( { $exe => 1, $libraries => 1 } );
# $exe and $libraries are equal for libstdc++.6.dylib
# --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
# bash$ otool -L /tmp/CASA.app/Contents/Frameworks/libstdc++.6.dylib
# /tmp/CASA.app/Contents/Frameworks/libstdc++.6.dylib:
# /opt/casa/01/lib/libgcc/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.20.0)
# /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
# /opt/casa/01/lib/libgcc/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
# bash$
unless ( $common || $exe eq $libraries ) { die "could not resolve local path from executable $exe to library $libraries" }
my $local_libfile = basename($libraries);
my $local_libpath = dirname($libraries);
my $local_exepath = dirname($exe);
$local_libpath =~ s|^$common||;
$local_exepath =~ s|^$common||;
$local_libpath =~ s|^/||;
$local_exepath =~ s|^/||;
$local_exe =~ s|^$common/?||;
$local_lib =~ s|^$common/?||;
print "#######################################\n";
print "local_libfile $local_libfile\n";
print "local_libpath $local_libpath\n";
print "local_exepath $local_exepath\n";
print "common $common\n";
print "libraries $libraries\n";
print "#######################################\n";
my @local_offset = split( '/', $local_exepath );
print "Loffset 1: @local_offset\n";
print "local_libpath : @local_libpath\n";
#my $local_offset = scalar(@local_offset) == 0 ? ($local_libpath ? "lib/$local_libpath" : 'lib') : ( $local_libpath ? join( '/', ('..') x scalar(@local_offset), "$local_libpath") : join( '/', ('..') x scalar(@local_offset) ) );
my $local_offset = scalar(@local_offset) == 0 ? ($local_libpath ? "$local_libpath" : '') : ( $local_libpath ? join( '/', ('..') x scalar(@local_offset), "$local_libpath") : join( '/', ('..') x scalar(@local_offset) ) );
print "Loffset 2: $local_offset\n";
if ($libraries =~ m|libplugin.dylib|) {
@split_path = split( '/', $libraries);
$local_offset = "../../Plugins/" . join('/', @split_path[-2]);
print "Exe: $exe \n";
print "Plugin offset: $local_offset\n";
}
print "install_name_tool -change \"$libref\" \"\@loader_path/$local_offset/$local_libfile\" \"$exe\" 2>&1 |\n";
unless ( -e dirname($exe) . "/$local_offset/$local_libfile" ) { die "resolved local path does not exist: " . dirname($exe) . "/$local_offset/$local_libfile" }
unless ( -w $exe ) { add_write_permission( $exe ) }
my $printed_label = '';
open( CHG, "install_name_tool -change \"$libref\" \"\@loader_path/$local_offset/$local_libfile\" \"$exe\" 2>&1 |" );
foreach ( <CHG> ) {
unless ( $printed_label ) {
print " $exe\n";
$printed_label='yes';
}
print "\t\t>\t$_";
}
close( CHG );
}
sub exp_path {
my $path = shift( @_ );
return File::Spec->rel2abs( $path );
}
sub expand_prefix {
my $link = shift(@_);
$result = '';
if ( scalar(@prefix) > 0 ) {
for $dir ( @prefix ) {
my $path = $link;
###
### these @@...@@[name] search for a file called 'name'
###
if ( $path =~ m|\@\@PREFIX\@\@\[(\S+)\]| ) {
my $file = $1;
my $found = '';
## find sets $main::_
my $find_func = sub { unless ( $found ) {
if ( $file eq $main::_ ) {
$found = "$File::Find::dir/$main::_";
}
}
};
find( { wanted => $find_func }, $dir );
if ( $found ) { return $dir; }
} elsif ( $path =~ s|\@\@PREFIX\@\@|$dir| ) {
if ( -e $path ) {
print "[expand_prefix]: found " . basename($path) . "\n";
print " " . $path . "\n";
return $dir;
}
}
}
}
return $result;
}
sub expand_stub {
my $dir = '';
my $current_record = shift(@_);
if ($current_record =~ m|^\@\@WS\@\@|) {
$dir = $workspace;
}
if ($current_record =~ m|^\@\@ARCH\@\@|) {
$dir = "$workspace/$arch";
}
if ($current_record =~ m|^\@\@PREFIX\@\@|) {
$dir = expand_prefix($current_record);
}
# die "unrecognized stub substitution..." unless $dir;
if ( $dir ) {
if ( $current_record =~ m|\@\@\S+\@\@\[(\S+)\]| ) {
my $file = $1;
my $found = '';
## find sets $main::_
my $find_func = sub { unless ( $found ) {
if ( $file eq $main::_ ) {
$found = "$File::Find::dir/$main::_";
}
}
};
my $default = $current_record;
find( { wanted => $find_func }, $dir );
if ( $found ) { return $found; }
else { return $default; }
} elsif ( $current_record =~ m|^\@\@\S+\@\@/| ) {
if ( $dir ) {
$current_record =~ s|^\@\@\S+\@\@/|$dir/|;
}
}
}
return $current_record;
}
sub fill_union {
my $link = shift(@_);
my @elements = @_;
die "union used with fewer than two elements, $link" unless scalar(@elements) > 1;
my @available = ( );
for ( @elements ) {
if ( m|\@\@WS\@\@| && $workspace ) {
my $path = $_;
$path =~ s|\@\@WS\@\@|$workspace|;
die "workspace substitution $_ in UNION(...) does not map to a directory" unless -d $path;
push( @available, $path );
} elsif ( m|\@\@ARCH\@\@| && $workspace ) {
my $path = $_;
$path =~ s|\@\@ARCH\@\@|$workspace/$arch|;
die "arch substitution $_ in UNION(...) does not map to a directory" unless -d $path;
push( @available, $path );
} elsif ( m|\@\@PREFIX\@\@| && scalar(@prefix) > 0 ) {
my $prefix;
my $path = '';
foreach $prefix ( @prefix ) {
my $trypath = $_;
$trypath =~ s|\@\@PREFIX\@\@|$prefix|;
if ( -d $trypath ) {
$path = $trypath;
last;
}
}
die "prefix substitution $_ in UNION(...) failed to find a match with prefix == " . join(' ',@prefix) unless $path;
die "prefix substitution $_ in UNION(...) does not map to a directory" unless -d $path;
push( @available, $path );
}
}
if ( scalar(@available) == scalar(@elements) ) {
my $limit = 3;
my @output = ( );
my $target = $_;
unlink($_) or die "could not remove $_";
make_path($_) or die "could not create path $_";
for my $src ( @available ) {
print "copying from $src to $target...\n";
##
## we really only want to dereference links to directories... not e.g. shared library symlinks
##
#open( my $tar, "(tar --dereference --hard-dereference -C $src -cf - . | tar -C $target -xf -) 2>&1 |" );
open( my $tar, "(tar -C $src -cf - . | tar -C $target -xf -) 2>&1 |" );
while ( <$tar> ) {
unless ( $quiet ) {
if ( scalar(@output) < $limit ) { print "\t<tar> $_" }
push( @output, $_ );
if ( scalar(@output) > $limit ) { shift(@output) }
}
}
close( $tar );
unless ( $quiet ) {
print "\t<tar>\t...\n";
foreach ( @output ) {
print "\t<tar> $_";
}
}
}
}
}
##
## creating a casaviewer disto will require a subset of the python files...
## i.e. those that implement the viewertool and the viewer/imview/msview tasks
## this will fill based upon a regular expression which files are matched
## against...
##
sub fill_match {
my $path = shift(@_);
my $sub = shift(@_);
}
sub fill_stubs {
my $app_path = shift(@_);
my $copy_func = shift(@_);
my $msg = shift(@_);
my $find_func = sub {
if ( -l $_ && ! -e $_ ) {
my $link = readlink($_);
if ( $link =~ m|^UNION\(\s*(\@\@.+\@\@.*?)\s*\)| ) {
fill_union($_,split(/\s+/,$1));
} elsif( $link =~ m|^MATCH\(\s*\@\@(.*)\@\@.*?\s*\)| ) {
fill_match($_,$1);
} elsif( $link =~ m|^SVN\(\s*\@\@(.*)\@\@.*?\s*\)| ) {
unlink($_);
fetch_svn(dirname($_),$1,'',basename($_));
} elsif( $link =~ m|^\@\@\S+\@\@| ) {
$copy_func->(expand_stub(readlink($_)),$_);
}
}
};
print $msg ? $msg . "\n" : "filling template stubs...\n";
find( { wanted => $find_func, no_chdir=>1 }, $app_path );
}
sub copy_file {
my $from = shift(@_);
my $to = shift(@_);
unless ( $from =~ m|^\@\@\S+\@\@| ) {
if ( ! -e $from ) {
die "source for copy does not exist, $from";
}
### stubs are a symlink that point to something like
### @@ARCH@@[somefile] or @@ARCH@@/some/path
### or UNION(@@WS@@/lib/python @@PREFIX@@/lib/python)
### or MATCH(@@regular-expression@@)
### or SVN(@@subversion-url@@)
if ( -l $to && readlink($to) =~ m|^\@\@\S+\@\@| ) {
unlink($to);
} elsif ( -e $to ) {
if ( basename($to) eq basename($from) &&
( -f $to && -f $from ||
-l $to && -l $from ||
-d $to && -d $from ) ) {
### assume target has already been copied...
### could check for differences...
return;
} elsif ( -d $to && ( -f $from || -d $from || -l $from ) ) {
my $file = basename($from);
### copy file to target directory...
if ( -f "$to/$file" ) {
return;
}
$to = "$to/$file";
} elsif ( -l $to ) {
die "will not copy through symbolic link, $to";
} else {
die "unknown condition while trying to copy $from to $to";
}
}
if ( $verbose ) {
print "<V>\tcopy\t$from\n";
print "<V>\tto\t$to\n";
}
if ( -l $to ) { unlink($to) }
ditto($from,$to);
}
}
sub collect_dependencies {
state %processed;
my $orig = shift(@_);
my $recursive = shift(@_);
%processed = ( );
collect_dependencies_work( \%processed, $orig, $recursive );
}
sub collect_dependencies_work {
my $been_there = shift(@_);
my $orig = shift(@_);
my $recursive = shift(@_);
my $absorig = abs_path($orig);
###
### use a hash to keep track or our (recursive) traversal...
###
return if exists $been_there->{$absorig};
$been_there->{$absorig} = 1;
my ($vol, $dir, $file) = File::Spec->splitpath($orig);
# generate a tally of dependent library locations...
# may need to make this a two-pass solution to be able to use info gleaned
# from known subdirectories (e.g. 'Frameworks' or 'lib') in deciding how to
# handle outliers...
if ( $match && $file =~ $match && $file =~ m|$match| ) { print "<match:source> $file\n" }
my $libname = basename(abs_path($orig));
open( INFO, ($osname eq "linux" ? "ldd $orig 2> /dev/null |" : "otool -L $orig |") );
# Dependency line reference
$i=0;
foreach ( <INFO> ) {
$rpathflag=0;
# Patch relative rpaths
if ($osname eq "darwin"){
# print "$i $_";
chomp($_);
$id = "";
if ($i == "0") {
$currfile = $_;
$currfile =~ s/://g;
$orig = $currfile
}
if ($i == "1") {
$id = $_;
$id =~ s/ \(com.*//g;
unless ( $quiet ) { print "ID: $id\n"; }
$id=trim($id);
unless ( $quiet ) { print "Trimmed ID $id\n"; }
# Strip @rpath from id
$id =~ s/\@rpath\///g;
unless ( $quiet ) { print "Stripped ID $id\n"; }
#$prefix = $currfile =~ s/$id//gr;
$prefix = $qtpath;
# And because we have Versions, take those out also
$id =~ s/\/Versions.*//g;
unless ( $quiet ) { print "Stripped id $id\n"; }
#$prefix = $currfile =~ s/$id.*//gr;
$prefix = $qtpath;
}
if ($_ =~ "\@rpath") {
$rpathflag=1;
#chomp($_);
#$orig = trim($_);
#$orig =~ s/ \(com.*//g;
$_ = trim($_);
$_ =~ s/ \(com.*//g;
unless ( $quiet ) { print "Orig: $orig\n"; }
unless ( $quiet ) { print "Currfile: $currfile\n"; }
unless ( $quiet ) { print "Prefix: $prefix\n"; }
$_ =~ s/\@rpath/$prefix/g;
$modifiedpath = trim($_);
#$modifiedpath =~ s/ \(com.*//g;
unless ( $quiet ) { print "Modified path: $modifiedpath\n"; }
#print "Update command:\n";
#print "install_name_tool -change \\$orig $modifiedpath $currfile\n";
$_=$modifiedpath;
}
$i++;
}
###
### sometimes otool output for shared libraries does not include paths:
###
### bash$ otool -L /tmp/CASA.app/Contents/Plugins/iconengines/libqsvgicon.dylib
### /tmp/CASA.app/Contents/Plugins/iconengines/libqsvgicon.dylib:
### libqsvgicon.dylib (compatibility version 0.0.0, current version 0.0.0)
### /opt/casa/01/Library/Frameworks/QtSvg.framework/Versions/4/QtSvg (compatibility version 4.8.0, current version 4.8.6)
### /opt/casa/01/Library/Frameworks/QtGui.framework/Versions/4/QtGui (compatibility version 4.8.0, current version 4.8.6)
### /opt/casa/01/Library/Frameworks/QtCore.framework/Versions/4/QtCore (compatibility version 4.8.0, current version 4.8.6)
### /opt/casa/01/Library/Frameworks/QtXml.framework/Versions/4/QtXml (compatibility version 4.8.0, current version 4.8.6)
### /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
### /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
### $bash$
###
### initial 'libqsvgicon.dylib' dependency causes 'libqsvgicon.dylib' to
### show up as the directory... throw this away...
###
# print "Orig: $orig Dependency: $_\n";
my $orig_backup = $orig;
my $dependency_backup = $_;
next if $osname eq "darwin" && m|$libname\s+|;
next if $osname eq "linux" && m|not found$|;
next if $osname eq "linux" && m|statically linked|;
### some special libraries have no corresponding path:
### linux-vdso.so.1 => (0x00007fffd1cfa000)
next if $osname eq "linux" && m|\s+\S+\s+=>\s+\(0x\S+\)$|;
next if m|\s+(?:\S+ => )?(\S+)| && exists $exclude{libroot($1)};
if ( $match && m|$match| && m|\s+(?:\S+ => )(\S+)| ) { print "<match:depend> $file [$1]\n" }
if ( m|^\s+(?:\S+ => )?(\S+?/Frameworks)| ||
m|^\s+(?:\S+ => )?($workspace/$arch)| ||
m|^\s+(?:\S+ => )?($workspace/../core/)| ||
m|^\s+(?:\S+ => )?(\S*?/lib)/| ||
m|^\s+(?:\S+ => )?(\S*?/lib64)/| ||
m|^.*\.framework/.*| || # Qt 5.7+ Frameworks
m|^\s+(?:\S+ => )?(\S+)| ) {
my $dir = $1;
if ($rpathflag) {
$dir = $qtpath;
}
###
### ignore direct system dependencies...
###
next if $osname eq "darwin" && $dir =~ m@^(?:/usr/lib|/System)@;
### nrao sysadmins like to install things in /opt/local
### and make them available in an ad hoc fashion...
next if $osname eq "linux" && $dir =~ m@^/usr/bin|^/opt/local/bin|^/opt/local/lib@;
if ( m|\s+(?:\S+ => )?(\S+)| || m|(^.*\.framework/.*)| ) {
$tmpfile = $1;
$tmpfile=~ s/://g;
my $file = $tmpfile;
if ($rpathflag) {
$file = $_;
}
my $abs_file = abs_path($file);
my $abs_orig = abs_path($orig);
if ($abs_file eq "") {
unless ( $quiet ) { print "invalid abs_file\n"; }
unless ( $quiet ) { print "Orig: $orig_backup Dependency: $dependency_backup\n"; }
next;
}
if ($abs_orig eq "") {
### Use of uninitialized value $_[0] in substitution (s///) at /System/Library/Perl/5.18/File/Basename.pm line 341.
### fileparse(): need a valid pathname at ./make-app-carta line 832.
unless ( $quiet ) { print "invalid abs_orig\n"; }
unless ( $quiet ) { print "Orig: $orig_backup Dependency: $dependency_backup\n"; }
next;
}
# $abs_file = basename(abs_path($file)); usually empty but ok
# $abs_orig = basename(abs_path($orig)); usually empty but ok, seems will not be "eq", most of two empty
if ( basename(abs_path($file)) eq basename(abs_path($orig)) ) {
my $file_name = basename(abs_path($orig));
###
### ASAP python module references itself, and the path where it expects to find itself does not exist...
###
### razor:casa dschieb$ otool -L /tmp/CASA.app/Contents/Resources/python/asap/_asap.324.10.120.so | grep asap
### /tmp/CASA.app/Contents/Resources/python/asap/_asap.324.10.120.so:
### .../release-4_2_2-prerelease-10/darwin/lib/_asap.324.10.120.so (compatibility version 324.10.120, current version 0.0.0)
### .../release-4_2_2-prerelease-10/darwin/lib/libasap.324.10.120.dylib (compatibility version 324.10.120, current version 0.0.0)
###
### so there should be no harm in assuming that we can skip dependencies where the filename
### of the dependency is the same as the filename of the thing it depends on...
if ( ! $quiet && $final_pass ) { print "\tskipping self-reference to $file_name\n" }
### the $final_pass flag signals the final scan where dependencies are tallied, using
### it in this conditional prevents notifying the user of self-references twice...
next;
}
if ( $final_pass ) {
if ( ! -e $file ) {
if ( $ignore_missing_dep ) {
print "warning: dependency $file does NOT exist, $orig depends on it\n";
} else {
if ( basename(abs_path($file)) ne basename(abs_path($orig)) ) {
die "dependency $file does NOT exist, $orig depends on it";
}
}
}
}
unless ( exists $main::libraries_dirs{$dir} ) {
$main::libraries_dirs{$dir} = { };
}
my $ref = $main::libraries_dirs{$dir};
my $subpath = $file;
$subpath =~ s|^$dir/?||;
unless ( exists $ref->{$subpath} ) {
$ref->{$subpath} = 1;
} elsif ( $recursive ) {
if ( $verbose ) {
state $longest_line;
if ( (length($file)+10) > $longest_line ) {
$longest_line = length($file)+10;
}
my $str = sprintf( "%-$longest_line".'s', $file );
print "\r\t<R> $str";
}
collect_dependencies_work($been_there,$file,$recursive);
}
} else {
}
}
}
close( INFO );
#exit(0);
}
sub scan_application {
###
### modifies global %libraries_dirs
###
my $dir = shift( @_ );
my $msg = shift( @_ );
my $final_pass = shift( @_ );
my @binaries = ( );
my @scripts = ( );
my @libraries = ( );
%main::libraries_dirs = ( );
print "Scanning application dir $dir\n";
my $find_func = sub { my $dir = $File::Find::dir;
## skip subversion files, cmake-created applications, and cmake build-tree files...
# if ( $dir =~ m|/\.svn| || $dir =~ m|/MacOS| || $dir =~ m|/CMakeFiles| ) { return }
## skip subversion files and cmake build-tree files... (scanning our application tree)
if ( $dir =~ m|/\.svn| || $dir =~ m|/CMakeFiles| ) { return }
if ( -f $_ ) {
$absf = abs_path($_);
# print "Checking: $absf\n";
my $file_type = resolve_file_type($_);
$file_type eq 'exe' && push( @binaries, "$File::Find::dir/$_" );
$file_type eq 'sh' && push( @scripts, "$File::Find::dir/$_" );
$file_type eq 'lib' && push( @libraries, "$File::Find::dir/$_" );
$file_type eq 'ldso' && push( @libraries, "$File::Find::dir/$_" );
}
};
unless ( $quiet ) { print $msg ? $msg . "\n" : "scanning application...\n" }
find( { wanted => $find_func }, $dir );
unless ( $quiet ) { print "\tfound ",sprintf($spf,scalar(@binaries))," binaries\n" }
unless ( $quiet ) { print "\t ",sprintf($spf,scalar(@scripts))," scripts\n" }
unless ( $quiet ) { print "\t ",sprintf($spf,scalar(@libraries))," shared libraries\n" }
foreach ( @binaries, @libraries ) {
collect_dependencies( $_, $final_pass );
}
### collect_dependencies(...) uses carriage return '\r' to rewrite
### over the output of the files being scanned... and a newline here
### moves us down for more output...
if ( $verbose ) { print "\n\n" }
}
sub resolve_file_type {
state %cache;
my $type = '';
my $f = shift(@_);
if ( $osname eq "linux" && -l $f ) {
### internal symlinks (like all of the mpi binaries) should be disregarded
### OSX looks for python library linked to Python (i.e. a symlink)... so
### we only do this break for linux (there's probably a better way)
return "?";
}
if ( -f $f ) {
my $file = abs_path($f);
return $cache{$file} if exists $cache{$file};
$type = '?';
# assign type based upon filename
$f =~ m|\.h(?:pp)?$| && ($type = 'h');
$f =~ m@\.c(?:pp|c)?$@ && ($type = 'c');
$f =~ m|\.f$| && ($type = 'f');
$f eq 'CMakeLists.txt' && ($type = 'cmake');
$f =~ m@^(?:m|GNUm|M)akefile$@ && ($type = 'make');
if ( $type eq '?' ) {
# resort to using file utility...
my $current_file = `file '$file'`;
if ($current_file =~ m|Mach-O.*?executable|) { $type = 'exe'; }
if ($current_file =~ m|Bourne-Again shell script|) {$type = 'sh'; }
if ($current_file =~ m|Mach-O 64-bit bundle|) { $type = 'ldso'; }
if ($current_file =~ m|Mach-O 64-bit x86_64 bundle|) { $type = 'ldso'; }
if ($current_file =~ m|archive random library|) {$type = 'a'; }
if ($current_file =~ m|ELF.*executable|) { $type = 'exe'; }
if ( $current_file =~ m@dynamically linked shared library|ELF.*shared object@ ) {
my $nme = basename($f);
### for osx framework libraries can look like 'Python' or 'QtCore' so
### this exception keys off of "*.so" for osx... for linux, the
### 'lib...' check is probably enough...
$type = $nme=~ m|^lib| ? 'lib' : $nme =~ m|\.so$| ? 'ldso' : 'lib';
}
##
## we absolutely have to check, because CASA's build system seems to think
## that every python script should be marked as executable...
##
if ( -x $f && $current_file =~ m|python.*?script(?:, ASCII)? text executable| ) {
open( my $py, "< $f" );
my $first_line = <$py>;
close($py);
if ( $first_line =~ m|^#!| ) { $type = 'py'; }
## should create remove_execute_permission(...)
else { system( "chmod -x $f" ) == 0 or die "could not chmod, $f" } ## fix permissions
}
}
$cache{$file} = $type;
}
return $type;
}
sub usage {
my $error = shift(@_);
print "usage:\n\tmake-app [-help] [-q] [-ni] [version=<VERSION-STR>] [template=<LOCAL-TEMPLATE-DIR>] [data=<LOCAL-DATA-DIR>] [branch=<BRANCH-FOR-REVISION>] \\\n";
print "\t ws=<WORKSPACE-PATH> arch=<ARCHITECTURE> out=<OUTPUT-DIR>\n";