How to Install Verible: Google's SystemVerilog Tool

SystemVerilog developers often struggle with the complexity of the language and the need for various tools to improve their codebase. Verible, a versatile open-source project by Google, aims to address these challenges by offering a suite of applications designed for SystemVerilog development, including style linting, formatting, parsing, and more. In this blog post, we'll guide you through the process of installing and using Verible and its components, along with tips on how to integrate them into your development workflow.

Introduction to Verible

Verible is an open-source project developed by Google with the primary mission of parsing SystemVerilog (IEEE 1800-2017) code for a wide range of applications. Born from the need to parse un-preprocessed source files, Verible is suitable for single-file applications like style linting and formatting. The project aims to eliminate the need for developers to create custom SystemVerilog parsers, saving time and effort by providing a standard-compliant, well-tested parser.

In addition to its SystemVerilog capabilities, Verible's language-agnostic components can be used to develop language support tools for other programming languages.

Verible Tools Overview

The Verible project includes a comprehensive list of tools to enhance your SystemVerilog development experience:

  • Parser (verible-verilog-syntax): Parses SystemVerilog code.
  • Style Linter (verible-verilog-lint): Enforces coding style guidelines.
  • Formatter (verible-verilog-format): Automatically formats code according to predefined rules.
  • Language Server (verible-verilog-ls): Provides language server capabilities for better IDE integration.
  • Lexical Diff (verible-verilog-diff): Compares two source files at the token level.
  • Verible Project Tool (verible-verilog-project): Manages project configurations and settings.
  • Code Obfuscator (verible-verilog-obfuscate): Obfuscates source code for IP protection.
  • Preprocessor (verible-verilog-preprocessor): Processes source files with preprocessor directives.
  • Source Code Indexer (verible-verilog-kythe-extractor): Extracts indexing data for code navigation and cross-referencing.

System Requirements and Compatibility

Verible is compatible with Linux, macOS, and Windows operating systems. Depending on the installation method you choose, the requirements may differ:

Building and Compiling Verible from Source

To build and compile Verible from source, you will need the following:

  • A C++17-capable compiler like GCC, Clang, or Microsoft Visual Studio, depending on your OS.
  • The Bazel build system to compile the project.

Using Precompiled Verible Binaries

If you opt to use the precompiled binaries, the requirements are minimal:

  • A compatible operating system: Linux, macOS, or Windows.

In this case, you won't need to install a C++17-capable compiler or the Bazel build system, as the binaries are already compiled for your operating system.

Installing Verible and Its Components

Step 1: Install Dependencies

Ensure that you have the necessary dependencies installed:

  1. Bazel: Follow the official Bazel installation instructions for your operating system.
  2. C++17-capable compiler: Install GCC, Clang, or Microsoft Visual Studio, depending on your OS.

Step 2: Download Verible

Clone the Verible repository from GitHub:

git clone https://github.com/google/verible.git
cd verible

Step 3: Build and Compile Verible

Build and compile Verible using Bazel:

bazel build //...

Step 4: Add Verible Executables to Your PATH

After building Verible, add the Verible executables to your system's PATH for easy access:

# Linux and macOS
export PATH=$PATH:$PWD/bazel-bin

# Windows
set PATH=%PATH%;%CD%\bazel-bin

Alternatives for Installing Verible and Its Components

In addition to building and compiling Verible from source as described earlier, you can also download precompiled binaries for your operating system. This method is faster and does not require the installation of dependencies like Bazel or a C++17-capable compiler.

Step 1: Download Verible Precompiled Binaries

Navigate to the Verible repository's Releases page. Download the appropriate .tar.gz file for your operating system (Linux, macOS, or Windows).

Step 2: Extract Verible Binaries

Extract the downloaded .tar.gz file to a directory of your choice:

# Linux and macOS
tar -xzvf verible-<version>-<OS>.tar.gz -C /path/to/extract

# Windows
# Use a file archiver like 7-Zip to extract the .tar.gz file

Replace <version> with the downloaded Verible version and <OS> with your operating system (Linux, macOS, or Windows).

Step 3: Add Verible Executables to Your PATH

After extracting the Verible binaries, add the bin directory to your system's PATH for easy access:

# Linux and macOS
export PATH=$PATH:/path/to/extracted/verible/bin

# Windows
set PATH=%PATH%;C:\path\to\extracted\verible\bin

Now, you can use Verible tools directly from the command line, without having to build and compile the project from source.

Remember to adjust the paths in the commands above to match the location where you extracted the Verible binaries.

Using Verible Tools in Your Workflow

In this section, we'll provide an overview of how to use some of the most common Verible tools in your development workflow, specifically the style linter and formatter.

Style Linting with Verible

The Verible Style Linter helps ensure that your SystemVerilog code follows best practices and guidelines. To perform style linting on a SystemVerilog file, use the following command:

verible-verilog-lint /path/to/your/file.sv

This command will check your file for coding style violations and provide suggestions on how to fix them. For example, if your code contains a style issue like an improperly named module, the linter will output a message similar to this:

/path/to/your/file.sv:3:14: Style guide rule 'module-name-style' violated. See https://github.com/lowRISC/style-guides/blob/master/VerilogCodingStyle.md#naming for guidance.

Formatting with Verible

The Verible Formatter automatically formats your SystemVerilog code according to predefined style rules, ensuring a consistent appearance across your codebase. To format a SystemVerilog file, use the following command:

verible-verilog-format /path/to/your/file.sv > /path/to/formatted/file.sv

This command will reformat your code and save the result to a new file. You can also format the code in-place by using the --inplace option:

verible-verilog-format --inplace /path/to/your/file.sv

By incorporating the Verible Style Linter and Formatter into your development workflow, you can maintain a clean, consistent, and easy-to-read codebase, improving overall code quality and readability.

Integrating Verible with Your Development Workflow

In addition to using Verible tools individually, you can integrate them into your development workflow for seamless code quality checks and formatting.

Using Verible with Git Hooks

To automatically lint and format your SystemVerilog files before committing changes, you can set up a Git pre-commit hook. This will help ensure that only clean, well-formatted code is committed to your repository.

Create a .git/hooks/pre-commit file in your Git repository and add the following script:

#!/bin/sh

git diff --cached --name-only --diff-filter=AM | grep '\.sv$' | while read -r file; do
  verible-verilog-lint "$file" || exit 1
  verible-verilog-format --inplace "$file" || exit 1
  git add "$file"
done

Make the script executable:

chmod +x .git/hooks/pre-commit

Now, whenever you commit changes, the pre-commit hook will automatically lint and format your SystemVerilog files.

Configuring Your IDE for Verible

Many popular IDEs and text editors can be configured to work with Verible for real-time linting, formatting, and language server support. Consult your IDE's documentation for instructions on configuring it to use Verible tools.

Conclusion

Verible is an invaluable toolset for SystemVerilog developers, providing a range of applications that can greatly enhance code quality and readability. By following this guide, you should now have a better understanding of how to install and use Verible tools, and how to integrate them into your development workflow for a smoother, more efficient coding experience.