Skip to content

Instantly share code, notes, and snippets.

@bartman
Last active January 23, 2023 13:30
Show Gist options
  • Save bartman/e35a471ff1d3c33b0f7ca11c8e4de756 to your computer and use it in GitHub Desktop.
Save bartman/e35a471ff1d3c33b0f7ca11c8e4de756 to your computer and use it in GitHub Desktop.
mktags
#!/usr/bin/make -f
#
# This is a script that bilds ctags/csope files.
#
# By default, it will build indexes in the current directory:
#
# mktags
#
# You can invoke it to build different subdirectories:
#
# mktags DIRS="foo bar"
#
# You can build a global index on /usr/include (uses sudo if needed):
#
# mktags usr
#
# You can build a global index of the running kernel's source tree:
#
# mktags kernel
#
SELF = $(lastword $(MAKEFILE_LIST))
DIRS ?= .
IGNORE_DIRS ?= .svn BitKeeper CVS SCCS .hg .git
EXISTING_IGNORE_DIRS = $(wildcard ${IGNORE_DIRS})
FIND_FILTER = $(shell for d in ${EXISTING_IGNORE_DIRS} ; do echo ! -path "\"*/$$d/*\"" -a ; done)
.PHONY: all cscope cscope.files cscope.out tags clean
all: cscope tags
cscope: cscope.out
cscope.out: cscope.files
cscope -b
tags: cscope.files
@rm -f tags
ctags -L cscope.files
ABS_DIRS1 = $(foreach d,${DIRS},$(shell cd ${d} && pwd))
ABS_DIRS = $(foreach d,${ABS_DIRS1},${d}/)
cscope.files:
( find ${ABS_DIRS} ${FIND_FILTER} -type f \
-name '*.[chCH]' -o -name '*.[cC][cC]' \
-o -name '*.[chCH][pPxX+][pPxX+]' \
-o -name '*.py' ; \
find ${ABS_DIRS} ${FIND_FILTER} -type f \
| grep -E -e '.*/[a-z_]+$$' \
| xargs file -e apptype -e encoding -e tokens -e cdf -e compress -e elf -e tar -F '%' \
| sed -r -n -e 's/^([^%]*)%.*C.*source.*text/\1/p' \
) > cscope.files
clean:
-rm -f tags cscope.files cscope.out
.PHONY: help
help:
@echo "mktags - create cscope and tags databases"
@echo
@echo "Usage:"
@echo
@echo " mktags -- index current directory"
@echo " mktags DIRS=\"foo bar\" -- index specified directories"
@echo " mktags usr -- index /usr/include (uses sudo)"
@echo " mktags kernel -- index running kernel source tree (uses sudo)"
@echo " mktags clean -- remove tags and cscope files"
.PHONY: global kernel usr kernel-priv kernel-usr
global: kernel usr
UID := $(shell id -u)
SUDO ?= $(if $(filter ${UID},0),,sudo)
KVER ?= $(shell uname -r)
KDIR ?= $(shell readlink /lib/modules/${KVER}/build)
# if it's in /usr/src/ assume run kernel-usr, otherwise kernel-priv
kernel: $(if $(filter /usr/src/linux-headers-%,${KDIR}),kernel-usr,kernel-priv)
KBSE = $(shell echo ${KDIR} | sed -r -e 's/-[^-]*$$//')
KCMN = ${KBSE}-common
KDIRS = $(foreach dir,${KDIR} ${KBSE} ${KCMN},$(wildcard ${dir}/))
kernel-usr:
@echo Running on: ${KDIRS}
${SUDO} ${MAKE} -C ${KDIR} -f ${SELF} tags cscope DIRS="${KDIRS}"
kernel-priv:
${MAKE} -C ${KDIR} tags cscope
USRINC = /usr/include /usr/include/x86_64-linux-gnu
LIBGCC = $(shell find /usr/lib/gcc/x86_64-linux-gnu -maxdepth 1 -type d | xargs ls -dvr | head -n1)
usr:
${SUDO} ${MAKE} -C /usr/include -f ${SELF} tags cscope DIRS="${USRINC} ${LIBGCC}"
# print any variable
print-% : ; @echo "$* = $($*)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment