Access Tcl Global Variables in an SDC File

When you read an SDC file from the command line, it works fine. When you read it using a Tcl procedure, it won’t work, because the global variables cannot be accessed. Here’s how to fix this annoying problem.

Tcl source Works on the Command Line

Suppose you reference the global variable CLK_SKEW in SDC file clk.sdc:

# File clk.sdc, which is a Tcl script
set_clock_uncertainty [get_clocks clk] -setup $CLK_SKEW

You will have no problem when you read the SDC file on the command line using Tcl source or read_sdc:

set CLK_SKEW 15
source clk.sdc
# or alternatively,
read_sdc clk.sdc

This works because on the command line, you are in the global context.

Tcl source Fails in a Tcl Procedure

Now, suppose that you define a Tcl proc to do something fancy, like source multiple files:

proc read_multiple_sdc {sdc_file_list} {
    foreach sdc_file $sdc_file_list {
        source $sdc_file
    }
}

upon execution, this fails:

set CLK_SKEW 15
read_multiple_sdc {"clk.sdc" "design.sdc"}
Error: can't read "CLK_SKEW": no such variable

This is because global variable CLK_SKEW cannot be referenced from within the context of the procedure read_multiple_sdc.

Solutions

There are two solutions.

Declare Global Variables using Tcl global

The best solution is to declare each global variable you will reference as global:

# File clk.sdc
global CLK_SKEW
set_clock_uncertainty [get_clocks clk] -setup $CLK_SKEW

This approach forces the author of the SDC file to think carefully about each global variable being accessed, while the other global variables remain safely hidden.

Use Tcl uplevel

Another approach is for your procedure to execute read_sdc or source using Tcl uplevel:

proc read_multiple_sdc {sdc_file_list} {
    foreach sdc_file $sdc_file_list {
        uplevel #0 source $sdc_file
    }
}

uplevel #0 means to execute the specified script in context zero, the global context. This gives the Tcl script in the SDC file access to all global variables, but it increases the risk that the SDC file might inadvertently modify a global variable. The advantage is that the SDC file requires no modification.

Background

These techniques should work with any any Tcl script. The above discussion is oriented toward reading Standard Design Constraint (SDC) files, which are Tcl scripts used to describe design intent in digital IC design. SDC files are used with logic synthesis or place and route tools like Synopsys Design Compiler, Primetime and IC Compiler, Cadence RTL Compiler and SoC Encounter.


This article was originally published by John McGehee, Voom, Inc. under the CC BY 3.0 license.  Changes have been made.