MATLAB: How to prevent the rt_main.c or ert_main.c file from being included in the packNGo package when using ert.tlc

ert_mainpackngort_mainsimulink coder

We're generating code from Simulink using our own custom target which is based on the ERT target. Often, we like to create a ZIP of our generated code (and all necessary files for compilation) that can be provided to another team who might not have MATLAB (or the same version) installed on their system.
In our target, we create our own "main" source file and function to wrap the functions (e.g., init, output, etc.) in the generated <model_name>.c source file. However, when we use the packaging routines, we've found that it always seems to include the rt_main.c or ert_main.c (depending upon which version of MATLAB is used) source files from the MATLAB_ROOT directory. This file isn't needed nor used in the compilation as we have our own. We have already turned off the option to generate example main function (setting the Configuration Parameters > Code Generation > Templates > Generated an example main program parameter value to 'off').
I've looked in the buildInfo, and found that the rt_main.c or ert_main.c source files are listed automatically. Is there a way to disable these from being included?

Best Answer

With an ert.tlc based target, the typical workflow normally includes the rt_main.c (for applications using the static main module and portable for multiple platforms) or the ert_main.c (as an example application for a chosen platform) so executable can be generating without missing the 'main' function. Therefore, when you check "Generate an example main program", the ert_main.c will be generated. When you uncheck that option, the rt_main.c (together with rtwmodel.h) will be included. More details about such deployment mechanism can be found here:
Either way, they are captured in the buildInfo.mat file. The 'packNGo' function uses the buildInfo.mat to find all needed files.
Therefore, if we remove the 'ert_main.c' or 'rt_main.c' entry in the 'buildInfo' variable saved in the buildInfo.mat file, 'packNGo' will not include them in the ZIP file. When passing the modified 'buildInfo' to 'packNGo', make sure that there is no other buildInfo.mat file presented in the current working directory as that stale file could override the modified 'buildInfo'.
There are two possible workflows of utilizing the above fact to exclude the (e)rt_main.c file from the ZIP package. In both cases, the attached script removeExampleMain.m will be used.
1) Use the Post code generation command parameter. Here, we are calling the modifying script during the code generation process, so the generated buildInfo.mat file is already free of the main files. In order to do that, go to Configuration Parameters and search for 'Post code generation command parameter', use 'removeExampleMain' as the value. Save the configuration and generate code. Once the code is generated, load the buildInfo.mat file and do 'packNGo' with the loaded 'buildInfo' variable. Note, if there is an existing buildInfo.mat file under the current working directory, you need to delete it before running 'packNGo'.
2) Run the script right before 'packNGo'. In this case, you do not need to modify the model. Generate the code and load the generated buildInfo.mat file. After that, run 'removeExampleMain' before running 'packNGo'.