www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/RegisterAndUseToolchainToBuildExecutableExample.m

    %% Register and Use Toolchain To Build Executable
%
% This example shows how to register and use a toolchain to build an
% executable. This example uses the Intel(R) compiler. However, the concepts 
% and programming interface apply for other toolchains. Once you register a
% toolchain, you can configure a model such that the code generator uses that 
% toolchain to build an executable for the model. 
%

% Copyright 2013-2015 The MathWorks, Inc.

%% Toolchain
% A toolchain is a collection of tools required to compile, link, download,
% and run code on a specified platform. A toolchain consists of multiple tools, such as a
% compiler, linker, and archiver. You can configure the tools in a toolchain with multiple
% options and group tool specifications into types of configurations. 

%% Create a New Folder and Copy Relevant Files
% The following code creates a folder in your current working folder (pwd).
% The new folder contains files for this example. If you do not want to
% affect the current folder, or cannot generate files in this folder, change
% your working folder before invoking the command.
copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','toolchain_demo'),'toolchain_demo','f')
cd toolchain_demo

%% Open a Model
% Open the |rtwdemo_counter| model. By default, this model is configured
% to use the Generic Real-Time Target (GRT) configuration and the toolchain 
% approach for building an executable. The capability demonstrated in this 
% example is available for models configured to use the Toolchain approach.
model = 'rtwdemo_counter';
open_system(model)

%% Choose a Toolchain
% In the model window, click *Simulation* > *Model Configuration Parameters* to open the
% *Configuration Parameters* dialog box. Select *Code Generation*. The
% *Toolchain settings* section contains parameters for configuring a
% toolchain. From the *Toolchain* drop-down list, select
% the toolchain installed on your development system for building an executable
% from the code generated from your model.
%

%%
% By default, the *Faster Runs* build configuration is selected. 
% Click *Show settings* to see the toolchain flags specified for building the
% generated code. Choose a build configuration based on your current 
% application development goal.

%% 
% Close the *Configuration Parameters* dialog box.

%% Create a ToolchainInfo Object
% This example shows how you can register a custom toolchain and add it
% as a selectable *Toolchain* option.

%%
% The first step to registering a custom toolchain is to create a |ToolchainInfo|
% object that contains information about the toolchain. Methods are
% available to set toolchain specifications. You can share a |ToolchainInfo| 
% object across installations.  

%%
% Open the toolchain definition file for an Intel compiler.
% This file creates a |ToolchainInfo| object that contains information
% about the Intel toolchain on a 64-bit Windows(R) platform. 
edit intel_tc
type intel_tc

%%
% Run the toolchain definition file to generate the |ToolchainInfo| object.
tc = intel_tc;

%%
% Save the |ToolchainInfo| object |tc| to a MAT file.
save intel_tc tc

%% Register the Custom Toolchain
% After you create the |ToolchainInfo| object for the new toolchain, 
% register it. Register a toolchain in |RTW.TargetRegistry|. 
% To register the toolchain, write an |rtwTargetInfo.m| file. Then, 
% add that file to the MATLAB path so the system loads it automatically. 
type rtwTargetInfo

%%
% Reset the |TargetRegistry| to use the new |rtwTargetInfo.m| file.
RTW.TargetRegistry.getInstance('reset');

%% Choose the Custom Toolchain
% Reopen the *Configuration Parameters* dialog box. Click *Toolchain*. You 
% should see the new toolchain in the list. Select the |Intel v12.1| toolchain.

%%
% Programmatically, you can accomplish the same task with the following
% commands: 
cs = getActiveConfigSet(model);
set_param(cs, 'Toolchain', tc.Name)

%%
% Verify your selection.
toolchain = get_param(cs, 'Toolchain')

%% Build the Model Using the Custom Toolchain
% You can now build the model with the new custom toolchain.

%%
% Note: If you do not have the Intel toolchain installed, you can use the
% following command to generate the code and makefile only.
set_param(cs, 'GenCodeOnly', 'on')

%%
% Build the model to generate the code and makefile that uses the new
% toolchain.
rtwbuild(model)

%%
% Examine the generated makefile.
type(fullfile([model '_grt_rtw'], [model '.mk']))

%%
% Once the build process is finished, and you had Intel compilers installed, 
% you can run the generated executable.

 % if ispc
 %     system([model '.exe'])
 % else
 %     system(model)
 % end

%% Restore
% You can optionally remove the folder you created earlier.
cd ..
% rmdir('toolchain_demo', 's')

%%
% Reset the |TargetRegistry| to remove the toolchain that you registered
% above. 
RTW.TargetRegistry.getInstance('reset');

%% 
% Close the model.
close_system(model, 0)

%%
% Clear the variables introduced in the workspace.
clear INC K LIMIT RESET model tc cs toolchain