cancel
Showing results for 
Search instead for 
Did you mean: 

How would you go about adding the version and 8 characters of the git hash to the hexfile name so we can also access it in the program?

KiptonM
Lead

Say we have a STM32 project called Foo. When it is compiled, it generates an intel hex file called Foo.hex that we can use to upgrade the file in the field. That is easy.

What we would like to do as add the version say 10.1 or 10_1 and the last 8 digits of the git hash. And the number of compiles since the last git commit to the file name. We can do that;

So we commit say version 10.1 with the last 8 digits of the commit being 63245699.

We want the name of the file to be Foo10_1_0_63245699.hex. And when asked for the version over I2C the Foo program provides 10_1_0 or 10_1_0_63245699. If the file on the computer is a newer version than on the STM32 board, we activate the boot loader and update the software over I2C. (That part is working) .

If we compile the program again, the name adds 1 to the number of files since last commit. so the name changes to Foo10_1_1_63245699.hex

If we compile again Foo10_1_2_63245699.hex

When we commit we up the version and the number of compiles since the last commit goes back to zero. So the name is Foo10_2_0_23798241.hex (the hash also changes when we commit)

How do we get that name into the program so when the program is asked for the version we can output it on the I2C?

Someone else has done this with the Microchip compiler/IDE and now I have been asked to add the same capabilities to the STM32CubeIDE for when we compile.

How do I start? For the microchip IDE they modified the makefile, but the STM32CubeIDE makefile says "Automatically-generated file. Do not edit" so if I did edit it, somewhere down the line, an update could change it and wipe out my modifications?

What is the recommendation?

1 ACCEPTED SOLUTION

Accepted Solutions
KiptonM
Lead
import subprocess
import os
result = subprocess.run(['git', 'describe'], capture_output=True, text=True)
version = result.stdout[:-1]
print("version: ",version)
 
cwd = os.getcwd()
print("cwd: ",cwd)
os.chdir("..")
cwd2 = os.getcwd()
print("cwd2: ",cwd2)
 
filename = r"Core\Inc\Version.h"
newlines = []
 
f = open(filename, "r")
lines = f.readlines()
for l in lines:
    if "AUTOVERSION" in l:
        index = l.find('"')
        new = l[0:index] + '"' + version + '"\n'
        newlines.append(new)
    else:
        newlines.append(l)  
f.close()
f = open(filename, "w")
f.writelines(newlines)
f.close()
 
print("Start search")
os.chdir(cwd)

Here is my python code that works. It is the preprocess.py

Next is the postprocess.py

import os
import sys
import shutil
#get Version
 
cwd = os.getcwd()
print("cwd: ",cwd)
os.chdir("..")
cwd2 = os.getcwd()
print("cwd2: ",cwd2)
filename = r"Core\Inc\Version.h"
f = open(filename, "r")
lines = f.readlines()
for l in lines:
    if "AUTOVERSION" in l:
        index = l.find('"')
        version = l[index+1:-2]
#        print(version2) 
#        index2 = version2.find('-')
#        version = version2[0:index2]
        print("version: ",version)
f.close()
#print("version2:",version2)
print("version: ",version)
 
# find basename
 
directory = os.listdir(cwd2)
#print("directory: ",directory)
for fname in directory:
    print(fname)
    if '.ioc' in fname:
        basename = fname[0:-4]
        print("basename: ",basename)
        break
oldname = basename + '.hex'
print("oldname: ",oldname)
 
#search and replace oldname
for root, dirs, files in os.walk(cwd2):
    if (oldname in files):
        print(root,dirs,files)
        fulloldname = root + "\\" + oldname
        print(fulloldname)
        fullnewname = root + "\\" + basename + "_" + version + '.hex'
        print(fullnewname)
        if os.path.exists(fullnewname):
            os.remove(fullnewname)
        
        #os.rename(fulloldname,fullnewname)
        shutil.copy(fulloldname,fullnewname)
        
os.chdir(cwd)

