function status = construct_context_for_machine(auxiliaryInfo, isTemporaryMachineContext)

%   Copyright 1995-2020 The MathWorks, Inc.

global gMachineInfo gTargetInfo

status = 0;

if(nargin<1)
    if gTargetInfo.codingHDL
        % HDL uses this to get at the CG::Ctx of the scope thet has already
        % been populated onto the chart.  Otherwise, SF makes a new CG::Ctx,
        % and there would be two of them running around.
        auxiliaryInfo = gMachineInfo.charts;
    else
        auxiliaryInfo = [];
    end
end

if(nargin<2)
    isTemporaryMachineContext = false;
end

ctxInfo.machine = gMachineInfo.machineId;
ctxInfo.relevantMachine = get_relevant_machine;
ctxInfo.eventVariableType = gMachineInfo.eventVariableType;
ctxInfo.eventVariableUsed = false;
ctxInfo.minimumComplexity = coder_options('inlineThreshold');
ctxInfo.maximumComplexity = coder_options('inlineThresholdMax');
ctxInfo.inlineStackLimit = coder_options('inlineStackLimit');
ctxInfo.maxStackUsage = coder_options('maxStackUsage');
ctxInfo.recursionLimit = coder_options('recursionLimit');
ctxInfo.codingNoEcho = gTargetInfo.codingNoEcho;
ctxInfo.codingBLAS = gTargetInfo.codingBLAS;
ctxInfo.codingOpenMP = gTargetInfo.codingOpenMP;
ctxInfo.codingSLCCVariadic = gTargetInfo.codingSLCCVariadic;
ctxInfo.codingIntelIPP = gTargetInfo.codingIntelIPP;
ctxInfo.codingComments = gTargetInfo.codingComments;
ctxInfo.codingAutoComments = gTargetInfo.codingAutoComments;
ctxInfo.codingEmitObjectDescriptions = gTargetInfo.codingEmitObjectDescriptions;
ctxInfo.codingEmitTestSequenceDescriptions = gTargetInfo.codingEmitTestSequenceDescriptions;
ctxInfo.codingEmitObjectRequirements = gTargetInfo.codingEmitObjectRequirements;
ctxInfo.codingSFunction = gTargetInfo.codingSFunction;
ctxInfo.codingRTW = gTargetInfo.codingRTW;
ctxInfo.codingIRConstructionForRTW = gTargetInfo.codingIRConstructionForRTW;
ctxInfo.codingHDL = gTargetInfo.codingHDL;
ctxInfo.codingPLC = gTargetInfo.codingPLC;
ctxInfo.codingPLCTargetIDE = gTargetInfo.codingPLCTargetIDE;
ctxInfo.leavingIntegrityChecks = gTargetInfo.leavingIntegrityChecks;
ctxInfo.leavingExtrinsicCalls = gTargetInfo.leavingExtrinsicCalls;
ctxInfo.leavingCtrlCChecks = gTargetInfo.leavingCtrlCChecks;
ctxInfo.genImportedTypeDefs = gTargetInfo.genImportedTypeDefs;
ctxInfo.modelReferenceSimTarget = gTargetInfo.modelReferenceSimTarget;
ctxInfo.modelReferenceRTWTarget = gTargetInfo.modelReferenceRTWTarget;
ctxInfo.rtwMultiInstancedERT = gTargetInfo.isErtMultiInstanced;
ctxInfo.codingDebugForMachine = gTargetInfo.codingDebug;
ctxInfo.codingLegacyDebug = gTargetInfo.codingLegacyDebug;
ctxInfo.codingCoverage = gTargetInfo.codingCoverage;
ctxInfo.codingForAutoVerifier = gTargetInfo.codingForAutoVerifier;
ctxInfo.codingFasterRun = gTargetInfo.codingFasterRun;
ctxInfo.codingSLCI = gTargetInfo.codingSLCI;
ctxInfo.shouldCacheInferenceReportOnJITEngine = 0;

