forked from universal-ctags/codebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase
More file actions
executable file
·559 lines (491 loc) · 10.4 KB
/
codebase
File metadata and controls
executable file
·559 lines (491 loc) · 10.4 KB
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
#!/bin/bash
#
# codebase -- a tool for managing and utilizing codebase for Universal-ctags
#
# Copyright (C) 2019 Red Hat, Inc.
# Copyright (C) 2019 Masatake YAMATO
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
: ${VCS:=git}
: ${MAIN_BRANCH:=master}
: ${CTAGS=u-ctags}
ERROR ()
{
local status_="$1"
local msg="$2"
shift 2
echo "ERROR: " "$msg" 1>&2
exit $status_
}
WARN ()
{
local msg="$1"
shift 1
echo "WARNING: " "$msg" 1>&2
}
verify_cmd()
{
if ! type "$1" > /dev/null 2>&1; then
ERROR 1 "$1 is not found"
fi
}
member_csv()
{
local t=$1
shift
local elt
local OLDIFS=$IFS
IFS=,
for elt in $@; do
if [[ "$elt" = "$t" ]]; then
return 0
fi
done
IFS=$OLDIFS
return 1
}
clone_verify()
{
if [[ ! -d code ]]; then
ERROR 1 "no output directory"
fi
}
#
# HELP SUBCOMMAND
#
cmd_help_help_simple()
{
cat <<EOF
$0 help [SUBCOMMAND]
EOF
}
help_cmd()
{
if type "cmd_${1}_help" > /dev/null 2>&1; then
echo -n Usage:
"cmd_${1}_help_simple"
echo
"cmd_${1}_help"
elif type "cmd_${1}_help_simple" > /dev/null 2>&1; then
echo -n Usage:
"cmd_${1}_help_simple"
else
ERROR 1 "no such command: $1"
fi
}
help()
{
local cmd=$1
if [[ -n "${cmd}" ]]; then
help_cmd "${cmd/-/_}"
return $?
fi
echo Usage:
for cmd in clone ctags list-code list-languages list-profiles help; do
if type "cmd_${cmd/-/_}_help_simple" > /dev/null 2>&1; then
echo -n " "
"cmd_${cmd/-/_}_help_simple"
fi
done
return 0
}
#
# CLONE SUBCOMMAND
#
cmd_clone_help_simple()
{
cat <<EOF
$0 clone [LANGUAGE|all]
EOF
}
git_clone()
{
local repo=$1
local odir=$2
git clone "${repo}" "${odir}" || ERROR 1 "failed to clone git repo: ${repo}"
}
git_checkout()
{
local branch=$1
git checkout "${branch}" || ERROR 1 "failed to checkout git branch: ${branch}"
}
hg_clone()
{
local repo=$1
local odir=$2
hg clone "${repo}" "${odir}" || ERROR 1 "failed to clone hg repo: ${repo}"
}
hg_checkout()
{
local branch=$1
hg update "${branch}" || ERROR 1 "failed to checkout hg update: ${branch}"
}
clone_prep()
{
mkdir -p code
}
cmd_clone_run()
{
local language=$1
local lcopy
clone_prep
clone_verify
if [[ -z "$language" ]]; then
ERROR 1 "no language specified (use 'all' for cloning all repositories)"
fi
for lcopy in lcopy.d/*.lcopy; do
if [[ -f "${lcopy}" ]]; then
local odir=$(basename "$lcopy" .lcopy)
if [[ ! -d code/"${odir}" ]]; then
(
if source ${lcopy}; then
verify_cmd ${VCS}
if [[ -z "${REPO}" ]]; then
ERROR 1 "no REPO given in ${lcopy}"
fi
if [[ -z "${ALIGNMENT}" ]]; then
ERROR 1 "no ALIGNMENT given in ${lcopy}"
fi
if [[ -z "${LANGUAGES}" ]]; then
ERROR 1 "no LANGUAGES given in ${lcopy}"
fi
if [[ "${language}" = all ]] ||
member_csv "${language}" "${LANGUAGES}"; then
type "${VCS}"_clone > /dev/null 2>&1 || ERROR 1 "no such vcs handler: ${VCS}"
"${VCS}"_clone "${REPO}" "code/${odir}"
cd "code/${odir}"
"${VCS}"_checkout "${ALIGNMENT}"
fi
fi
)
fi
else
ERROR 1 "no lcopy file for language: ${language}"
fi
done
}
#
# LIST-CODE SUBCOMMAND
#
cmd_list_code_help_simple()
{
cat <<EOF
$0 list-code
EOF
}
cmd_list_code_run()
{
local lcopy
printf "#%19s CLONED? %s\n" "NAME" "LANGUAGES"
for lcopy in lcopy.d/*.lcopy; do
if [[ -f "${lcopy}" ]]; then
local name=$(basename "$lcopy" .lcopy)
(
if source ${lcopy}; then
local cloned=no
if [[ -d code/${name} ]]; then
cloned=yes
fi
printf "%20s %-7s %s\n" "${name}" "${cloned}" "${LANGUAGES/,/ }"
fi
)
else
ERROR 1 "no lcopy file for language: ${language}"
fi
done
}
#
# LIST-LANGUAGES SUBCOMMAND
#
cmd_list_languages_help_simple()
{
cat <<EOF
$0 list-languages
EOF
}
cmd_list_languages_run()
{
local lcopy
local languages=$(for lcopy in lcopy.d/*.lcopy; do
if [[ -f "${lcopy}" ]]; then
local name=$(basename "$lcopy" .lcopy)
(
if source ${lcopy}; then
local lang
local OLDIFS=$IFS
IFS=,
for lang in $LANGUAGES; do
echo $lang
done
IFS=$OLDIFS
fi
)
else
ERROR 1 "no lcopy file for language: ${language}"
fi
done | sort -u)
printf "#%19s %s\n" "LANGUAGE" "CODE"
local lang
for lang in $languages; do
local names=$(for lcopy in lcopy.d/*.lcopy; do
if [[ -f "${lcopy}" ]]; then
local name=$(basename "$lcopy" .lcopy)
(
if source ${lcopy}; then
if member_csv $lang $LANGUAGES; then
echo $name
fi
fi
)
fi
done)
printf "%20s %s\n" "${lang}" "$(echo ${names})"
done
}
#
# LIST-PROFILES SUBCOMMAND
#
cmd_list_profiles_help_simple()
{
cat <<EOF
$0 list-profiles
EOF
}
cmd_list_profiles_run()
{
local profile
printf "%19s %s\n" "PROFILE" "DESCRIPTION"
for profile in profile.d/*.ctags; do
if [[ ! -f "$profile" ]]; then
break;
fi
local name=$(basename "$profile" .ctags)
local desc=$(grep "^# @" $profile | sed 's/^# @//' | tail -1)
printf "%20s %s\n" "${name}" "${desc}"
done
}
#
# CTAGS SUBCOMMAND
#
cmd_ctags_help_simple()
{
cat <<EOF
$0 ctags [-v] [-o TAGOUTPUT|-O] [--valgrind|--perf|--gdb] [LANGUAGE|all] [PROFILE]
EOF
}
cmd_ctags_help()
{
cat <<EOF
-v|--verbose
Makes the output of $0 verbose (not for ctags itself).
-o|--output
Specify the tags output (default: /dev/null).
-O
Record tags output to a file having an automatically generated unique name.
--valgrid
Run ctags under valgrind command.
--perf
Run ctags under perf record.
--gdb
Run ctags under gdb command. -o and -O options are ignored.
EOF
}
ctags_prep()
{
mkdir -p results
}
cmd_ctags_run()
{
local language=
local profile=
local valgrind_flags=--leak-check=full
local gdb
local verbose
local measure=time
local ofile=/dev/null
local opt=--options=profile.d/maps
while [[ $# -gt 0 ]]; do
case "$1" in
(--verbose|-v)
verbose=1
shift 1
;;
(--valgrind)
measure=valgrind
shift 1
;;
(--valgrind=*)
valgrind_flags=${1/--valgrind=/}
measure=valgrind
shift 1
;;
(--perf)
measure=perf
shift 1
;;
(--output|-o)
shift 1
ofile=$1
shift 1
;;
(--output=*)
ofile=${1/--output=/}
shift 1
;;
(-o=*)
ofile=${1/-o=/}
shift 1
;;
(-O)
ofile=
shift
;;
(--gdb)
measure=gdb
shift 1
;;
(--help|-h)
help ctags
exit 0
;;
(-*)
ERROR 1 "ctags subcommand: unknown option $1"
;;
(*)
break;
;;
esac
done
verify_cmd "$measure"
language=$1
profile=$2
if [[ -z "${language}" ]]; then
language=all
fi
if [[ -n "${profile}" ]]; then
if [[ -f "profile.d/${profile}.ctags" ]]; then
opt="$opt --options=profile.d/${profile}.ctags"
else
ERROR 1 "No such profile: ${profile}"
fi
fi
local lcopy
local odirs
local odir
if ! type "${CTAGS}" > /dev/null 2>&1; then
ERROR 1 "No ${CTAGS} found (set CTAGS environment variable)"
fi
odirs=$(
for lcopy in lcopy.d/*.lcopy; do
(
if source ${lcopy}; then
if [[ "$language" = 'all' ]] || member_csv "${language}" "${LANGUAGES}"; then
echo $(basename "$lcopy" .lcopy)
fi
fi
)
done
)
if [[ -z "$odirs" ]]; then
ERROR 1 "No lcopy for \"${language}\""
fi
for odir in ${odirs}; do
if [[ ! -d code/$odir ]]; then
if [[ "$language" = 'all' ]]; then
WARN "$odir is not cloned yet"
else
ERROR 1 "$odir used as input for $language language is not cloned yet"
fi
fi
done
ctags_prep
local start=$(date "+%F-%R:%S")
local ctags_version=$(${CTAGS} --version | sed -ne 's/.*\([0-9a-f]\{8\}\).*/\1/p')
local ctags_features=$(${CTAGS} --version | grep features: | sed -e 's/^[^:]\+: //' -e 's/, / /g')
local alignment=default
local inputs=$(for odir in ${odirs}; do echo "code/${odir}"; done)
local fbody=$(printf "%s,% -20s,% -10s,% -10s,% -10s,%s" "${ctags_version}" "${language}" "${profile}" "${measure}" "${alignment}" "${start}" | tr ' ' .)
local log=results/${fbody}.log
if [[ -z "$ofile" ]]; then
ofile=results/${fbody}.tags
elif [[ "$ofile" = "-" ]]; then
ofile=/dev/stdout
fi
if [[ "${measure}" != "gdb" ]]; then
(
echo version: ${ctags_version}
echo features: ${ctags_features}
echo log: ${log}
echo tagsoutput: $ofile
[[ -n "${verbose}" ]] && printf "%s" "Making page caches hot..." 1>&2
for odir in ${odirs}; do
find code/${odir} -type f -print0 | xargs -0 cat > /dev/null
done
[[ -n "${verbose}" ]] && printf "done\n" 1>&2
printf "cmdline: " 1>&2
if [[ "${measure}" = "valgrind" ]]; then
set -x
valgrind ${valgrind_flags} ${CTAGS} --quiet --options=NONE --sort=no $opt --totals=yes --languages=${language} -o - \
-R $inputs \
> $ofile
set +x
elif [[ "${measure}" = "perf" ]]; then
set -x
perf record -g ${CTAGS} --quiet --options=NONE --sort=no $opt --totals=yes --languages=${language} -o - \
-R $inputs \
> $ofile
set +x
else
set -x
time ${CTAGS} --quiet --options=NONE --sort=no $opt --totals=yes --languages=${language} -o - \
-R $inputs \
> $ofile
set +x
fi
) 2>&1 | grep -v '^set +x' | tee ${log}
else
gdb -e ${CTAGS} -ex "run --quiet --options=NONE --sort=no $opt --totals=yes --languages=${language} -R $input"
rm -f ./tags
fi
}
#
# MAIN DRIVER
#
main()
{
local cmd
if [[ $# = 0 ]]; then
help 1>&2
exit 1
fi
case $1 in
(--help|help|-h)
shift
help "$@"
return $?
;;
(-*)
ERROR 1 "no such option: $1"
;;
*)
cmd=$1
shift
;;
esac
type "cmd_${cmd//-/_}_run" > /dev/null 2>&1 || ERROR 1 "no such subcommand: ${cmd}"
"cmd_${cmd//-/_}_run" "$@"
return $?
}
main "$@"