function blockHandles = crossReleaseImport(buildFolder, configSetOrModel, varargin)
%CROSSRELEASEIMPORT imports generated model code from a previous release.
%
% BLOCKHANDLE = crossReleaseImport(BUILDFOLDER,CONFIGSETORMODEL,'SimulationMode',MODE)
% imports previously generated model component code from folder BUILDFOLDER
% into the current release as SIL and PIL blocks. CONFIGSETORMODEL is a
% Simulink.ConfigSet object, or the name of a Simulink model on the MATLAB
% path. MODE is 'SIL', 'PIL', 'none' or {'SIL','PIL'}.
%
% You can replace the model component in an existing model with the SIL,
% PIL or code generation only (none) block. Or, you can use the block as
% a component in a new model.
%
% When you generate code with a SIL, PIL or code generation only block,
% the model component uses generated code from the previous release.
%
% When you run a simulation with a SIL or PIL block
% the model component uses generated code from the previous release.
%
% It is not possible to run a Normal, Accelerator or Rapid-Accelerator
% mode simulation for a model with a code generation only block. To
% simulate using generated code from the previous release, either create
% a cross-release SIL or PIL block by specifying 'SimulationMode' as
% 'SIL' or 'PIL', or include the code generation block in a top-model
% SIL/PIL or Model block SIL/PIL simulation.
%
% To build a SIL or PIL block, the function by default uses these parameters
% from the Simulink configuration set or model CONFIGSETORMODEL:
% - SystemTargetFile
% - Toolchain or TemplateMakefile
% - ExistingSharedCode
% - PortableWordSizes
% - TargetLang
% - TargetLangStandard
% - TargetLibSuffix
% - ModelReferenceNumInstancesAllowed
% - Hardware Implementation pane parameters
% If you want to generate code, these settings must be compatible
% with the configuration of the model that uses the block.
%
% BLOCKHANDLE = crossReleaseImport(BUILDFOLDER,CONFIGSETORMODEL,'SimulationMode',MODE,'ConfigParams',ADDITIONALPARAMETERLIST)
% uses additional parameters for building the SIL or PIL block.
%
% BLOCKHANDLE = crossReleaseImport(BUILDFOLDER,CONFIGSETORMODEL,'SimulationMode',MODE,'OriginalPaths',ORIGINALPATHS, 'ReplacementPaths', REPLACEMENTPATHS)
% imports generated model code with custom source or include paths
% originally located in ORIGINALPATHS relocated to REPLACEMENTPATHS.
%
% BLOCKHANDLE = crossReleaseImport(BUILDFOLDER,CONFIGSETORMODEL,'SimulationMode',MODE,'DataDictionary',DICTIONARYFILE)
% specifies a data dictionary that contains data types used by the generated
% model code.
%
% BLOCKHANDLE = crossReleaseImport(BUILDFOLDER,CONFIGSETORMODEL,'SimulationMode',MODE,'SFunctionName',SFUNCTIONNAME)
% renames the generated SIL or PIL block as SFUNCTIONNAME_sil or
% SFUNCTIONNAME_pil. Use the 'SFunctionName' argument if the default
% block name results in associated MATLAB identifiers being longer than
% 63 characters.
%

% Copyright 2016-2018 The MathWorks, Inc.

narginchk(4,16);

p = inputParser;
p.addRequired('BuildFolder',...
@(x)validateattributes(x,{'char', 'string'}, {'nonempty', 'scalartext'},mfilename,'buildFolder'));

p.addRequired('ConfigSetOrModel',...
@(x) validateattributes(x,{'char', 'string', 'Simulink.ConfigSet'}, {'nonempty'},mfilename,'configSetOrModel'));

p.addParameter('SimulationMode',[],...
@(x) validateattributes(x,{'char', 'cell', 'string'}, {'nonempty','row'},mfilename,'SimulationMode'));

p.addParameter('OriginalPaths', {},...
@(x) validateattributes(x,{'char', 'cell', 'string'}, {},mfilename,'OriginalPaths'));

p.addParameter('ReplacementPaths', {},...
@(x) validateattributes(x,{'char', 'cell', 'string'}, {},mfilename,'ReplacementPaths'));

p.addParameter('DataDictionary', '',...
@(x) validateattributes(x,{'char', 'string'}, {'scalartext'}, mfilename,'DataDictionary'));

p.addParameter('SFunctionName', '',...
@(x) validateattributes(x,{'char', 'string'}, {'scalartext'}, mfilename,'SFunctionName'));

p.addParameter('ConfigParams',{},...
@(x) validateattributes(x,{'cell', 'string'}, {},mfilename,'ConfigParams'));

p.addParameter('CodeLocation', '',...
@(x) validateattributes(x,{'char', 'string'}, {'scalartext'}, mfilename,'CodeLocation'));

p.parse(buildFolder, configSetOrModel, varargin{:});

importer = coder.internal.xrel.Importer( ...
p.Results.BuildFolder, ...
p.Results.CodeLocation, ...
p.Results.ConfigSetOrModel,...
p.Results.SimulationMode,...
p.Results.DataDictionary, ...
p.Results.OriginalPaths, ...
p.Results.ReplacementPaths, ...
p.Results.SFunctionName, ...
p.Results.ConfigParams);

blockHandles = importer.createXrelBlocks;

end