Real-Time Workshop | ![]() ![]() |
Template Makefiles
To configure or customize template makefiles, you should be familiar with how the make
command works and how the make
command processes makefiles. You should also understand makefile build rules. For information of these topics, please refer to the documentation provided with the make utility you use. There are also several good books on the make utility.
Template makefiles are made up of statements containing tokens. The Real-Time Workshop build process expands tokens and creates a makefile, model
.mk
. Template makefiles are designed to generate makefiles for specific compilers on specific platforms. The generated model
.mk
file is specifically tailored to compile and link code generated from your model, using commands specific to your development system.
Figure 14-5: Creation of model.mk
Template Makefile Tokens
The make_rtw
M-file command (or a different command provided with some targets) directs the process of generating model
.mk.
The make_rtw
command
processes the template makefile specified on the Target configuration section of the Real-Time Workshop pane of the Simulation Parameters dialog. make_rtw
copies the template makefile, line by line, expanding each token encountered. Table 14-3 lists the tokens and their expansions.
These tokens are expanded by substitution of parameter values known to the build process. For example, if the source model contains blocks with two different sample times, the template makefile statement
expands to the following in model
.mk
.
In addition to the above, make_rtw
expands tokens from other sources:
rtwoptions
structure array that contain the field makevariable
are expanded.
matlabroot
/rtw/c/grt/grt.tlc.
The section starting with BEGIN_RTW_OPTIONS
contains M-file code that sets up rtwoptions
. The directive
causes the |>EXT_MODE<|
token to be expanded into 1
(on) or 0
(off), depending on how you set the External mode option in the Code generation options section of the Real-Time Workshop pane.
The Make Command
After creating model.mk
from your template makefile, Real-Time Workshop invokes a make
command. To invoke make
, Real-Time Workshop issues this command.
makecommand
is defined by the MAKE
macro in your system's template makefile (see Figure 14-6). You can specify additional options to make
in the Make command field of the Real-Time Workshop pane. (see Make Command Field and Template Makefiles and Make Options.)
For example, specifying OPT_OPTS=-O2
in the Make command field causes make_rtw
to generate the following make
command.
A comment at the top of the template makefile specifies the available make
command options. If these options do not provide you with enough flexibility, you can configure your own template makefile.
The make utility lets you control nearly every aspect of building your real-time program. There are several different versions of make
available. Real-Time Workshop provides the Free Software Foundation's GNU Make for both UNIX and PC platforms in the platform-specific subdirectories below matlabroot
/rtw/bin
.
It is possible to use other versions of make
with Real-Time Workshop, although GNU Make is recommended. To ensure compatibility with Real-Time Workshop, make sure that your version of make
supports the following command format.
Structure of the Template Makefile
A template makefile has four sections:
make_rtw
how to process the template makefile. The macros are:
MAKE
-- This is the command used to invoke the make utility. For example, if MAKE
= mymake
, then the make
command invoked is
HOST
-- What platform this template makefile is targeted for. This can be HOST=PC
, UNIX
, computer_name
(see the MATLAB computer
command), or ANY
.
BUILD
-- This tells make_rtw
whether or not (BUILD=yes
or no
) it should invoke make
from the Real-Time Workshop build procedure.
SYS_TARGET_FILE
-- Name of the system target file. This is used for consistency checking by make_rtw
to verify that the correct system target file was specified in the Target configuration section of the Real-Time Workshop pane of the Simulation Parameters dialog box.
BUILD_SUCCESS
-- An optional macro that specifies the build success string to be displayed on successful make
completion on the PC. For example,
BUILD_SUCCESS = ### Successful creation of
The BUILD_SUCCESS
macro, if used, replaces the standard build success string found in the template makefiles distributed with the bundled Real-Time Workshop targets (such as GRT):
Your template makefile must include either the standard build success string, or use the BUILD_SUCCESS
macro. For an example of the use of BUILD_SUCCESS
, see
DOWNLOAD
options apply only to the Tornado target:
DOWNLOAD
-- An optional macro that you can specify as yes or no. If specified as yes (and BUILD=yes
), then make
is invoked a second time with the download target.
DOWNLOAD_SUCCESS
-- An optional macro that you can use to specify the download success string to be used when looking for a successful download. For example,
make_rtw
expands (see Table 14-3).
Figure 14-6 shows the general structure of a template makefile.
Figure 14-6: Structure of aTemplate Makefile
Customizing and Creating Template Makefiles
To customize or create a new template makefile, we recommend that you copy an existing template makefile to your local working directory and modify it.
This section shows, through an example, how to use macros and file-pattern-matching expressions in a template makefile to generate commands in the model
.mk
file.
The make utility processes the model
.mk
makefile and generates a set of commands based upon dependency rules defined in model
.mk
. After make
generates the set of commands needed to build or rebuild test
, make
executes them.
For example, to build a program called test
, make
must link the object files. However, if the object files don't exist or are out of date, make
must compile the C code. Thus there is a dependency between source and object files.
Each version of make
differs slightly in its features and how rules are defined. For example, consider a program called test
that gets created from two sources, file1.c
and file2.c.
Using most versions of make
, the dependency rules would be
test: file1.o file2.o cc -o test file1.o file2.o file1.o: file1.c cc -c file1.c file2.o: file2.c cc -c file2.c
In this example, we assumed a UNIX environment. In a PC environment the file extensions and compile and link commands will be different.
make
sees that to build test
, it needs to build file1.o
and file2.o
. To build file1.o
, make
processes the rule
If file1.o
doesn't exist, or if file1.o
is older than file1.c
, make
compiles file1.c
.
The format of Real-Time Workshop template makefiles follows the above example. Our template makefiles use additional features of make
such as macros and file-pattern-matching expressions. In most versions of make
, a macro is defined via
References to macros are made via $(MACRO_NAME)
. When make
sees this form of expression, it substitutes value
for $(MACRO_NAME)
.
You can use pattern matching expressions to make the dependency rules more general. For example, using GNU Make you could replace the two "file1.o: file1.c
" and "file2.o: file2.c"
rules with the single rule
Note that $<
above is a special macro that equates to the dependency file (i.e., file1.c
or file2.c
). Thus, using macros and the "%
" pattern matching character, the above example can be reduced to
Note that the $@
macro above is another special macro that equates to the name of the current dependency target, in this case test
.
This example generates the list of objects (OBJS
) from the list of sources (SRCS
) by using the string substitution feature for macro expansion. It replaces the source file extension (.c
) with the object file extension (.o
). This example also generalized the build rule for the program, test
, to use the special "$@
" macro.
Customizing the Makefile Include Path
Real-Time Workshop template makefiles provide rules and macros that let you add source directories, include directories, and libraries to generated makefiles without having to modify the template makefiles themselves. This feature is useful if you need to include your code when building S-functions.
To include a directory needed for a S-Function, you must create an M-function, rtwmakecfg
, in a file rtwmakecfg.m
. This file must reside in the same directory as your S-function component (.dll
on Windows, .mex
on UNIX). The rtwmakecfg
function is called during the build process. The rtwmakecfg
function must return a structured array with following elements:
makeInfo.includePath
: a cell array containing additional include directory names, which must be organized as row vector. These directory names will be expanded into include instructions in the generated makefile.
makeInfo.sourcePath
: a cell array containing additional source directory names, which must be organized as a row vector. These directory names will be expanded into make rules in the generated makefile.
makeInfo.library
: a structure containing additional runtime library names and module objects, which must be organized as a row vector. This information will be expanded into make rules in the generated makefile.
![]() | Adding a Custom Target to the System Target File Browser | Creating Device Drivers | ![]() |