forked from kohler/click
-
Notifications
You must be signed in to change notification settings - Fork 0
/
click-buildtool.in
1676 lines (1517 loc) · 49.1 KB
/
click-buildtool.in
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/sh
# click-buildtool -- build tools for Click
# Eddie Kohler
#
# Copyright (c) 2000-2001 Massachusetts Institute of Technology
# Copyright (c) 2000-2008 Mazu Networks, Inc.
# Copyright (c) 2001-2003 International Computer Science Institute
# Copyright (c) 2008 Meraki, Inc.
# Copyright (c) 2004-2011 Regents of the University of California
# Copyright (c) 2000-2014 Eddie Kohler
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, subject to the conditions
# listed in the Click LICENSE file. These conditions include: you must
# preserve this copyright notice, and you cannot mention the copyright
# holders in advertising related to the Software without their permission.
# The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
# notice is a summary of the Click LICENSE file; the license in that file is
# legally binding.
set_clickbuild () {
clickbuild_prefix="`echo "$1" | sed 's,/$,,'`"
clickbuild_clickdatadir="@clickbuild_clickdatadir@"
}
prefix=@prefix@
set_clickbuild "$prefix"
gmake=@GMAKE@
verbose=""
elem2=""
default_provisions="@provisions@"
driver_provisions="@DRIVERS@"
export LC_COLLATE=C
trap "exit 1" HUP
# find good versions of awk and grep
awk="@AWK@"
grep="@GREP@"
egrep="@EGREP@"
echo_n () {
# suns can't echo -n, and Mac OS X can't echo "x\c"
echo "$@" | tr -d '
'
}
###############
# SHORTENSYMS #
###############
shortensyms_usage () {
echo "Usage: click-buildtool shortensyms OBJ..." 1>&2
echo "Try 'click-buildtool shortensyms --help' for more information." 1>&2
exit 1
}
shortensyms () {
driver=""
date=`date`
objs=
len=56
while [ x"$1" != x ]; do
case $1 in
-n|--l|--le|--len|--leng|--lengt|--length)
if test $# -lt 2; then shortensyms_usage; fi
len="$2"; shift 2;;
-n*)
len="`echo "$1" | sed 's/^-n//'`"; shift 1;;
--l=*|--le=*|--len=*|--leng=*|--lengt=*|--length=*)
len="`echo "$1" | sed 's/^[^=]*=//'`"; shift 1;;
-h|--h|--he|--hel|--help)
cat <<'EOF' 1>&2
'Click-buildtool shortensyms' shortens the symbols in the object files listed
on the command line to be at most 56 characters long.
Usage: click-buildtool shortensyms OBJ...
Options:
-n, --length=N Set maximum symbol length to N.
-h, --help Print this message and exit.
Report bugs to <[email protected]>.
EOF
exit 0;;
*)
objs="$objs
$1"; shift 1;;
esac
done
test -z "$objs" && shortensyms_usage
objs=`echo "$objs" | sort | uniq`
if test -z "$NM"; then NM="@NM@"; fi
if test -z "$OBJCOPY"; then OBJCOPY="@OBJCOPY@"; fi
if test -z "$MD5SUM"; then MD5SUM="@MD5SUM@"; fi
for obj in $objs; do
longsyms=`$NM -g $obj | sed 's/^[^ ]*[ ]*.[ ]*//
/^.\{0,'$len'\}$/d'`
if test -z "$longsyms"; then
continue
elif test -z "$OBJCOPY"; then
echo "$obj: 'objcopy' missing, 'click-buildtool shortensyms' must leave long symbols unchanged" 1>&2
continue
elif test -z "$MD5SUM"; then
# We don't need security, so default to a weaker checksum.
if echo | sum >/dev/null; then MD5SUM=sum; else MD5SUM=cat; fi
fi
/bin/rm -f $obj.shortenmap $obj~
echo "$longsyms" | $awk 'BEGIN {
split("28HashContainer_const_iterator 3HCj \
22HashContainer_iterator 3HCi \
21HashContainer_adapter 3HCa \
13HashContainer 2HC \
26TokenBucketJiffyParameters 3TBJ \
12TokenBucketX 3TBX \
20can_live_reconfigure 10can_reconf \
16live_reconfigure 6reconf \
24HashTable_const_iterator 3HTj \
18HashTable_iterator 3HTi \
9HashTable 2HT \
16OrderedHashTable 3OHT \
12ErrorHandler 2EH \
10ArgContext 4Argx \
14NotifierSignal 4NSig \
9IPAddress 2IP \
12EtherAddress 3Eth \
10IP6Address 3IP6 \
13vector_memory 1v \
18typed_array_memory 1T \
18sized_array_memory 1S \
10char_array 1C \
6Vector 1V \
11StringAccum 2SA \
6String 1S \
7Element 2El \
6Packet 1P", pats, "[^A-Za-z0-9_]*");
}
{
orig = $0;
for (i = 1; length() > '$len' && pats[i]; i += 2)
gsub(pats[i], pats[i+1]);
if (length() > '$len') {
("echo " orig " | '"$MD5SUM"'") | getline hash
sub("[^A-Za-z_0-9.].*", "", hash)
$0 = substr($0, 1, '$len' - 12) "_H" substr(hash, 1, 10)
}
print orig, $0;
}' > $obj.shortenmap || exit 1
$OBJCOPY --redefine-syms=$obj.shortenmap $obj || { /bin/rm -f $obj.shortenmap; exit 1; }
/bin/rm -f $obj.shortenmap
done
}
if test "$1" = shortensyms; then
shift 1; shortensyms "$@"; exit 0
fi
#######################
# COMPILE-SHORTENSYMS #
#######################
compile_shortensyms_usage () {
echo "Usage: click-buildtool compile-shortensyms [BUILD COMMAND]..." 1>&2
echo "Try 'click-buildtool compile-shortensyms --help' for more information." 1>&2
exit 1
}
compile_shortensyms () {
args=
obj=
while [ x"$1" != x ]; do
case $1 in
-o)
if test $# -lt 2; then compile_shortensyms_usage; fi
obj="$2"; args="$args -o '$obj'"; shift 2;;
-h|--h|--he|--hel|--help)
cat <<'EOF' 1>&2
'Click-buildtool compile-shortensyms' runs a build command and then runs
'click-buildtool shortensyms' on the results. The BUILD COMMAND must contain
a '-o OBJECT' argument.
Usage: click-buildtool compile-shortensyms [BUILD COMMAND]
Options:
-h, --help Print this message and exit.
Report bugs to <[email protected]>.
EOF
exit 0;;
*)
args="$args '$1'"; shift 1;;
esac
done
eval $args
result=$?
test $result = 0 || exit $result
shortensyms $obj
}
if test "$1" = compile-shortensyms; then
shift 1; compile_shortensyms "$@"; exit 0
fi
############
# FINDELEM #
############
findelem_usage () {
echo "Usage: click-buildtool findelem [-a] [-V] [-p PREFIX] < [FILES AND DIRECTORIES]
Try 'click-buildtool findelem --help' for more information." 1>&2
exit 1
}
elementmap_provisions () {
elementmap="$1"
$grep "^<elementmap \|^<entry " <"$elementmap" | $awk '/ name="([^"]*)"/ {
sub(/.* name="/, "", $0);
sub(/".*/, "", $0);
prov[$0] = 1;
}
/ provides="([^"]*)"/ {
sub(/.* provides="/, "", $0);
sub(/".*/, "", $0);
split($0, d, / +/);
for (j in d) prov[d[j]] = 1;
}
END {
# delete references to drivers
delete prov["userlevel"]; delete prov["linuxmodule"];
delete prov["bsdmodule"]; delete prov["ns"];
for (j in prov) print j;
}'
}
expand_provisions () {
tr -s ', \n\r\f\t\v' '\n'
}
findelem () {
pfx=
all=
provisions=
filenames=
standards=
unprovisions='false 0'
checksum_data=
while [ x"$1" != x ]; do
case $1 in
-S|--s|--st|--sta|--stan|--stand|--standa|--standar|--standard|--standards)
standards=1; shift 1;;
-p|--pre|--pref|--prefi|--prefix)
if test $# -lt 2; then findelem_usage; fi
shift 1; pfx="$1/"; shift 1;;
-p*)
pfx="`echo "$1" | sed 's/^-p//'`"/; shift 1;;
--pre=*|--pref=*|--prefi=*|--prefix=*)
pfx="`echo "$1" | sed 's/^[^=]*=//'`"/; shift 1;;
-f|--filenames)
filenames=f; shift 1;;
-F|--filename-|--filename-p|--filename-pa|--filename-pai|--filename-pair|--filename-pairs)
filenames=F; shift 1;;
-V|--verb|--verbo|--verbos|--verbose)
verbose=1; shift 1;;
-a|--a|--al|--all)
all=1; shift 1;;
-r|--pro|--prov|--provi|--provid|--provide)
if test $# -lt 2; then findelem_usage; fi
provisions="`echo "$2" | expand_provisions`
$provisions"; shift 2;;
-r*)
provisions="`echo "$1" | sed 's/^-r//' | expand_provisions`
$provisions"; shift 1;;
--pro=*|--prov=*|--provi=*|--provid=*|--provide=*)
provisions="`echo "$1" | sed 's/^[^=]*=//' | expand_provisions`
$provisions"; shift 1;;
-x|--u|--un|--unp|--unpr|--unpro|--unprov|--unprovi|--unprovid|--unprovide)
if test $# -lt 2; then findelem_usage; fi
unprovisions="`echo "$2" | expand_provisions`
$unprovisions"; shift 2;;
-x*)
unprovisions="`echo "$1" | sed 's/^-x//' | expand_provisions`
$unprovisions"; shift 1;;
--u=*|--un=*|--unp=*|--unpr=*|--unpro=*|--unprov=*|--unprovi=*|--unprovid=*|--unprovide=*)
unprovisions="`echo "$1" | sed 's/^[^=]*=//' | expand_provisions`
$unprovisions"; shift 1;;
-X|--unprovide-|--unprovide-f|--unprovide-fi|--unprovide-fil|--unprovide-file)
if test $# -lt 2; then findelem_usage; fi
shift 1; if test -f "$1"; then unprovisions="`cat "$1"`
$unprovisions"; fi; shift 1;;
-X*)
f="`echo "$1" | sed 's/^-X//'`"; if test -f "$f"; then unprovisions="`cat "$f"`
$unprovisions"; fi; shift 1;;
--unprovide-=*|--unprovide-f=*|--unprovide-fi=*|--unprovide-fil=*|--unprovide-file=*)
f="`echo "$1" | sed 's/^[^=]*=//'`"; if test -f "$f"; then unprovisions="`cat "$f"`
$unprovisions"; fi; shift 1;;
-e|--e|--el|--ele|--elem|--eleme|--elemen|--element|--elementm|--elementma|--elementmap)
if test $# -lt 2; then findelem_usage; fi
shift 1; provisions="`elementmap_provisions "$1"`
$provisions"; shift 1;;
-e*)
emap="`echo "$1" | sed 's/^-e//'`"
provisions="`elementmap_provisions "$emap"`
$provisions"; shift 1;;
--e=*|--el=*|--ele=*|--elem=*|--eleme=*|--elemen=*|--element=*|--elementm=*|--elementma=*|--elementmap=*)
emap="`echo '$1' | sed 's/^[^=]*=//'`"
provisions="`elementmap_provisions "$emap"`
$provisions"; shift 1;;
-P|--pa|--pac|--pack|--packa|--packag|--package)
provisions="`elementmap_provisions "${clickbuild_clickdatadir}/elementmap.xml"`
$provisions"; shift 1;;
--c|--ch|--che|--chec|--check|--checks|--checksu|--checksum)
if test $# -lt 2; then findelem_usage; fi
checksum_data="$2"; shift 2;;
-h|--h|--he|--hel|--help)
cat <<'EOF' 1>&2
'Click-buildtool findelem' locates valid Click element source code. It starts
with a collection of source code, then eliminates files whose requirements
are not available until reaching a stable set of sources. It expects a list of
files and directories on standard input. Directories are searched for .cc/.c
source files. Only files containing EXPORT_ELEMENT() or ELEMENT_PROVIDES() are
considered. The initial list of available requirements is the list of
requirements specified with '-r', plus the list of EXPORT_ELEMENT() and
ELEMENT_PROVIDES() keywords.
Usage: click-buildtool findelem [OPTIONS] < [FILES AND DIRECTORIES]
Options:
-a, --all Include all subdirectories of 'elements' rather
than reading standard input, and pretend all
requirements are available (except for '-x').
-V, --verbose Print more information about dependency checking.
-p, --prefix PREFIX Prepend PREFIX to every file and/or directory.
-r, --provide REQ Provide requirement(s) in REQ.
-e, --elementmap EMAP Provide requirement(s) from EMAP.
-P, --package Provide requirement(s) from default elementmap.
-S, --standards Mark standard elements as available.
-x, --unprovide REQ Reject requirement(s) in REQ.
-X, --unprovide-file FILE Reject requirement(s) from FILE.
-f, --filenames Output filenames only.
-F, --filename-pairs Output "sourcefile:headerfile" pairs for elements.
--checksum FILE Write checksum data to FILE.
-h, --help Print this message and exit.
Report bugs to <[email protected]>.
EOF
exit 0;;
*)
findelem_usage;;
esac
done
if test -n "$verbose" -a -n "$pfx"; then
echo "Prefix: $pfx" 1>&2
fi
# add defaults to provisions
provisions="$provisions $default_provisions"
# add standards to provisions if necessary
if test "x$standards" != x; then
provisions="$provisions AddressInfo AlignmentInfo ErrorElement PortInfo ScheduleInfo Storage"
fi
# add 'multithread' provision based on driver
if echo "$provisions" | $grep userlevel >/dev/null 2>&1; then
if echo "$provisions" | $grep umultithread >/dev/null 2>&1; then
provisions="$provisions multithread"
fi
fi
if echo "$provisions" | $grep linuxmodule >/dev/null 2>&1; then
if echo "$provisions" | $grep smpclick >/dev/null 2>&1; then
provisions="$provisions multithread"
fi
fi
# expand provisions and unprovisions: require one per line
provisions=`echo "$provisions" | tr -s ' \011\015\014\013' '\012'`
unprovisions=`echo "$unprovisions" | tr -s ' \011\015\014\013' '\012'`
if test -n "$verbose" -a -n "$provisions"; then
echo 1>&2
echo "Provisions: $provisions" 1>&2
fi
if test -n "$verbose" -a -n "$unprovisions"; then
echo 1>&2
echo "Unprovisions: $unprovisions" 1>&2
fi
# expand list of files
if test -n "$all"; then
first_files=${pfx}
test -d ${pfx}elements && first_files=${pfx}elements
first_files=`cd $first_files >/dev/null && ls`
else
first_files=`cat`
fi
# eliminate unprovisions
bad_first_files=`echo "$first_files
$unprovisions" | sort | uniq -d`
first_files=`echo "$first_files
$bad_first_files" | sort | uniq -u`
# create first list of files
files=""
checksum_files=
for i in $first_files; do
ppfx="$pfx"
if test -d "${pfx}elements/$i" && echo "$i" | $grep -v '^\.' >/dev/null; then
ppfx="${pfx}elements/"
fi
if test -d "${ppfx}$i"; then
files="$files
"`find ${ppfx}$i -follow \( -name \*.cc -o -name \*.c \) -print | $grep -v '/[.,][^/]*$'`
elif test -r "${ppfx}$i"; then
files="$files
${ppfx}$i"
fi
if expr "$checksum_data" != "" '&' "${ppfx}$i" : '[^$
]*$' >/dev/null; then
checksum_files="$checksum_files ${ppfx}$i"
elif test -n "$checksum_data"; then rm -f "$checksum_data"; checksum_data=; fi
done
files=`echo "$files" | sort | uniq | $grep .`
# create checksum to help run "make elemlist" automatically
if test -n "$checksum_data"; then
rm -f "$checksum_data"
if test -z "$MD5SUM"; then MD5SUM="@MD5SUM@"; fi
if test -n "$MD5SUM"; then
checksum_command="LC_ALL=C ls -1R $checksum_files | $MD5SUM | sed 's/[^0-9a-f].*//'"
checksum=`eval "$checksum_command"`
echo "ELEMENT_CHECKSUM=$checksum" >"$checksum_data"
echo "ELEMENT_CHECKSUMCOMMAND=$checksum_command" >>"$checksum_data"
fi
fi
# die if no files
if test -z "$files"; then
echo "no files found" 1>&2
exit 1
fi
# if '$all', then accept all dependencies except the unprovisions
dep_test='<='
if test -n "$all"; then
dep_test='<'
fi
# check dependencies: generate a list of bad files, then remove those files
# from the list of good files
# first remove files that provide an unprovision
awk_exports=`echo "$unprovisions" | sed 's/\(..*\)/dep["\1"]=-1;/'`
bad_files=`$egrep '^EXPORT_ELEMENT|^ELEMENT_PROVIDES' $files | sed 's/EXPORT_ELEMENT[ ]*(\(.*\)).*/\1/
s/ELEMENT_PROVIDES[ ]*(\(.*\)).*/\1/' | $awk -F: 'BEGIN {OFS="";'"$awk_exports"'}
{
split($2, deps, / +/);
for (j in deps) {
if (dep[deps[j]] < 0) {
print $1;
break;
}
}
}' | sort | uniq`
if test -n "$verbose" -a -n "$bad_files"; then
echo 1>&2
echo "Files: $files" 1>&2
echo 1>&2
echo "Bad files: $bad_files" 1>&2
fi
if test -n "$bad_files"; then
files=`echo "$files
$bad_files" | sort | uniq -u`
fi
# then cycle, removing files that require something not provided
while true; do
provides=`$egrep '^EXPORT_ELEMENT|^ELEMENT_PROVIDES' $files | sed 's/.*(\(.*\)).*/\1/' | tr ' \011' '\012'`
awk_exports=`echo "$provides"'
'"$provisions" | sed 's/\(..*\)/dep["\1"]=1;/'`"
"`echo "$unprovisions" | sed 's/\(..*\)/dep["\1"]=-1;/'`
new_bad_files=`$grep '^ELEMENT_REQUIRES' $files | sed 's/ELEMENT_REQUIRES[ ]*(\(.*\)).*/\1/' | $awk -F: 'BEGIN {OFS="";'"$awk_exports"'dep["true"]=1; dep["1"]=1;}
{
split($2, deps, / +/);
for (j in deps) {
i = deps[j]
if (dep[i] <= 0) {
bad = 1;
split(i, or_deps, /\|+/);
for (k in or_deps) {
if (!(dep[or_deps[k]] '"$dep_test"' 0))
bad = 0;
}
if (bad) {
print $1;
break;
}
}
}
}' | sort | uniq`
if test -n "$verbose"; then
echo 1>&2
echo "Files: $files" 1>&2
echo 1>&2
echo "Bad files: $new_bad_files" 1>&2
fi
if test -z "$new_bad_files"; then
break
else
files=`echo "$files
$new_bad_files" | sort | uniq -u`
bad_files="$new_bad_files
$bad_files"
fi
done
header_files=`echo "$files" | sed 's/\.cc/\.hh/'`
# generate output
if test "$filenames" = f; then
outscriptlet=i
elif test "$filenames" = F; then
outscriptlet='i, ":", header[i]'
else
echo "# Generated by 'click-buildtool findelem' on" `date`
outscriptlet='i, "\t", header[i], "\t", ex'
fi
$egrep '^(ELEMENT_|EXPORT_|class|[ ]*(virtual[ ]*)?const[ ]*char[ ]*\*[ ]*class_name|[ ]*static[ ]*void[ ]*static_[ic]|[ ]*(virtual[ ]*)?[bv]o..[ ]*run_t|[ ]*(virtual[ ]*)?int[ ]*initial_home|[}])' $files $header_files /dev/null 2>/dev/null | $awk -F: 'BEGIN {
OFS = ""; cur_class = ""; errors = 0; class_name_warned = 0; nexports = 0
}
/:class[ ]/ {
sub(/^class[ ]*/, "", $2);
sub(/[ ].*$/, "", $2);
cur_class = $2;
next
}
/:}/ {
cur_class = "";
next
}
/:EXPORT_ELEMENT/ {
sub(/.*EXPORT_ELEMENT[ ]*\([ ]*/, "", $2);
sub(/[ ]*\).*/, "", $2);
if (exports[$1] != "") {
exports[$1] = exports[$1] " " $2
} else {
exports[$1] = $2; nexports++
}
next
}
/:ELEMENT_PROVIDES/ {
sub(/.*ELEMENT_PROVIDES[ ]*\([ ]*/, "", $2);
sub(/[ ]*\).*/, "", $2);
split($2, provs, / /)
for (i in provs) {
if (provs[i] != "") {
if (exports[$1] == "")
nexports++;
else
exports[$1] = exports[$1] " ";
exports[$1] = exports[$1] "*" provs[i]
}
}
next
}
/:ELEMENT_HEADER/ {
sub(/.*ELEMENT_HEADER[ ]*\(/, "", $2)
sub(/\).*/, "", $2)
header[$1] = $2
next
}
/:ELEMENT_LIBS/ {
sub(/.*ELEMENT_LIBS[ ]*\(\(?/, "", $2)
sub(/\)?\).*/, "", $2)
gsub(/[ ][ ]*/, ";", $2)
gsub(/-L;/, "-L", $2)
libs[$1] = $2
next
}
/class_name.*return[ ]*"/ {
sub(/.*return[ ]*"/, "", $0);
sub(/".*/, "", $0);
class_name[cur_class] = $0
next
}
/class_name/ {
print $1, ": ", cur_class, "::class_name method malformed" | "cat 1>&2"
if (++class_name_warned == 1)
print " (class_name methods must be written on a single line.)" | "cat 1>&2"
++errors
}
/static_initialize/ {
static_initialize[cur_class] = 1
next
}
/static_cleanup/ {
static_cleanup[cur_class] = 1
next
}
/void[ ]*run_task/ {
print $1, ": ", cur_class, "::run_task must return bool" | "cat 1>&2"
++errors
}
/[^_a-z]run_task *\( *\)/ {
print $1, ": ", cur_class, "::run_task should take a Task * argument" | "cat 1>&2"
++errors
}
/[^_a-z]run_timer *\( *\)/ {
print $1, ": ", cur_class, "::run_timer should take a Timer * argument" | "cat 1>&2"
++errors
}
/[^_a-z]initial_home_thread_id *\([ ]*E/ {
print $1, ": ", cur_class, "::initial_home_thread_id should take ONLY a const Element * argument" | "cat 1>&2"
++errors
}
END {
if (nexports == 0)
print "click-buildtool: No elements found" | "cat 1>&2"
if (errors || nexports == 0)
system("kill -HUP '$$'")
for (i in exports) {
if (header[i] == "") {
header[i] = "\"" i "\""; sub(/\.cc/, ".hh", header[i])
}
ex = exports[i]
split(ex, exes, / /); ex = ""
for (j in exes) {
star = index(exes[j], "*")
dash = index(exes[j], "-")
if (star == 0 && dash == 0) {
dash = length(exes[j]);
if (class_name[exes[j]] == "")
exes[j] = exes[j] "-" exes[j];
else
exes[j] = exes[j] "-" class_name[exes[j]];
}
if (star == 0) {
ex = ex exes[j] " "
klass = substr(exes[j], 1, dash)
} else
klass = substr(exes[j], star + 1)
if (static_initialize[klass]) {
ex = ex klass "-!si ";
static_initialize[klass] = 0
}
if (static_cleanup[klass]) {
ex = ex klass "-!sc ";
static_cleanup[klass] = 0
}
if (libs[i])
ex = ex "-!lib" libs[i] " "
}
print '"$outscriptlet"'
}
}' | sort
# attempt to detect reuse of a header protection #define
for x in $header_files; do
xp=`echo "$x" | tr -c -d 'a-zA-Z0-9 /_.@~=:+;,?-'`
head -n 10 $x 2>/dev/null | sed 's,^,'"$xp"':,'
done | $awk -F: 'BEGIN {
OFS = ""; errors = 0
}
/: *# *ifndef/ {
sub(/ *# *ifndef[ ]*/, "", $2);
sub(/[ \/].*/, "", $2);
if (!protector[$1]) {
protector[$1] = $2;
if (protecting[$2]) {
print $1, ": header protector #ifndef ", $2, " reused" | "cat 1>&2";
print protecting[$2], ": first use was here" | "cat 1>&2";
if (errors == 0)
print " (Every element header must use a different #ifndef symbol.)" | "cat 1>&2";
++errors;
} else
protecting[$2] = $1;
}
}
END {
if (errors)
system("kill -HUP '$$'");
}'
exit $?
}
##########################
# ELEM2MAKE/ELEM2PACKAGE #
##########################
elem2xxx_usage () {
echo_n "Usage: click-buildtool elem2$elem2 [-p PREFIX] [-V]" 1>&2
if test "$elem2" = package; then echo_n " PKGNAME" 1>&2; fi
echo " < elements.conf
Try 'click-buildtool elem2$elem2 --help' for more information." 1>&2
exit 1
}
elem2make () {
driver=""
makevar=""
date=`date`
linux=x
bsdmake=0
while [ x"$1" != x ]; do
case $1 in
-t|--d|--dr|--dri|--driv|--drive|--driver)
if test $# -lt 2; then elem2xxx_usage; fi
shift 1; driver="$1"; shift 1;;
-t*)
driver=`echo "$1" | sed 's/^-D//'`; shift 1;;
--d=*|--dr=*|--dri=*|--driv=*|--drive=*|--driver=*)
driver=`echo "$1" | sed 's/^[^=]*=//'`; shift 1;;
--t|--ta|--tar|--targ|--targe|--target)
if test $# -lt 2; then elem2xxx_usage; fi
shift 1; driver="$1"; shift 1;;
--t=*|--ta=*|--tar=*|--targ=*|--targe=*|--target=*)
driver=`echo "$1" | sed 's/^[^=]*=//'`; shift 1;;
-v|--m|--ma|--mak|--make|--make-|--make-v|--make-va|--make-var|--make-vari|--make-varia|--make-variab|--make-variabl|--make-variable)
if test $# -lt 2; then elem2xxx_usage; fi
shift 1; makevar="$1"; shift 1;;
-v*)
makevar=`echo "$1" | sed 's/^-v//'`; shift 1;;
--m=*|--ma=*|--mak=*|--make=*|--make-=*|--make-v=*|--make-va=*|--make-var=*|--make-vari=*|--make-varia=*|--make-variab=*|--make-variabl=*|--make-variable=*)
makevar=`echo "$1" | sed 's/^[^=]*=//'`; shift 1;;
-x|--e|--ex|--exc|--excl|--exclu|--exclud|--exclude)
if test $# -lt 2; then elem2xxx_usage; fi
shift 1
for i in $1; do excludes=";/^$i"' \\$'"/d$excludes"; done
shift 1;;
-x*)
this_exclude=`echo "$1" | sed 's/^-p//'`
for i in $this_exclude; do excludes=";/^$i"' \\$'"/d$excludes"; done
shift 1;;
--e=*|--ex=*|--exc=*|--excl=*|--exclu=*|--exclud=*|--exclude=*)
this_exclude=`echo "$1" | sed 's/^[^=]*=//'`
for i in $this_exclude; do excludes=";/^$i"' \\$'"/d$excludes"; done
shift 1;;
--l|--li|--lin|--linu|--linux)
linux=1; shift 1;;
--b|--bs|--bsd)
bsdmake=1; shift 1;;
--no-l|--no-li|--no-lin|--no-linu|--no-linux)
linux=0; shift 1;;
-V|--verb|--verbo|--verbos|--verbose)
verbose=1; shift 1;;
-h|--h|--he|--hel|--help)
cat <<'EOF' 1>&2
'Click-buildtool elem2make' reads an 'elements.conf' file generated by
'click-buildtool findelem' on the standard input, and writes a Makefile
fragment defining the ELEMENT_OBJS variable to the standard output.
Usage: click-buildtool elem2make [-t DRIVER] [-V] < elements.conf
Options:
-t, --driver DRIVER Set target driver to DRIVER ('userlevel',
'linuxmodule', 'bsdmodule', 'ns', or 'tool').
-v, --make-variable N Use make variable N.
--linux Generate Linux style makefile fragment.
--bsd Generate BSD make compatible Makefile.
-x, --exclude FILE Do not include FILE.
-V, --verbose Print more information.
-h, --help Print this message and exit.
Report bugs to <[email protected]>.
EOF
exit 0;;
*)
elem2xxx_usage;;
esac
done
L=
defmakevar="ELEMENT_OBJS"
if test -n "$driver"; then
if test "$driver" = 'user' -o "$driver" = 'userlevel'; then
L=u
elif test "$driver" = 'kernel' -o "$driver" = 'linuxmodule'; then
L=k
elif test "$driver" = 'bsdmodule'; then
L=b
elif test "$driver" = 'ns' -o "$driver" = 'nsmodule'; then
L=n
elif test "$driver" = 'tool'; then
L=t
else
echo "Unknown driver $driver" 1>&2
exit 1
fi
fi
if test "$L" = k -a "$linux" = x -a "$bsdmake" = 0; then
linux=1
fi
if test "$linux" = 1 -a "$bsdmake" = 1; then
echo "--linux and --bsd are mutually exclusive" 1>&2
exit 1
fi
if test -n "$L"; then osuffix=".$L.o"; else osuffix=".o"; fi
if test -z "$makevar"; then
makevar=$defmakevar
fi
# expand list of files
elemconf=`cat`
files=`echo "$elemconf" | $grep -v '^#' | sed 's/[ ].*//'`
# find libraries
libs=`echo "$elemconf" | sed '/-!lib/!d;s/^.*-!lib//g;s/;/ /g' | $grep .`
if test -n "$libs"; then
libs=`echo "$libs" | sed 's/^\([^ ][^ ]*\) *$/\1 \1/'`
while echo "$libs" | $grep ' [^ ].* [^ ]' >/dev/null; do
libs=`echo "$libs" | sed '/ [^ ].* [^ ]/s/^\([^ ][^ ]*\) *\([^ ][^ ]*\) /\1 \2\\
\2 /'`
done
fi
test -n "$libs" && libs=`echo "$libs" | tsort`
# massage awk script based on Linux 2.6 or not
if test "$linux" = 1; then
set_subdir_scriptlet='
if (dir != "" && substr(dir, 1, 1) == "/")
subdirs[dirid] = dir;
else
subdirs[dirid] = "$(obj)/" dir;
'
pattern_scriptlet='$(addprefix $(obj)/,$('"$makevar"'__", i, "))'
obj_scriptlet=' obj = "$(obj)/%'"$osuffix"'";'
action_scriptlet=' action["c" obj] = " $(call if_changed_dep,ccompile)";
action["C" obj] = " $(call if_changed_dep,cxxcompile)";'
else
set_subdir_scriptlet='subdirs[dirid] = dir;'
pattern_scriptlet='$('"$makevar"'__", i, ")'
obj_scriptlet=' obj = "%'"$osuffix"'";'
action_scriptlet=' action["c" obj] = " $(call ccompile,-c $< -o $@,CC)";
action["C" obj] = " $(call cxxcompile,-c $< -o $@,CXX)";
action["c%.i"] = " $(call ccompile_nodep,-E $< -o $@,CPP)";
action["C%.ii"] = " $(call cxxcompile_nodep,-E $< -o $@,CXXCPP)";
action["c%.s"] = " $(call ccompile_nodep,-S $< -o $@,CC -S)";
action["C%.s"] = " $(call cxxcompile_nodep,-S $< -o $@,CXX -S)";'
if test -n "$L"; then
action_scriptlet="$action_scriptlet"'
action["c" obj] = action["c" obj] "\n $(FIXDEP)";
action["C" obj] = action["C" obj] "\n $(FIXDEP)";'
fi
fi
# output
echo "# Generated by 'click-buildtool elem2make' on" `date`
echo "$files" | $awk '
BEGIN { OFS = "";
'"$obj_scriptlet"'
'"$action_scriptlet"'
}
{ filetype = ($0 ~ /\.c$/ ? "c" : "C");
sub(/\.cc*$/, "'"$osuffix"'");
i = match($0, /\/[^\/]*$/);
if (i == 0) {
dir = "";
file = $0;
} else {
dir = substr($0, 1, RSTART);
file = substr($0, RSTART + 1);
}
decdir = filetype dir;
if (decdir in subdirid)
dirid = subdirid[decdir];
else {
dirid = nsubdirs++;
subdirid[decdir] = dirid;
subdirtype[dirid] = filetype;
'"$set_subdir_scriptlet"'
}
elements[dirid] = elements[dirid] file " \\\n";
}
END {
for (i = 0; i < nsubdirs; i++) {
print "'"$makevar"'__", i, " = \\\n", elements[i];
allsubdirs = allsubdirs "$('"$makevar"'__" i ") \\\n";
}
print "'"$makevar"' = \\\n", allsubdirs;
for (i = 0; i < nsubdirs; i++) {
for (j in action) {
if (substr(j, 1, 1) != subdirtype[i])
continue;
suffix = (subdirtype[i] == "C" ? "%.cc" : "%.c");
x = substr(j, 2) ": " subdirs[i] suffix;
if (substr(j, 2) == obj)
print "'"$pattern_scriptlet"': ", x;
else
print "$(patsubst %'"$osuffix"',", substr(j, 2), ",'"$pattern_scriptlet"'): ", x;
print action[j];
}
print "";
}
}' | sed "$excludes"
if test -n "$libs"; then
libvar=`echo $makevar | sed 's/OBJS$/LIBS/'`
echo $libvar = $libs
fi
}
elem2xxx () {
package=""
date=`date`
standards=''
includes=''
while [ x"$1" != x ]; do
case $1 in
-S|--s|--st|--sta|--stan|--stand|--standa|--standar|--standard|--standards)
standards=1; shift 1;;
-V|--verb|--verbo|--verbos|--verbose)
verbose=1; shift 1;;
-i|--i|--in|--inc|--incl|--inclu|--includ|--include)
if test $# -lt 2; then elem2xxx_usage; fi
includes="$includes print '#include $2';"; shift 2;;
-i*)
this_include=`echo "$1" | sed 's/^-i//'`
includes="$includes print '#include $this_include';"; shift 1;;
--i=*|--in=*|--inc=*|--incl=*|--inclu=*|--includ=*|--include=*)
this_include=`echo "$1" | sed 's/^[^=]*=//'`
includes="$includes print '#include $this_include';"; shift 1;;
-h|--h|--he|--hel|--help)
if test "$elem2" = export; then
cat <<'EOF' 1>&2
'Click-buildtool elem2export' reads an 'elements.conf' file generated by
'click-buildtool findelem' on the standard input, examines those files for
exported elements, and writes a C++ source file defining the
click_export_elements() function to the standard output.
Usage: click-buildtool elem2export [-V] < elements.conf
EOF
elif test "$elem2" = package; then
cat <<'EOF' 1>&2
'Click-buildtool elem2package' reads an 'elements.conf' file generated by
'click-buildtool findelem' on the standard input, examines those files for
exported elements, and writes a C++ source file suitable for creating a
dynamically linked package with those elements to the standard output. PKGNAME
is the name of the package.
Usage: click-buildtool elem2package [-p PREFIX] [-V] PKGNAME < elements.conf
EOF
fi
cat <<'EOF' 1>&2
Options:
-S, --standards Export standard required elements as well.
-i, --include HDR Emit "#include HDR" at top of file.
-V, --verbose Print more information.
-h, --help Print this message and exit.
Report bugs to <[email protected]>.
EOF
exit 0;;
-*)
elem2xxx_usage;;
*)
if test -z "$package" -a "$elem2" = package; then package="$1"; shift 1; else elem2xxx_usage; fi;;
esac
done
# set up awk program
if test -n "$package"; then
includes=" $includes "'print "#define WANT_MOD_USE_COUNT 1\n#include <click/config.h>\n#include <click/package.hh>\n#include <click/glue.hh>", includefiles;'
awk_program='BEGIN {
OFS = ""; nrebecca = 0; packname="'"$package"'"; includefiles=""
}
/^#/ { next; }
{
if (NF == 1)
next;
anyprovides = 0;
for (i = 3; i <= NF; i++) {
split($i, ans, /-/);
if (ans[2] == "!si") {
B = B " " ans[1] "::static_initialize();\n";
anyprovides++;
} else if (ans[2] == "!sc") {
C = C " " ans[1] "::static_cleanup();\n";
anyprovides++;
} else if (ans[2] !~ /^!/) {
B = B " hatred_of_rebecca[" nrebecca "] = click_add_element_type(\"" ans[2] "\", beetlemonkey, " nrebecca ");\n"
C = C " click_remove_element_type(hatred_of_rebecca[" nrebecca "]);\n";
D = D " case " nrebecca ": return new " ans[1] ";\n";
anyprovides++; nrebecca++;
}
}