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:
- Create the MATLAB® language - rtwmakecfgfunction in the- rtwmakecfg.mfile. The code generator associates this file with your S-function based on its folder location. Create the rtwmakecfg Function describes the requirements for the- rtwmakecfgfunction and the data it returns.
- 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 - rtwmakecfgfunction 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
                        . on Microsoft®
                Windows® systems). The function must return a structured array that contains
                these fields.mexext
| Field | Description | 
|---|---|
| makeInfo.includePath | A 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.sourcePath | A 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 SFunctionModulesparameter 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.sources | A 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 SFunctionModulesparameter
                                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 thesourcePathfield. | 
| makeInfo.linkLibsObjs | A 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.precompile | A Boolean flag that indicates whether the libraries specified in
                                the rtwmakecfg.mfile 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.library | A 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.
| Element | Description | 
|---|---|
| makeInfo.library(n).Name | A character array that specifies the name of the library (without an extension). | 
| makeInfo.library(n).Location | A character array that specifies the folder in which the library
                                is located when precompiled. For more information, see the
                                description of makeInfo.precompilein the
                                preceding table. A target can use theTargetPreCompLibLocationparameter to
                                override this value. See Specify the Location of Precompiled Libraries. | 
| makeInfo.library(n).Modules | A 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 Flagsand/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<|