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

%   Copyright 2014-2019 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 MException and thrown.
me = MException( '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
