function [tc, results] = ibis_ami_ms_vc
%ibis_ami_ms_vc Specifications for "Microsoft Visual C++" toolchain.

% Copyright 2011-2023 The MathWorks, Inc.

% Get existing ms_vc core toolchain
toolchainDir = fullfile(matlabroot, 'toolbox', 'coder', 'compile', 'tools');
addpath(toolchainDir);
[tc, results] = ms_vc;

% Customize toolchain for IBIS-AMI
for ndx = 1:3
    if ~results(ndx).error
        %% Modify generic toolchain for IBIS-AMI
        % Update flags for Macros
        tc(ndx).setMacro('CPPFLAGS_ADDITIONAL', '-EHs -D_CRT_SECURE_NO_WARNINGS');
        % Set Executable extension for C linker
        tool = tc(ndx).getBuildTool('C Linker');
        tool.setFileExtension('Executable', '.dll');
        % Set Executable extension for C++ linker
        tool = tc(ndx).getBuildTool('C++ Linker');
        tool.setFileExtension('Executable', '.dll');
        % Limit Executable extensions for C++ compiler
        tool = tc(ndx).getBuildTool('C++ Compiler');
        tool.setInputFileExtensions({'Source'});

        % Get the debug flag per build tool
        debugFlag.Linker      = getDebugFlag('Linker');
        %debugFlag.CppLinker   = getDebugFlag('C++ Linker');
        debugFlag.CppLinker   = '$(LDDEBUG)';
    
        %% Modify Baseline Build options
        linkerOpts       = {'$(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll'};
        sharedLinkerOpts = {'$(ldebug) $(conflags) $(LIBS_TOOLCHAIN) -dll  -def:$(DEF_FILE)'};

        %% Set flags for "Faster Runs" Build Configuration
        cfg = tc(ndx).getBuildConfiguration('Faster Runs');
        % Set CPP_LDFLAGS
        cfg.setOption('C++ Linker', linkerOpts);
        % Set CPP_SHAREDLIB_LDFLAGS
        cfg.setOption('C++ Shared Library Linker', sharedLinkerOpts);
        % Set LDFLAGS
        cfg.setOption('Linker', linkerOpts);
        % Set SHAREDLIB_LDFLAGS
        cfg.setOption('Shared Library Linker', sharedLinkerOpts);
    
        %% Set flags for "Faster Builds" Build Configuration
        cfg = tc(ndx).getBuildConfiguration('Faster Builds');
        % Set CPP_LDFLAGS
        cfg.setOption('C++ Linker', linkerOpts);
        % Set CPP_SHAREDLIB_LDFLAGS
        cfg.setOption('C++ Shared Library Linker', sharedLinkerOpts);
        % Set LDFLAGS
        cfg.setOption('Linker', linkerOpts);
        % Set SHAREDLIB_LDFLAGS
        cfg.setOption('Shared Library Linker', sharedLinkerOpts);
    
        %% Set flags for "Debug" Build Configuration
        cfg = tc(ndx).getBuildConfiguration('Debug');
        % Set CPP_LDFLAGS
        cfg.setOption('C++ Linker', horzcat(linkerOpts, debugFlag.CppLinker));
        % Set CPP_SHAREDLIB_LDFLAGS
        cfg.setOption('C++ Shared Library Linker', horzcat(sharedLinkerOpts, debugFlag.CppLinker));
        % Set LDFLAGS
        cfg.setOption('Linker', horzcat(linkerOpts, debugFlag.Linker));
        % Set SHAREDLIB_LDFLAGS
        cfg.setOption('Shared Library Linker', horzcat(sharedLinkerOpts, debugFlag.Linker));
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Helper Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    function flag = getDebugFlag(toolkey)
        flag = tc(ndx).getBuildTool(toolkey).Directives.getValue('Debug').getRef();
    end

end
% LocalWords:  toolchain nmake msvc env SDK vcvarsall vsvars vcvars microsoft
% LocalWords:  LDFLAGS COMNTOOLS vcx tmf Compat msvcver OUTDIR pdb mssdk ntwin
% LocalWords:  mak MDFLAG EXTERNLIB NODEBUG cvarsdll cvarsmt conlibsmt ldebug
% LocalWords:  conflags cflags aspx msdn CVARSFLAG