if ctxInfo.codingForAutoVerifier == 1

    % If SLDV is being invoked, copy the flags regarding analysis options
    % from gTargetInfo. These flags will be used to initialize
    % CdrCtxInfo->ctx.fSldvOptions in cdr/cdr_semantics_vars.cpp

    ctxInfo.DVoptMode = gTargetInfo.DVoptMode;
    ctxInfo.DVoptMinmaxConstr = gTargetInfo.DVoptMinmaxConstr;
    if ctxInfo.DVoptMode == 1
        ctxInfo.DVoptCoverageObj = gTargetInfo.DVoptCoverageObj;
        ctxInfo.DVoptRelBoundary = gTargetInfo.DVoptRelBoundary;
    elseif ctxInfo.DVoptMode == 2
        ctxInfo.DVoptDeadLogic = gTargetInfo.DVoptDeadLogic;
        ctxInfo.DVoptActiveLogic = gTargetInfo.DVoptActiveLogic;
        ctxInfo.DVoptDivByZero = gTargetInfo.DVoptDivByZero;
        ctxInfo.DVoptIntOvf = gTargetInfo.DVoptIntOvf;
        ctxInfo.DVoptMinmaxCheck = gTargetInfo.DVoptMinmaxCheck;
        ctxInfo.DVoptOutOfBound = gTargetInfo.DVoptOutOfBound;
        ctxInfo.DVoptDetectDSMAccessViolations = gTargetInfo.DVoptDetectDSMAccessViolations;
        ctxInfo.DVoptFloatInf = gTargetInfo.DVoptFloatInf;
        ctxInfo.DVoptFloatNaN = gTargetInfo.DVoptFloatNaN;
        ctxInfo.DVoptFloatSubnormal = gTargetInfo.DVoptFloatSubnormal;
        ctxInfo.DVoptBlockCondition = gTargetInfo.DVoptBlockCondition;
    end
end

