Main Content

Use rtwmakecfg.m API to Customize Generated Makefiles

Note

To customize generated makefiles for S-functions, MathWorks® recommends the use of the makecfg approach instead of the rtwmakecfg function. For more information, see Use makecfg to Customize Generated Makefiles for S-Functions.

Both the toolchain approach and the template makefile approach for builds let you add the following items to generated makefiles:

  • Source folders

  • Include folders

  • Library names

  • Module objects

About the rtwmakecfg Function

Using an rtwmakecfg function, you add this information to the makefile during the build operation for S-functions. The rtwmakecfg function is useful when specifying added sources and libraries to build a model that contains one or more of your S-function blocks.

To add information pertaining to an S-function to the makefile:

  1. Create the MATLAB® language rtwmakecfg function in the rtwmakecfg.m file. The code generator associates this file with your S-function based on its folder location. Create the rtwmakecfg Function describes the requirements for the rtwmakecfg function and the data it returns.

  2. If you are using the template makefile approach, modify the TMF of your target such that it supports macro expansion for the information that the rtwmakecfg function returns. Modify the Template Makefile for rtwmakecfg describes the required modifications. If you are using the toolchain approach, the information that the rtwmakecfg function returns is used by the generated makefile; no further configuration is required.

After the TLC phase of the build process, when generating a makefile, the code generator searches for an rtwmakecfg.m file in the folder that contains the S-function MEX file. If it finds the file, the build process calls the rtwmakecfg function.

Create the rtwmakecfg Function

Create the rtwmakecfg.m file containing the rtwmakecfg function in the same folder as your S-function component (a MEX-file with a platform-dependent extension, such as .mexext on Microsoft® Windows® systems). The function must return a structured array that contains these fields.

FieldDescription
makeInfo.includePathA cell array that specifies additional include folder names, organized as a row vector. The build process expands the folder names into include instructions in the generated makefile.
makeInfo.sourcePathA cell array that specifies additional source folder names, organized as a row vector. Include the folder names of files entered into the S-function modules field on the S-Function Block Parameters dialog box or into the SFunctionModules parameter of the block if they are not in the same folder as the S-function. The build process expands the folder names into make rules in the generated makefile.
makeInfo.sourcesA cell array that specifies additional source file names (C or C++), organized as a row vector. Do not include the name of the S-function or files entered into the S-function modules field on the S-Function Block Parameters dialog box or into the SFunctionModules parameter of the block. The build process expands the file names into make variables that contain the source files. Specify only file names (with extension). Specify path information with the sourcePath field.
makeInfo.linkLibsObjsA cell array that specifies additional, fully qualified paths to object or library files against which the generated code links. The build process does not compile the specified objects and libraries. However, it includes them when linking the final executable. This inclusion can be useful for incorporating libraries that you do not want the build process to recompile or for which the source files are not available. You can also use this element to integrate source files from languages other than C and C++. This integration is possible if you first create a C compatible object file or library outside of the build process.
makeInfo.precompileA Boolean flag that indicates whether the libraries specified in the rtwmakecfg.m file exist in a specified location (precompile==1) or if you must create the libraries in the build folder during the build process (precompile==0).
makeInfo.libraryA structure array that specifies additional run-time libraries and module objects, organized as a row vector. The build process expands the information into make rules in the generated makefile. For a list of the library fields, see the next table.

The makeInfo.library field consists of the following elements.

ElementDescription
makeInfo.library(n).NameA character array that specifies the name of the library (without an extension).
makeInfo.library(n).LocationA character array that specifies the folder in which the library is located when precompiled. For more information, see the description of makeInfo.precompile in the preceding table. A target can use the TargetPreCompLibLocation parameter to override this value. See Specify the Location of Precompiled Libraries.
makeInfo.library(n).ModulesA cell array that specifies the C or C++ source file base names (without an extension) that comprise the library. Do not include the file extension. The makefile appends the object extension.

Note

The makeInfo.library field must fully specify each library and how to build it. The modules list in the makeInfo.library(n).Modules element cannot be empty. To specify a link-only library, use the makeInfo.linkLibsObjs field instead.

