function [success,problem,varargout] = toolchainSetup(h, lBuildLogger, varargin)
%toolchainSetup performs setup prior to compilation with the toolchain

%   Copyright 2014-2019 The MathWorks, Inc.

persistent p;
if isempty(p)
    p = inputParser;
    addParameter(p, 'test', false);
    addParameter(p, 'runMatlabScriptOnly', false)
end
parse(p, varargin{:});
invokedFromTest = p.Results.test;
runMatlabScriptOnly = p.Results.runMatlabScriptOnly;

% Validate
h.checkHandleIfValid( 'setup' );
% Invoke setup routines
[success,problem,reportStr] = h.configureToolchain...
    ('DoToolchainSetup', lBuildLogger, runMatlabScriptOnly);
% When testing, we just want to make sure this routine ran.
% No need to throw an error.
if invokedFromTest
    varargout{ 1 } = reportStr;
    return
end
% Throw an error if a problem is encountered.
if ~isempty( problem )
    error( message( 'CoderFoundation:toolchain:ConfigToolchainError', problem ) )
end
end
