+ Post New Thread
Results 1 to 7 of 7
  1. #1
    Full Member level 3
    Points: 1,557, Level: 9

    Join Date
    May 2014
    Posts
    182
    Helped
    18 / 18
    Points
    1,557
    Level
    9

    Tcl to update version number

    Hello all,

    Previous on a different machine in a land far far away I've implemented this.

    However I can't recall how to do it. The ; was previously attached to the number which made it even more complicated.

    I would like to take this line
    constant FIRMWARE_VERSION_C : integer := 71 ;
    and turn it into the following
    constant FIRMWARE_VERSION_C : integer := 72 ;

    Code TCL - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    
    set filename "./hdl/FWKS_PIU_PKG.vhd"
    set filename2 "./hdl/FWKS_PIU_PKG_temp.vhd"
     
    set f [open $filename]
    set f2 [open $filename2 w]
    set content [read $f]
     
    set lines [split $content "\n"]
     
    foreach line $lines {
        if [regexp "FIRMWARE_VERSION_C" $line] {
            set a [lindex [split $line] end]
            
            # set b [expr {int($a) +1}]; #kinda works
            # lreplace $line end-1 end-1 $b
            # puts $f2 $line
            puts $f2 $a
     
     
        } else {
            puts $f2 $line
        }
    }
    # It is important to not lock out the file
     
    close $f 
    close $f2

    When I try the line replacement my tcl compiler in libero is saying "[ Line 12: expected number but got ":=" ]" - which is the foreach line.

    - - - Updated - - -

    FYI.
    Error mentioned above brought about by line in the original file which didn't have space between number and ";"

    -- constant FIRMWARE_VERSION_C : integer := 72;

    Now it will run but not update the number!

    - - - Updated - - -

    Solved own question, however would appreciate if anyone knows of a better way of doing this.

    Code TCL - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    set filename "./hdl/FWKS_PIU_PKG.vhd"
    set filename2 "./hdl/FWKS_PIU_PKG_temp.vhd"
     
    set f [open $filename]
    set f2 [open $filename2 w]
    set content [read $f]
    close $f 
     
    set lines [split $content "\n"]
     
     
    foreach line $lines {
     
        if [regexp "FIRMWARE_VERSION_C" $line] {
            set a [lindex [split $line] end-1]
            set b [expr {int($a)+1}]
     
            # set line2 [lreplace $line end-1 end "$b ;"]
            # lreplace $line end-1 end "$b ;"
            # lreplace $line end-1 end {$b ;}
            lreplace $line end-1 end-1 {$b}
            puts $f2 $line
            
        } else {
            puts $f2 $line
        }
    }
    # It is important to not lock out the file
     
    close $f2

    •   Alt18th August 2017, 14:15

      advertising

        
       

  2. #2
    Advanced Member level 4
    Points: 4,901, Level: 16

    Join Date
    Apr 2016
    Posts
    1,034
    Helped
    184 / 184
    Points
    4,901
    Level
    16

    Re: Tcl to update version number

    linux sed utility is by far a best solution to this
    Really, I am not Sam.



  3. #3
    Full Member level 3
    Points: 1,557, Level: 9

    Join Date
    May 2014
    Posts
    182
    Helped
    18 / 18
    Points
    1,557
    Level
    9

    Re: Tcl to update version number

    Unfortunately I'm on a Windows machine. I was thinking maybe using a batch script with string replacement, however ideally I'd like it to all be implemented in my tcl build script



    •   Alt21st August 2017, 07:52

      advertising

        
       

  4. #4
    Member level 3
    Points: 2,243, Level: 10

    Join Date
    Nov 2006
    Posts
    58
    Helped
    5 / 5
    Points
    2,243
    Level
    10

    Re: Tcl to update version number

    Have you tried https://www.cygwin.com/?
    It provides tools similar to what you can find on linux. It has the "sed" command and other useful stuff that exist in linux.



  5. #5
    Super Moderator
    Points: 27,697, Level: 40
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    6,323
    Helped
    1535 / 1535
    Points
    27,697
    Level
    40

    Re: Tcl to update version number

    Quote Originally Posted by sherif123 View Post
    Have you tried https://www.cygwin.com/?
    It provides tools similar to what you can find on linux. It has the "sed" command and other useful stuff that exist in linux.
    Actually there are win32 gnu tool ports available for sed and a bunch of other utilities. Unfortunately it doesn't appear to be maintained anymore, but is useful when you don't want to create a linux environment as you are scripting for a tool that is running under windows.
    http://gnuwin32.sourceforge.net/

    Though in pure Tcl you probably want to use regsub. You use the regexp you originally had to find the FIRMWARE_VERSION_C line then use regsub {\d+} $regexp_result_string "72" to replace the 71.

    Code:
    set line "constant FIRMWARE_VERSION_C : integer := 71 ;"
    if [regexp "FIRMWARE" $line] {
      regsub {\d+} $line "72" newline
    }
    puts $newline
    though if you want to increment thee 71 then you would have to extract the 71 out of the $line first then increment it and put it in place of the "72" in the regsub line.

    - - - Updated - - -

    This will extract the version number and increment it by 1, it also ignores any white space around the version number.
    Code TCL - [expand]
    1
    2
    3
    4
    5
    6
    
    set line "constant FIRMWARE_VERSION_C : integer := 72 ;"
     
    if [regexp {.*FIRMWARE_VERSION_C.*?(\d+)} $line matched sub1] {
      regsub {\d+} $line [expr $sub1 + 1] newline
    }
    puts $newline
    The ? makes the .* non-greedy, my original version was missing this and the results were always 2
    Last edited by ads-ee; 21st August 2017 at 18:02. Reason: removed non-working increment version


    1 members found this post helpful.

    •   Alt21st August 2017, 18:13

      advertising

        
       

  6. #6
    Full Member level 3
    Points: 1,557, Level: 9

    Join Date
    May 2014
    Posts
    182
    Helped
    18 / 18
    Points
    1,557
    Level
    9

    Re: Tcl to update version number

    Thanks Add-ee, this use of \d+ is good, because the way a VHDL signal is declared there should only ever be one digit. However what would you do if the "71 ;" was "71;" (id.est no space)



  7. #7
    Super Moderator
    Points: 27,697, Level: 40
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    6,323
    Helped
    1535 / 1535
    Points
    27,697
    Level
    40

    Re: Tcl to update version number

    the \d+ means 1 or more digits and it only means digits so 0-9 are extracted whitespace and other characters are ignored.

    If you need to deal with lines like:

    constant SOME_UGLY_NUMBERED_263_CONSTANT2 : integer :=71;

    then the following regex should work (untested):

    .*UGLY.*CONSTANT.*:=\s*?(\d+)[ ;].*

    which breaks down into, find UGLY followed after some number of other characters by CONSTANT followed by more characters up to the := followed by zero or more whitespace (space or tab, non-greedy), followed by the number you want to extract followed by a space or semi colon.

    The stuff after the (\d+) is utterly unnecessary but you asked about detecting if the number is followed by space or ;.

    Try playing around with the above expressions on this site: https://regex-golang.appspot.com/assets/html/index.html


    1 members found this post helpful.

--[[ ]]--