Example:

 disp(['Running rtwmakecfg from folder: ',pwd]);
    makeInfo.includePath = { fullfile(pwd, 'somedir2') };
    makeInfo.sourcePath = {fullfile(pwd, 'somedir2'), fullfile(pwd, 'somedir3')};
    makeInfo.sources  = { 'src1.c', 'src2.cpp'};
    makeInfo.linkLibsObjs = { fullfile(pwd, 'somedir3', 'src3.object'),...
                              fullfile(pwd, 'somedir4', 'mylib.library')};
    makeInfo.precompile = 1;
    makeInfo.library(1).Name     = 'myprecompiledlib';
    makeInfo.library(1).Location = fullfile(pwd,'somdir2','lib');
    makeInfo.library(1).Modules  = {'srcfile1' 'srcfile2' 'srcfile3' };

Note

If a path that you specify in the rtwmakecfg.m API contains spaces, the build process does not convert the path to its nonspace equivalent. If the build environments you intend to support do not support spaces in paths, refer to Build Process Support for File and Folder Names.

Modify the Template Makefile for rtwmakecfg

To expand the information that an rtwmakecfg function generates, modify the following sections in the TMF of your target:

  • Include Path

  • C Flags and/or Additional Libraries

  • Rules

It is possible that these TMF code examples do not apply to your make utility. For additional examples, see the GRT or ERT TMFs located in matlabroot/toolbox/coder/compile/tmf.

Add Folder Names to the Makefile Include Path

The following TMF code example adds folder names to the include path in the generated makefile:

ADD_INCLUDES = \
|>START_EXPAND_INCLUDES<|   -I|>EXPAND_DIR_NAME<| \
|>END_EXPAND_INCLUDES<|

Also, the ADD_INCLUDES macro must be added to the INCLUDES line.

INCLUDES = -I. -I.. $(ADD_INCLUDES) $(USER_INCLUDES)

Add Library Names to the Makefile

The following TMF code example adds library names to the generated makefile.

LIBS =
|>START_PRECOMP_LIBRARIES<|
LIBS += |>EXPAND_LIBRARY_NAME<|.a |>END_PRECOMP_LIBRARIES<|
|>START_EXPAND_LIBRARIES<|
LIBS += |>EXPAND_LIBRARY_NAME<|.a |>END_EXPAND_LIBRARIES<|

For more information, see Control Library Location and Naming During Build.

Add Rules to the Makefile

The TMF code example adds rules to the generated makefile.

|>START_EXPAND_RULES<|
$(BLD)/%.o: |>EXPAND_DIR_NAME<|/%.c $(SRC)/$(MAKEFILE) rtw_proj.tmw
    @$(BLANK)
    @echo ### "|>EXPAND_DIR_NAME<|\$*.c"
    $(CC) $(CFLAGS) $(APP_CFLAGS) -o $(BLD)$(DIRCHAR)$*.o \
    |>EXPAND_DIR_NAME<|$(DIRCHAR)$*.c > $(BLD)$(DIRCHAR)$*.lst
|>END_EXPAND_RULES<|

|>START_EXPAND_LIBRARIES<|MODULES_|>EXPAND_LIBRARY_NAME<| = \
|>START_EXPAND_MODULES<|    |>EXPAND_MODULE_NAME<|.o \
|>END_EXPAND_MODULES<|

|>EXPAND_LIBRARY_NAME<|.a : $(MAKEFILE) rtw_proj.tmw
$(MODULES_|>EXPAND_LIBRARY_NAME<|:%.o=$(BLD)/%.o)
    @$(BLANK)
    @echo ### Creating $@
    $(AR) -r $@ $(MODULES_|>EXPAND_LIBRARY_NAME<|:%.o=$(BLD)/%.o)
|>END_EXPAND_LIBRARIES<|

|>START_PRECOMP_LIBRARIES<|MODULES_|>EXPAND_LIBRARY_NAME<| = \
|>START_EXPAND_MODULES<|    |>EXPAND_MODULE_NAME<|.o \
|>END_EXPAND_MODULES<|

|>EXPAND_LIBRARY_NAME<|.a : $(MAKEFILE) rtw_proj.tmw
$(MODULES_|>EXPAND_LIBRARY_NAME<|:%.o=$(BLD)/%.o)
    @$(BLANK)
    @echo ### Creating $@
    $(AR) -r $@ $(MODULES_|>EXPAND_LIBRARY_NAME<|:%.o=$(BLD)/%.o)
|>END_PRECOMP_LIBRARIES<|