-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathshellb
2440 lines (2191 loc) · 59.5 KB
/
shellb
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 bash
# Sourced from https://raw.githubusercontent.com/damphat/kv-bash/master/kv-bash
# ABOUT kv-bash:
# key/value dabatase
# database store in HOME directory
# each user has 1 database
# imports 5 bash functions via ```$ source kv-bash```
#
# Author: damphat
# Version: 0.1
# Requirements: unix-like environement, no dependencies
#
# USAGE:
# source ./kv-bash # import kv-bash functions
# kvset <key> <value> # assign value to key
# kvget <key> # get value of key
# kvdel <key> # kvdelete by key
# kvlist # list all current key/value pairs
# kvclear # clear database
#
# EXAMPLES:
# $ source ./kv-bash
# $ kvset user mr.bob
# $ kvset pass abc@123
# $ kvlist
# user mr.bob
# pass abc@123
# $ kvget user
# mr.bob
# $ kvget pass
# abc@123
# $ kvdel pass
# $ kvget pass
#
# $ kvclear
########################
# CONSTANTS
########################
KV_USER_DIR="$HOME/.kv-bash"
########################
# LOCAL FUNCTIONS
########################
# print to stderr, red color
kv_echo_err() {
echo -e "\e[01;31m$@\e[0m" >&2
}
# Usage: kv_echo_err_box <err-msg> <function-name>
kv_echo_err_box() {
kv_echo_err " +-------------------------------+"
kv_echo_err " | ERROR: $1"
kv_echo_err " | function: $2"
kv_echo_err " +-------------------------------+"
}
# Usage: kv_validate_key <key>
kv_validate_key() {
[[ "$1" =~ ^[0-9a-zA-Z._:-]+$ ]]
}
########################
# ENSURE THIS-FILE IS CALL BY 'source ./kv-bash'
########################
#[[ "${BASH_SOURCE[0]}" != "${0}" ]] || {
# kv_echo_err " +------------------------------------------------+"
# kv_echo_err " | FALTAL ERROR: wrong usage :( |"
# kv_echo_err " | You should use this via source |"
# kv_echo_err " | $ source ./kv-bash |"
# kv_echo_err " | |"
# kv_echo_err " | Examples: |"
# kv_echo_err " | $ source ./kv-bash |"
# kv_echo_err " | $ kvset user mr.bob |"
# kv_echo_err " | $ kvset pass abc@123 |"
# kv_echo_err " | $ kvlist |"
# kv_echo_err " | user mr.bob |"
# kv_echo_err " | pass abc@123 |"
# kv_echo_err " | $ kvget user |"
# kv_echo_err " | mr.bob |"
# kv_echo_err " | $ kvget pass |"
# kv_echo_err " | abc@123 |"
# kv_echo_err " | $ kvdel pass |"
# kv_echo_err " | $ kvget pass |"
# kv_echo_err " | |"
# kv_echo_err " | $ kvclear |"
# kv_echo_err " +------------------------------------------------+"
# exit 1
#}
########################
# PUBLIC FUNCTIONS
########################
# Usage: kvget <key>
kvget() {
key="$1"
kv_validate_key "$key" || {
kv_echo_err_box 'invalid param "key"' 'kvget()'
return 1
}
VALUE="$([ -f "$KV_USER_DIR/$key" ] && cat "$KV_USER_DIR/$key")"
echo "$VALUE"
[ "$VALUE" != "" ]
}
# Usage: kvset <key> [value]
kvset() {
key="$1"
value="$2"
kv_validate_key "$key" || {
kv_echo_err_box 'invalid param "key"' 'kvset()'"$key"
return 1
}
test -d "$KV_USER_DIR" || mkdir "$KV_USER_DIR"
echo "$value" > "$KV_USER_DIR/$key"
}
# Usage: kvdel <key>
kvdel() {
key="$1"
kv_validate_key "$key" || {
kv_echo_err_box 'invalid param "key"' 'kvdel()'
return 1
}
test -f "$KV_USER_DIR/$key" && rm -f "$KV_USER_DIR/$key"
}
# list all key/value pairs to stdout
# Usage: kvlist
kvlist() {
for i in "$KV_USER_DIR/"*; do
if [ -f "$i" ]; then
key="$(basename "$i")"
echo "$key" "$(kvget "$key")"
fi
done
}
# clear all key/value pairs in database
# Usage: kvclear
kvclear() {
rm -rf "$KV_USER_DIR"
}
#utils/commands.sh
# Removes any non-alphabetical and non numeric characters from a possible
# shell variable name, allows only underscore
function sanitize_var() {
echo $(echo ${1} | sed -e "s|[^a-zA-Z0-9_]||g")
}
# Removes any non-alphabetical and non numeric characters from a possible
# shell variable name, allows only underscore
function sanitize_var1() {
echo $(echo ${1} | sed -e "s|[^a-zA-Z0-9_]|_|g")
}
# Maintains a list of process ids of background processes
BG_PIDS=
# Execute a background process
# if LOG_MODE=1, then echo the message passed and then execute the command
# if LOG_MODE=2, then echo the command itself before executing the command
function bexe() {
if [ "$LOG_MODE" = "1" ]; then
echo -e "$1" >> $cmds_log_file
shift
"$@" >> $cmds_log_file 2>&1 &
else
shift
echo "\$ $@" >> $cmds_log_file
"$@" >> $cmds_log_file 2>&1 &
fi
BG_PIDS+=" $!"
}
function wait_bexe() {
# Wait for all processes to finish, will take max 14s
# as it waits in order of launch, not order of finishing
for p in $BG_PIDS; do
BG_PIDS=$(echo ${BG_PIDS/$p /})
if wait $p; then
:
else
: #exit 1
fi
done
}
# Execute a foreground process
# if LOG_MODE=1, then echo the message passed and then execute the command
# if LOG_MODE=2, then echo the command itself before executing the command
function exe() {
if [ "$LOG_MODE" = "1" ]; then
#echo -e "$1"
shift
"$@" >> $cmds_log_file 2>&1
else
shift
echo "\$ $@" >> $cmds_log_file 2>&1
"$@" >> $cmds_log_file 2>&1
fi
return $?
}
# Remove any relative path identifiers from a path variable
function remove_relative_path() {
tem_=$1
if [[ "$1" =~ ^../ ]]; then
tem_=$(echo "${tem_#../}")
remove_relative_path "$tem_"
elif [[ "$1" =~ ^./ ]]; then
tem_=$(echo "${tem_#./}")
remove_relative_path "$tem_"
elif [[ "$1" =~ ^/ ]]; then
tem_=$(echo "${tem_#/}")
remove_relative_path "$tem_"
else
echo $tem_
fi
}
# Check if a command exists in the PATH
function exists() {
command -v "$1" >/dev/null 2>&1
}
# Get an md5sum value of a string value
function get_key() {
t_=`echo -n $1 | md5sum | xargs`
t_=${t_% *}
echo $t_
}
# Get the value from key-value map -- kv-bash
function get_cmd() {
kvget "SH_$(get_key ${1})"
}
# Find an executable among the various options provided if it exists on PATH
function find_cmd() {
cmd_=
for ((i = 3; i <= $#; i++ ))
do
if exists ${!i}
then
cmd_=${!i}
break
fi
done
if [ "$cmd_" = "" ]
then
echo "no $1 command found"
if [ "$2" != "" ]
then
exit 1
fi
else
#echo "$1 command found... $cmd_"
echo "$cmd_"
fi
}
function showprogress() {
if [ "$BUILD_SYS" = "emb" ]; then
percent_numer=$((percent_numer+$1))
ProgressBar "$percent_numer" "$percent_denom" "$2"
fi
}
function completeprogress() {
ProgressBar "$percent_denom" "$percent_denom" "$1"
}
_maxlen=0
#https://github.com/fearside/ProgressBar/blob/master/progressbar.sh
# 1. Create ProgressBar function
# 1.1 Input is currentState($1) and totalState($2)
function ProgressBar {
# Process data
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
# Build progressbar string lengths
_done=$(printf "%${_done}s")
_left=$(printf "%${_left}s")
_msg="Progress"
if [ "$1" != "" ]; then
_msg="$3"
fi
mlen=${#_msg}
if [ "$mlen" -lt 8 ]
then
rem_=$((8-mlen))
for i in $(seq 1 $rem_)
do
_msg+=' '
done
fi
if [ $mlen -gt $_maxlen ]
then
_maxlen=$mlen
fi
#echo $_maxlen
rmsg=""
rem_=$((_maxlen-8))
for i in $(seq 1 $_maxlen)
do
rmsg+=' '
done
# 1.2 Build progressbar strings and print the ProgressBar line
# 1.2.1 Output example:
# 1.2.1.1 Progress : [########################################] 100%
printf "\r${_msg} : [${_done// /#}${_left// /-}] ${_progress}%%${rmsg}"
}
#tools/bazel/bazel-util.sh
BZL_SRCES=
BZL_DEFINES=
BZL_LINKOPTS=
BZL_COPTS=
BZL_DEPS=
BZL_HDRS=
BZL_REPO_PATH=
BZL_REPO_BUILD_FILE=
BZL_REPO_HDRS=
BZL_SRC_PATH=
BZL_SRC_NAME=
BZL_SRC_NAME_R=
count=0
bzl_bin_build_=$(cat <<-END
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_import(
name = "@BZL_SRC_NAME@-inc",
hdrs = glob(["**/*.h"]),
visibility = ["//visibility:public"]
)
cc_binary(
name = "@BZL_SRC_NAME@",
srcs = [@BZL_SRCES@],
local_defines = [@BZL_DEFINES@],
linkopts = [@BZL_LINKOPTS@],
copts = [@BZL_COPTS@],
deps = [@BZL_DEPS@],
visibility = ["//visibility:public"]
)
END
)
bzl_libst_build_=$(cat <<-END
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_import(
name = "@BZL_SRC_NAME@-inc",
hdrs = glob(["**/*.h"]),
visibility = ["//visibility:public"]
)
cc_library(
name = "@BZL_SRC_NAME@",
srcs = [@BZL_SRCES@],
local_defines = [@BZL_DEFINES@],
linkopts = [@BZL_LINKOPTS@],
copts = [@BZL_COPTS@],
deps = [@BZL_DEPS@],
hdrs = [@BZL_HDRS@],
visibility = ["//visibility:public"]
)
END
)
bzl_libso_build_=$(cat <<-END
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_import(
name = "@BZL_SRC_NAME@-inc",
hdrs = glob(["**/*.h"]),
shared_library = "lib@BZL_SRC_NAME@.@SHLIB_EXT@",
visibility = ["//visibility:public"]
)
cc_binary(
linkshared = True,
name = "@BZL_SRC_SO_NAME@",
srcs = [@BZL_SRCES@],
local_defines = [@BZL_DEFINES@],
linkopts = [@BZL_LINKOPTS@],
copts = [@BZL_COPTS@],
deps = [@BZL_DEPS@],
visibility = ["//visibility:public"]
)
END
)
bzl_libstso_build_=$(cat <<-END
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_import(
name = "@BZL_SRC_NAME@-inc",
hdrs = glob(["**/*.h"]),
shared_library = "lib@BZL_SRC_NAME@.@SHLIB_EXT@",
visibility = ["//visibility:public"]
)
cc_binary(
linkshared = True,
name = "@BZL_SRC_SO_NAME@",
srcs = [@BZL_SRCES@],
local_defines = [@BZL_DEFINES@],
linkopts = [@BZL_LINKOPTS@],
copts = [@BZL_COPTS@],
deps = [@BZL_DEPS@],
visibility = ["//visibility:public"]
)
cc_library(
name = "@BZL_SRC_NAME@",
srcs = [@BZL_SRCES@],
local_defines = [@BZL_DEFINES@],
linkopts = [@BZL_LINKOPTS@],
copts = [@BZL_COPTS@],
deps = [@BZL_DEPS@],
hdrs = [@BZL_HDRS@],
visibility = ["//visibility:public"]
)
END
)
bzl_ws_local_repo_build_=$(cat <<-END
local_repository(
name = "@BZL_SRC_NAME@-repo",
path = "@BZL_SRC_PATH@",
)
END
)
bzl_ws_new_ext_repo_build_=$(cat <<-END
new_local_repository(
name = "@BZL_SRC_NAME_R@-r",
path = "@BZL_REPO_PATH@",
build_file = "@BZL_REPO_BUILD_FILE@"
)
END
)
bzl_ws_local_repodef_build_=$(cat <<-END
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "@BZL_SRC_NAME_R@-rd",
hdrs = @BZL_REPO_HDRS@,
includes = [
"."
],
visibility = ["//visibility:public"]
)
END
)
BZL_DONE_REPOS=
function bzl_gen_build_file() {
BZL_LINKOPTS="$LPATHSQ$4"
BZL_LINKOPTS="${BZL_LINKOPTS%?}"
BZL_SRCES="${BZL_SRCES%?}"
BZL_HDRS="${BZL_HDRS%?}"
BZL_DEPS="$3"
count=1
ex3_=
for ex3_ in ${INCSQ//,/ }
do
if [[ $ex3_ != "/"* ]] && [[ $ex3_ != "."* ]]; then
PINCSQ+="\"-I$ex3_\","
fi
if [[ $ex3_ != "/"* ]]; then
continue
fi
BZL_SRC_NAME_R=$(sanitize_var1 ${ex3_})
if [[ $BZL_SRC_NAME_R = "_"* ]]; then
BZL_SRC_NAME_R="${BZL_SRC_NAME_R:1}"
fi
BZL_DEPS+="\"@${BZL_SRC_NAME_R}-r//:${BZL_SRC_NAME_R}-rd\","
if [[ $BZL_DONE_REPOS = *"${ex3_} "* ]]; then
continue
fi
BZL_DONE_REPOS+="${ex3_} "
BZL_REPO_PATH="$ex3_"
BZL_REPO_BUILD_FILE="BUILD_$count.bazel"
printf "$bzl_ws_new_ext_repo_build_\n" > /tmp/.bzl_ws_new_ext_repo_build_
templatize "/tmp/.bzl_ws_new_ext_repo_build_" /tmp/.bzl_ws_new_ext_repo_build__ "BZL_SRC_NAME_R,BZL_REPO_PATH,BZL_REPO_BUILD_FILE,count"
cat /tmp/.bzl_ws_new_ext_repo_build__ >> "$DIR/WORKSPACE.bazel"
BZL_REPO_HDRS="glob([\"**/*.h\"]) + glob([\"**/*.hh\"]) + glob([\"**/*.hpp\"])"
printf "$bzl_ws_local_repodef_build_\n" > /tmp/.bzl_ws_local_repo_build_
templatize "/tmp/.bzl_ws_local_repo_build_" "$DIR/$BZL_REPO_BUILD_FILE" "BZL_SRC_NAME_R,BZL_REPO_HDRS,count"
count=$((count+1))
done
BZL_COPTS="${PINCSQ%?}"
BZL_DEPS="${BZL_DEPS%?}"
BZL_SRC_PATH="$1"
printf "$bzl_ws_local_repo_build_\n" > /tmp/.bzl_ws_local_repo_build_
templatize "/tmp/.bzl_ws_local_repo_build_" /tmp/.bzl_ws_local_repo_build__ "BZL_SRC_NAME,BZL_SRC_PATH,count"
cat /tmp/.bzl_ws_local_repo_build__ >> "$DIR/WORKSPACE.bazel"
count=$((count+1))
src="$1"
#BZL_SRCES=${BZL_SRCES//$1\//}
if [ "$5" != "" ]; then
for idir in ${5//,/ }
do
src1=`printf "%s\n%s\n" "$src" "$idir" | sed -e 'N;s/^\(.*\).*\n\1.*$/\1/'`
#echo "$src -- $idir -- $src1"
src="$src1"
BZL_COPTS+=",\"-I$idir\""
done
#BZL_COPTS="${BZL_COPTS%?}"
BZL_HDRS=${BZL_HDRS//$src/}
BZL_SRCES=${BZL_SRCES//$src/}
else
BZL_HDRS=${BZL_HDRS//$src\//}
BZL_SRCES=${BZL_SRCES//$src\//}
fi
#echo "$src"
if [[ $src = *"/" ]]; then
src="${src%?}"
fi
kvset "BN_$(get_key $BZL_SRC_NAME-inc)" "//$src:$BZL_SRC_NAME-inc"
if [ "$2" = "binary" ]; then
printf "$bzl_bin_build_\n" > /tmp/.bzl_bin_build_
kvset "BN_$(get_key $BZL_SRC_NAME)" "//$src:$BZL_SRC_NAME"
elif [ "$2" = "shared" ]; then
BZL_SRC_SO_NAME="lib$BZL_SRC_NAME.$SHLIB_EXT"
printf "$bzl_libso_build_\n" > /tmp/.bzl_bin_build_
kvset "BN_$(get_key $BZL_SRC_NAME)" "//$src:$BZL_SRC_SO_NAME"
elif [ "$2" = "static" ]; then
printf "$bzl_libst_build_\n" > /tmp/.bzl_bin_build_
kvset "BN_$(get_key $BZL_SRC_NAME)" "//$src:$BZL_SRC_NAME"
elif [ "$2" = "stared" ]; then
BZL_SRC_SO_NAME="lib$BZL_SRC_NAME.$SHLIB_EXT"
printf "$bzl_libstso_build_\n" > /tmp/.bzl_bin_build_
kvset "BN_$(get_key $BZL_SRC_NAME)" "//$src:$BZL_SRC_SO_NAME"
fi
if [ ! -d ".bztmp/$src" ]; then
mkdir -p ".bztmp/$src"
fi
templatize "/tmp/.bzl_bin_build_" ".bztmp/$src/.bzl_bin_build_" "BZL_SRC_NAME,BZL_SRCES,BZL_DEFINES,BZL_LINKOPTS,BZL_COPTS,BZL_DEPS,BZL_HDRS,SHLIB_EXT,BZL_SRC_SO_NAME"
if [ ! -f ".bztmp/$src/BUILD.bazel" ]; then
cat ".bztmp/$src/.bzl_bin_build_" > ".bztmp/$src/BUILD.bazel"
else
cat ".bztmp/$src/.bzl_bin_build_" >> ".bztmp/$src/BUILD.bazel"
fi
cp ".bztmp/$src/BUILD.bazel" "$DIR/$src/BUILD.bazel"
#cat "$DIR/$1/BUILD.bazel"
}
function do_bazel_build() {
ex4_=
for ex4_ in ${1//,/ }
do
if [ "$ex4_" != "" ]; then
tmp=$(get_key $ex4_)
tmp=$(kvget "BN_$tmp")
if [ "$tmp" != "" ]; then
ret=`bazel build $tmp`
if [ "$?" -eq "0" ]; then
tmp="${tmp:1}"
tmp=${tmp//:/\/}
echo "bazel-bin$tmp"
exe "" cp -f "bazel-bin$tmp" $SB_OUTDIR/.bin
fi
fi
fi
done
rm -rf .bztmp
}
function do_bazel_pre_build() {
rm -rf .bztmp || true
mkdir .bztmp
echo "" > "$DIR/WORKSPACE.bazel"
}
#tools/buck2/buck2-util.sh
BCK2_SRCES=
BCK2_DEFINES=
BCK2_LINKOPTS=
BCK2_COPTS=
BCK2_DEPS=
BCK2_HDRS=
BCK2_REPO_PATH=
BCK2_REPO_BUILD_FILE=
BCK2_REPO_HDRS=
BCK2_SRC_PATH=
BCK2_SRC_NAME=
BCK2_SRC_NAME_R=
BCK2_EXT_INCS=
BCK2_EX_FLAGS="\"-I/usr/local/include\","
count=0
bck2_bin_build_=$(cat <<-END
cxx_binary(
name = "@BCK2_SRC_NAME@",
srcs = [@BCK2_SRCES@],
linker_flags = [@BCK2_LINKOPTS@],
include_directories = [@BCK2_COPTS@],
compiler_flags = [@BCK2_EX_FLAGS@],
deps = [@BCK2_DEPS@],
visibility = ["PUBLIC"]
)
END
)
bck2_libst_build_=$(cat <<-END
cxx_library(
name = "@BCK2_SRC_NAME@",
srcs = [@BCK2_SRCES@],
linker_flags = [@BCK2_LINKOPTS@],
link_style = "static",
include_directories = [@BCK2_COPTS@],
compiler_flags = [@BCK2_EX_FLAGS@],
deps = [@BCK2_DEPS@],
exported_headers = [@BCK2_HDRS@],
visibility = ["PUBLIC"]
)
END
)
bck2_libso_build_=$(cat <<-END
cxx_library(
name = "@BCK2_SRC_SO_NAME@",
soname = "@BCK2_SRC_SO_NAME@",
srcs = [@BCK2_SRCES@],
linker_flags = [@BCK2_LINKOPTS@],
link_style = "shared",
include_directories = [@BCK2_COPTS@],
compiler_flags = [@BCK2_EX_FLAGS@],
deps = [@BCK2_DEPS@],
exported_headers = [@BCK2_HDRS@],
visibility = ["PUBLIC"]
)
END
)
bck2_libstso_build_=$(cat <<-END
cxx_library(
name = "@BCK2_SRC_NAME@",
srcs = [@BCK2_SRCES@],
linker_flags = [@BCK2_LINKOPTS@],
link_style = "static",
include_directories = [@BCK2_COPTS@],
compiler_flags = [@BCK2_EX_FLAGS@],
deps = [@BCK2_DEPS@],
exported_headers = [@BCK2_HDRS@],
visibility = ["PUBLIC"]
)
cxx_library(
name = "@BCK2_SRC_SO_NAME@",
soname = "@BCK2_SRC_SO_NAME@",
srcs = [@BCK2_SRCES@],
linker_flags = [@BCK2_LINKOPTS@],
link_style = "shared",
include_directories = [@BCK2_COPTS@],
compiler_flags = [@BCK2_EX_FLAGS@],
deps = [@BCK2_DEPS@],
exported_headers = [@BCK2_HDRS@],
visibility = ["PUBLIC"]
)
END
)
bck2_bconfig_build_=$(cat <<-END
[project]
ignore = .git
[repositories]
root = .
prelude = prelude
toolchains = toolchains
none = none
[repository_aliases]
config = prelude
fbcode = none
fbsource = none
buck = none
[parser]
default_build_file_syntax = SKYLARK
target_platform_detector_spec = target:root//...->prelude//platforms:default
[cxx]
cxxflags = @CPPFLAGS@
should_remap_host_platform = true
; untracked_headers = error
untracked_headers_whitelist = /usr/include/.*, /usr/local/include/.*, /usr/lib/.*, @BCK2_EXT_INCS@
ldflags = -L/usr/local/lib
END
)
bck2_prebuilt_dep_build_=$(cat <<-END
prebuilt_cxx_library(
name = '@DEP@',
header_namespace = '',
header_only = True,
exported_linker_flags = [
'-l@DEP@',
],
)
END
)
BCK2_DONE_REPOS=
function bck2_gen_build_file() {
BCK2_LINKOPTS="$LPATHSQ"
BCK2_LINKOPTS="${BCK2_LINKOPTS%?}"
BCK2_COPTS="${PINCSQ%?}"
BCK2_SRCES="${BCK2_SRCES%?}"
BCK2_HDRS="${BCK2_HDRS%?}"
BCK2_DEPS="$3"
ex5_=
for ex5_ in ${CPPFLAGS// / }
do
if [[ $BCK2_EX_FLAGS != *"\"$ex5_\""* ]]; then
BCK2_EX_FLAGS+="\"$ex5_\","
fi
done
ex6_=
for ex6_ in ${INCSQ//,/ }
do
if [[ $ex6_ = "../"* ]]; then
if [[ $BCK2_EX_FLAGS != *"\"-I$ex6_\""* ]]; then
BCK2_EX_FLAGS+="\"-I$ex6_\","
fi
fi
if [[ $ex6_ != "/"* ]]; then
continue
fi
if [[ $BCK2_DONE_REPOS = *"${ex6_} "* ]]; then
continue
fi
BCK2_DONE_REPOS+="${ex6_} "
if [[ $ex6_ != "/usr/include/"* ]] && [[ $ex6_ != "/usr/local/include/"* ]]; then
BCK2_EXT_INCS+="$ex6_/.*, "
fi
if [[ $BCK2_EX_FLAGS != *"\"-I$ex6_\""* ]]; then
BCK2_EX_FLAGS+="\"-I$ex6_\","
fi
done
BCK2_DEPS="${BCK2_DEPS%?}"
if [[ $BCK2_EX_FLAGS = *"," ]]; then
BCK2_EX_FLAGS="${BCK2_EX_FLAGS%?}"
fi
BCK2_SRC_PATH="$1"
src="$1"
#BCK2_SRCES=${BCK2_SRCES//$1\//}
if [ "$5" != "" ]; then
for idir in ${5//,/ }
do
src1=`printf "%s\n%s\n" "$src" "$idir" | sed -e 'N;s/^\(.*\).*\n\1.*$/\1/'`
#echo "$src -- $idir -- $src1"
src="$src1"
BCK2_COPTS+=",\"-I$idir\""
done
#BCK2_COPTS="${BCK2_COPTS%?}"
#BCK2_HDRS=${BCK2_HDRS//$src/}
#BCK2_SRCES=${BCK2_SRCES//$src/}
#BCK2_COPTS=${BCK2_COPTS//$src/}
else
#BCK2_HDRS=${BCK2_HDRS//$src\//}
#BCK2_SRCES=${BCK2_SRCES//$src\//}
#BCK2_COPTS=${BCK2_COPTS//$src\//}
:
fi
#BCK2_COPTS=${BCK2_COPTS//\"$src\"/}
BCK2_COPTS=${BCK2_COPTS//\"-I/\"}
#echo "$src"
if [[ $src = *"/" ]]; then
src="${src%?}"
fi
#kvset "BN_$(get_key $BCK2_SRC_NAME-inc)" "//$src:$BCK2_SRC_NAME-inc"
if [ "$2" = "binary" ]; then
printf "$bck2_bin_build_\n" > /tmp/.bck2_bin_build_
kvset "BN_$(get_key $BCK2_SRC_NAME)" "//:$BCK2_SRC_NAME"
elif [ "$2" = "shared" ]; then
BCK2_SRC_SO_NAME="lib$BCK2_SRC_NAME.$SHLIB_EXT"
printf "$bck2_libso_build_\n" > /tmp/.bck2_bin_build_
kvset "BN_$(get_key $BCK2_SRC_NAME)" "//:$BCK2_SRC_SO_NAME"
elif [ "$2" = "static" ]; then
printf "$bck2_libst_build_\n" > /tmp/.bck2_bin_build_
kvset "BN_$(get_key $BCK2_SRC_NAME)" "//:$BCK2_SRC_NAME"
elif [ "$2" = "stared" ]; then
BCK2_SRC_SO_NAME="lib$BCK2_SRC_NAME.$SHLIB_EXT"
printf "$bck2_libstso_build_\n" > /tmp/.bck2_bin_build_
kvset "BN_$(get_key $BCK2_SRC_NAME)" "//:$BCK2_SRC_SO_NAME"
fi
templatize "/tmp/.bck2_bin_build_" "/tmp/.bck2_bin_build__" "BCK2_EX_FLAGS,BCK2_SRC_NAME,BCK2_SRCES,BCK2_DEFINES,BCK2_LINKOPTS,BCK2_COPTS,BCK2_DEPS,BCK2_HDRS,SHLIB_EXT,BCK2_SRC_SO_NAME"
cat /tmp/.bck2_bin_build__ >> "$DIR/BUCK"
}
function do_buck2_build() {
printf "$bck2_bconfig_build_\n" > /tmp/.bck2_bconfig_build_
templatize "/tmp/.bck2_bconfig_build_" /tmp/.bck2_bconfig_build___ "BCK2_EXT_INCS,CPPFLAGS"
cat /tmp/.bck2_bconfig_build___ >> "$DIR/.buckconfig"
ex7_=
for ex7_ in ${1//,/ }
do
if [ "$ex7_" != "" ]; then
tmp=$(get_key $ex7_)
tmp=$(kvget "BN_$tmp")
if [ "$tmp" != "" ]; then
#buck2 build $tmp --show-full-output
out_path=`buck2 build $tmp --show-full-output | cut -d' ' -f2-`
echo "$out_path"
if [ "$out_path" != "" ]; then
exe "" cp -f "$out_path" $SB_OUTDIR/.bin
fi
fi
fi
done
}
function do_buck2_pre_build() {
rm -rf prelude || true
wget https://github.com/facebook/buck2-prelude/archive/refs/heads/main.zip && unzip main.zip && mv buck2-prelude-main prelude
buck2 clean
rm -f main.zip
echo "" > "$DIR/.buckconfig"
echo "" > "$DIR/BUCK"
}
#platform/c_cpp/checks.sh
# The c compiler
MY_CC=
# The c++ compiler
MY_CPP=
# The static library archiver
MY_AR=
# The list of #define(s) to be declared post a configuration check succeeds
define_=
# The error message to be displayed in case of a configuration check failure
error_=
COUNT=0
# The platform setup variable identifying the #defines include header file
DEFS_FILE=$DIR/.shellb/.AppDefs.h
# All the #define preprocessor macros defined
ALL_DEFS=
# The list of all library paths to be used during shared library builds
LPATHS=
# Unused
LPATHSQ=
# The list of all libraries to be used during shared library builds
LIBS=
# The list of all include directories to be used during compilation
INCS=
INCSQ=
PINCSQ=
PINCSD=
# The c compiler flags for build
CFLAGS=
# The c++ compiler flags for build
CPPFLAGS=
# The linker flags for build
LFLAGS=
# The standard shared library extension
SHLIB_EXT="so"
# The standard static library extension
STLIB_EXT="a"
if [[ "$OSTYPE" == "darwin"* ]]; then
SHLIB_EXT="dylib"
fi
test_c_code_=$(cat <<-END
#include "INC_FILE"
int main() {return 0;}
END
)
test_func_code_=$(cat <<-END
#include <assert.h>
#ifdef __cplusplus
extern "C"
#endif
char FUNC_NAME();
#if _MSC_VER && !__INTEL_COMPILER
#pragma function(accept4)
#endif
int main(void) {
#if defined (__stub_accept4) || defined (__stub___accept4)
fail fail fail
#else
FUNC_NAME();
#endif
return 0;
}
END
)
test_lib_code_=$(cat <<-END
int main() {return 0;}
END
)
# The additional set of directores to be considered as include/library paths
EX_DIR=
# Are we building c or c++ files
BUILD_TYPE=c
# Add #define macros, will be passed to the $DEFS_FILE or passed as a compiler flag (-D)
function add_def() {
if [ "$1" = "" ]
then
echo "Invalid defines"
fi
for ((i = 1; i <= $#; i++ ))
do
s_def=$(sanitize_var ${!i})
if [ "${!i}" = "$s_def" ]
then
if [[ $ALL_DEFS != *";${!i};"* ]]; then
ALL_DEFS+=";${!i};"
eval "$(sanitize_var ${!i})"='1'
if [ -f "$DEFS_FILE" ]; then
echo "#define ${!i} 1" >> $DEFS_FILE
else
CFLAGS+="-D$1=1 "
CPPFLAGS+="-D$1=1 "
fi
fi
else
echo "Invalid defines ${!i}"
exit 1
fi
done
}
# Specify library path to be used during linking
function add_lib_path() {
for ((i = 1; i <= $#; i++ ))
do
if [ "${!i}" != "" ] && [ -d "${!i}" ]
then
if [[ $LPATHS != *"-L${!i} "* ]]; then
LPATHS+="-L${!i} "
LPATHSQ+="\"-L${!i}\","
fi
else
echo "Skipping invalid library path ${!i}"
fi
done
}
# Specify library to be used during linking
function add_lib() {
for ((i = 1; i <= $#; i++ ))
do
if [ "${!i}" != "" ]
then