Automatically including a timestamp when building

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Automatically including a timestamp when building

Jump to solution
1,310 Views
NickT
Contributor II

How would I include the date/time my code was built at so it could be printed by the software when requested?

 

I tried adding a -DTIME=%time% to the compliler options (%time% on Windows is a special env. var) but it's passed literally in (as though "#define TIME %time%").

 

Using a define would seem to be the cleanest method; but I would also be content with including a file that was generated by some batch/Python script as part of the build process.  However, I don't know how to automatically run other things at build time.

Labels (1)
0 Kudos
1 Solution
690 Views
NickT
Contributor II

Had some spare time to hack up a solution; here's what I cooked up with information borrowed from several other posts.

 

1. Configure (Edit...)Standard Settings:

  - Target/Target Settings

  --- Pre-linker: BatchRunner PreLinker

  - Linker/BatchRunner PreLinker

  --- {Project}verstamp.bat

 

2. verstamp.bat in my Project root

 

python verstamp.py

3. verstamp.py in my Project root

 

import sys
import os
import subprocess
import time

fname = 'verstamp'

cwpath = 'C:/Program Files/Freescale/CodeWarrior for Microcontrollers V6.3'
srcpath = './CODE'
objpath = './c6v2_Data/Standard/ObjectCode'

rev_code_only = True

cfile = '%s/%s.c' % (srcpath, fname)
ofile = '%s/%s.o' % (objpath, fname)

def main(argv=None):
    if argv is None:
        argv = sys.argv
   
    #datetimestamp = iso8601datetime(sdec=3)
    datetimestamp = time.strftime('%Y-%m-%d %H:%M:%S')
   
    # Running 'svnversion' on the entire project directory can take
    # a long time.  Probably TargetDataWindows.tdt, which causes a
    # significant pause when 'svn status' scans it.
    if rev_code_only:
        revstamp = subprocess.Popen(
            'svnversion %s' % srcpath,
            shell=True, 
            stdout=subprocess.PIPE).stdout.read().strip()
    else:
        revstamp = subprocess.Popen(
            'svnversion',
            shell=True, 
            stdout=subprocess.PIPE).stdout.read().strip()
   
    with open(cfile,'w') as fstamp:
        fstamp.write('// autogenerated file\n')
        fstamp.write('const char bldDateTime[] = "Built on: %s";\n'
                        % datetimestamp)
        fstamp.write('const char bldSVNRev[]   = "SVN rev : %s";\n'
                        % revstamp)

    # echo generated file
    with open(cfile) as f:
        for line in f.readlines(): print line,
       
    # compile file
    subprocess.call(' '.join([
        r'"%s/prog/chc08.exe"' % cwpath,        # compiler
        r'"%s"' % cfile,                        # - file to compile
        r'-I"%s/lib/hc08c/include"' % cwpath,   # - include path
        r'-Cs08',                               # - HCS08 core
        r'-Ms',                                 # - small memory model
        r'-D__NO_FLOAT__',                      # - #defines
        r'-WmsgFbv',                            # - verbose message file
        r'-WmsgFob"%f%e(%l): %k %d: %m\n"'      # - message format
        ]))

    # (just leave the .o file with the code...it's found by the linker fine)
    #os.remove(ofile)
    #os.rename(cfile, ofile)
   
if __name__ == "__main__":
    sys.exit(main())

4. Was using PE so had to(?) disable the PRM file generation under the CPU/Build Options/Generate PRM File = No. (This was after it was initially generated)

 

5. Added this snippet to the end of the PRM so it would link my compiled object file.

 

ENTRIES verstamp.o:* END

 

I don't fully understand the build process, so this is a dirty hack, but it gets the SVN revision (and date/time) that the project was compiled with into the binary, so...success.

 

View solution in original post

0 Kudos
2 Replies
691 Views
NickT
Contributor II

Had some spare time to hack up a solution; here's what I cooked up with information borrowed from several other posts.

 

1. Configure (Edit...)Standard Settings:

  - Target/Target Settings

  --- Pre-linker: BatchRunner PreLinker

  - Linker/BatchRunner PreLinker

  --- {Project}verstamp.bat

 

2. verstamp.bat in my Project root

 

python verstamp.py

3. verstamp.py in my Project root

 

import sys
import os
import subprocess
import time

fname = 'verstamp'

cwpath = 'C:/Program Files/Freescale/CodeWarrior for Microcontrollers V6.3'
srcpath = './CODE'
objpath = './c6v2_Data/Standard/ObjectCode'

rev_code_only = True

cfile = '%s/%s.c' % (srcpath, fname)
ofile = '%s/%s.o' % (objpath, fname)

def main(argv=None):
    if argv is None:
        argv = sys.argv
   
    #datetimestamp = iso8601datetime(sdec=3)
    datetimestamp = time.strftime('%Y-%m-%d %H:%M:%S')
   
    # Running 'svnversion' on the entire project directory can take
    # a long time.  Probably TargetDataWindows.tdt, which causes a
    # significant pause when 'svn status' scans it.
    if rev_code_only:
        revstamp = subprocess.Popen(
            'svnversion %s' % srcpath,
            shell=True, 
            stdout=subprocess.PIPE).stdout.read().strip()
    else:
        revstamp = subprocess.Popen(
            'svnversion',
            shell=True, 
            stdout=subprocess.PIPE).stdout.read().strip()
   
    with open(cfile,'w') as fstamp:
        fstamp.write('// autogenerated file\n')
        fstamp.write('const char bldDateTime[] = "Built on: %s";\n'
                        % datetimestamp)
        fstamp.write('const char bldSVNRev[]   = "SVN rev : %s";\n'
                        % revstamp)

    # echo generated file
    with open(cfile) as f:
        for line in f.readlines(): print line,
       
    # compile file
    subprocess.call(' '.join([
        r'"%s/prog/chc08.exe"' % cwpath,        # compiler
        r'"%s"' % cfile,                        # - file to compile
        r'-I"%s/lib/hc08c/include"' % cwpath,   # - include path
        r'-Cs08',                               # - HCS08 core
        r'-Ms',                                 # - small memory model
        r'-D__NO_FLOAT__',                      # - #defines
        r'-WmsgFbv',                            # - verbose message file
        r'-WmsgFob"%f%e(%l): %k %d: %m\n"'      # - message format
        ]))

    # (just leave the .o file with the code...it's found by the linker fine)
    #os.remove(ofile)
    #os.rename(cfile, ofile)
   
if __name__ == "__main__":
    sys.exit(main())

4. Was using PE so had to(?) disable the PRM file generation under the CPU/Build Options/Generate PRM File = No. (This was after it was initially generated)

 

5. Added this snippet to the end of the PRM so it would link my compiled object file.

 

ENTRIES verstamp.o:* END

 

I don't fully understand the build process, so this is a dirty hack, but it gets the SVN revision (and date/time) that the project was compiled with into the binary, so...success.

 

0 Kudos
690 Views
CrasyCat
Specialist III

Hello

 

Did you check following post?

 

https://community.freescale.com/message/32584#32584

 

This could be a solution to your request.

 

CrasyCat

0 Kudos