AIX: Implement shared_target = "aix-solib" support
[openssl.git] / util / ctags.sh
1 #!/bin/sh
2 #
3 # Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
4 #
5 # Licensed under the Apache License 2.0 (the "License").  You may not use
6 # this file except in compliance with the License.  You can obtain a copy
7 # in the file LICENSE in the source distribution or at
8 # https://www.openssl.org/source/license.html
9
10 #
11 # Usage: ./util/ctags.sh [...arguments for ctags...]
12 #
13 # This script runs ctags twice. In the first pass, ctags extract macro
14 # definitions. readtags that is part of Universal Ctags converts them
15 # to ctags options. In the second pass, ctags reads the options and
16 # extracts language objects with expanding the macros.
17 #
18 # Universal Ctags 6.0.0 or higher is assumed.
19 #
20 : ${CTAGS=ctags}
21 : ${READTAGS=readtags}
22
23 if ! type "${CTAGS}" > /dev/null; then
24     echo "${CTAGS}: not found" 1>&2
25     exit 1
26 fi
27
28 if [ $# -eq 0 ]; then
29     set - -R
30 fi
31
32 if ! "${CTAGS}" --version | grep -q "Universal Ctags"; then
33     "${CTAGS}" "$@"
34     exit $?
35 fi
36
37 if "${CTAGS}" --version | grep -q "Universal Ctags 5.*"; then
38     "${CTAGS}" "$@"
39     exit $?
40 fi
41
42 if ! type "${READTAGS}" > /dev/null 2>&1; then
43     echo "WARNING: ${READTAGS}: not found" 1>&2
44     echo "WARNING: \"tagging after macro expanding\" doesn't work" 1>&2
45     "${CTAGS}" "$@"
46     exit $?
47 fi
48
49 if ! [ -d ./.ctags.d ]; then
50     echo "No ./.ctags.d directory" 1>&2
51     exit 1
52 fi
53
54 {
55     # At the first pass, ctags should not be affected by personal
56     # configuration files. So --options=NONE is passed.
57     #
58     # However, if the option is passed, ctags doesn't load the project
59     # default configuration files under $project/.ctags.d. So we load
60     # the project default configuration files, add-dir.ctags and
61     # exclude.ctags, explicitly.
62     #
63     # openssl-stage1 contains a configuration file specialized to
64     # extract macro definitions. It should not be used in normal ctags
65     # usage.
66     $CTAGS --quiet --options=NONE \
67            --options=./.ctags.d/add-dir.ctags \
68            --options=./.ctags.d/exclude.ctags \
69            --options=openssl-stage1
70 } | {
71     macros=.ctags.d/openssl-stage2/50macro-definitons.ctags
72     cat > "$macros" <<EOF
73 #
74 # This file is automatically generated by $0.
75 # DON'T EDIT THIS FILE MANUALLY
76 #
77 EOF
78     # Extract macro definitions and convert them to ctags options.
79     $READTAGS --tag-file - \
80               -Q '(and (eq? $kind "d") ($ "macrodef"))'  \
81               -F '(list "-D" $name $signature "=" ($ "macrodef") #t)' \
82               -l >> "$macros" &&
83         # At the second path, ctags extract tags with expanding macros stored in
84         # 50macro-definitons.ctags.
85         $CTAGS --options=openssl-stage2 \
86                "$@"
87 }