xref: /openssl/util/ctags.sh (revision 6ea4da6e)
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
23if ! type "${CTAGS}" > /dev/null; then
24    echo "${CTAGS}: not found" 1>&2
25    exit 1
26fi
27
28if [ $# -eq 0 ]; then
29    set - -R
30fi
31
32if ! "${CTAGS}" --version | grep -q "Universal Ctags"; then
33    "${CTAGS}" "$@"
34    exit $?
35fi
36
37if "${CTAGS}" --version | grep -q "Universal Ctags 5.*"; then
38    "${CTAGS}" "$@"
39    exit $?
40fi
41
42if ! 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 $?
47fi
48
49if ! [ -d ./.ctags.d ]; then
50    echo "No ./.ctags.d directory" 1>&2
51    exit 1
52fi
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#
77EOF
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}
88