Build Actions (pre-build and post-build)

Technical Note 99436

Architectures:

All

Component:

IDE

Updated:

12/5/2019 10:37 AM

Introduction

This Technical Note discusses the use of pre- and post-build actions.

This includes advice on the use of .vbs files, if .cmd files are to be used.

Discussion

Disclaimer

While based on practical use of Microsoft products and documentation, these comments and suggestions are to be viewed as guidelines and inspiration.

Versions of the Embedded Workbench for ARM with other behaviour

To clarify: The suggestions in this technical note applies to versions 8.xx.x and older.

  • The suggestions can also apply to version 9.xx, but there are differences:
    • In version 9.10 and version 9.20 the tools always (and unconditionally) run the pre-build and post-build actions.
    • Version 9.30 always run the pre-build, but not the postbuild action (the postbuild action has reverted to the 8.xx behavior.)

Pre-build and post-build command line environment

When specifying pre-build and post-build action command lines:

  • IAR Embedded Workbench argument variables can be used
  • The directory $TOOLKIT_DIR$\bin is included in the environment variable PATH
  • The initial working directory is $EW_DIR$\common\bin

Using IAR Embedded Workbench for Arm as an example, any of the following commands can be used to display the version of ielfdumparm.exe in the build log window:

$TOOLKIT_DIR$\bin\ielfdumparm.exe --version
ielfdumparm.exe --version
ielfdumparm --version
 

Figure 1 ielfdumparm --version

Shell commands

To use shell-internal commands, such as DIR, ECHO, PATH, or REM, a shell must be started. See the screenshot below for an example.

Figure 2 - CMD.EXE /C DIR "$PROJ_DIR$\*.c"

Shell I/O redirection

Using the shell, it is possible to redirect output from shell commands and executables to a file. The following examples use a fictitious Windows executable named app.exe.

Example I/O 1: Interactively at a Windows command prompt:

app.exe > output.txt 2>&1

Example I/O 2: From a build action command line:

CMD /C "app.exe > $PROJ_DIR$\output.txt 2>&1"

Example I/O 3: The same applies when only one "CMD /C" leaves the output file empty:

CMD /C "CMD /C "app.exe > $PROJ_DIR$\output.txt 2>&1""

Using .cmd files

Using a .cmd file, you get a detachable command shell automatically. Therefore, you need one less "CMD /C" compared with a plain build action command line.

For example, with a file build.cmd located in the project directory, an alternative to Example I/O 3 could be a build action like

  $PROJ_DIR$\build.cmd "$PROJ_DIR$"

And a file build.cmd containing

  PATH > %1\output.txt 2>&1

How to create a detachable process, using a Visual basic script (.vbs)

In IAR Embedded Workbench for Arm 8.20.2 (and later), a detachable command shell (.cmd) cannot be used, however, a detachable process can be used, and from that process the .cmd can be invoked. The only drawback is that the I/O input back to IAR Embedded Workbench cannot be used.

A detachable process can be created from a Visual basic script (file extension .vbs).

  1. Make a .vbs script like redir.vbs and write a .cmd file, for example like the_doer.cmd.
  2. Put the line Wscript $PROJ_DIR$\redir.vbs "cmd /c $PROJ_DIR$\the_doer.cmd" in your post-build step.

The redir.vbs file looks like:

Dim runner
Set runner = CreateObject("WScript.Shell")
args = WScript.Arguments(0)
WScript.Echo(args)
Call runner.Run(args, 1, true)

The the_doer.cmd file looks like:

Echo "I am doing something"
pause

 

Figure 3 – ' Wscript $PROJ_DIR$\redir.vbs "cmd /c $PROJ_DIR$\the_doer.cmd" '

Example files

Some example files are available via this link. Note that these project files (.ewp) require IAR Embedded Workbench for Arm 7.80 or later.

The example files for the detachable process are available via this link. Note that is in IAR Embedded Workbench for Arm 8.20.2 or later that detachable process can be used.

Some further advices

  • To see output from build actions in the Build Log window, use the context menu in that window and select All.
    • In an older IDE (that is, common components 7 or older), enable the option Tools>Options>IDE Options>Messages>Show build messages>All.
    • When the project is up-to-date, the build actions will not be invoked, that is, build actions are only performed if at least one tool in the toolchain is invoked.
      • See example configuration XA for one way to ensure that build actions are invoked.
      • IAR Embedded Workbench argument variables and Microsoft environment variables are similar. However, they are not the same. To use the value of an argument variable like $PROJ_DIR$ in a .cmd file, pass the wanted argument variable as a parameter.
      • If the pre-build or post-build command returns an error code (return value != 0), the entire Build/Make command is aborted.
        • See example configuration XB.

Conclusion

This Technical Note discusses the use of pre-build and post-build actions as available in IAR Embedded Workbench.

All product names are trademarks or registered trademarks of their respective owners.

 

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.