Main Content

Run Polyspace Analysis on Generated Code by Using Packaged Options Files

When you start a Polyspace® analysis directly from the Simulink® toolstrip, the analysis takes the model-specific context, such a design ranges, into consideration. When running a Polyspace analysis without access to Simulink, you must specify the model-specific information by using options files. Instead of authoring these options files, use the options files generated and packaged by the function polyspacePackNGo.

Preserving the Simulink model context information when running a Polyspace analysis can be useful in various situations. For instance:

  • Distributed workflow: A Simulink user generates code from a model and sends the code to another development environment. In this environment, a Polyspace user, who might not have Simulink, runs a separate analysis of the generated code. By using the packaged options files, the design ranges and other model-specific information is preserved in the Polyspace analysis.

  • Analysis options not available in Simulink: Some Polyspace analysis options are available only when the Polyspace analysis is run separately from Simulink. Use packaged options files to run a separate Polyspace analysis while preserving the model-specific information. For instance, analyze concurrent threads in generated code by running a Polyspace analysis in the generated code by using the packaged options files.

You must have Simulink to run the function polyspacePackNGo. You do not need Polyspace to generate the options files from a Simulink model. The polyspacePackNGo function supports code generated by Embedded Coder® and TargetLink®.

Generate and Package Polyspace Options Files

To generate and package Polyspace options file for analyzing code generated from a Simulink model, use polyspacePackNGo.

  1. In the Simulink Editor, open the Configuration Parameters dialog box and configure the model for code generation.

  2. To configure the model for compatibility with Polyspace, select ert.tlc as the System target file

  3. To enable generating a code archive, select the option Package code and artifacts. Optionally, provide a name for the options package in the field Zip file name. If your code contains a custom code block, select Use the same custom code settings as Simulation target in the Code Generation> Custom Code pane.

    Alternatively, in the MATLAB® Command Window, enter:

    % Configure the Simulink model mdlName for code generation
    configSet = getActiveConfigSet(mdlName);
    set_param(configSet, 'PackageGeneratedCodeAndArtifacts', true);
    set_param(configSet, 'PackageName', 'CodeArchive.zip');
    set_param(configSet, 'SystemTargetFile', 'ert.tlc');
    set_param(configSet,'RTWUseSimCustomCode','on');

  4. Generate the code archive.

    • To generate an archive of standalone generated code from the top model, use the function rtwbuild (Embedded Coder).

    • To generate code as a model reference, use the function slbuild. After generating code as model reference, create the code archive by using the function packNGo (Embedded Coder).

    • Alternatively, you can use TargetLink to generate the code. Create the code archive by archiving the generated code into a zip file.

  5. To generate and package the Polyspace option files, in the MATLAB Command Window ,use the polyspacePackNGo function :

    zipFile = polyspacePackNGo(mdlName);
    See Generate and Package Polyspace Options Files.

    If you use TargetLink to generate code, then use the TargetLink subsystem name as the input argument to polyspacepacknGo.

  6. Optionally, you can use a pslinkoptions object as a second argument to modify the default options for the Polyspace analysis. Create a pslinkoptions object containing the additional options and specify the object when creating the archive:

    psOpt = pslinkoptions(mdlName);
    psOpt.InputRangeMode = 'FullRange';
    psOpt.ParamRangeMode = 'DesignMinMax';
    zipFile = polyspacePackNGo(mdlName,psOpt);
    
    See Package Polyspace Options Files That Have Specific Polyspace Analysis Options.

  7. Use the optional third argument to specify whether to generate and package Polyspace options files for code generated as a model reference. Suppose you generated code as a model reference by using the slbuild function. To generate and package Polyspace options for the code, at the MATLAB Command Window, enter:

    zipFile = polyspacePackNGo(mdlName,psOpt,true);
    See Package Polyspace Options Files for Code Generated as a Model Reference.

    The function polyspacepackNGo returns the full path to the archive containing the options files. The files are located in the polyspace folder within the archived folder hierarchy. The content of the polyspace folder depends on the inputs of polyspacePackNGo function.

    • If you do not specify the optional second and third arguments, then the folder polyspace contains these options files in a flat hierarchy:

      • optionsFile.txt: This file specifies the source files, the include files, data range specifications, and analysis options required for analyzing the generated code by using Polyspace. If your code contains custom C code, then this file specifies the relative paths of the custom source and header files.

      • modelname_drs.xml: This file specifies the design range specification of the model.

      • linkdata.xml: This file links the generated code to the components of the model.

    • If you specify psOpts.ModelbyModelRef = true, then corresponding options files are generated for all referenced models. These options files are stored in separate folders named polyspace_<referenced model name> within the code archive. The folder polyspace contains the options files for the top model.

Run Polyspace Analysis by Using the Packaged Options Files