ctxInfo.codingForRangeAnalysis = gTargetInfo.codingForRangeAnalysis;
ctxInfo.codingOverflow = gTargetInfo.codingOverflow;
ctxInfo.codingSaturate = gTargetInfo.codingSaturate;
ctxInfo.codingTelemetry = 0;
ctxInfo.codingStateBitsets = gTargetInfo.codingStateBitsets;
ctxInfo.codingDataBitsets = gTargetInfo.codingDataBitsets;
ctxInfo.dataflowAnalysisThreshold = coder_options('dataflowAnalysisThreshold');
ctxInfo.machineNumberVarName = gMachineInfo.machineNumberVariableName;
ctxInfo.codingLogicalOps = gTargetInfo.codingLogicalOps;
ctxInfo.codingElseIfDetection = gTargetInfo.codingElseIfDetection;
ctxInfo.codingConstantFolding = gTargetInfo.codingConstantFolding;
ctxInfo.codingRedundantElim = gTargetInfo.codingRedundantElim;
ctxInfo.codingGlobalIO = gTargetInfo.codingGlobalIO;
ctxInfo.codingPackedIO = gTargetInfo.codingPackedIO;
ctxInfo.codingPreserveConstantNames = gTargetInfo.codingPreserveConstantNames;
ctxInfo.codingIntegerCodeOnly = gTargetInfo.codingIntegerCodeOnly;
ctxInfo.codingRealCodeOnly = gTargetInfo.codingRealCodeOnly;
ctxInfo.maximumIdentifierLength = gTargetInfo.maximumIdentifierLength;
ctxInfo.dynamicStringBufferSize = gTargetInfo.dynamicStringBufferSize;
ctxInfo.parenthesesLevel = gTargetInfo.parenthesesLevel;
ctxInfo.codingExtMode = gTargetInfo.codingExtMode;
ctxInfo.codingRapidAccel = ~strcmpi(get_param(gMachineInfo.mainMachineName, 'RapidAcceleratorSimStatus'), 'inactive');
ctxInfo.xpcExtModeAnimation = gTargetInfo.xpcExtModeAnimation;
ctxInfo.ignoreTestpoints = gTargetInfo.ignoreTestpoints;
ctxInfo.codingBlockComments = gTargetInfo.codingBlockComments;
ctxInfo.isBlockPathComment = gTargetInfo.isBlockPathComment;
ctxInfo.codingMATLABSourceComments = gTargetInfo.codingMATLABSourceComments;
ctxInfo.codingMATLABFcnDesc = gTargetInfo.codingMATLABFcnDesc;
ctxInfo.codingMemcpy = gTargetInfo.codingMemcpy;
ctxInfo.codingMemsetDouble = gTargetInfo.codingMemsetDouble;
ctxInfo.codingMemcpyThreshold = gTargetInfo.codingMemcpyThreshold;
ctxInfo.codingConvertIfToSwitch = gTargetInfo.codingConvertIfToSwitch;
ctxInfo.codingSupressUnreachableDefaultCases = gTargetInfo.codingSupressUnreachableDefaultCases;
ctxInfo.codingUseSpecifiedMinMax = gTargetInfo.codingUseSpecifiedMinMax;
ctxInfo.isTemporaryMachineContext = isTemporaryMachineContext;
ctxInfo.codingROWMajor = gTargetInfo.codingROWMajor;
ctxInfo.codingPreserveStateflowLocalDataDimensions = gTargetInfo.codingPreserveStateflowLocalDataDimensions;
ctxInfo.codingAUTOSAR = gTargetInfo.codingAUTOSAR;
ctxInfo.layoutIsRowMajor = gTargetInfo.layoutIsRowMajor;
ctxInfo.systemTargetIsAUTOSAR = gTargetInfo.systemTargetIsAUTOSAR;
ctxInfo.codingRapidAccel = gTargetInfo.codingRapidAccel;
ctxInfo.codingSIL = gTargetInfo.codingSIL;
ctxInfo.codingPIL = gTargetInfo.codingPIL;
ctxInfo.codingSLRT = gTargetInfo.codingSLRT;
ctxInfo.codingSVDPI = gTargetInfo.codingSVDPI;

[algorithmWordsizes,targetWordsizes,algorithmHwInfo,targetHwInfo,rtwSettingsInfo,algorithmSizeInfo,targetSizeInfo] = get_word_sizes_local;

ctxInfo.algorithmWordsizes = algorithmWordsizes;
ctxInfo.targetWordsizes = targetWordsizes;

ctxInfo.algorithmHwDeviceType                 = algorithmHwInfo.hwDeviceType;
ctxInfo.algorithmDivByZeroProtectionNotWanted = algorithmHwInfo.divByZeroProtectionNotWanted;
ctxInfo.algorithmSignedDivRounding            = algorithmHwInfo.signedDivRounding;
ctxInfo.algorithmSignedShiftIsArithmetic      = algorithmHwInfo.signedShiftIsArithmetic;
ctxInfo.algorithmEnableLongLong               = algorithmHwInfo.enableLongLong;

ctxInfo.targetHwDeviceType                    = targetHwInfo.hwDeviceType;
ctxInfo.targetDivByZeroProtectionNotWanted    = targetHwInfo.divByZeroProtectionNotWanted;
ctxInfo.targetSignedDivRounding               = targetHwInfo.signedDivRounding;
ctxInfo.targetSignedShiftIsArithmetic         = targetHwInfo.signedShiftIsArithmetic;
ctxInfo.targetEnableLongLong                  = targetHwInfo.enableLongLong;

