function varargout = addBuildToolCommon(h,byUser,buildToolType,name,tool)
%

%   Copyright 2014-2020 The MathWorks, Inc.

narginchk( 4, 5 );
nargoutchk( 0, 1 );
buildToolType = convertStringsToChars(buildToolType);
name = convertStringsToChars(name);

% If user adds the tool, make sure he does not use reserved
% names
if byUser
    reserved = h.getReservedNames( 'BuildTools' );
    if ~isempty( reserved ) && any( strcmp( name, reserved ) )
        error( message( 'coder_compile:toolchain:ReservedBuildToolKey', name ) );
    end
end
% Determine what property to add tool to
prop = h.getToolsProp( buildToolType );
% Make sure the tool is valid
if nargin==5
    if h.isBuildTool( tool )
        h.initializeBuildConfigOptions( tool );
    end
    h.checkBuildToolsForAdd( 'build tool', prop, name, tool );
else
    tool = h.createBuildTool();
end
% Add the tool
prop.add( name, tool );
% Return if requested
if nargout==1
    varargout{ 1 } = tool;
end
end
