tags.sh 9.1 KB

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