function updateSharedCodeLocation(model)
%UPDATESHAREDCODELOCATION merges existing shared code to the local folder
%   This function analyzes code in the existing shared code folder and copies or merges it to the
%   local shared utilities folder. Diagnostics are generated according to the setting of
%   UseOnlyExistingSharedCode.

% Copyright 2014-2020 The MathWorks, Inc.

% Identify the shared utilities folder
bDir = RTW.getBuildDir(model);
lSharedUtilsFolder = fullfile(bDir.CodeGenFolder, bDir.SharedUtilsTgtDir);

% Identify and validate the folder containing existing shared code
lExistingSharedCode = coder.internal.xrel.get_ExistingSharedCode_param(model);

assert(~isempty(lExistingSharedCode), 'The existing shared code folder must be non-empty')

filesToMerge = {}; % Not explicitly specified means merge all files
[~, newLocallyGeneratedFiles, constParamsUpdatedLocally] = ...
    coder.internal.xrel.mergeExistingSharedCode...
    (lExistingSharedCode, lSharedUtilsFolder, filesToMerge, ...
     'ModelName', model);

% Check compliance with diagnostic setting
lUseOnlyExistingSharedCode = get_param(model, 'UseOnlyExistingSharedCode');
coder.internal.xrel.existingSharedCodeDiagnostic...
    (lExistingSharedCode, lSharedUtilsFolder, ...
     lUseOnlyExistingSharedCode, model, ...
     newLocallyGeneratedFiles, constParamsUpdatedLocally);
