forked from zain1337/vely
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vv
executable file
·1306 lines (1191 loc) · 39.9 KB
/
vv
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
#SPDX-License-Identifier: EPL-2.0
#Copyright 2019 DaSoftver LLC. Written by Sergio Mijatovic.
#Licensed under Eclipse Public License - v 2.0. See LICENSE file.
#On the web https://vely.dev/ - this file is part of Vely framework.
#variable names for Vely start with VV_ (VV_C_ being compiling-related) and _ (internal)
#
#
#make script for Vely application
#
#
#
#Set one or the other
#
#_BETA=""
export VV_USER=$(whoami)
#make sure HOME is available, some apps wipe it out like apache setenv
if [ "$HOME" == "" ]; then
HOME=$(eval echo ~$VV_USER)
fi
#cannot run as run as it might mess up permissions.
if [[ $EUID -eq 0 ]]; then echo "You cannot run vely as root or sudo"; exit -1; fi
#enable "extended globs" (such as "parameter expansion" or ${!X}
shopt -s extglob
#display error context if vely has a shell error, source file bash only
set -eE -o functrace
trap 'echo "Error: status $?, $(caller), line ${BASH_SOURCE[0]}/${LINENO}"' ERR
#do not change these values, they are changed here by Makefile per platform when make install is done
export VV_LIBRARY_PATH=/usr/lib/vely
. "$VV_LIBRARY_PATH"/sys
#end of do-not-change
#by default, make is silent (except for messages we emit). For debugging, use -e to see everything that's going on (this shows vely execution too!)
VV_SHOW_MAKE="-s"
#location of Vely data
export VV_DATA="/var/lib/vv"
export VV_C_RESTPATH=""
export VV_C_MAXUPLOAD="25000000"
export VV_C_MAXERRORS="5"
#plain diagnostics must have value, as it's passed in a script (vdiag) in vmakefile
export VV_C_PLAINDIAG="0"
#defaults for quick install
_PROXYPORT="80"
#display Vely usage
#Internal:
#--asan build with ASAN
#--asan-opt print out ASAN option to prefix the execution with, also works with prefix vf for better coverage of FCGI program
function use_message() {
echo "Usage: $0 OPTIONS
OPTIONS:
-c clean make artifacts for a rebuild
-v show version
-s show detailed execution (script tracing)
-e N show last N program errors
-t N show trace files for N most recent processes
-o show documentation directory
-l show library directory
-m setup syntax highlighting for Vim
-u substitute environment variables in stdin
-r display shell commands to run a program
--req=\"/<request name><url payload>\"
--method=\"<request method>\"
--content=\"<file name>\"
--content-type=\"<content type>\"
--exec
--app=\"application path\"
--server
--remote=\"server IP:port\"
--socket=\"socket path\"
-q build application
--db=\"mariadb:<db name>|postgres:<db name> ...\"
--lflag=<linker flags>
--cflag=<c flags>
--trace
--c-lines
--posix-regex
--debug
--maxupload=<max upload size>
--path=\"/some/path\"
--max-errors=<max errors>
--plain-diag
-i display build flags for Vely FastCGI client
--include
displays C compile flags
--link
displays C linking flags
-a show release notes for this release
-h this help
Type 'man vv' for more information.
"
}
#process all command-line options
function main() {
#list of Vely options for getop
_OPT_STATUS="0"
#do NOT use optional :: args - they must always be used as -e3 for example - there can be no space. This is awkward and unwieldy.
_opts=$(getopt -a -n $0 -o ae:t:,q,o,l,m,i,h,c,v,s,u,r --long db:,lflag:,cflag:,trace,plain-diag,posix-regex,asan,asan-opt,content:,content-type:,silent-header,server,remote:,socket:,app:,path:,c-lines,debug,req:,exec,method:,maxupload:,max-errors:,include,link -- "$@") || _OPT_STATUS=$?
if [ $_OPT_STATUS -ne 0 ]; then
use_message 1>&2
exit -1
fi
#init flags used to emit helpful messages
_DEVOPT=0
_DO_QUICK=0
#if nothing at all passed that is valid: make vely application - that is just 'vely'
eval set -- "$_opts"
while true; do
case "$1" in
--trace )
export VV_C_TRACE="1"
_DEVOPT=1
shift
;;
--c-lines )
export VV_C_SKIPLINES="1"
_DEVOPT=1
shift
;;
--plain-diag )
export VV_C_PLAINDIAG="1"
_DEVOPT=1
shift
;;
--debug )
export VV_C_DEBUG="1"
_DEVOPT=1
shift
;;
--asan-opt )
echo "ASAN_OPTIONS=log_path=asan:halt_on_error=0"
exit 0
;;
--asan )
export VV_C_ASAN="1"
_DEVOPT=1
shift
;;
#vv -r to talk to server (local socket by default)
--server )
_REQ_SRV="1"
shift
;;
--path )
export VV_C_RESTPATH="$2"
_DEVOPT=1
shift 2
;;
--maxupload )
export VV_C_MAXUPLOAD="$2"
_DEVOPT=1
shift 2
;;
--max-errors )
export VV_C_MAXERRORS="$2"
_DEVOPT=1
shift 2
;;
--posix-regex )
export VV_C_POSIXREGEX="1"
_DEVOPT=1
shift
;;
--cflag )
export VV_C_CFLAGS="$2"
_DEVOPT=1
shift 2
;;
-t )
#display last N trace files (for N most recent processes)
_SHOWTRACE="$2"
shift 2
;;
-u )
#subst env vars from stdin to stdout
"$VV_LIBRARY_PATH"/v1 -envsub
exit 0
;;
-a )
#display release notes
cat "$VV_LIBRARY_PATH/CHANGELOG"
shift 1
exit 0
;;
-e )
#display last N errors from backtrace
_SHOWERROR="$2"
shift 2
;;
--lflag )
export VV_C_LFLAGS="$2"
_DEVOPT=1
shift 2
;;
--content-type )
#content type
_REQ_CONTENT_TYPE="$2"
shift 2
;;
--content )
#content file (read from file)
_REQ_CONTENT="$2"
shift 2
;;
--silent-header )
#silent-header for --exec
_REQ_SILENT_HEADER=1
shift 1
;;
--method )
#request method (GET, POST...)
_REQ_METHOD="$2"
shift 2
;;
--exec )
#request execute
_REQ_EXEC="1"
shift 1
;;
--app )
#app name (script name) for -r
_REQ_APP="$2"
shift 2
;;
--socket )
#socket for -r --exec
_REQ_SOCK="$2"
shift 2
;;
--remote )
#serverIP:port for -r --exec
_REQ_IP="$2"
shift 2
;;
--req )
#request name+payload
_REQ_URL="$2"
shift 2
;;
-r )
#display bash to run command-line program
_COMMRUN="1"
shift
;;
-h )
#help message
use_message
shift
exit
;;
-m )
#Vim highlighting
_DO_HIGHLIGHT="1"
shift
;;
--link )
#display FastCGI link
_DO_CLIENT_LINK="1"
shift
;;
--include )
#display FastCGI include
_DO_CLIENT_INCLUDE="1"
shift
;;
-i )
#display FastCGI client flags
_DO_CLIENT="1"
shift
;;
-q )
#quick setup
_DO_QUICK="1"
shift
;;
--db )
#db vendor
_ALLDB="$2"
_DEVOPT=1
shift 2
;;
-c )
#clean the project's temp and object files. This is to be able to fully recompile the project afterwards.
_DO_CLEAN="1"
shift
;;
-d )
#include debugging info in program
export VV_C_DEBUG="1"
_DEVOPT=1
shift
;;
-l )
#show lib directory
echo "$VV_LIBRARY_PATH"
shift
exit
;;
-o )
#show documentation directory
docdir
shift
exit
;;
-s )
#display vely script's execution in detail
export VV_SHOW_MAKE=
set -x
_DEVOPT=1
shift
;;
-v )
#display version of Vely
vely_version
exit
;;
-- )
shift
break
;;
* )
use_message 1>&2
exit -1
esac
done
#check if old pcre2 lib, use glibc regex
if [ "$VV_PCRE2_NEW" == "0" ]; then export VV_C_POSIXREGEX="1"; fi
#handle client fastcgi app building
if [[ "$_DO_QUICK" == "1" && "$_DO_CLIENT" == "1" ]]; then
error "Cannot use both -i and -q options. Use one or the other"
fi
if [ "$_DO_CLIENT" == "1" ]; then
show_client_bld
exit 0
fi
#ask --debug to be used for tracing
if [[ "$VV_C_TRACE" == "1" && "$VV_C_DEBUG" != "1" ]]; then
error "In order to use tracing, you must use --debug option"
fi
#internal: Address Sanitizer only with debug
if [[ "$VV_C_ASAN" == "1" && "$VV_C_DEBUG" != "1" ]]; then
error "In order to use ASAN, you must use --debug option"
fi
#something extra and unexpected, complain about it, probably an error
if [ "$1" != "" ]; then
error "Unknown input [$1]"
fi
#before app name checking b/c it doesn't have anything to do with it
if [ "$_DO_HIGHLIGHT" != "" ]; then
setup_highlighting
exit 0
fi
#can run this from anywhere if has --req and (--server or --app), otherwise must be in app dir
if [[ "$_COMMRUN" != "" && ("$_REQ_APP" != "" || ("$_REQ_SRV" != "" && ("$_REQ_IP" != "" || "$_REQ_SOCK" != ""))) ]]; then
_TOEXEC=".vely_reqexec"
#need || true to remove temp file
show_run_program || true
rm -f "$_TOEXEC"
exit 0
fi
#application name created with vf in .vappname, which must exist
if [ -f ".vappname" ]; then
export VV_C_NAME=$(cat .vappname)
else
error "Vely application was not created yet. Please use vf to create an application first"
fi
check_name "$VV_C_NAME" "application name"
export VV_BLD0=$VV_DATA/bld
export VV_BLD=$VV_BLD0/$VV_C_NAME
export VV_H="$VV_DATA/$VV_C_NAME"
export VV_A="$VV_H/app"
#THEN perform any actions
#check for source only if compiling or cleaning up
if [[ "$_DO_QUICK" == "1" || "$_DO_CLEAN" == "1" ]]; then
_SRCC=$(ls *.vely 2>/dev/null|wc -l)
if [ "$_SRCC" == "0" ]; then
error "No Vely source source code found."
fi
if [[ ! -d "$VV_A" || ! -d "$VV_BLD" ]]; then
error "Vely application [$VV_C_NAME] does not exist. Use vf to create it."
fi
fi
if [[ "$_DO_QUICK" == "0" && "$_DEVOPT" == "1" ]]; then
error "Use -q if you intend to make the application."
fi
if [ "$_SHOWERROR" != "" ]; then
showerr $_SHOWERROR
exit 0
fi
if [ "$_COMMRUN" != "" ]; then
_TOEXEC="$VV_BLD/.reqexec"
. $VV_BLD/.blds
show_run_program
exit 0
fi
if [ "$_SHOWTRACE" != "" ]; then
showtrace $_SHOWTRACE
exit 0
fi
#perform cleaning FIRST. Must
if [ "$_DO_CLEAN" == "1" ]; then
vely_clean
exit 0
fi
#perform any actions
#autoapp make app automatically. It exits at its end
if [ "$_DO_QUICK" == "1" ]; then
#build app
autoapp
fi
#these are 'in-conclusion' actions, they happen LAST
error "No action specified."
}
#
#
#Functions used in processing
#
#
#
#
#
#Show flags for client FastCGI app building.
#
#
#
function show_client_bld() {
#if neither --cflag nor --lflag specified, print out both
if [[ "$_DO_CLIENT_INCLUDE" != "1" && "$_DO_CLIENT_LINK" != "1" ]]; then
_DO_CLIENT_INCLUDE=1
_DO_CLIENT_LINK=1
fi
#print the flag asked for, if both, place them on the same line
if [ "$_DO_CLIENT_INCLUDE" == "1" ]; then
echo -n "-I$VV_INCLUDE_PATH "
fi
if [ "$_DO_CLIENT_LINK" == "1" ]; then
echo -n "-L$VV_LIBRARY_PATH -lvelyfcli -Wl,--rpath=$VV_LIBRARY_PATH "
fi
echo ""
}
#
#
#
#Setup keyword highlighting for Vim
#
#
#
function setup_highlighting() {
mkdir -p $HOME/.vim
mkdir -p $HOME/.vim/syntax
cp $VV_LIBRARY_PATH/v.vim $HOME/.vim/syntax/v.vim
#set .vely file type to use v.vim above
_USEV="autocmd BufRead,BufNewFile *.vely set filetype=v"
if [ ! -f "$HOME/.vimrc" ]; then
echo "$_USEV" > $HOME/.vimrc
else
_HERE=$(grep -F "$_USEV" $HOME/.vimrc |wc -l)
if [ "$_HERE" == "0" ]; then
echo "$_USEV" >> $HOME/.vimrc
fi
fi
#make sure syntax is on
_SYNON="syntax on"
_HERE=$(grep -F "$_SYNON" $HOME/.vimrc |wc -l)
if [ "$_HERE" == "0" ]; then
echo "$_SYNON" >> $HOME/.vimrc
fi
}
#
#
#
#Display bash to run command-line program
#
#
#
function show_run_program() {
if [ "$VV_C_RESTPATH" == "" ]; then
_APATH="/$VV_C_NAME"
else
_APATH="$VV_C_RESTPATH"
fi
#--app overrides current working directory application above
if [ "$_REQ_APP" != "" ]; then
_APATH="$_REQ_APP"
fi
#get app name from _APATH, it's the last path segment, since _APATH is application path
_ANAME=$(echo "$_APATH" | awk -F "/" '{print $NF}')
_REQ_PATH="/<request name><url payload>"
_REQ_QUERY="<request query>"
if [ "$_REQ_URL" != "" ]; then
if [[ "$_REQ_URL" =~ ^.*\?.*$ ]]; then
_REQ_PATH=$(echo "$_REQ_URL" | cut -d "?" -f 1)
_REQ_QUERY=$(echo "$_REQ_URL" | cut -d "?" -f 2)
else
_REQ_PATH="$_REQ_URL"
_REQ_QUERY=""
fi
fi
if [ "$_REQ_METHOD" == "" ]; then
_REQ_METHOD="GET"
fi
_CONT=""
_CONTL=""
_CONTT="export CONTENT_TYPE="
_SHEADER="export VV_SILENT_HEADER=no"
if [ "$_REQ_SILENT_HEADER" == "1" ]; then
_SHEADER="export VV_SILENT_HEADER=yes"
fi
if [[ "$_REQ_CONTENT_TYPE" != "" && "$_REQ_CONTENT" == "" ]]; then
error "Cannot use --content-type without --content"
fi
if [ "$_REQ_CONTENT" != "" ]; then
if [ ! -f "$_REQ_CONTENT" ]; then
error "Cannot access file $_REQ_CONTENT"
fi
_CONT="cat '$_REQ_CONTENT' | "
_CONTL="export CONTENT_LENGTH=\$(stat -c%s '$_REQ_CONTENT')"
if [ "$_REQ_CONTENT_TYPE" != "" ]; then
_CONTT="export CONTENT_TYPE='$_REQ_CONTENT_TYPE'"
fi
else
_CONTL="export CONTENT_LENGTH="
fi
echo "$_CONTT
$_CONTL
$_SHEADER
export REQUEST_METHOD=$_REQ_METHOD
export SCRIPT_NAME=\"$_APATH\"
export PATH_INFO=\"$_REQ_PATH\"
export QUERY_STRING=\"$_REQ_QUERY\""> $_TOEXEC
if [ "$_REQ_SRV" == "1" ]; then
if [[ "$_REQ_IP" != "" && "$_REQ_SOCK" != "" ]]; then
error "Cannot use both --remote and --socket"
fi
if [ "$_REQ_IP" != "" ]; then
echo "${_CONT}cgi-fcgi -connect \"$_REQ_IP\" $_APATH" >> $_TOEXEC
else
if [ "$_REQ_SOCK" != "" ]; then
if [ ! -S "$_REQ_SOCK" ]; then
error "Cannot access socket $_REQ_SOCK or is not a socket file"
fi
echo "${_CONT}cgi-fcgi -connect $_REQ_SOCK $_APATH" >> $_TOEXEC
else
echo "${_CONT}cgi-fcgi -connect /var/lib/vv/$_ANAME/sock/sock $_APATH" >> $_TOEXEC
fi
fi
else
if [[ "$_REQ_IP" != "" || "$_REQ_SOCK" != "" ]]; then
error "Cannot use --remote or --socket without --server"
fi
echo "${_CONT}/var/lib/vv/bld/$_ANAME/$_ANAME" >> $_TOEXEC
fi
if [[ "$_REQ_EXEC" == "1" && "$_REQ_URL" == "" ]]; then
error "Cannot use --exec without --req"
fi
if [ "$_REQ_EXEC" == "1" ]; then
chmod +x $_TOEXEC
RCODE=0
. $_TOEXEC || RCODE=$?
if [ "$RCODE" != "0" ]; then
exit $RCODE
fi
else
cat $_TOEXEC
fi
}
#
#
#
#Show last $1 trace files
#
#
#
function showtrace() {
ls -aslrt /var/lib/vv/$VV_C_NAME/app/trace/trace* |tail -n $1
echo "Backtrace: /var/lib/vv/$VV_C_NAME/app/trace/backtrace"
}
#
#
#
#Show last $1 errors from backtrace
#
#
#
function showerr() {
grep -a "ERROR:" /var/lib/vv/$VV_C_NAME/app/trace/backtrace |tail -n $1
echo "Backtrace: /var/lib/vv/$VV_C_NAME/app/trace/backtrace"
}
#
#
#
#Get database vendors and names from dbvendor:dbname ... format, which is the input ($1)
#
#
#
function getdbs() {
#database vendor/name pairs
_DBL="$1"
_j=0
for _i in $(echo "$_DBL"); do
#-n says do not print non-matches, p says print matches only
_DBV[$_j]=$(sed -n 's/\(.*\):\(.*\)/\1/p' <<<$_i)
_DBN[$_j]=$(sed -n 's/\(.*\):\(.*\)/\2/p' <<<$_i)
if [[ "${_DBV[$_j]}" != "mariadb" && "${_DBV[$_j]}" != "postgres" && "${_DBV[$_j]}" != "sqlite" ]]; then
error "Database [${_DBV[$_j]}] is not supported"
fi
if [ "${_DBN[$_j]}" == "" ]; then
error "Database configuration file not specified"
fi
if [[ ! "$_DBVALL" =~ " ${_DBV[$_j]} " ]]; then
_DBVALL="$_DBVALL ${_DBV[$_j]} "
fi
((_j=_j+1))
done
}
#
#
#
#Autocreate settings file used to recompile if one (or more) of them changes
#File $VV_BLD/blds is used in vmakefile as a signal to recompile all (if changed)
#Sole purpose is to know when to recompile, nothing is cached as far as command-line params go.
#
#VV_C_MAXERRORS and VV_C_PLAINDIAG do not affect compilation, only the output of errors, so not
#part of mkset.
#
#
function mkset() {
echo "VV_C_TRACE='$VV_C_TRACE'
VV_C_SKIPLINES='$VV_C_SKIPLINES'
VV_C_ASAN='$VV_C_ASAN'
VV_C_DEBUG='$VV_C_DEBUG'
VV_C_MAXUPLOAD='$VV_C_MAXUPLOAD'
VV_C_RESTPATH='$VV_C_RESTPATH'
VV_C_CFLAGS='$VV_C_CFLAGS'
VV_C_LFLAGS='$VV_C_LFLAGS'
VV_C_POSIXREGEX='$VV_C_POSIXREGEX'
VV_C_MODULES='$VV_C_MODULES'
VV_C_V1_MOD='$(stat -c "%Y" $VV_LIBRARY_PATH/v1)'
VV_C_VV_MOD='$(stat -c "%Y" $(which vv))'
VV_DBS='$_ALLDB'
" > $VV_BLD/.blds
if [ ! -f "$VV_BLD/blds" ]; then
cp -f $VV_BLD/.blds $VV_BLD/blds
else
_ECODE="0"
diff $VV_BLD/.blds $VV_BLD/blds > /dev/null || _ECODE="$?"
if [ "$_ECODE" != 0 ]; then
cp -f $VV_BLD/.blds $VV_BLD/blds
fi
fi
}
#
#
#
#Show documentation directory
#
#
#
function docdir() {
if [ "$VV_ISFEDORA" == "1" ]; then
echo "$(rpm -E '%{_datadir}')/vely"
elif [ "$VV_ISDEBIAN" == "1" ]; then
echo "/usr/share/vely"
elif [ "$VV_ISARCH" == "1" ]; then
echo "/usr/share/vely"
else
error "Unknown Operating System"
fi
}
#
#
#
#Check if app name is valid
#
#
#
function check_name() {
#$1 is the name
#$2 is the type of it (used in error message)
if [[ ! "$1" =~ ^[-a-zA-Z0-9_]{1,30}$ ]]; then
error "$2 name can be made up of alphanumerical characters only, hyphen or underscore, and its length must be between 1 and 30 characters, found [$1]"
fi
}
#
#
#
#Emitting error messages
#
#
#
function error() {
#$1 is message
echo -e "** Error: $1" 1>&2
exit -1
}
#
#
#if VV_C_MODULES read from .cache file and available, get the usage and set modules
#
#
function read_modules() {
#Set _ISMOD_* variables and call set_modules to set variables for building of application. This is for databases only.
for _i in $(echo "$VV_C_MODULES"); do
if [ "$_i" == "mariadb" ]; then
_ISMOD_MARIADB="1"
elif [ "$_i" == "postgres" ]; then
_ISMOD_POSTGRES="1"
elif [ "$_i" == "sqlite" ]; then
_ISMOD_SQLITE="1"
else
error "Unknown module [$_i]"
fi
done
#
#begin automatic libs discovery
#
#figure out if there's need for these libs
#these find out if there's mention of these calls. A more native C approach was attempted
#where during v1 compilation, information was collected and then used later, however that method ended up being either same or
#slower than this due to complexities of collecting and manipulating, and having to change (now) simple Makefile rules.
#Trying to obtain modification times, save and compare later was alone more than double the time of what's below.
#This gives all files to grep, so avoid slow "for i in $(ls *.vely)", and checks for file that has other than filename:0, so
#that's where :[1-9] comes from. By far the fastest method.
#Overall, the slowdown in performance of full recompile with this is only about 0.06% for 67 .vely files and about 8000 lines of code.
#The limit here is about 100,000 file with names of 20 chars long, so unlikely to have that many source files with that long names
#This approach also scales the best. For example, adding 10 more libs put it at 0.4%, whereas the approach with grepping
#each lib separately puts it at 0.74%, or almost double.
#
#first look for all possible statements
grep -h -o "^[[:space:]]*\(call-web \|match-regex \|[a-z]\+-tree \|hash-string \|decrypt-data \|encrypt-data \|decode-base64 \|encode-base64 \|random-crypto \|new-server \)" *.vely>$VV_BLD/.findmod || true
#then from occurrances, look for curl
if [[ $(grep -m 1 -o "^[[:space:]]*call-web " $VV_BLD/.findmod) != "" ]]; then _ISMOD_CURL="1"; fi
#then from occurrances, look for tree
if [[ $(grep -m 1 -o "^[[:space:]]*[a-z]\+-tree " $VV_BLD/.findmod) != "" ]]; then _ISMOD_TREE="1"; fi
#then from occurrances, look for pcre2
if [[ $(grep -m 1 -o "^[[:space:]]*match-regex " $VV_BLD/.findmod) != "" ]]; then _ISMOD_PCRE2="1"; fi
#then from occurrances, look for fcgi
if [[ $(grep -m 1 -o "^[[:space:]]*new-server " $VV_BLD/.findmod) != "" ]]; then _ISMOD_FCGI="1"; fi
#then from occurrances, look for crypto
if [[ $(grep -m 1 -o "^[[:space:]]*\(hash-string \|decrypt-data \|encrypt-data \|decode-base64 \|encode-base64 \|random-crypto \)" $VV_BLD/.findmod) != "" ]]; then _ISMOD_CRYPTO="1"; fi
#
#end automatic libs discovery
#
VV_MOD_LIBS=
VV_MODULES=
VV_MODULES_INCLUDE=
VV_STUBS=
VV_LIST_ALL_MODULES=
#sqlite module
if [ "$_ISMOD_SQLITE" == "1" ]; then
VV_SQLITE_USED="-DVV_SQLITE_INCLUDE"
#sqlite3.h is installed in include directory, so no need to specify
#VV_MODULES_INCLUDE="$VV_MODULES_INCLUDE -I /usr/include/sqlite3.h"
VV_MODULES="$VV_MODULES -lsqlite3"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelylite"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES sqllite"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_sqlite.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES sqlite"
fi
#postgres module
if [ "$_ISMOD_POSTGRES" == "1" ]; then
VV_POSTGRES_USED="-DVV_POSTGRES_INCLUDE"
VV_C_PGCONF=$(. "$VV_LIBRARY_PATH"/sys pgconf)
if [ "$VV_C_PGCONF" == "yes" ]; then
VV_MODULES_INCLUDE="$VV_MODULES_INCLUDE -I $(pg_config --includedir)"
else
VV_MODULES_INCLUDE="$VV_MODULES_INCLUDE $(pkg-config --cflags libpq)"
fi
VV_MODULES="$VV_MODULES -lpq"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelypg"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES postgres"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_postgres.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES postgres"
fi
#mariadb module
if [ "$_ISMOD_MARIADB" == "1" ]; then
VV_MARIADB_USED="-DVV_MARIADB_INCLUDE"
VV_MODULES_INCLUDE="$VV_MODULES_INCLUDE $(mariadb_config --include)"
VV_MODULES="$VV_MODULES $(mariadb_config --libs)"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelymys"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES mariadb"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_mariadb.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES mariadb"
fi
#fcgi module
if [ "$_ISMOD_FCGI" == "1" ]; then
VV_FCGI_USED="-DVV_FCGI_INCLUDE"
#VV_MODULES="$VV_MODULES -lfcgi" - nothing here since fcgi client implemented from scratch
VV_MOD_LIBS="$VV_MOD_LIBS -lvelyfsrv"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES fcgi"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_fcgi.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES curl"
fi
#pcre2 module
if [ "$_ISMOD_PCRE2" == "1" ]; then
#this is if not using plain posix regex, otherwise nothing to do
if [ "$VV_C_POSIXREGEX" != "1" ]; then
VV_PCRE2_USED="-DVV_PCRE2_INCLUDE"
VV_MODULES="$VV_MODULES $VV_PCRE2_LIBS"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelypcre2 -ldl"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES pcre2"
else
#pcre is not used, but we still use glibc regex, so include regex.h and compile pcre2.c (which does both
#glibc and pcre2)
VV_PCRE2_USED="-DVV_PCRE2_INCLUDE"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelypcre2glibc -ldl"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES pcre2"
fi
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_pcre2.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES pcre2"
fi
#tree module
if [ "$_ISMOD_TREE" == "1" ]; then
VV_TREE_USED="-DVV_TREE_INCLUDE"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelytree"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES tree"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_tree.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES tree"
fi
#curl module
if [ "$_ISMOD_CURL" == "1" ]; then
VV_CURL_USED="-DVV_CURL_INCLUDE"
VV_MODULES="$VV_MODULES -lcurl"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelycurl"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES curl"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_curl.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES curl"
fi
#crypto module
if [ "$_ISMOD_CRYPTO" == "1" ]; then
VV_CRYPTO_USED="-DVV_CRYPTO_INCLUDE"
VV_MODULES="$VV_MODULES -lssl -lcrypto"
VV_MOD_LIBS="$VV_MOD_LIBS -lvelysec"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES crypto"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_crypto.o"
VV_LIST_ALL_MODULES="$VV_LIST_ALL_MODULES crypto"
fi
if [[ "$_ISMOD_MARIADB" == "1" || "$_ISMOD_POSTGRES" == "1" || "$_ISMOD_SQLITE" == "1" ]]; then
VV_MOD_LIBS="-lvelydb $VV_MOD_LIBS"
else
VV_STUBS="$VV_STUBS $VV_LIBRARY_PATH/stub_gendb.o"
fi
#used in vmakecommon/install to properly link modules at link-time and to substitute stubs for those modules that are not used
export VV_MODULES
export VV_MODULES_INCLUDE
export VV_STUBS
export VV_MOD_LIBS
export VV_C_MODULES
export VV_LIST_ALL_MODULES
export VV_MARIADB_USED
export VV_FCGI_USED
export VV_CURL_USED
export VV_TREE_USED
export VV_PCRE2_USED
export VV_CRYPTO_USED
}
#
#
#
#make generated files affect recompilation ONLY if they are different
#
#
#
function copy_if_diff () {
F="$1"
_ISDIFF=$(diff $VV_BLD/$F.new $VV_BLD/$F 2>/dev/null) || true
if [[ "$_ISDIFF" != "" || ! -f "$VV_BLD/$F" ]]; then
mv $VV_BLD/$F.new $VV_BLD/$F
else
rm -rf $VV_BLD/$F.new
fi
}
#
#
#sets a list of many source code files (source.vely, velyapp.h, vely_dispatch_request.vely etc.)
#takes care of generated vs provided source code: if a certain handler is provided, then don't generate it.
#
#
function gen_src() {
#
#Files generated here are $VV_BLD/source.vely,velyapp.h,vely_dispatch_request.vely.
#
#
#Generate source.vely, all the source files needed for Vely app
#
_T="source.vely"
#this is all source files, including non-request ones
_SRCF=$(ls *.vely 2>/dev/null) || true
_HDRF=$(ls *.h 2>/dev/null) || true
echo "#SPDX-License-Identifier: EPL-2.0" >> $VV_BLD/$_T.new
echo "#Copyright 2019 DaSoftver LLC. Written by Sergio Mijatovic." >> $VV_BLD/$_T.new
echo >> $VV_BLD/$_T.new
echo "#" >> $VV_BLD/$_T.new
echo "#Lists source files in your application. This is an auto-generated file." >> $VV_BLD/$_T.new
echo "#" >> $VV_BLD/$_T.new
echo >> $VV_BLD/$_T.new
echo "#Include files here:" >> $VV_BLD/$_T.new
_HFILES=$(echo "$_HDRF" | grep -a -v velyapp.h | xargs)
echo "VV_HEADER_FILES=$(echo $_HFILES)" >> $VV_BLD/$_T.new
echo >> $VV_BLD/$_T.new
echo "#Source files here:" >> $VV_BLD/$_T.new
echo "VV_SOURCE_FILES=$(echo $_SRCF)" >> $VV_BLD/$_T.new
_ISDIFF=$(diff $VV_BLD/$_T.new $VV_BLD/$_T 2>/dev/null) || true
if [[ "$_ISDIFF" != "" || ! -f "$VV_BLD/$_T" ]]; then
mv $VV_BLD/$_T.new $VV_BLD/$_T
else
rm -rf $VV_BLD/$_T.new
fi
#
#Generate velyapp.h, a file that has a list of C declarations neeeded to build an app
#
_T="velyapp.h"