Search code examples
tagssconsctags

scons and ctags


I am trying to get ctags to be automatically run when I do a scons. I have a custom builder based on this answer that allows me to run ctags from within my SConscript file. This works in as far as I get a tags file in the appropriate directory. However, since the builder runs within the root directory and not the subdirectory, I get the wrong paths (aka from the root, not the subdirectory) for all the files contains within the tags file. Apart from using sed to strip paths in the tags file, anyone can offer a suggestion as to how to make scons generate the right paths?

In effects, I would like to run the builder in a specific directory (aka the one where the SConscript is located, maybe passed as an option?) and not from the project root.


Solution

  • This is the builder code I ended up using. Notice that the ctags command has a --tag-relative=yes option to it.

    # -*- coding: utf-8 -*-
    import SCons.Builder
    import SCons.Action
    
    def complain_ctags(target, source, env):
        print 'INFORMATION: ctags binary was not found (see above). Tags have not been built.'
    
    def generate(env):
        env['CTAGS'] = find_ctags(env)
        if env['CTAGS'] != None:
            #env['CTAGSCOM'] = 'cd $TARGET.dir; ctags -R .'
            env['CTAGSCOM'] = '$CTAGS --tag-relative=yes -f $TARGET $SOURCES'
            env['BUILDERS']['ctags'] = SCons.Builder.Builder(action=env['CTAGSCOM'])
        else:
            env['BUILDERS']['ctags'] = SCons.Builder.Builder(action=env.Action(complain_ctags))
    
    def find_ctags(env):
        b=env.WhereIs('ctags')
        if b == None:
            print 'Searching for ctags: not found. Tags will not be built'
        else:
            print 'Searching for ctags: ', b
        return b
    
    def exists(env):
        if find_ctags(env) == None:
            return 0
        return 1
    

    In my Sconscript file(s), I can now do:

    Alias('tags', env.ctags(source=all_python_files, target='tags'))
    

    The tags file now contains tags with path relative to the location of the tags file and not from the scons root directory.