Build Actions (pre-build and post-build)

Technical Note 99436

Targets:
All

Component:
IDE

Updated:
6/11/2013 3:55 PM

Introduction

This Technical Note gives comments and suggestions for the use of 'Build Actions'.

Note 1:

In order to see output from build actions in the "Build window", enable the option "Tools > Options... > IDE Options > Messages > Show build messages > All".

Note 2:

When the project is up-to-date, the build actions will not be invoked, that is, build actions are only performed if any tool in the tool-chain is invoked.

Pre- and Post-build command line

You can enter "application.exe" on the pre- and post-build command lines, if the environment variable PATH includes the directory where the "application.exe" is located.

You can check the PATH variable with the MS-DOS command "path" on a Windows command prompt, but note that MS-DOS commands cannot be executed directly on the pre- and post-build command lines. Instead you have to run indirectly using:

cmd.exe /c "command line"

To run the MS-DOS "path" command on the pre-build command line, use:

cmd /c "path"

By default, the $TOOLKIT_DIR$\bin folder is added to the PATH variable, thus you can run IAR Embedded Workbench applications directly from the pre- and post-build command lines.

Post-build command line (example for IAR Embedded Workbench for ARM):

ielfdumparm

Redirect stdout and stderr

To redirect stdout and stderr on a Windows command prompt you can enter a command like:

path > output.txt 2>&1

To run the same MS-DOS command on the pre-build command line, you have to run indirectly like:

cmd /c "path > output.txt 2>&1"

Note: The current directory is where IarIdePm.exe is located, so you have also to specify a location for the output file, for example the project directory:

cmd /c "path > $PROJ_DIR$\output.txt 2>&1"

To redirect output from "application.exe" on a Windows command prompt you can enter a command like:

application.exe > output.txt 2>&1

The expected pre- or post-build command line is

cmd /c "application.exe > output.txt 2>&1"

but you will most probably end up with an empty output.txt file.

In this case, run indirectly once more, like:

cmd /c "cmd /c "application.exe > output.txt 2>&1""

Post-build command line (example for IAR Embedded Workbench for ARM):

 cmd /c "cmd /c "ielfdumparm > $PROJ_DIR$\output.txt 2>&1""

However, since this command line looks cumbersome, a better alternative might be to call a .bat file (described below) from the post-build command line.

Using .bat files

With a .bat file, you can run MS-DOS commands as usual, and you can skip the "cmd /c" that is required on the pre- and post-build command line.

Note: If you have two "cmd /c" it's just the first "cmd /c" that you can skip.

  • To run the MS-DOS "path" command using a .bat file located in project directory:

    Pre-build command line:
     $PROJ_DIR$\pre-build.bat

    File pre-build.bat:
     path

  • To redirect output from the MS-DOS "path" command using a .bat file located in project directory:

    Pre-build command line:
    $PROJ_DIR$\pre-build.bat "$PROJ_DIR$"

    File pre-build.bat:

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

    Note: You cannot use argument variables like $PROJ_DIR$ in a .bat file, since they are unknown to MS-DOS. Instead you can pass argument variables as parameters to the .bat file.

  • To run ielfdumparm using a .bat file located in project directory:

    Post-build command line:
    $PROJ_DIR$\post-build.bat

    File post-build.bat (example for IAR Embedded Workbench for ARM):
    ielfdumparm

    Note: The command runs, but the output from the command will probably not be displayed in the Build window of IAR Embedded Workbench. In this case, redirect output and dump the file using the "type" command (see below).

 

  • To redirect output from ielfdumparm using a .bat file located in project directory:

    Post-build command line:
    $PROJ_DIR$\post-build.bat "$PROJ_DIR$"

    File post-build.bat (example for ARM):
    cmd /c "ielfdumparm > "%1\output.txt 2>&1""

    To dump the content of the output file, add this line to the .bat file:
    type "%1\output.txt"

Note: If you run several commands in a .bat file and want to redirect output to the same output file, follow the link COMMAND.COM and read about Redirection, piping, and chaining.

Example for ARM

See example project in Technical Note 28924 and check project "Common", which uses a post-build.bat file for the post-build action.

Return values

If the post- or pre-build command returns an error code ( != 0 ), the entire Build/Make command is aborted. See the example project and screenshots below.

View output only from the build action

If you feel that the build window shows too much information and makes it very hard to see the output from your build action, an alternative might be to redirect output to a text file and start notepad to show the output.

Post-build command line:

 $PROJ_DIR$\post-build.bat $PROJ_DIR$

File post-build.bat (example for ARM):

 echo off
cmd /c "ielfdumparm > "%1\output.txt 2>&1""
echo Look in the notepad window and then close
notepad "%1\output.txt"
echo on

Note: When there are blank characters (spaces) in the path name, the usual workaround is to quote such a path. But if that does not work, for example when invoking notepad, try with a path that doesn't contain spaces or quotes.

Cygwin touch command

You can enter "cygwin-application.exe" on the pre- and post-build command lines, if the environment variable PATH includes the directory where the "cygwin-application.exe" is located.

You can run the Cygwin command "touch" on the pre-build command line, but if you add a file path, for example "touch d:/test.c", the file path is not accepted by Cygwin.

Cygwin expects the POSIX path /cygdrive/d/test.c so the resulting command line would be "touch /cygdrive/d/test.c", however this command cannot be executed directly on the pre- and post-build command. Instead you have to run indirectly using:

cmd /c "touch /cygdrive/d/test.c"

The .bat file (located in project directory) alternative would look like:

Pre-build command line:

 $PROJ_DIR$\pre-build.bat

File pre-build.bat:

 touch /cygdrive/d/test.c

Note 1:

There is no point in passing argument variables like $PROJ_DIR$ as a parameter to this .bat file since it cannot be used directly (cygwin expects a POSIX path).

Note 2:

If you want to run the "touch" command without making changes, build actions will not work, and you should look for another approach. An alternative is to make a separate .bat file (that you invoke manually) to run "touch" followed by "IarBuild.exe -build". (see Technical Note 47884 for info about IarBuild.exe)

Note 3:

An alternative to the "touch" command is to have a pre-build action that deletes the object file, for example the Pre-build command line:

 cmd /c "del "$OBJ_DIR$\test.o""

 

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

Realated Tech Notes

© IAR Systems 1995-2016 - All rights reserved.