Skip to main content
MPast.1
Senior
March 27, 2025
Solved

Artifact Filename based on a Fw costant

  • March 27, 2025
  • 3 replies
  • 1578 views

Hi All,

I need to obtain an artifact with the name present in some costant in "main.h".

In other words, my project is called "TestProject" and main.h I have these costants:

#define GUI_MAJOR_VER 		0		// Major
#define GUI_MINOR_VER 		1		// Minor
#define GUI_RELEASE_VER 	3		// Release

I would like to ahve a final executable named "TestProject v013.hex"

This means that every time I will change the #defines in main.h, output file will change automatically with a new numeration.

Does someone has some suggestion? or better, an example?

thanks in advance for your help.

 

 

Best answer by MPast.1

I found a solution.

This is based on git "bash.exe" command. The follow instructions show you how I obtaine my scope in only few steps. For first I create a sh script calles "FW_version.sh", and I placed on the project root:

MPast1_0-1743860222556.png

the content of "FW_version.sh" is the follow:.

#!/bin/bash

export LC_ALL=en_US.UTF-8

# Path to the config.h file
HEADER_FILE="Core/Inc/main.h"

# Path to the build output
BUILD_DIR="Debug" # or "Release", depending on your build configuration
OUTPUT_FILE="${BUILD_DIR}/TestProject.hex" # Adjust to match your output artifact

# Extract the GUI_MAJOR_VER, GUI_MINOR_VER, and GUI_LETTER_VER values
echo "Extracting GUI_MAJOR_VER..."
VERSION_MAJOR=$(grep -oP 'GUI_MAJOR_VER \s*\K\d+' "$HEADER_FILE")
echo "Extracted GUI_MAJOR_VER: $VERSION_MAJOR"

echo "Extracting GUI_MINOR_VER..."
VERSION_MINOR=$(grep -oP 'GUI_MINOR_VER \s*\K\d+' "$HEADER_FILE")
echo "Extracted GUI_MINOR_VER: $VERSION_MINOR"

echo "Extracting GUI_LETTER_VER..."
VERSION_LETTER=$(grep -oP 'GUI_LETTER_VER \s*\K\d+' "$HEADER_FILE")
echo "Extracted GUI_LETTER_VER: $VERSION_LETTER"


# Check if all version values were found
if [ -z "$VERSION_MAJOR" ] || [ -z "$VERSION_MINOR" ] || [ -z "$VERSION_LETTER" ]; then
 echo "Error: One or more version constants not found in $HEADER_FILE"
 exit 1
fi

# Construct the full version string (e.g., v_1.23)
VERSION="${VERSION_MAJOR}_${VERSION_MINOR}${VERSION_LETTER}"


# Define the new artifact name based on the extracted version
ARTIFACT_NAME="${BUILD_DIR}/TestProject_v${VERSION}.hex"

# Rename the generated firmware (I.E. firmware.hex -> firmware_0.13.hex)
if [ -f "${BUILD_DIR}/TestProject.hex" ]; then
 mv "$OUTPUT_FILE" "$ARTIFACT_NAME"
 echo "Firmware renamed to: $ARTIFACT_NAME"
else
 echo "Error: Compiled FW not found"
 exit 1
fi

Second step is to create a new builder and inserting it into the project:

MPast1_1-1743860340376.png

So, right click on the project, properties --> Builders --> New.

Compile the step 1,2 ,3 with your preference:

"1" is the "bash.exe" program present on GIT folders

"2" is your project folder

"3" is the script locations (I.E. = ${workspace_loc:/TestProject/FW_version.sh}  )  [pay attention to the syntax]

MPast1_2-1743860470758.png

Last step is compile your post-build command with : ${workspace_loc:/TestProject/.externalToolBuilders/RenameArtifact} [pay attention to the syntax]

MPast1_3-1743860705334.png

if everything works correctly, when you compile your code you will obtain the following result:

MPast1_4-1743860788892.png

(in my case I used the script on the debug configuration. if you need, change it to release or something else configuration).

 

 

 

 

3 replies

Pavel A.
Super User
March 27, 2025

Sure, you can do this in post-build action: Extract the version numbers from the .h file, then copy or rename the hex file.

Post-build action can be a "unix" shell script (sh is provided in the CubeIDE environment) so we don't have to ****  with ******* bat files.

MPast.1
MPast.1Author
Senior
March 28, 2025

HI @Pavel A. ,

thanks for suggestion but I should want a practical example to copy the various instructions.

you told about  ready scripts on CubeIDE: can you sueggest to me the folder?

Do I need to search for *.bat files? or on makefiles?

 

 thanks.

Pavel A.
Super User
March 28, 2025

Apologies for been not clear enough. There are no ready examples of such scripts. I only said that we can use the normal "unix" shell tool, even on Windows. Which is easier than dreadful bat files.

The post-build command goes into the project settings:

PavelA_0-1743175133663.png

As you see, CubeIDE variables can be used in the commands, with ${...} syntax.

 

 

 

Pavel A.
Super User
March 28, 2025

