Tuesday, March 22, 2016

How can I run cscope to search source code in an arbitrary location only and write the cross-reference files in another arbitrary location?

Leave a Comment

This is how my source code directory looks like.

learner@centos:~/lab$ tree . +-- cscope.out +-- src ¦   +-- bar ¦   ¦   +-- bar.c ¦   ¦   +-- baz ¦   ¦       +-- baz.c ¦   +-- foo.c ¦   +-- qux ¦       +-- qux.c +-- work 

I want to create cross-reference for source code files in an arbitrary location on the filesystem (~/lab/src/bar in this example) and write the cross-reference to any arbitrary location (~/lab/work/cscope.out) in this case.

Nothing apart from ~/lab/src/bar should be included in the cross-reference.

The following command, of course, did not work. It includes ~/lab/src/*.c and ~/lab/src/qux/*.c also in the cross-reference

learner@centos:~/lab$ cscope -b -R -f ~/lab/work/cscope.out learner@centos:~/lab$ grep "@.*src" ~/lab/work/cscope.out         @src/bar/bar.c         @src/bar/baz/baz.c         @src/foo.c         @src/qux/qux.c 

The following command also suffers from the same problem because searching the current directory is automatically implied by default.

learner@centos:~/lab$ rm ~/lab/work/cscope.out learner@centos:~/lab$ cscope -b -R -f ~/lab/work/cscope.out -s ~/lab/src/bar learner@centos:~/lab$ grep "@.*src" ~/lab/work/cscope.out         @/home/learner/lab/src/bar/bar.c         @/home/learner/lab/src/bar/baz/baz.c         @src/bar/bar.c         @src/bar/baz/baz.c         @src/foo.c         @src/qux/qux.c 

The following command does not work.

learner@centos:~/lab$ rm ~/lab/work/* learner@centos:~/lab$ cscope -b -R -f ~/lab/work/cscope.out ~/lab/src/bar Cannot open file /home/learner/lab/src/bar 

With ctags, a similar command works fine.

learner@centos:~/lab$ ctags -R -f ~/lab/work/tags ~/lab/src/bar learner@centos:~/lab$ grep src ~/lab/work/tags bar     /home/learner/lab/src/bar/bar.c  /^void bar()$/;"        f baz     /home/learner/lab/src/bar/baz/baz.c      /^void baz()$/;"        f main    /home/learner/lab/src/bar/bar.c  /^int main()$/;"        f main    /home/learner/lab/src/bar/baz/baz.c      /^int main()$/;"        f 

How can I do the same thing with cscope?

The only workaround I have found so far is to first change into an empty directory and then run the cscope command with -s ~/lab/src/bar. This would search the specified directory along with the current directory but since the current directory is empty, only files from the specified directory would be included in the cross-reference.

learner@centos:~/lab$ rm ~/lab/work/* learner@centos:~/lab$ cd ~/lab/work learner@centos:~/lab/work$ cscope -b -R -f ~/lab/work/cscope.out -s ~/lab/src/bar learner@centos:~/lab/work$ grep "@.*src" ~/lab/work/cscope.out         @/home/learner/lab/src/bar/bar.c         @/home/learner/lab/src/bar/baz/baz.c 

Is there another solution that won't require me to change the current directory to an empty directory?

Note: The solution should work for a general use-case.

  • There may be hundreds of other files and directories that must not be included in the cross-reference (like foo.c and qux in the above example).
  • There may be files with other extension names such as .h, .l, .y, etc. that need to be cross-referenced.

1 Answers

Answers 1

the trick here is knowing the cscope will also accept a list a sources files rather than searching on its own. So passing the list of files is the problem to solve.

find to the rescue.

cscope -b -f <path_to_refdir>/cscope.out `find <path_of_desired_src_tree> -name *.[chly]` 

in your case

cscope -b -f work/cscope.out `find src/bar -name *.[chly]` 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment