Quartus® II Tcl Get Subversion Revision Number



This sample script shows how you can access a version control software version number for your project. You can write the version number in a design file that is compiled in your FPGA design. With additional design logic, the version number can be reported by the FPGA. This information can be very useful during debugging, especially if you switch between multiple programming files. You can easily find out which version of the design is running on the FPGA, based on the version number in the FPGA.

This example uses the Subversion revision number for your project. It uses the svn info command to get information about the specified file. The svn info command prints information about items in your working copy, and it includes a line with the revision number, in the following form:

Revision: <revision number>

This example uses two procedures to run the svn info command and parse the output to get the revision number. The get_subversion_revision procedure starts the svn info command. Call the procedure with the file name to use with the svn info command. The procedure returns with an error if the command could not be run. Otherwise, the procedure returns nothing, but sets certain global variables. If the svn info command timed out, the value of the global variable done is -1. If the revision number was found, the value of the global variable done is 1 and the revision number is in the revision_number global variable. You can display the revision number in a message, as in this example, or write it into a design file.

The get_version_info procedure is a helper procedure that parses the command output a line at a time. It includes a regular expression that matches the revision number line and extracts the revision number.

proc get_subversion_revision { file_name } {

    global done

    # The maximum number of seconds to wait for the svn info
    # command to complete
    set timeout_seconds 30

    # The svn info command with filename that is run
    set cmd "svn info ${file_name}"

    # Attempt to get the version information.
    # If the command can't be run, return an error.
    # Otherwise set up a file event to process the command output.
    if { [catch {open "|$cmd"} input] } {
        return -code error $input
    } else {

        fileevent $input readable [list get_revision_info $input ]

        # Set up a timeout so that the process can't hang if the
        # repository is down.
        set timeout [after [ expr { $timeout_seconds * 1000 } ] \
            [list set done -1] ]

        # Don't continue until the revision number is found,
        # or the operation times out. Cancel the timeout anyway.
        vwait done
        after cancel $timeout

proc get_revision_info { inp  } {

    global done revision_number

    if { [eof $inp] } {
        catch {close $inp}
        set done 1
    } elseif { $done } {
        gets $inp line
    } else {
        gets $inp line
        # Use a regular expression to match the line with the
        # revision number.
        if { [regexp {^Revision:\s+(\d+)\s*$} $line match revision_number] } {
            set done 1

set done 0
set revision_number ""

# The file name is usually your project file

set file_name [lindex $quartus(args) 0]

if { [catch { get_subversion_revision $file_name } msg] } {
    post_message -type critical_warning "Couldn't run command to get\
        revision number. $msg"
} else {

    if { -1 == $done } {
        post_message -type critical_warning "Timeout getting revision number."
    } elseif { [string equal "" $revision_number] } {
        post_message -type critical_warning \
            "Couldn't find revision number in output of svn info $file_name."
    } else {
        post_message "Revision for $file_name is $revision_number"

You could run the script at a system command prompt with the following command (assuming the script is in a file named svn_revision.tcl):

quartus_sh -t svn_revision.tcl myproject.qpf

The script produces a message like this to show the revision:

Info: Revision for myproject.qpf is 417

You can display a message with the revision number in the global variable revision_number, as in this example, or write it into a design file.