function customCodeSettings = get_custom_code_settings(targetId,parentTargetId)

%   Copyright 2004-2020 The MathWorks, Inc.

% Assumption: parent model and child model have the same targetName

  relevantTargetId = targetId;
  targetName       = sf('get', targetId, 'target.name');
  machineId        = sf('get', targetId, 'target.machine');
  machineName      = sf('get', machineId, 'machine.name');
  isLib            = sf('get', machineId,'machine.isLibrary');
  
  isRTW = strcmp(targetName, 'rtw');
  if isRTW
      mainMachineName = sf('get', sf('get', parentTargetId, 'target.machine'), 'machine.name');      
      % isRTW is false if it is a rapid accel or mdlref sim target.
      isRTW = ~CGXE.Utils.isRaccelOrMdfRefSimTarget(mainMachineName);
  end
  
  customCodeSettings = internal.stateflow.CustomCodeSettings(machineId);

  % targets other than sfun and rtw: custom target or hdl target
  if ~strcmp(targetName, 'rtw') && ~strcmp(targetName, 'sfun') 
    if isLib && ~sf('get',targetId,'.useLocalCustomCodeSettings')
      relevantTargetId = parentTargetId;
    end
    customCodeSettings.relevantTargetId  = relevantTargetId;
    customCodeSettings.customCode        = sf('get',relevantTargetId,'target.customCode');
    customCodeSettings.customSourceCode  = '';
    customCodeSettings.userIncludeDirs   = sf('get',relevantTargetId,'target.userIncludeDirs');
    customCodeSettings.userSources       = sf('get',relevantTargetId,'target.userSources');
    customCodeSettings.userLibraries     = sf('get',relevantTargetId,'target.userLibraries');
    customCodeSettings.reservedNames     = sf('get',relevantTargetId,'target.reservedNames');
    customCodeSettings.customInitializer = sf('get',relevantTargetId,'target.customInitializer');
    customCodeSettings.customTerminator  = sf('get',relevantTargetId,'target.customTerminator');
    customCodeSettings.customUserDefines = '';
    return;
  end

  cs = getActiveConfigSet(machineName);  

  if isLib
    % Change the config set to the main model's config set if we need to pull from the main model
    if isRTW
      useMainModelSettings = strcmp(get_param(cs, 'RTWUseLocalCustomCode'), 'off');
    else
      useMainModelSettings = strcmp(get_param(cs, 'SimUseLocalCustomCode'), 'off');
    end    
    customCodeSettings.isLocalLibrary = ~useMainModelSettings;
    if useMainModelSettings
      relevantTargetId = parentTargetId;
      machineId = sf('get', parentTargetId, 'target.machine');
      cs = getActiveConfigSet(sf('get', machineId, 'machine.name'));
      customCodeSettings.machineId = machineId;
    end
  end
  
  parseCC = strcmp(get_param(cs, 'SimParseCustomCode'), 'on');
  useRTWCustomCodePanel = strcmp(get_param(cs, 'RTWUseSimCustomCode'), 'off');

  customCodeSettings.parseCC = parseCC;
  customCodeSettings.relevantTargetId  = relevantTargetId;

  defaultCCFcnLayoutIsRowMajor = strcmp(get_param(cs, 'DefaultCustomCodeFunctionArrayLayout'), 'Row-major');
  customCodeSettings.defaultCCFcnLayoutIsRowMajor = defaultCCFcnLayoutIsRowMajor;

 if isRTW && useRTWCustomCodePanel
        customCodeSettings.setFromConfigSet(cs, true);
 else
    % sfun target
        customCodeSettings.setFromConfigSet(cs, false);   
 end

