Build Actions (pre-build and post-build)

本文介绍如何在Embedded Workbench中有效利用BuildActions进行自动化任务,包括预构建和后构建命令行的使用、重定向标准输出和错误输出的方法、通过批处理文件简化命令行操作等实用技巧。
EW targets: All
EW component: Embedded Workbench & GUI
Last update: June 11, 2013

Here follows some comments and suggestions for the use of Build Actions .

Note 1: To see output from build actions in the "Build window", enable the option "Tools > Options... > IDE Options > Messages > Show build messages > All".

Note 2: If the project already is up-to-date, the build action will not be 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 Embedded Workbench applications directly from the pre- and post-build command lines.

Post-build command line (example 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 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 ARM):
ielfdumparm

Note: The command runs, but the output from the command will probably not be displayed in the Build window of 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, see link to "COMMAND.COM" and read about "Redirection, piping, and chaining".

Example for ARM

See example project in Technical Note 28924 (link to the right) 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 to the right on this page.

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 link to 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:

Pre-build command line:
cmd /c "del "$OBJ_DIR$\test.o""

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

### Protobuf for AARCH64-Linux Installation and Usage For the installation of Protocol Buffers (protobuf) on an `aarch64-linux` platform, one can follow a series of commands that ensure all necessary dependencies are installed correctly. The process involves installing development libraries specifically tailored to this architecture. #### Installing Dependencies To prepare the system for protobuf compilation or use, it is essential first to install required packages using package managers like `apt`. For systems based on Debian or Ubuntu derivatives targeting the ARM64 (`aarch64`) architecture: ```bash sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install -y build-essential cmake git pkg-config zip g++ zlib1g-dev unzip python3 python3-pip wget curl libcurl4-gnutls-dev libssl-dev ``` This command installs basic tools needed for building software from source along with Python-related utilities which might be useful when working alongside protobuf applications[^1]. #### Obtaining and Building Protobuf Source Code Given that pre-built binaries may not always exist for every target environment such as `aarch64`, compiling directly from sources becomes necessary. Downloading the latest stable release tarball from GitHub releases page ensures compatibility while also providing access to any recent improvements made by contributors. After extracting the archive into a directory named after its version number—for example, `protobuf-all-x.x.x`—one proceeds inside said folder before executing configuration scripts provided within: ```bash cd /path/to/extracted/folder/ ./autogen.sh ./configure --host=aarch64-linux-gnu CC=/usr/bin/aarch64-linux-gnu-gcc CXX=/usr/bin/aarch64-linux-gnu-g++ make -j$(nproc) sudo make install ``` The above sequence configures the project according to cross-compilation requirements specified through options passed during invocation; here `/usr/bin/aarch64-linux-gnu-gcc` serves as an illustrative path pointing towards appropriate GCC toolchain executables capable of generating code suitable for execution upon devices featuring ARM Cortex-A processors running Linux operating systems[^2]. #### Verifying Installation Success Once completed successfully without errors encountered throughout previous steps, verification checks should confirm proper functioning post-installation via simple test cases utilizing compiled binaries residing under standard locations defined at compile-time unless overridden explicitly earlier. Testing protocol buffer compiler functionality could involve creating minimalistic `.proto` files describing message structures followed by invoking `protoc` utility against them so generated output matches expectations set forth in official documentation accompanying distribution archives downloaded initially. ```bash echo 'syntax = "proto3";message TestMessage { string content = 1;}' >test.proto protoc --version protoc --cpp_out=. ./test.proto ls ./*.pb.cc rm *.pb.* test.proto ``` These operations generate C++ stubs corresponding to messages declared previously then clean up artifacts afterward leaving no trace behind except confirmation logs printed out onto terminal consoles indicating successful completion status codes returned accordingly[^3]. #### Using Protobuf Compiler Effectively With everything now properly configured, developers have multiple ways they can leverage protobuf capabilities depending on specific needs ranging from serializing/deserializing data efficiently across network boundaries down to defining contract-first APIs adhering strictly typed schemas enforced consistently regardless of underlying programming languages chosen later stages implementing business logic atop these foundations laid out beforehand carefully considering performance implications associated closely related aspects involved simultaneously ensuring maintainability over extended periods maintenance cycles expected typically seen enterprise-grade solutions built today's interconnected world wide web infrastructure spanning diverse ecosystems connected together forming vast networks communicating seamlessly exchanging information securely reliably fast accurately cost-effectively scalable resilient robust fault-tolerant architectures supporting modern application development practices widely adopted industry standards open-source communities alike contributing positively overall progress humanity makes collectively advancing knowledge frontiers pushing boundaries further exploring uncharted territories discovering new possibilities emerging continuously horizon expands ever outwardly boundless potential awaits those willing embrace change adapt evolving landscapes transforming rapidly around us constantly shifting paradigms redefining what possible tomorrow promises beyond imagination limits constrained only by current understanding reality shaping future generations inherit legacy leave behind today actions taken shape tomorrows yet unseen written history books read centuries henceforth remembered fondly celebrated achievements milestones reached overcoming challenges faced head-on courageously pursued dreams realized aspirations fulfilled visions materialized concrete forms tangible results manifested physical realm experienced senses perceive consciously aware existence itself.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值