Relocate or Share Generated Code
To relocate or share static and generated code files, you can use the packNGo
function. For example, use the function when you want to
relocate generated code to a development environment that does not provide MATLAB® and Simulink® products.
Package and Relocate Generated Code
To package generated code:
In the Configuration Parameters dialog box, select Package code and artifacts. This option configures the build process to run the
packNGo
function after code generation.In the Zip file name field, enter the name of the ZIP file in which you want to package the generated code and artifacts. You can specify the file name with or without the
.zip
extension. If you do not specify an extension or an extension other than.zip
, the ZIP utility adds the.zip
extension. If you do not specify a value, the build process uses the name
, wheremodel
.zip
is the name of the top model.model
Apply the changes and then generate code for your model.
Verify that the packaged code is ready for relocation by using a ZIP tool to inspect the resulting ZIP file.
Relocate the ZIP file to the destination development environment and use a ZIP tool to unpack the file. To compile the unpacked code, use a build tool.
If the destination environment provides Simulink
Coder™ or Embedded Coder®, you can use codebuild
to compile the unpacked code.
Package and Relocate Generated Code From Command Line
To package and relocate generated code by using line commands, use this workflow:
Select a Structure for the ZIP File
Before you generate and package the files for a model build, decide whether
you want the files to be packaged in a flat or hierarchical folder structure. By
default, the packNGo
function packages the
files in a single, flat folder structure.
If... | Then Use a... |
---|---|
You are relocating files to an IDE that does not use the generated makefile, or the code is not dependent on the relative location of required static files | Single, flat folder structure |
The destination development environment must maintain the folder structure of the source environment because it uses the generated makefile, or the code depends on the relative location of files | Hierarchical structure |
If you use a hierarchical structure, the packNGo
function creates two
levels of zip
files, a primary zip
file,
which in turn contains the following secondary zip
files:
mlrFiles.zip
— files in your
folder treematlabroot
sDirFiles.zip
— files in and under your build folder where you initiated code generation for the modelotherFiles.zip
— required files not in the
ormatlabroot
start
folder trees
Paths for the secondary zip
files are relative to the root
folder of the primary zip
file, maintaining the source
development folder structure.
Select a Name for the Zip File
By default, the packNGo
function names the primary
zip
file
. You have the option of
specifying a different name. If you specify a file name and omit the file type
extension, the function appends model
.
to the name that you
specify.
Package Model Code in a Zip File
Package model code files by using the PostCodeGenCommand
configuration parameter, packNGo
function, and build
information object for the model. You can set up the packaging operation to
use:
A system generated build information object.
In this case, before generating the model code, use
set_param
to set the configuration parameterPostCodeGenCommand
to an explicit call to thepackNGo
function. For example:set_param(bdroot, 'PostCodeGenCommand', 'packNGo(buildInfo);');
After generating and writing the model code to disk and before generating a makefile, this command instructs the build process to evaluate the call to
packNGo
. This command uses the system generated build information object for the currently selected model.A build information object that you construct programmatically.
In this case, you could use other build information functions to include paths and files selectively in the build information object that you then specify with the
packNGo
function. For example:. . . myModelBuildInfo = RTW.BuildInfo; addSourceFiles(myModelBuildInfo, {'test1.c' 'test2.c' 'driver.c'}); . . . packNGo(myModelBuildInfo);
The following examples show how you can change the default behavior of
packNGo
.
To... | Specify... |
---|---|
Change the structure of the file packaging to hierarchical | packNGo(buildInfo, 'packType'
'hierarchical'); |
Rename the primary zip file | packNGo(buildInfo, 'fileName'
'zippedsrcs'); |
Change the structure of the file packaging to hierarchical
and rename the primary zip file | packNGo(buildInfo, 'packType'
'hierarchical'... |
Include header files found on the include path in the
zip file | packNGo(buildInfo, 'minimalHeaders'
false); |
Generate warnings for parse errors and missing files | packNGo(buildInfo, 'ignoreParseError'
true... |
Note
The packNGo
function can modify
the build information in the RTW.BuildInfo
object that is
passed as the first argument of the function. The function might find
additional files from source and include paths recorded in build information
for the model and add that information to the
RTW.BuildInfo
object.
Inspect Generated ZIP File
To verify that the generated ZIP file is ready for relocation, use a ZIP tool to inspect the file. Some ZIP tools allow you to view the file contents without unpacking the file. If unpacking is required and the model code files are packaged as a hierarchical structure, you have to unpack the primary and secondary ZIP files. When you unpack the secondary ZIP files, relative paths of the files are preserved.
Relocate and Unpack ZIP File
Relocate the ZIP file to the destination development environment and use a ZIP tool to unpack the file. To compile the unpacked code, use a build tool.
If the destination environment provides Simulink
Coder or Embedded Coder, you can use codebuild
to compile the unpacked code.
Code Packaging Example
This example shows how to package code files generated for the example model
CounterModel
:
Open the model.
openExample('CounterModel');
In the Command Window, enter:
set_param('CounterModel', 'PostCodeGenCommand',... 'packNGo(buildInfo, ''packType'', ''hierarchical'')');
Generate code for the model.
Inspect the generated ZIP file,
CounterModel.zip
. The ZIP file contains a secondary ZIP file,sDirFiles.zip
.Inspect
sDirFiles.zip
.Relocate the ZIP file to your destination folder and unpack it.
To compile the unpacked code, use the
codebuild
function.
Limitations
packNGo Function
For information about limitations that apply to this function, see packNGo
.
Executable File with Nondefault Extension
If a build process uses the template makefile approach, then packNGo
uses the executable file
extension specified by the linker tool to determine binary artifacts that
require packaging.
If you generate an executable file with an extension that is not a default value, check that the extension is saved in the toolchain associated with the template makefile. For more information, see Associate the Template Makefile with a Toolchain.
If the build process generates an executable file with an extension that is
different from the extension saved in the toolchain, packNGo
does not package the executable file.