function me = constructBuildException(buildError, makeResults, compileErrorDiagnosticFcns, ...
    lBuildLogger)
%

%   Copyright 2014-2023 The MathWorks, Inc.


if ~isVerboseBuild(lBuildLogger)
    % For the non-verbose case, the error log must be displayed
    forceDisplay(lBuildLogger, makeResults)
end

% The message has already been translated and formatted, it just needs to
% be packaged into an MSLException and thrown.
%
% Note that, unlike MException, MSLException > getReport makes the full
% stack trace available, including stack traces for its causes
me = MSLException( 'RTW:buildProcess:fatalBuildError', '%s', buildError );

% Perform diagnostics on the compiler error message and augment the build
% error if any recognized syndromes are identified
for i = 1:length( compileErrorDiagnosticFcns )
    compileErrorDiagnosticFcn = compileErrorDiagnosticFcns{ i };
    try
        feval( compileErrorDiagnosticFcn, makeResults );
    catch e
        me = me.addCause(e);
    end
end
