tags.sh 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #!/bin/bash
  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, SUBARCH, SRCARCH, srctree, src, obj
  9. if [ "$KBUILD_VERBOSE" = "1" ]; then
  10. set -x
  11. fi
  12. # RCS_FIND_IGNORE has escaped ()s -- remove them.
  13. ignore="$(echo "$RCS_FIND_IGNORE" | sed 's|\\||g' )"
  14. # tags and cscope files should also ignore MODVERSION *.mod.c files
  15. ignore="$ignore ( -name *.mod.c ) -prune -o"
  16. # Do not use full path if we do not use O=.. builds
  17. # Use make O=. {tags|cscope}
  18. # to force full paths for a non-O= build
  19. if [ "${KBUILD_SRC}" = "" ]; then
  20. tree=
  21. else
  22. tree=${srctree}/
  23. fi
  24. # ignore userspace tools
  25. ignore="$ignore ( -path ${tree}tools ) -prune -o"
  26. # Detect if ALLSOURCE_ARCHS is set. If not, we assume SRCARCH
  27. if [ "${ALLSOURCE_ARCHS}" = "" ]; then
  28. ALLSOURCE_ARCHS=${SRCARCH}
  29. elif [ "${ALLSOURCE_ARCHS}" = "all" ]; then
  30. ALLSOURCE_ARCHS=$(find ${tree}arch/ -mindepth 1 -maxdepth 1 -type d -printf '%f ')
  31. fi
  32. # find sources in arch/$ARCH
  33. find_arch_sources()
  34. {
  35. for i in $archincludedir; do
  36. prune="$prune -wholename $i -prune -o"
  37. done
  38. find ${tree}arch/$1 $ignore $subarchprune $prune -name "$2" \
  39. -not -type l -print;
  40. }
  41. # find sources in arch/$1/include
  42. find_arch_include_sources()
  43. {
  44. include=$(find ${tree}arch/$1/ $subarchprune \
  45. -name include -type d -print);
  46. if [ -n "$include" ]; then
  47. archincludedir="$archincludedir $include"
  48. find $include $ignore -name "$2" -not -type l -print;
  49. fi
  50. }
  51. # find sources in include/
  52. find_include_sources()
  53. {
  54. find ${tree}include $ignore -name config -prune -o -name "$1" \
  55. -not -type l -print;
  56. }
  57. # find sources in rest of tree
  58. # we could benefit from a list of dirs to search in here
  59. find_other_sources()
  60. {
  61. find ${tree}* $ignore \
  62. \( -path ${tree}include -o -path ${tree}arch -o -name '.tmp_*' \) -prune -o \
  63. -name "$1" -not -type l -print;
  64. }
  65. find_sources()
  66. {
  67. find_arch_sources $1 "$2"
  68. }
  69. all_sources()
  70. {
  71. find_arch_include_sources ${SRCARCH} '*.[chS]'
  72. if [ ! -z "$archinclude" ]; then
  73. find_arch_include_sources $archinclude '*.[chS]'
  74. fi
  75. find_include_sources '*.[chS]'
  76. for arch in $ALLSOURCE_ARCHS
  77. do
  78. find_sources $arch '*.[chS]'
  79. done
  80. find_other_sources '*.[chS]'
  81. }
  82. all_compiled_sources()
  83. {
  84. for i in $(all_sources); do
  85. case "$i" in
  86. *.[cS])
  87. j=${i/\.[cS]/\.o}
  88. j="${j#$tree}"
  89. if [ -e $j ]; then
  90. echo $i
  91. fi
  92. ;;
  93. *)
  94. echo $i
  95. ;;
  96. esac
  97. done
  98. }
  99. all_target_sources()
  100. {
  101. if [ -n "$COMPILED_SOURCE" ]; then
  102. all_compiled_sources
  103. else
  104. all_sources
  105. fi
  106. }
  107. all_kconfigs()
  108. {
  109. find ${tree}arch/ -maxdepth 1 $ignore \
  110. -name "Kconfig*" -not -type l -print;
  111. for arch in $ALLSOURCE_ARCHS; do
  112. find_sources $arch 'Kconfig*'
  113. done
  114. find_other_sources 'Kconfig*'
  115. }
  116. docscope()
  117. {
  118. (echo \-k; echo \-q; all_target_sources) > cscope.files
  119. cscope -b -f cscope.out
  120. }
  121. dogtags()
  122. {
  123. all_target_sources | gtags -i -f -
  124. }
  125. # Basic regular expressions with an optional /kind-spec/ for ctags and
  126. # the following limitations:
  127. # - No regex modifiers
  128. # - Use \{0,1\} instead of \?, because etags expects an unescaped ?
  129. # - \s is not working with etags, use a space or [ \t]
  130. # - \w works, but does not match underscores in etags
  131. # - etags regular expressions have to match at the start of a line;
  132. # a ^[^#] is prepended by setup_regex unless an anchor is already present
  133. regex_asm=(
  134. '/^\(ENTRY\|_GLOBAL\)(\([[:alnum:]_\\]*\)).*/\2/'
  135. )
  136. regex_c=(
  137. '/^SYSCALL_DEFINE[0-9](\([[:alnum:]_]*\).*/sys_\1/'
  138. '/^BPF_CALL_[0-9](\([[:alnum:]_]*\).*/\1/'
  139. '/^COMPAT_SYSCALL_DEFINE[0-9](\([[:alnum:]_]*\).*/compat_sys_\1/'
  140. '/^TRACE_EVENT(\([[:alnum:]_]*\).*/trace_\1/'
  141. '/^TRACE_EVENT(\([[:alnum:]_]*\).*/trace_\1_rcuidle/'
  142. '/^DEFINE_EVENT([^,)]*, *\([[:alnum:]_]*\).*/trace_\1/'
  143. '/^DEFINE_EVENT([^,)]*, *\([[:alnum:]_]*\).*/trace_\1_rcuidle/'
  144. '/^DEFINE_INSN_CACHE_OPS(\([[:alnum:]_]*\).*/get_\1_slot/'
  145. '/^DEFINE_INSN_CACHE_OPS(\([[:alnum:]_]*\).*/free_\1_slot/'
  146. '/^PAGEFLAG(\([[:alnum:]_]*\).*/Page\1/'
  147. '/^PAGEFLAG(\([[:alnum:]_]*\).*/SetPage\1/'
  148. '/^PAGEFLAG(\([[:alnum:]_]*\).*/ClearPage\1/'
  149. '/^TESTSETFLAG(\([[:alnum:]_]*\).*/TestSetPage\1/'
  150. '/^TESTPAGEFLAG(\([[:alnum:]_]*\).*/Page\1/'
  151. '/^SETPAGEFLAG(\([[:alnum:]_]*\).*/SetPage\1/'
  152. '/\<__SETPAGEFLAG(\([[:alnum:]_]*\).*/__SetPage\1/'
  153. '/\<TESTCLEARFLAG(\([[:alnum:]_]*\).*/TestClearPage\1/'
  154. '/\<__TESTCLEARFLAG(\([[:alnum:]_]*\).*/TestClearPage\1/'
  155. '/\<CLEARPAGEFLAG(\([[:alnum:]_]*\).*/ClearPage\1/'
  156. '/\<__CLEARPAGEFLAG(\([[:alnum:]_]*\).*/__ClearPage\1/'
  157. '/^__PAGEFLAG(\([[:alnum:]_]*\).*/__SetPage\1/'
  158. '/^__PAGEFLAG(\([[:alnum:]_]*\).*/__ClearPage\1/'
  159. '/^PAGEFLAG_FALSE(\([[:alnum:]_]*\).*/Page\1/'
  160. '/\<TESTSCFLAG(\([[:alnum:]_]*\).*/TestSetPage\1/'
  161. '/\<TESTSCFLAG(\([[:alnum:]_]*\).*/TestClearPage\1/'
  162. '/\<SETPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/SetPage\1/'
  163. '/\<CLEARPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/ClearPage\1/'
  164. '/\<__CLEARPAGEFLAG_NOOP(\([[:alnum:]_]*\).*/__ClearPage\1/'
  165. '/\<TESTCLEARFLAG_FALSE(\([[:alnum:]_]*\).*/TestClearPage\1/'
  166. '/^PAGE_TYPE_OPS(\([[:alnum:]_]*\).*/Page\1/'
  167. '/^PAGE_TYPE_OPS(\([[:alnum:]_]*\).*/__SetPage\1/'
  168. '/^PAGE_TYPE_OPS(\([[:alnum:]_]*\).*/__ClearPage\1/'
  169. '/^TASK_PFA_TEST([^,]*, *\([[:alnum:]_]*\))/task_\1/'
  170. '/^TASK_PFA_SET([^,]*, *\([[:alnum:]_]*\))/task_set_\1/'
  171. '/^TASK_PFA_CLEAR([^,]*, *\([[:alnum:]_]*\))/task_clear_\1/'
  172. '/^DEF_MMIO_\(IN\|OUT\)_[XD](\([[:alnum:]_]*\),[^)]*)/\2/'
  173. '/^DEBUGGER_BOILERPLATE(\([[:alnum:]_]*\))/\1/'
  174. '/^DEF_PCI_AC_\(\|NO\)RET(\([[:alnum:]_]*\).*/\2/'
  175. '/^PCI_OP_READ(\(\w*\).*[1-4])/pci_bus_read_config_\1/'
  176. '/^PCI_OP_WRITE(\(\w*\).*[1-4])/pci_bus_write_config_\1/'
  177. '/\<DEFINE_\(MUTEX\|SEMAPHORE\|SPINLOCK\)(\([[:alnum:]_]*\)/\2/v/'
  178. '/\<DEFINE_\(RAW_SPINLOCK\|RWLOCK\|SEQLOCK\)(\([[:alnum:]_]*\)/\2/v/'
  179. '/\<DECLARE_\(RWSEM\|COMPLETION\)(\([[:alnum:]_]\+\)/\2/v/'
  180. '/\<DECLARE_BITMAP(\([[:alnum:]_]*\)/\1/v/'
  181. '/\(^\|\s\)\(\|L\|H\)LIST_HEAD(\([[:alnum:]_]*\)/\3/v/'
  182. '/\(^\|\s\)RADIX_TREE(\([[:alnum:]_]*\)/\2/v/'
  183. '/\<DEFINE_PER_CPU([^,]*, *\([[:alnum:]_]*\)/\1/v/'
  184. '/\<DEFINE_PER_CPU_SHARED_ALIGNED([^,]*, *\([[:alnum:]_]*\)/\1/v/'
  185. '/\<DECLARE_WAIT_QUEUE_HEAD(\([[:alnum:]_]*\)/\1/v/'
  186. '/\<DECLARE_\(TASKLET\|WORK\|DELAYED_WORK\)(\([[:alnum:]_]*\)/\2/v/'
  187. '/\(^\s\)OFFSET(\([[:alnum:]_]*\)/\2/v/'
  188. '/\(^\s\)DEFINE(\([[:alnum:]_]*\)/\2/v/'
  189. '/\<DEFINE_HASHTABLE(\([[:alnum:]_]*\)/\1/v/'
  190. )
  191. regex_kconfig=(
  192. '/^[[:blank:]]*\(menu\|\)config[[:blank:]]\+\([[:alnum:]_]\+\)/\2/'
  193. '/^[[:blank:]]*\(menu\|\)config[[:blank:]]\+\([[:alnum:]_]\+\)/CONFIG_\2/'
  194. )
  195. setup_regex()
  196. {
  197. local mode=$1 lang tmp=() r
  198. shift
  199. regex=()
  200. for lang; do
  201. case "$lang" in
  202. asm) tmp=("${regex_asm[@]}") ;;
  203. c) tmp=("${regex_c[@]}") ;;
  204. kconfig) tmp=("${regex_kconfig[@]}") ;;
  205. esac
  206. for r in "${tmp[@]}"; do
  207. if test "$mode" = "exuberant"; then
  208. regex[${#regex[@]}]="--regex-$lang=${r}b"
  209. else
  210. # Remove ctags /kind-spec/
  211. case "$r" in
  212. /*/*/?/)
  213. r=${r%?/}
  214. esac
  215. # Prepend ^[^#] unless already anchored
  216. case "$r" in
  217. /^*) ;;
  218. *)
  219. r="/^[^#]*${r#/}"
  220. esac
  221. regex[${#regex[@]}]="--regex=$r"
  222. fi
  223. done
  224. done
  225. }
  226. exuberant()
  227. {
  228. setup_regex exuberant asm c
  229. all_target_sources | xargs $1 -a \
  230. -I __initdata,__exitdata,__initconst,__ro_after_init \
  231. -I __initdata_memblock \
  232. -I __refdata,__attribute,__maybe_unused,__always_unused \
  233. -I __acquires,__releases,__deprecated \
  234. -I __read_mostly,__aligned,____cacheline_aligned \
  235. -I ____cacheline_aligned_in_smp \
  236. -I __cacheline_aligned,__cacheline_aligned_in_smp \
  237. -I ____cacheline_internodealigned_in_smp \
  238. -I __used,__packed,__packed2__,__must_check,__must_hold \
  239. -I EXPORT_SYMBOL,EXPORT_SYMBOL_GPL,ACPI_EXPORT_SYMBOL \
  240. -I DEFINE_TRACE,EXPORT_TRACEPOINT_SYMBOL,EXPORT_TRACEPOINT_SYMBOL_GPL \
  241. -I static,const \
  242. --extra=+fq --c-kinds=+px --fields=+iaS --langmap=c:+.h \
  243. "${regex[@]}"
  244. setup_regex exuberant kconfig
  245. all_kconfigs | xargs $1 -a \
  246. --langdef=kconfig --language-force=kconfig "${regex[@]}"
  247. }
  248. emacs()
  249. {
  250. setup_regex emacs asm c
  251. all_target_sources | xargs $1 -a "${regex[@]}"
  252. setup_regex emacs kconfig
  253. all_kconfigs | xargs $1 -a "${regex[@]}"
  254. }
  255. xtags()
  256. {
  257. if $1 --version 2>&1 | grep -iq exuberant; then
  258. exuberant $1
  259. elif $1 --version 2>&1 | grep -iq emacs; then
  260. emacs $1
  261. else
  262. all_target_sources | xargs $1 -a
  263. fi
  264. }
  265. # Support um (which uses SUBARCH)
  266. if [ "${ARCH}" = "um" ]; then
  267. if [ "$SUBARCH" = "i386" ]; then
  268. archinclude=x86
  269. elif [ "$SUBARCH" = "x86_64" ]; then
  270. archinclude=x86
  271. else
  272. archinclude=${SUBARCH}
  273. fi
  274. elif [ "${SRCARCH}" = "arm" -a "${SUBARCH}" != "" ]; then
  275. subarchdir=$(find ${tree}arch/$SRCARCH/ -name "mach-*" -type d -o \
  276. -name "plat-*" -type d);
  277. mach_suffix=$SUBARCH
  278. plat_suffix=$SUBARCH
  279. # Special cases when $plat_suffix != $mach_suffix
  280. case $mach_suffix in
  281. "omap1" | "omap2")
  282. plat_suffix="omap"
  283. ;;
  284. esac
  285. if [ ! -d ${tree}arch/$SRCARCH/mach-$mach_suffix ]; then
  286. echo "Warning: arch/arm/mach-$mach_suffix/ not found." >&2
  287. echo " Fix your \$SUBARCH appropriately" >&2
  288. fi
  289. for i in $subarchdir; do
  290. case "$i" in
  291. *"mach-"${mach_suffix})
  292. ;;
  293. *"plat-"${plat_suffix})
  294. ;;
  295. *)
  296. subarchprune="$subarchprune \
  297. -wholename $i -prune -o"
  298. ;;
  299. esac
  300. done
  301. fi
  302. remove_structs=
  303. case "$1" in
  304. "cscope")
  305. docscope
  306. ;;
  307. "gtags")
  308. dogtags
  309. ;;
  310. "tags")
  311. rm -f tags
  312. xtags ctags
  313. remove_structs=y
  314. ;;
  315. "TAGS")
  316. rm -f TAGS
  317. xtags etags
  318. remove_structs=y
  319. ;;
  320. esac
  321. # Remove structure forward declarations.
  322. if [ -n "$remove_structs" ]; then
  323. LANG=C sed -i -e '/^\([a-zA-Z_][a-zA-Z0-9_]*\)\t.*\t\/\^struct \1;.*\$\/;"\tx$/d' $1
  324. fi