ctxInfo.castFloat2IntPortableWrapping = rtwSettingsInfo.castFloat2IntPortableWrapping;
ctxInfo.mapNaN2IntZero                = rtwSettingsInfo.mapNaN2IntZero;
ctxInfo.genFunctionFixptDiv           = rtwSettingsInfo.genFunctionFixptDiv;
ctxInfo.genFunctionFixptMul           = rtwSettingsInfo.genFunctionFixptMul;
ctxInfo.genFunctionFixptMisc          = rtwSettingsInfo.genFunctionFixptMisc;
ctxInfo.supportNonFinites             = rtwSettingsInfo.supportNonFinites;
ctxInfo.correctNetSlopeViaDiv         = rtwSettingsInfo.correctNetSlopeViaDiv;
ctxInfo.useFloatMulNetSlope           = rtwSettingsInfo.useFloatMulNetSlope;

ctxInfo.algorithmSizeInfo = algorithmSizeInfo;
ctxInfo.targetSizeInfo = targetSizeInfo;

ctxInfo.codingSupportVariableSizeSignals = gTargetInfo.codingSupportVariableSizeSignals;
 
ctxInfo.auxiliaryInfo = auxiliaryInfo;

if(gTargetInfo.codingRTW)
    ctxInfo.usedTargetFunctionLibH = get_param(gMachineInfo.mainMachineName, 'TargetFcnLibHandle');
elseif gTargetInfo.codingSFunction
    ctxInfo.usedTargetFunctionLibH = get_param(gMachineInfo.mainMachineName, 'SimTargetFcnLibHandle');
else
    ctxInfo.usedTargetFunctionLibH = 0;
end

targetId = sf('get', ctxInfo.machine, '.activeTarget'); 

customCodeSettings = sfc('private','get_custom_code_settings',targetId, gTargetInfo.parentTarget);
ctxInfo.parseCustomCode = customCodeSettings.parseCC;
ctxInfo.defaultCCFcnLayoutIsRowMajor = customCodeSettings.defaultCCFcnLayoutIsRowMajor;

if gTargetInfo.codingSFunction
    ctxInfo.shouldCacheInferenceReportOnJITEngine = Stateflow.JIT.Utils.cacheInferenceReportOnJITEngine;
end

ctxInfo.codingGPU = gTargetInfo.codingGPU;
if gTargetInfo.codingGPU
    ctxInfo.gpuMallocThreshold         = gTargetInfo.gpuMallocThreshold;
    ctxInfo.gpuStackLimitPerThread     = gTargetInfo.gpuStackLimitPerThread;
    ctxInfo.gpuErrorChecks             = gTargetInfo.gpuErrorChecks;
    ctxInfo.gpuCustomComputeCapability = gTargetInfo.gpuCustomComputeCapability;
    ctxInfo.gpuCompilerFlags           = gTargetInfo.gpuCompilerFlags;
    ctxInfo.gpuComputeCapability       = gTargetInfo.gpuComputeCapability;
end
ctxInfo.dlTargetLib         = gTargetInfo.dlTargetLib;
ctxInfo.dlAutoTuning        = gTargetInfo.dlAutoTuning;

ctxInfo.gencpp = gTargetInfo.gencpp;
if gTargetInfo.gencpp
    ctxInfo.fileNameInfo = gTargetInfo.fileNameInfo;
    ctxInfo.buildInfo = gTargetInfo.buildInfo;
end

gMachineInfo.ctxInfo = ctxInfo;

try
    sf('Cg','construct_context',ctxInfo);
catch ME
    sfprivate('coder_error_count_man','add',1);
    rethrow(ME);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [algorithmWordsizes,targetWordsizes,algorithmHwInfo,targetHwInfo,rtwSettingsInfo,algorithmSizeInfo,targetSizeInfo] = get_word_sizes_local

global gMachineInfo

relevantMachineName = sf('get',get_relevant_machine,'machine.name');

[algorithmWordsizes,targetWordsizes,algorithmHwInfo,targetHwInfo,rtwSettingsInfo,algorithmSizeInfo,targetSizeInfo] = get_word_sizes(relevantMachineName,gMachineInfo.targetName);