To get the version as string: make a small C file, for example ver.c  ver.h  [edited - use .h suffix so that Eclipse won't add it to the project as a source file]

 

#include "main.h"

#define STRINGIFY(w) #w
#define MAKEVERS(x, y, z) STRINGIFY(x) "." STRINGIFY(y) "." STRINGIFY(z)

MAKEVERS(GUI_MAJOR_VER, GUI_MINOR_VER, GUI_RELEASE_VER)

Then preprocess this file and filter junk out:

gcc -E -P ver.h | sed '/^[ \t]*$/d; s/[ \t\"]//g'

 Here you get the version suffix string, like "0.1.3"

MPast.1
MPast.1Author
Senior
March 28, 2025

hi @Pavel A. ,

I create "version.c"

immagine.png

I inserted these in pre-build:

MPast1_0-1743196400357.png

And I obtain an error:

MPast1_1-1743196489733.png

But I'm in trouble with the compiler in use:

MPast1_2-1743196738436.png

 trying to put on post-build command

MPast1_1-1743198307944.png

I get this error:

MPast1_0-1743198254504.png

 

 

MPast.1
MPast.1Author
Senior
March 28, 2025

Hi @Pavel A. ,

 maybe I have done a little bit of confusion.

After reading for 100  times your original message, maybe I understood the right steps to do :

right click on "version.c" -> proprieties -->settings --> build Steps..

MPast1_0-1743199352073.png

In this way everything is compiling without any error, but Output file has still the project name.

Pavel A.
Super User
March 28, 2025

Do not compile version.c with the application. It is not a correct C and cannot not compile to object file. Only preprocess. Better rename it to "version.h".

The "gcc" here should be the arm-none-eabi-gcc , the same compiler used for  STM32. The sed here is hidden in the "sh" so the whole command better should be wrapped in a script file, executed with "sh".  Unless there is other gcc and sed in the path (unlikely on Windows).

On Windows, Powershell can be used as well.

More help with scripting, shell and general software topics can be found here.

 

MPast.1
MPast.1Author
Senior
March 28, 2025

I don't understand nothing.

The arcifact name doesn't change: Tomorrow morning I will try one more times.

MPast.1
MPast.1AuthorBest answer
Senior
April 5, 2025

I found a solution.

This is based on git "bash.exe" command. The follow instructions show you how I obtaine my scope in only few steps. For first I create a sh script calles "FW_version.sh", and I placed on the project root:

MPast1_0-1743860222556.png

the content of "FW_version.sh" is the follow:.

#!/bin/bash

export LC_ALL=en_US.UTF-8

# Path to the config.h file
HEADER_FILE="Core/Inc/main.h"

# Path to the build output
BUILD_DIR="Debug" # or "Release", depending on your build configuration
OUTPUT_FILE="${BUILD_DIR}/TestProject.hex" # Adjust to match your output artifact

# Extract the GUI_MAJOR_VER, GUI_MINOR_VER, and GUI_LETTER_VER values
echo "Extracting GUI_MAJOR_VER..."
VERSION_MAJOR=$(grep -oP 'GUI_MAJOR_VER \s*\K\d+' "$HEADER_FILE")
echo "Extracted GUI_MAJOR_VER: $VERSION_MAJOR"

echo "Extracting GUI_MINOR_VER..."
VERSION_MINOR=$(grep -oP 'GUI_MINOR_VER \s*\K\d+' "$HEADER_FILE")
echo "Extracted GUI_MINOR_VER: $VERSION_MINOR"

echo "Extracting GUI_LETTER_VER..."
VERSION_LETTER=$(grep -oP 'GUI_LETTER_VER \s*\K\d+' "$HEADER_FILE")
echo "Extracted GUI_LETTER_VER: $VERSION_LETTER"


# Check if all version values were found
if [ -z "$VERSION_MAJOR" ] || [ -z "$VERSION_MINOR" ] || [ -z "$VERSION_LETTER" ]; then
 echo "Error: One or more version constants not found in $HEADER_FILE"
 exit 1
fi

# Construct the full version string (e.g., v_1.23)
VERSION="${VERSION_MAJOR}_${VERSION_MINOR}${VERSION_LETTER}"


# Define the new artifact name based on the extracted version
ARTIFACT_NAME="${BUILD_DIR}/TestProject_v${VERSION}.hex"

# Rename the generated firmware (I.E. firmware.hex -> firmware_0.13.hex)
if [ -f "${BUILD_DIR}/TestProject.hex" ]; then
 mv "$OUTPUT_FILE" "$ARTIFACT_NAME"
 echo "Firmware renamed to: $ARTIFACT_NAME"
else
 echo "Error: Compiled FW not found"
 exit 1
fi

Second step is to create a new builder and inserting it into the project:

MPast1_1-1743860340376.png

So, right click on the project, properties --> Builders --> New.

Compile the step 1,2 ,3 with your preference:

"1" is the "bash.exe" program present on GIT folders

"2" is your project folder

"3" is the script locations (I.E. = ${workspace_loc:/TestProject/FW_version.sh}  )  [pay attention to the syntax]

MPast1_2-1743860470758.png

Last step is compile your post-build command with : ${workspace_loc:/TestProject/.externalToolBuilders/RenameArtifact} [pay attention to the syntax]

MPast1_3-1743860705334.png

if everything works correctly, when you compile your code you will obtain the following result:

MPast1_4-1743860788892.png

(in my case I used the script on the debug configuration. if you need, change it to release or something else configuration).