I set the Project->Properties->Tool Settings Tab->MCU Post build outputs

And clicked "Convert to Intel Hex file (-O ihex)

 Then you have to have the IDE program call them. Select the Build Steps Tab

0693W00000YAq0JQAT.png 

View solution in original post

4 REPLIES 4
Bob S
Principal

Add a "post compile" build step that runs a batch (or even better as shell/bash script) to do this. Go to the project properties, an don the "C/C++ Build"->"Settings" dialog, click on the "Build Steps" tab.

KiptonM
Lead

I think I have a start.

I was looking at the Properties->C/C++ Build->Settings->BuildSteps Tab and they have a pre-build command and a post-build command.

So I can create version.h file that looks like this:

/*
 * Version.h
 *
 *  Created on: Feb 3, 2023
 *      Author: Moravec-Kipton
 */
 
#ifndef INC_VERSION_H_
#define INC_VERSION_H_
 
#define AUTOVERSION "10.1_0_69755423"
 
#endif /* INC_VERSION_H_ */

During the pre-build I can update the string in the Version.h file with a python or bash script. grabbing all of the pieces from where they are on my computer. It looks like my file name can have multiple "." in it. I am not used to that. (I am an old guy.)

In the post-build I can use that string to rename the .hex file. with a script or another small python program.

In my program I can make that string go out with the appropriate I2C version command.

Is there a better way to do it?

Great minds think alike. Looks like we posted at the same time.

KiptonM
Lead
import subprocess
import os
result = subprocess.run(['git', 'describe'], capture_output=True, text=True)
version = result.stdout[:-1]
print("version: ",version)
 
cwd = os.getcwd()
print("cwd: ",cwd)
os.chdir("..")
cwd2 = os.getcwd()
print("cwd2: ",cwd2)
 
filename = r"Core\Inc\Version.h"
newlines = []
 
f = open(filename, "r")
lines = f.readlines()
for l in lines:
    if "AUTOVERSION" in l:
        index = l.find('"')
        new = l[0:index] + '"' + version + '"\n'
        newlines.append(new)
    else:
        newlines.append(l)  
f.close()
f = open(filename, "w")
f.writelines(newlines)
f.close()
 
print("Start search")
os.chdir(cwd)

Here is my python code that works. It is the preprocess.py

Next is the postprocess.py

import os
import sys
import shutil
#get Version
 
cwd = os.getcwd()
print("cwd: ",cwd)
os.chdir("..")
cwd2 = os.getcwd()
print("cwd2: ",cwd2)
filename = r"Core\Inc\Version.h"
f = open(filename, "r")
lines = f.readlines()
for l in lines:
    if "AUTOVERSION" in l:
        index = l.find('"')
        version = l[index+1:-2]
#        print(version2) 
#        index2 = version2.find('-')
#        version = version2[0:index2]
        print("version: ",version)
f.close()
#print("version2:",version2)
print("version: ",version)
 
# find basename
 
directory = os.listdir(cwd2)
#print("directory: ",directory)
for fname in directory:
    print(fname)
    if '.ioc' in fname:
        basename = fname[0:-4]
        print("basename: ",basename)
        break
oldname = basename + '.hex'
print("oldname: ",oldname)
 
#search and replace oldname
for root, dirs, files in os.walk(cwd2):
    if (oldname in files):
        print(root,dirs,files)
        fulloldname = root + "\\" + oldname
        print(fulloldname)
        fullnewname = root + "\\" + basename + "_" + version + '.hex'
        print(fullnewname)
        if os.path.exists(fullnewname):
            os.remove(fullnewname)
        
        #os.rename(fulloldname,fullnewname)
        shutil.copy(fulloldname,fullnewname)
        
os.chdir(cwd)

I set the Project->Properties->Tool Settings Tab->MCU Post build outputs

And clicked "Convert to Intel Hex file (-O ihex)

 Then you have to have the IDE program call them. Select the Build Steps Tab

0693W00000YAq0JQAT.png