1#!/bin/sh
2# Generate tags or cscope files
3# Usage tags.sh <mode>
4#
5# mode may be any of: tags, TAGS, cscope
6#
7# Uses the following environment variables:
8# ARCH, SOC, BOARD, ZEPHYR_BASE
9
10
11find_sources()
12{
13	find "$1" -name "$2"
14}
15
16# Looks for assembly, c and header files of selected architectures
17all_sources()
18{
19	for dir in $ALLSOURCES
20	do
21		find_sources "$dir" '*.[chS]'
22	done
23}
24
25all_target_sources()
26{
27	all_sources
28}
29
30all_kconfigs()
31{
32	for dir in $ALLSOURCES; do
33		find_sources $dir 'Kconfig*'
34	done
35}
36
37all_defconfigs()
38{
39	for dir in $ALLSOURCES; do
40		find_sources $dir 'defconfig*'
41	done
42}
43
44docscope()
45{
46	(echo \-k; echo \-q; all_target_sources) > cscope.files
47	cscope -b -f cscope.out
48}
49
50dogtags()
51{
52	all_target_sources | gtags -i -f -
53}
54
55exuberant()
56{
57	all_target_sources | xargs $1 -a                        \
58	-I __initdata,__exitdata,__initconst,			\
59	-I __initdata_memblock					\
60	-I __refdata,__attribute,__maybe_unused,__always_unused \
61	-I __acquires,__releases,__deprecated			\
62	-I __read_mostly,__aligned,____cacheline_aligned        \
63	-I ____cacheline_aligned_in_smp                         \
64	-I __cacheline_aligned,__cacheline_aligned_in_smp	\
65	-I ____cacheline_internodealigned_in_smp                \
66	-I __used,__packed,__packed2__,__must_check,__must_hold	\
67	-I EXPORT_SYMBOL,EXPORT_SYMBOL_GPL,ACPI_EXPORT_SYMBOL   \
68	-I DEFINE_TRACE,EXPORT_TRACEPOINT_SYMBOL,EXPORT_TRACEPOINT_SYMBOL_GPL \
69	-I static,const						\
70	--extra=+f --c-kinds=+px                                \
71	--regex-asm='/^(ENTRY|_GLOBAL)\(([^)]*)\).*/\2/'        \
72	--regex-c='/^SYSCALL_DEFINE[[:digit:]]?\(([^,)]*).*/sys_\1/' \
73	--regex-c='/^COMPAT_SYSCALL_DEFINE[[:digit:]]?\(([^,)]*).*/compat_sys_\1/' \
74	--regex-c++='/^TRACE_EVENT\(([^,)]*).*/trace_\1/'		\
75	--regex-c++='/^TRACE_EVENT\(([^,)]*).*/trace_\1_rcuidle/'	\
76	--regex-c++='/^DEFINE_EVENT\([^,)]*, *([^,)]*).*/trace_\1/'	\
77	--regex-c++='/^DEFINE_EVENT\([^,)]*, *([^,)]*).*/trace_\1_rcuidle/' \
78	--regex-c++='/PAGEFLAG\(([^,)]*).*/Page\1/'			\
79	--regex-c++='/PAGEFLAG\(([^,)]*).*/SetPage\1/'			\
80	--regex-c++='/PAGEFLAG\(([^,)]*).*/ClearPage\1/'		\
81	--regex-c++='/TESTSETFLAG\(([^,)]*).*/TestSetPage\1/'		\
82	--regex-c++='/TESTPAGEFLAG\(([^,)]*).*/Page\1/'			\
83	--regex-c++='/SETPAGEFLAG\(([^,)]*).*/SetPage\1/'		\
84	--regex-c++='/__SETPAGEFLAG\(([^,)]*).*/__SetPage\1/'		\
85	--regex-c++='/TESTCLEARFLAG\(([^,)]*).*/TestClearPage\1/'	\
86	--regex-c++='/__TESTCLEARFLAG\(([^,)]*).*/TestClearPage\1/'	\
87	--regex-c++='/CLEARPAGEFLAG\(([^,)]*).*/ClearPage\1/'		\
88	--regex-c++='/__CLEARPAGEFLAG\(([^,)]*).*/__ClearPage\1/'	\
89	--regex-c++='/__PAGEFLAG\(([^,)]*).*/__SetPage\1/'		\
90	--regex-c++='/__PAGEFLAG\(([^,)]*).*/__ClearPage\1/'		\
91	--regex-c++='/PAGEFLAG_FALSE\(([^,)]*).*/Page\1/'		\
92	--regex-c++='/TESTSCFLAG\(([^,)]*).*/TestSetPage\1/'		\
93	--regex-c++='/TESTSCFLAG\(([^,)]*).*/TestClearPage\1/'		\
94	--regex-c++='/SETPAGEFLAG_NOOP\(([^,)]*).*/SetPage\1/'		\
95	--regex-c++='/CLEARPAGEFLAG_NOOP\(([^,)]*).*/ClearPage\1/'	\
96	--regex-c++='/__CLEARPAGEFLAG_NOOP\(([^,)]*).*/__ClearPage\1/'	\
97	--regex-c++='/TESTCLEARFLAG_FALSE\(([^,)]*).*/TestClearPage\1/' \
98	--regex-c++='/__TESTCLEARFLAG_FALSE\(([^,)]*).*/__TestClearPage\1/' \
99	--regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/'		\
100	--regex-c++='/TASK_PFA_TEST\([^,]*,\s*([^)]*)\)/task_\1/'	\
101	--regex-c++='/TASK_PFA_SET\([^,]*,\s*([^)]*)\)/task_set_\1/'	\
102	--regex-c++='/TASK_PFA_CLEAR\([^,]*,\s*([^)]*)\)/task_clear_\1/'\
103	--regex-c='/PCI_OP_READ\((\w*).*[1-4]\)/pci_bus_read_config_\1/' \
104	--regex-c='/PCI_OP_WRITE\((\w*).*[1-4]\)/pci_bus_write_config_\1/' \
105	--regex-c='/DEFINE_(MUTEX|SEMAPHORE|SPINLOCK)\((\w*)/\2/v/'	\
106	--regex-c='/DEFINE_(RAW_SPINLOCK|RWLOCK|SEQLOCK)\((\w*)/\2/v/'	\
107	--regex-c='/DECLARE_(RWSEM|COMPLETION)\((\w*)/\2/v/'		\
108	--regex-c='/DECLARE_BITMAP\((\w*)/\1/v/'			\
109	--regex-c='/(^|\s)(|L|H)LIST_HEAD\((\w*)/\3/v/'			\
110	--regex-c='/(^|\s)RADIX_TREE\((\w*)/\2/v/'			\
111	--regex-c='/DEFINE_PER_CPU\(([^,]*,\s*)(\w*).*\)/\2/v/'		\
112	--regex-c='/DEFINE_PER_CPU_SHARED_ALIGNED\(([^,]*,\s*)(\w*).*\)/\2/v/' \
113	--regex-c='/DECLARE_WAIT_QUEUE_HEAD\((\w*)/\1/v/'		\
114	--regex-c='/DECLARE_(TASKLET|WORK|DELAYED_WORK)\((\w*)/\2/v/'	\
115	--regex-c='/DEFINE_PCI_DEVICE_TABLE\((\w*)/\1/v/'		\
116	--regex-c='/(^\s)OFFSET\((\w*)/\2/v/'				\
117	--regex-c='/(^\s)DEFINE\((\w*)/\2/v/'				\
118	--regex-c='/DEFINE_HASHTABLE\((\w*)/\1/v/'
119
120	all_kconfigs | xargs $1 -a                              \
121	--langdef=kconfig --language-force=kconfig              \
122	--regex-kconfig='/^[[:blank:]]*(menu|)config[[:blank:]]+([[:alnum:]_]+)/\2/'
123
124	all_kconfigs | xargs $1 -a                              \
125	--langdef=kconfig --language-force=kconfig              \
126	--regex-kconfig='/^[[:blank:]]*(menu|)config[[:blank:]]+([[:alnum:]_]+)/CONFIG_\2/'
127
128	all_defconfigs | xargs -r $1 -a                         \
129	--langdef=dotconfig --language-force=dotconfig          \
130	--regex-dotconfig='/^#?[[:blank:]]*(CONFIG_[[:alnum:]_]+)/\1/'
131}
132
133emacs()
134{
135	all_target_sources | xargs $1 -a                        \
136	--regex='/^\(ENTRY\|_GLOBAL\)(\([^)]*\)).*/\2/'         \
137	--regex='/^SYSCALL_DEFINE[0-9]?(\([^,)]*\).*/sys_\1/'   \
138	--regex='/^COMPAT_SYSCALL_DEFINE[0-9]?(\([^,)]*\).*/compat_sys_\1/' \
139	--regex='/^TRACE_EVENT(\([^,)]*\).*/trace_\1/'		\
140	--regex='/^TRACE_EVENT(\([^,)]*\).*/trace_\1_rcuidle/'	\
141	--regex='/^DEFINE_EVENT([^,)]*, *\([^,)]*\).*/trace_\1/' \
142	--regex='/^DEFINE_EVENT([^,)]*, *\([^,)]*\).*/trace_\1_rcuidle/' \
143	--regex='/PAGEFLAG(\([^,)]*\).*/Page\1/'			\
144	--regex='/PAGEFLAG(\([^,)]*\).*/SetPage\1/'		\
145	--regex='/PAGEFLAG(\([^,)]*\).*/ClearPage\1/'		\
146	--regex='/TESTSETFLAG(\([^,)]*\).*/TestSetPage\1/'	\
147	--regex='/TESTPAGEFLAG(\([^,)]*\).*/Page\1/'		\
148	--regex='/SETPAGEFLAG(\([^,)]*\).*/SetPage\1/'		\
149	--regex='/__SETPAGEFLAG(\([^,)]*\).*/__SetPage\1/'	\
150	--regex='/TESTCLEARFLAG(\([^,)]*\).*/TestClearPage\1/'	\
151	--regex='/__TESTCLEARFLAG(\([^,)]*\).*/TestClearPage\1/'	\
152	--regex='/CLEARPAGEFLAG(\([^,)]*\).*/ClearPage\1/'	\
153	--regex='/__CLEARPAGEFLAG(\([^,)]*\).*/__ClearPage\1/'	\
154	--regex='/__PAGEFLAG(\([^,)]*\).*/__SetPage\1/'		\
155	--regex='/__PAGEFLAG(\([^,)]*\).*/__ClearPage\1/'	\
156	--regex='/PAGEFLAG_FALSE(\([^,)]*\).*/Page\1/'		\
157	--regex='/TESTSCFLAG(\([^,)]*\).*/TestSetPage\1/'	\
158	--regex='/TESTSCFLAG(\([^,)]*\).*/TestClearPage\1/'	\
159	--regex='/SETPAGEFLAG_NOOP(\([^,)]*\).*/SetPage\1/'	\
160	--regex='/CLEARPAGEFLAG_NOOP(\([^,)]*\).*/ClearPage\1/'	\
161	--regex='/__CLEARPAGEFLAG_NOOP(\([^,)]*\).*/__ClearPage\1/' \
162	--regex='/TESTCLEARFLAG_FALSE(\([^,)]*\).*/TestClearPage\1/' \
163	--regex='/__TESTCLEARFLAG_FALSE(\([^,)]*\).*/__TestClearPage\1/' \
164	--regex='/TASK_PFA_TEST\([^,]*,\s*([^)]*)\)/task_\1/'		\
165	--regex='/TASK_PFA_SET\([^,]*,\s*([^)]*)\)/task_set_\1/'	\
166	--regex='/TASK_PFA_CLEAR\([^,]*,\s*([^)]*)\)/task_clear_\1/'	\
167	--regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'		\
168	--regex='/PCI_OP_READ(\([a-z]*[a-z]\).*[1-4])/pci_bus_read_config_\1/' \
169	--regex='/PCI_OP_WRITE(\([a-z]*[a-z]\).*[1-4])/pci_bus_write_config_\1/'\
170	--regex='/[^#]*DEFINE_HASHTABLE(\([^,)]*\)/\1/'
171
172	all_kconfigs | xargs $1 -a                              \
173	--regex='/^[ \t]*\(\(menu\)*config\)[ \t]+\([a-zA-Z0-9_]+\)/\3/'
174
175	all_kconfigs | xargs $1 -a                              \
176	--regex='/^[ \t]*\(\(menu\)*config\)[ \t]+\([a-zA-Z0-9_]+\)/CONFIG_\3/'
177
178	all_defconfigs | xargs -r $1 -a                         \
179	--regex='/^#?[ \t]?\(CONFIG_[a-zA-Z0-9_]+\)/\1/'
180}
181
182xtags()
183{
184	if $1 --version 2>&1 | grep -iq exuberant; then
185		exuberant $1
186	elif $1 --version 2>&1 | grep -iq emacs; then
187		emacs $1
188	else
189		all_target_sources | xargs $1 -a
190	fi
191}
192
193
194# Used for debugging
195if [ "$TAGS_VERBOSE" = "1" ]; then
196	set -x
197fi
198
199# Set project base directory
200if [ "${ZEPHYR_BASE}" = "" ]; then
201	echo "error: run zephyr-env.sh before $0"
202	exit 1
203fi
204tree="${ZEPHYR_BASE}/"
205
206# List of always explored directories
207COMMON_DIRS="drivers dts include kernel lib misc subsys"
208
209# Detect if ARCH is set. If not, we look for all archs
210if [ "${ARCH}" = "" ]; then
211	ALLSOURCES="${COMMON_DIRS} arch boards soc"
212else
213	ALLSOURCES="${COMMON_DIRS} arch/${ARCH} boards/${ARCH} soc/${ARCH}"
214fi
215
216# TODO: detect if BOARD is set so we can select certain files
217
218
219# Perform main action
220remove_structs=
221case "$1" in
222	"cscope")
223		docscope
224		;;
225
226	"gtags")
227		dogtags
228		;;
229
230	"tags")
231		rm -f tags
232		xtags ctags
233		remove_structs=y
234		;;
235
236	"TAGS")
237		rm -f TAGS
238		xtags etags
239		remove_structs=y
240		;;
241	*)
242		echo "error: incorrect parameter"
243		;;
244esac
245
246# Remove structure forward declarations.
247if [ -n "$remove_structs" ]; then
248    LANG=C sed -i -e '/^\([a-zA-Z_][a-zA-Z0-9_]*\)\t.*\t\/\^struct \1;.*\$\/;"\tx$/d' $1
249fi
250