LinuxCommandLibrary

gtags

Generate tags for source code

SYNOPSIS

gtags [options] [files]

PARAMETERS

-c, --clean
    Delete all generated tag files (GTAGS, GRTAGS, GPATH, etc.) in the current project root.

-i, --incremental
    Update tag files incrementally. This is generally faster for small changes, updating only modified records.

-u, --update
    Update tag files. Similar to --incremental, but can re-scan more thoroughly if needed.

-f <file>, --file <file>
    Read file names to be processed from <file> instead of scanning the directory or standard input.

-P <path>, --path <path>
    Specify the project root directory. If not specified, gtags assumes the current directory as the root.

-v, --verbose
    Enable verbose output, showing more details about the tagging process.

-q, --quiet
    Suppress warning and error messages during the tagging process.

--exclude <pattern>
    Exclude files or directories matching the specified <pattern> from being parsed. Can be used multiple times.

--gtagsconf <file>
    Specify an alternative configuration file for gtags instead of the default gtags.conf.

--version
    Display version information and exit.

--help
    Display a help message and exit.

DESCRIPTION

gtags is a crucial component of GNU GLOBAL, a powerful source code tag system. Its primary function is to create a set of tag files (GTAGS, GRTAGS, GPATH, and optionally GSHTAGS) within a source code directory. These files act as an index, enabling rapid navigation and information retrieval about symbols, functions, and variables across an entire project. It works by parsing source files in various programming languages, including C, C++, Java, Python, PHP, and Ruby, to build a comprehensive database of symbol definitions and references. This database is then utilized by the global command-line tool, editor plugins, and other frontends to quickly jump to definitions, find all references to a symbol, or list files containing specific patterns. gtags is highly configurable, allowing users to specify which files to process, ignore certain directories, and even extend its capabilities to new languages or parsing rules, making it an indispensable tool for efficiently exploring and understanding large codebases.

CAVEATS

  • Parsing Accuracy: While gtags supports many languages, its parsing is primarily lexical. It may not always fully understand complex semantic constructs, potentially leading to minor inaccuracies compared to full compiler-based analysis.
  • Resource Usage: For extremely large codebases, the initial tag generation can be resource-intensive in terms of CPU, memory, and disk space. Incremental updates (-i) significantly reduce this for subsequent runs.
  • Configuration Dependency: Optimal performance and accurate tag generation often depend on a well-configured gtags.conf file, especially for projects with custom file extensions, unique directory structures, or specific exclusion requirements.

ENVIRONMENT VARIABLES

  • GTAGSROOT: Specifies the project root directory where tag files are expected or created if not the current working directory.
  • GTAGSCONF: Defines the path to the gtags.conf configuration file, overriding default locations.
  • GTAGSLABEL: Specifies the label of the configuration to use within gtags.conf, allowing for project-specific settings.
  • GTAGSOBJDIR: Specifies an alternative directory where tag files should be created, useful for keeping source directories clean.

CONFIGURATION FILE (<I>GTAGS.CONF</I>)

The gtags.conf file is central to customizing gtags' behavior. It typically defines:

  • Parsers: Specifies which parser to use for different file extensions and how to handle various programming languages.
  • Exclusions: Lists directories and file patterns to ignore during tag generation.
  • Language-specific Settings: Allows fine-tuning of parsing rules for supported languages.
  • Tool paths: Can define paths to external tools used by gtags.
It is usually found in `/etc/gtags.conf` or `~/.globalrc`, but can be overridden by GTAGSCONF.

GENERATED TAG FILES

gtags creates several files to store its index information:

  • GTAGS: Contains definitions of symbols (functions, variables, macros, etc.).
  • GRTAGS: Stores references to symbols, linking usages back to their definitions.
  • GPATH: Lists all source file pathnames included in the project.
  • GSHTAGS: (Optional) Used for shell command history tags, often generated when using cscope compatibility scripts.
  • GTAGSLOG: A log file used to track changes for incremental updates.

HISTORY

GNU GLOBAL was originally developed by Shigio Yamaguchi in 1993 with the goal of creating a language-independent and highly efficient source code tag system. gtags, as its core component, was designed to build a persistent, fast-access symbol database for various programming languages. Unlike older tag tools often tied to C, GLOBAL aimed for broader applicability and extensibility. Over the decades, it has evolved to support a wide array of languages and has been integrated into numerous developer workflows, particularly favored for its speed and reliability in large software projects and text editor environments like Vim and Emacs.

SEE ALSO

global(1), htags(1), gtags-cscope.sh(1), ctags(1)

Copied to clipboard