Once the code archive and the Polyspace option files are generated, you can use the archive to run a Polyspace analysis on the generated code in a different development environment without Simulink.

  1. Unzip the code archive and locate the polyspace folder.

  2. On a Windows® or Linux® command line, run: productname -options-file optionsFile.txt -results-dir resultdir.

    • productname corresponds to one of: polyspace-bug-finder, polyspace-code-prover, polyspace-bug-finder-server, or polyspace-code-prover-server.

    • resultdir corresponds to the location of the Polyspace results. This argument is optional.

    If the file linkdata.xml is not there, use the option Code Generator Support in Polyspace User Interface to specify which comments in the code act as links to the Simulink model. In the Polyspace User Interface, select Tools > Preferences and locate the Miscellaneous tab. From the context menu Code comments that act as code-to-model-link, select the code generator that you used. If you select User defined, then specify the comments that act as a code-to-model link by specifying their prefix in the field Comments beginning with. For instance, if you specify the prefix as //Link_to_model, then Polyspace interprets comments starting with //Link_to_model as links to model.

  3. To review the result, upload it to Polyspace Access and view the results in a web browser. Alternatively, view the result by using the user interface of the Polyspace desktop products.

Analyze Code Generated as Standalone Code in a Distributed Workflow

Generate and package Polyspace options files from a Simulink model by using the function polyspacepackNGo. Use these options files to run a Polyspace analysis on the generated code that uses model-specific information, such as design range specifications, without requiring Simulink.

Open Model

The model demo_math_operations performs various mathematical operations on the model inputs. The model has a C Function block that executes a custom C code. The model also has a C Caller block that calls the C function GMean, which is implemented in the source file GMean.c. To open the model for code generation and packaging Polyspace options file, search for the current topic in the MATLAB help browser and click the Open Model button. Alternatively, in the MATLAB Command Window, paste and run the following code.

open_system('demo_math_operations');

Configure Model

To configure the model for generating code and packaging Polyspace options files, specify these configuration parameters:

  • To create an archive containing the generated code, set 'PackageGeneratedCodeAndArtifacts' to true.

  • Specify a name for the code archive. For instance, set the name to genCodeArchive.zip.

  • To use the custom code setting specified in Simulation Target during code generation, set 'RTWUseSimCustomCode' to 'on'.

  • To make the model and the generated code compatible with Polyspace, set ert.tlc as the system target file. See Recommended Model Configuration Parameters for Polyspace Analysis (Polyspace Bug Finder).

In Command Window or Editor, enter these parameter configurations:

configSet = getActiveConfigSet('demo_math_operations');
set_param(configSet, 'PackageGeneratedCodeAndArtifacts', true);
set_param(configSet, 'PackageName', 'genCodeArchive.zip');
set_param(configSet, 'SystemTargetFile', 'ert.tlc');
set_param(configSet,'RTWUseSimCustomCode','on')

Generate Code Archive

Specify a folder for storing the generated code. To start code generation, in the Command Window or in the Editor, enter:

codegenFolder = 'demo_math_operations_ert_rtw';
if exist(fullfile(pwd,codegenFolder), 'dir') == 0
    rtwbuild('demo_math_operations')
end

Because PackageGeneratedCodeAndArtifacts is set to true, the generated code is packed into the archive genCodeArchive.zip.

Generate and Package Polyspace Options File

To generate Polyspace options files for the generated code, in the Command Window or in the Editor, enter:

zipFile = polyspacePackNGo('demo_math_operations');

In the archive genCodeArchive.zip, find the options files in the folder <current folder>/polyspace.

Run Polyspace Analysis by Using the Packaged Options Files

  1. Unzip the code archive genCodeArchive.zip and locate the <current folder>/polyspace folder.

  2. Open a command-line terminal and change your working folder to the polyspace subfolder of the unzipped folder by using the cd command.

  3. Start a Polyspace analysis.

    • To run a desktop Polyspace analysis, use either polyspace-code-prover or polyspace-bug-finder. To run the Polyspace analysis in a server, use either polyspace-bug-finder-server or polyspace-code-prover-server. Polyspace Bug Finder and Code Prover analyze the code differently. See Choose Between Polyspace Bug Finder and Polyspace Code Prover (Polyspace Bug Finder).

    • Specify the file optionsFile.txt as the argument to -options-file.

    To run a Code prover analysis, run this command: polyspace-code-prover -options-file optionsFile.txt -results-dir Results.

  4. Follow the progress of the analysis in the log file that is generated in the Results folder.

  5. To view the results in the desktop user interface, in the command-line interface, enter: polyspace Results\ps_results.pscp. The extension of the ps_results file changes depending on whether you run a Code Prover analysis or a Bug Finder analysis. The result contains several orange checks.

    Alternatively, upload the result to Polyspace Access. See Upload Results to Polyspace Access (Polyspace Bug Finder Access)

  6. Address the results. For more information, see Address Results Through Bug Fix or Comments (Polyspace Bug Finder).

See Also

| | (Embedded Coder) | (Embedded Coder)