-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.sh
2441 lines (1849 loc) · 75.2 KB
/
package.sh
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
#!/bin/bash
# license: lgpl-2.1-or-later
# programmer: Kevin Day
#
# The purpose of this script is to create releases from the project source.
# These release directories can then be used to compile the project or to package the project.
#
# The dependencies of this script are: basename, bash, chmod, grep, sed, and sort.
#
# This script can also be run under zsh rather than bash by setting the environment variable SHELL_ENGINE to "zsh", such as:
# SHELL_ENGINE="zsh" zsh ./package.sh --help
#
package_main() {
if [[ ${SHELL_ENGINE} == "zsh" ]] ; then
emulate ksh
fi
local public_name="Simple FLL Project Package Script"
local system_name=package
local called_name=$(basename $0)
local version=0.6.10
local grab_next=
local do_color=dark
local do_help=
local do_copyright=
local i=0
local p=
local t=0
local c_reset="\\033[0m"
local c_title="\\033[1;33m"
local c_error="\\033[1;31m"
local c_warning="\\033[0;33m"
local c_highlight="\\033[1;32m"
local c_notice="\\033[0;01m"
local c_important="\\033[0;32m"
local c_subtle="\\033[1;30m"
local c_prefix="\\"
local -A variables=()
local failure=0
local operation=
local operation_failure=
local mode_individual=
local mode_level=
local mode_monolithic=
local mode_program=
local mode_stand_alone=
local path_build=build/
local path_destination=package/
local path_sources=./
local prepend=
local verbosity=normal
local verbose=
local verbose_common=
local result=
if [[ $# -gt 0 ]] ; then
t=$#
while [[ ${i} -lt ${t} ]] ; do
let i=${i}+1
if [[ ${SHELL_ENGINE} == "zsh" ]] ; then
p=${(P)i}
else
p=${!i}
fi
if [[ ${grab_next} == "" ]] ; then
if [[ ${p} == "-h" || ${p} == "--help" ]] ; then
do_help=yes
elif [[ ${p} == "+C" || ${p} == "++copyright" ]] ; then
do_copyright="yes"
elif [[ ${p} == "+d" || ${p} == "++dark" ]] ; then
do_color=dark
context="+d"
elif [[ ${p} == "+l" || ${p} == "++light" ]] ; then
do_color=light
context="+l"
elif [[ ${p} == "+n" || ${p} == "++no_color" ]] ; then
do_color=none
context="+n"
elif [[ ${p} == "+Q" || ${p} == "++quiet" ]] ; then
verbosity="quiet"
verbose="+Q"
verbose_common=
elif [[ ${p} == "+E" || ${p} == "++error" ]] ; then
verbosity="error"
verbose="+E"
verbose_common=
elif [[ ${p} == "+N" || ${p} == "++normal" ]] ; then
verbosity=
verbose="+N"
verbose_common=
elif [[ ${p} == "+V" || ${p} == "++verbose" ]] ; then
verbosity="verbose"
verbose="+V"
verbose_common="-v"
elif [[ ${p} == "+D" || ${p} == "++debug" ]] ; then
verbosity="debug"
verbose="+D"
verbose_common="-v"
elif [[ ${p} == "+v" || ${p} == "++version" ]] ; then
echo $version
return 0
elif [[ ${p} == "-b" || ${p} == "--build" ]] ; then
grab_next=path_build
elif [[ ${p} == "-d" || ${p} == "--destination" ]] ; then
grab_next=path_destination
elif [[ ${p} == "-i" || ${p} == "--individual" ]] ; then
mode_individual="yes"
elif [[ ${p} == "-l" || ${p} == "--level" ]] ; then
mode_level="yes"
elif [[ ${p} == "-m" || ${p} == "--monolithic" ]] ; then
mode_monolithic="yes"
elif [[ ${p} == "-P" || ${p} == "--prepend" ]] ; then
grab_next=prepend
elif [[ ${p} == "-p" || ${p} == "--program" ]] ; then
mode_program="yes"
elif [[ ${p} == "-s" || ${p} == "--sources" ]] ; then
grab_next=path_sources
elif [[ ${p} == "-S" || ${p} == "--stand_alone" ]] ; then
grab_next=stand_alone
elif [[ ${operation} == "" ]] ; then
operation="${p}"
elif [[ ${operation_failure} == "" ]] ; then
operation_failure=fail-multiple
operation="${p}"
fi
else
if [[ ${grab_next} == "path_build" ]] ; then
path_build=$(echo ${p} | sed -e 's|^//*|/|' -e 's|/*$|/|')
elif [[ ${grab_next} == "path_destination" ]] ; then
path_destination=$(echo ${p} | sed -e 's|^//*|/|' -e 's|/*$|/|')
elif [[ ${grab_next} == "path_sources" ]] ; then
path_sources=$(echo ${p} | sed -e 's|^//*|/|' -e 's|/*$|/|')
elif [[ ${grab_next} == "stand_alone" ]] ; then
mode_stand_alone="${mode_stand_alone}${p} "
elif [[ ${grab_next} == "prepend" ]] ; then
# Provide a bare minimal sanitizer that probably doesn't catch everything that it ideally should.
prepend=$(echo ${p} | sed -e 's|[\!~\`@#$%^&*();:><?/"\\]||g' -e 's@|@@g' -e "s|'||g" -e "s|\s*||g")
fi
grab_next=
fi
done
p=
fi
package_handle_colors
if [[ ${do_help} == "yes" ]] ; then
package_help
package_cleanup
return 0
fi
if [[ ${do_copyright} == "yes" ]] ; then
package_copyright
package_cleanup
return 0
fi
if [[ ${operation_failure} == "fail-multiple" ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Only one operation may be specified at a time.${c_reset}"
fi
package_cleanup
return 1
fi
if [[ ${mode_stand_alone} != "" ]] ; then
for i in ${mode_stand_alone} ; do
if [[ ${i} == "all" ]] ; then
mode_stand_alone="all"
break
fi
if [[ ! -f build/stand_alone/${i}.settings ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Unknown or unsupported stand alone program '${c_notice}${i}${c_error}'.${c_reset}"
fi
package_cleanup
return 1
fi
done
if [[ ${mode_stand_alone} == "all" ]] ; then
mode_stand_alone=
for i in build/stand_alone/*.settings ; do
mode_stand_alone="${mode_stand_alone}$(echo ${i} | sed -e 's|build/stand_alone/||' -e 's|\.settings$||') "
done
fi
fi
if [[ ${operation} == "build" || ${operation} == "rebuild" ]] ; then
if [[ ! -d ${path_build} ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Build directory '${c_notice}${path_build}${c_error}' is invalid or missing.${c_reset}"
fi
package_cleanup
return 1
fi
if [[ ! -d ${path_destination} ]] ; then
mkdir ${verbose_common} -p ${path_destination}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Package directory '${c_notice}${path_destination}${c_error}' is invalid or could not be created.${c_reset}"
fi
package_cleanup
return 1
fi
fi
if [[ ! -d ${path_sources} ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Sources directory '${c_notice}${path_sources}${c_error}' is invalid or missing.${c_reset}"
fi
package_cleanup
return 1
fi
if [[ ! -d ${path_sources}level_0/ ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Build sources directory '${c_notice}${path_sources}level_0/${c_error}' is invalid or missing.${c_reset}"
fi
package_cleanup
return 1
fi
if [[ ! -d ${path_sources}level_1/ ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Build sources directory '${c_notice}${path_sources}level_1/${c_error}' is invalid or missing.${c_reset}"
fi
package_cleanup
return 1
fi
if [[ ! -d ${path_sources}level_2/ ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Build sources directory '${c_notice}${path_sources}level_2/${c_error}' is invalid or missing.${c_reset}"
fi
package_cleanup
return 1
fi
if [[ ! -d ${path_sources}level_3/ ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Build sources directory '${c_notice}${path_sources}level_3/${c_error}' is invalid or missing.${c_reset}"
fi
package_cleanup
return 1
fi
if [[ ${operation} == "rebuild" ]] ; then
package_operation_clean
if [[ ${?} -ne 0 ]] ; then
failure=1
fi
fi
if [[ ${mode_individual} == "" && ${mode_level} == "" && ${mode_monolithic} == "" && ${mode_program} == "" && ${mode_stand_alone} == "" ]] ; then
mode_individual="yes"
fi
if [[ ${failure} -eq 0 && ${mode_individual} == "yes" ]] ; then
package_operation_individual
if [[ ${?} -ne 0 ]] ; then
failure=1
fi
fi
if [[ ${failure} -eq 0 && ${mode_level} == "yes" ]] ; then
package_operation_level
if [[ ${?} -ne 0 ]] ; then
failure=1
fi
fi
if [[ ${failure} -eq 0 && ${mode_monolithic} == "yes" ]] ; then
package_operation_monolithic
if [[ ${?} -ne 0 ]] ; then
failure=1
fi
fi
if [[ ${failure} -eq 0 && ${mode_program} == "yes" ]] ; then
package_operation_program
if [[ ${?} -ne 0 ]] ; then
failure=1
fi
fi
if [[ ${failure} -eq 0 && ${mode_stand_alone} != "" ]] ; then
package_operation_stand_alone
if [[ ${?} -ne 0 ]] ; then
failure=1
fi
fi
elif [[ ${operation} == "dependencies" ]] ; then
if [[ ! -d ${path_sources} ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Sources directory '${c_notice}${path_sources}${c_error}' is invalid or missing.${c_reset}"
fi
package_cleanup
return 1
fi
package_operation_dependencies
elif [[ ${operation} == "clean" ]] ; then
if [[ ! -d ${path_destination} ]] ; then
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo -e "${c_warning}WARNING: Package directory '${c_notice}${path_destination}${c_error}' does not exist, there is nothing to clean.${c_reset}"
fi
package_cleanup
return 0
fi
package_operation_clean
elif [[ ${operation} == "" ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: No operation was given.${c_reset}"
fi
package_cleanup
return 1
else
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: The operation ${c_notice}${operation}${c_error} was not recognized.${c_reset}"
fi
package_cleanup
return 1
fi
package_cleanup
if [[ ${failure} -eq 1 ]] ; then
return 1
fi
return 0
}
package_handle_colors() {
if [[ $do_color == "light" ]] ; then
c_error="\\033[1;31m"
c_warning="\\033[0;31m"
c_title="\\033[1;34m"
c_highlight="\\033[0;34m"
c_notice="\\033[0;01m"
c_important="\\033[0;35m"
elif [[ $do_color == "none" ]] ; then
c_reset=
c_title=
c_error=
c_warning=
c_highlight=
c_notice=
c_important=
c_subtle=
c_prefix=
fi
}
package_help() {
echo -e "${c_title}${public_name}${c_reset}"
echo -e " ${c_notice}Version ${version}${c_reset}"
echo
echo -e "${c_highlight}${system_name}${c_reset} ${c_notice}[${c_reset} options ${c_notice}]${c_reset} ${c_notice}[${c_reset} operation ${c_notice}]${c_reset}"
echo -e " ${c_important}build${c_reset} Build the package."
echo -e " ${c_important}clean${c_reset} Delete all built packages."
echo -e " ${c_important}dependencies${c_reset} Rebuild all dependencies."
echo -e " ${c_important}rebuild${c_reset} Delete all built packages then build the package."
echo
echo -e "${c_highlight}Options:${c_reset}"
echo -e " -${c_important}h${c_reset}, --${c_important}help${c_reset} Print this help message."
echo -e " +${c_important}C${c_reset}, ++${c_important}copyright${c_reset} Print the copyright."
echo -e " +${c_important}d${c_reset}, ++${c_important}dark${c_reset} Output using colors that show up better on dark backgrounds."
echo -e " +${c_important}l${c_reset}, ++${c_important}light${c_reset} Output using colors that show up better on light backgrounds."
echo -e " +${c_important}n${c_reset}, ++${c_important}no_color${c_reset} Do not print using color."
echo -e " +${c_important}Q${c_reset}, ++${c_important}quiet${c_reset} Decrease verbosity, silencing most print.to."
echo -e " +${c_important}E${c_reset}, ++${c_important}error${c_reset} Decrease verbosity, using only error print.to."
echo -e " +${c_important}N${c_reset}, ++${c_important}normal${c_reset} Set verbosity to normal."
echo -e " +${c_important}V${c_reset}, ++${c_important}verbose${c_reset} Increase verbosity beyond normal print.to."
echo -e " +${c_important}D${c_reset}, ++${c_important}debug${c_reset} Enable debugging, significantly increasing verbosity beyond normal print.to."
echo -e " +${c_important}v${c_reset}, ++${c_important}version${c_reset} Print only the version number."
echo
echo -e "${c_highlight}Package Options:${c_reset}"
echo -e " -${c_important}d${c_reset}, --${c_important}destination${c_reset} Specify a custom package destination directory."
echo -e " -${c_important}b${c_reset}, --${c_important}build${c_reset} Specify a custom build directory."
echo -e " -${c_important}i${c_reset}, --${c_important}individual${c_reset} Build packages by individual package (levels 0. 1. and 2)."
echo -e " -${c_important}l${c_reset}, --${c_important}level${c_reset} Build packages by level (levels 0. 1. and 2)."
echo -e " -${c_important}m${c_reset}, --${c_important}monolithic${c_reset} Build a monolithic package (levels 0. 1. and 2)."
echo -e " -${c_important}P${c_reset}, --${c_important}prepend${c_reset} Prepend a string (with a restricted character set) before the built directory name."
echo -e " -${c_important}p${c_reset}, --${c_important}program${c_reset} Build program packages (level 3)."
echo -e " -${c_important}s${c_reset}, --${c_important}sources${c_reset} Specify a custom sources directory."
echo -e " -${c_important}S${c_reset}, --${c_important}stand_alone${c_reset} Build a specified program package as stand alone."
}
package_copyright() {
echo "Copyright © 2007-2024 Kevin Day."
echo
echo "Source code license lgpl-2.1-or-later."
echo "Standard and specification license open-standard-license-1.0-or-later."
echo "Documentation license cc-by-sa-4.0."
}
package_create_base_files() {
if [[ ! -d ${package} ]] ; then
mkdir ${verbose_common} -p ${package}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to create directory ${c_notice}${package}${c_error}.${c_reset}"
fi
let failure=1
fi
fi
if [[ ${failure} -eq 0 && -d ${path_build}documents ]] ; then
cp ${verbose_common} -R ${path_build}documents ${package}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to copy directory ${c_notice}${path_build}documents${c_error} to ${c_notice}${package}${c_error}.${c_reset}"
fi
let failure=1
fi
fi
if [[ ${failure} -eq 0 ]] ; then
if [[ -d ${path_build}licenses ]] ; then
cp ${verbose_common} -R ${path_build}licenses ${package}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to copy directory ${c_notice}${path_build}licenses${c_error} to ${c_notice}${package}${c_error}.${c_reset}"
fi
let failure=1
fi
elif [[ -d ${path_build}../licenses ]] ; then
cp ${verbose_common} -R ${path_build}../licenses ${package}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to copy directory ${c_notice}${path_build}../licenses${c_error} to ${c_notice}${package}${c_error}.${c_reset}"
fi
let failure=1
fi
else
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Neither directory ${c_notice}${path_build}licenses${c_error} nor ${c_notice}${path_build}../licenses${c_error} can be found or are invalid.${c_reset}"
fi
let failure=1
fi
fi
if [[ ${failure} -eq 0 ]] ; then
cp ${verbose_common} -R ${path_build}scripts/bootstrap.sh ${package}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to copy script ${c_notice}${path_build}bootstrap.sh${c_error} to ${c_notice}${package}${c_error}.${c_reset}"
fi
let failure=1
fi
if [[ ${failure} -eq 0 ]] ; then
chmod ugo+x ${package}bootstrap.sh
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to set executable permissions on script ${c_notice}${package}bootstrap.sh${c_error}.${c_reset}"
fi
let failure=1
fi
fi
if [[ ${failure} -eq 0 ]] ; then
cp ${verbose_common} -R ${path_build}scripts/install.sh ${package}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to copy script ${c_notice}${path_build}install.sh${c_error} to ${c_notice}${package}${c_error}.${c_reset}"
fi
let failure=1
fi
fi
if [[ ${failure} -eq 0 ]] ; then
chmod ugo+x ${package}install.sh
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to set executable permissions on script ${c_notice}${package}install.sh${c_error}.${c_reset}"
fi
let failure=1
fi
fi
fi
if [[ ${failure} -eq 1 ]] ; then
return 1
fi
return 0
}
package_dependencies_individual() {
local level=
local level_current=
local directory=
local settings=
local name=
local dependencies=
local dependencies_0=
local dependencies_1=
local dependencies_2=
local dependencies_thread=
local dependencies_individual=
local dependency=
local dependency_file=
local dependency_files=
local dependency_suffix=
local has_thread=
local sub_level=
local sub_dependencies=
local sub_dependency=
local sub_sub_level=
local sub_sub_dependencies=
local sub_sub_dependency=
for directory in ${path_sources}level_0/* ${path_sources}level_1/* ${path_sources}level_2/* ${path_sources}level_3/* ; do
name="$(echo ${directory} | sed -e "s|${path_sources}level_0/||" -e "s|${path_sources}level_1/||" -e "s|${path_sources}level_2/||" -e "s|${path_sources}level_3/||")"
level_current="$(echo ${directory} | grep -o '\<level_[[:digit:]]/' | sed -e 's|level_||' -e 's|/$||')"
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo
echo -e "${c_highlight}Building Dependencies for ${c_reset}${c_notice}${name}${c_reset}${c_highlight}.${c_reset}"
fi
if [[ ! -d ${directory}/data/build/ ]] ; then
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo -e "${c_warning}WARNING: The project build directory ${c_notice}${dependency_file}${c_warning} is not found.${c_reset}"
fi
continue
fi
# Additional dependency files are only supported in level_3.
if [[ $(echo "${directory}" | grep -o "^${path_sources}level_3/") != "" ]] ; then
dependency_files="yes"
# When the files don't exist bash treats the pattern as a literal file.
# Do not loop through this wild-carded name (a file with the unexpanded name 'dependencies.*' is therefore not supported).
for dependency_file in ${directory}/data/build/dependencies.* ; do
if [[ "${directory}/data/build/dependencies.*" == ${dependency_file} ]] ; then
dependency_files="no"
fi
break
done
else
dependency_files="no"
fi
for dependency_file in ${directory}/data/build/dependencies{,.*} ; do
if [[ ! -f ${dependency_file} ]] ; then
if [[ $dependency_files == "yes" && "${directory}/data/build/dependencies" != ${dependency_file} ]] ; then
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo -e "${c_warning}WARNING: The dependency file ${c_notice}${dependency_file}${c_warning} is not found.${c_reset}"
fi
elif [[ $dependency_files == "no" && "${directory}/data/build/dependencies.*" != ${dependency_file} ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Cannot build dependencies, failed to find ${c_notice}${dependency_file}${c_error} file(s).${c_reset}"
fi
fi
continue
fi
if [[ ${dependency_file} == "${directory}/data/build/dependencies" ]] ; then
dependency_suffix=
else
dependency_suffix=$(echo ${dependency_file} | sed -e "s|^${directory}/data/build/dependencies||")
fi
if [[ ! -f ${directory}/data/build/settings${dependency_suffix} ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Cannot build dependencies for ${c_reset}${c_notice}${name}${c_reset}${c_error}, failed to find ${c_notice}${directory}/data/build/settings${dependency_suffix}${c_error}.${c_reset}"
fi
let failure=1
return 1
fi
dependencies=
dependencies_0=
dependencies_1=
dependencies_2=
dependencies_thread=
dependencies_individual=
has_thread=
dependencies=$(cat ${dependency_file} | sed -e "/^\s*#/d" -e "s|#\.*$||")
if [[ $(echo "$dependencies" | grep -o "\<f_thread\>") != "" ]] ; then
has_thread="yes"
fi
for dependency in $dependencies ; do
if [[ $(echo "${dependency}" | grep -o "^f_") != "" ]] ; then
level=level_0
elif [[ $(echo "${dependency}" | grep -o "^fl_") != "" ]] ; then
level=level_1
elif [[ $(echo "${dependency}" | grep -o "^fll_") != "" ]] ; then
level=level_2
else
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo -e "${c_warning}WARNING: Failed to detect level for dependency ${c_notice}${dependency}${c_warning}.${c_reset}"
fi
continue
fi
if [[ ! -d ${path_sources}${level}/${dependency}/data/build/ ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to find dependency data directory ${c_notice}${path_sources}${level}/${dependency}/data/build/${c_error}.${c_reset}"
fi
let failure=1
return 1
fi
sub_dependencies=
if [[ -f ${path_sources}${level}/${dependency}/data/build/dependencies ]] ; then
sub_dependencies=$(cat ${path_sources}${level}/${dependency}/data/build/dependencies | sed -e "/^\s*#/d" -e "s|#\.*$||")
fi
for sub_dependency in $sub_dependencies ; do
if [[ $(echo "${sub_dependency}" | grep -o "^f_") != "" ]] ; then
sub_level=level_0
elif [[ $(echo "${sub_dependency}" | grep -o "^fl_") != "" ]] ; then
sub_level=level_1
else
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo -e "${c_warning}WARNING: Failed to detect level for sub-dependency ${c_notice}${sub_dependency}${c_warning}.${c_reset}"
fi
continue
fi
if [[ ! -d ${path_sources}${sub_level}/${sub_dependency}/data/build/ ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to find dependency data directory ${c_notice}${path_sources}${sub_level}/${sub_dependency}/data/build/${c_error}.${c_reset}"
fi
let failure=1
return 1
fi
sub_sub_dependencies=
if [[ -f ${path_sources}${sub_level}/${sub_dependency}/data/build/dependencies ]] ; then
sub_sub_dependencies=$(cat ${path_sources}${sub_level}/${sub_dependency}/data/build/dependencies | sed -e "/^\s*#/d" -e "s|#\.*$||")
fi
for sub_sub_dependency in $sub_sub_dependencies ; do
if [[ $(echo "${sub_sub_dependency}" | grep -o "^f_") != "" ]] ; then
sub_sub_level=level_0
else
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo -e "${c_warning}WARNING: Failed to detect level for sub-sub-dependency ${c_notice}${sub_sub_dependency}${c_warning}.${c_reset}"
fi
continue
fi
package_dependencies_individual_append "${sub_sub_level}" "${sub_sub_dependency}"
done
if [[ ${failure} -eq 1 ]] ; then
break;
fi
package_dependencies_individual_append "${sub_level}" "${sub_dependency}"
if [[ ${failure} -eq 1 ]] ; then
break;
fi
done
if [[ ${failure} -eq 1 ]] ; then
break;
fi
package_dependencies_individual_append "${level}" "${dependency}"
if [[ ${failure} -eq 1 ]] ; then
break;
fi
done
if [[ ${failure} -eq 1 ]] ; then
break;
fi
if [[ ${failure} -eq 1 ]] ; then
break;
fi
dependencies_individual=$(echo "${dependencies_individual}" | sed -e 's|^[[:space:]]*||' -e 's|[[:space:]]*$||' -e 's|[[:space:]][[:space:]]*$| |')
if [[ ${dependencies_individual} != "" ]] ; then
if [[ ${verbosity} == "verbose" ]] ; then
echo -e " ${dependencies_individual}"
fi
dependencies_individual=" ${dependencies_individual}"
fi
settings=${directory}/data/build/settings${dependency_suffix}
sed -i -e "s|^\s*build_libraries-individual[[:space:]].*\$|build_libraries-individual${dependencies_individual}|" ${settings} &&
sed -i -e "s|^\s*build_libraries-individual\$|build_libraries-individual${dependencies_individual}|" ${settings}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to update settings file ${c_notice}${settings}${c_error}.${c_reset}"
fi
let failure=1
break
fi
# All level 3 are expected to support all modes: individual, level, and monolithic.
if [[ ${level_current} == "3" ]] ; then
sed -i -e "s|^\s*build_libraries-level\>.*\$|build_libraries-level -lfll_2 -lfll_1 -lfll_0|" ${settings} &&
sed -i -e "s|^\s*build_libraries-level\$|build_libraries-level -lfll_2 -lfll_1 -lfll_0|" ${settings}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to update settings file ${c_notice}${settings}${c_error}.${c_reset}"
fi
let failure=1
break
fi
sed -i -e "s|^\s*build_libraries-monolithic\>.*\$|build_libraries-monolithic -lfll|" ${settings} &&
sed -i -e "s|^\s*build_libraries-monolithic\$|build_libraries-monolithic -lfll|" ${settings}
if [[ ${?} -ne 0 ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to update settings file ${c_notice}${settings}${c_error}.${c_reset}"
fi
let failure=1
break
fi
fi
done
if [[ ${failure} -eq 1 ]] ; then
break;
fi
done
if [[ ${failure} -eq 1 ]] ; then
return 1
fi
return 0
}
package_dependencies_individual_append() {
local level="$1"
local dependency="$2"
local settings=
local libraries=
settings=${path_sources}${level}/${dependency}/data/build/settings
if [[ ! -f ${settings} ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to find dependency settings file ${c_notice}${settings}${c_error}.${c_reset}"
fi
let failure=1
return 1
fi
libraries=$(grep -o '^\s*build_sources_library\s.*$' ${settings} | sed -e 's|^\s*build_sources_library\>||' -e 's|^\s*||' -e 's|\s*$||')
if [[ $libraries != "" ]] ; then
if [[ $(echo -n ${dependencies_individual} | grep -o "\-l${dependency}\>") == "" ]] ; then
if [[ "$(type -p sort)" != "" ]] ; then
if [[ ${level} == "level_0" ]] ; then
dependencies_0="-l${dependency} ${dependencies_0}"
dependencies_0="$(for i in ${dependencies_0}; do echo ${i} ; done | sort)"
dependencies_0=$(echo ${dependencies_0})
elif [[ ${level} == "level_1" ]] ; then
dependencies_1="-l${dependency} ${dependencies_1}"
dependencies_1=" $(for i in ${dependencies_1}; do echo ${i} ; done | sort)"
dependencies_1=$(echo ${dependencies_1})
else
dependencies_2="-l${dependency} ${dependencies_2}"
dependencies_2=" $(for i in ${dependencies_2}; do echo ${i} ; done | sort)"
dependencies_2=$(echo ${dependencies_2})
fi
else
# Sort is not available, so do not bother trying to sort.
if [[ ${level} == "level_0" ]] ; then
dependencies_0="-l${dependency} ${dependencies_0}"
elif [[ ${level} == "level_1" ]] ; then
dependencies_1="-l${dependency} ${dependencies_1}"
else
dependencies_2="-l${dependency} ${dependencies_2}"
fi
fi
dependencies_individual=
if [[ ${dependencies_0} != "" ]] ; then
dependencies_individual="${dependencies_0}"
fi
if [[ ${dependencies_1} != "" ]] ; then
dependencies_individual="${dependencies_1} ${dependencies_individual}"
fi
if [[ ${dependencies_2} != "" ]] ; then
dependencies_individual="${dependencies_2} ${dependencies_individual}"
fi
fi
fi
if [[ ${failure} -eq 1 ]] ; then
return 1
fi
return 0
}
package_dependencies_level() {
local directory=
local name=
local settings=
local library=
local libraries=
local header=
local headers=
package_dependencies_level_update "level_0" ""
if [[ ${failure} -eq 0 ]] ; then
package_dependencies_level_update "level_1" " -lfll_0"
fi
if [[ ${failure} -eq 0 ]] ; then
package_dependencies_level_update "level_2" " -lfll_1 -lfll_0"
fi
if [[ ${failure} -eq 1 ]] ; then
return 1
fi
return 0
}
package_dependencies_level_update() {
local level="$1"
local level_libraries="$2"
local level_sources_library=
local level_sources_headers=
local level_sources_library_threaded=
local level_sources_headers_threaded=
local monolithic_libraries=
local monolithic_headers=
local monolithic_libraries_threaded=
local monolithic_headers_threaded=
if [[ ${verbosity} != "quiet" && ${verbosity} != "error" ]] ; then
echo
echo -e "${c_highlight}Building Dependencies: ${c_notice}${level}${c_highlight}.${c_reset}"
fi
for directory in ${path_sources}${level}/* ; do
name="$(echo ${directory} | sed -e "s|${path_sources}${level}/||")"
settings=${directory}/data/build/settings
if [[ ! -f ${settings} ]] ; then
if [[ ${verbosity} != "quiet" ]] ; then
echo -e "${c_error}ERROR: Failed to find settings file ${c_notice}${settings}${c_error}.${c_reset}"
fi
let failure=1
return 1
fi
libraries=$(grep -o '^\s*build_sources_library\s.*$' ${settings} | sed -e 's|^\s*build_sources_library\>||' -e 's|^\s*||' -e 's|\s*$||')
for library in $libraries ; do
if [[ ${name} == "f_thread" ]] ; then
level_sources_library_threaded="${level_sources_library_threaded} $library"
monolithic_libraries_threaded="${monolithic_libraries_threaded} ${level}/${library}"
else
level_sources_library="${level_sources_library} $library"
monolithic_libraries="${monolithic_libraries} ${level}/${library}"
fi
done
headers=$(grep -o '^\s*build_sources_headers\s.*$' ${settings} | sed -e 's|^\s*build_sources_headers\>||' -e 's|^\s*||' -e 's|\s*$||')
for header in $headers ; do
if [[ ${name} == "f_thread" ]] ; then
level_sources_headers_threaded="${level_sources_headers_threaded} $header"
monolithic_headers_threaded="${monolithic_headers_threaded} ${level}/${header}"
else
level_sources_headers="${level_sources_headers} $header"
monolithic_headers="${monolithic_headers} ${level}/${header}"
fi
done
done