function oString = setup_for_visual_x64(args)
%
% Function: setup_for_visual_x64 ==============================================
% Abstract:
%
%       Configure the build process for Visual C++ for the x64 platform
%
%       When configuring the build process for Visual, this function wraps the
%       raw make command, args.makeCmd (e.g. 'nmake -f model.mk MAT_FILE=1') in
%       a batch (.bat) file that sets up some environment variables needed
%       during the build process.
%-------------------------------------------------------------------------------
%

% Copyright 1994-2022 The MathWorks, Inc.
% 

  if isfield(args, 'EnvVarSuggestions')

      locErrorNoCompiler();
      return;
  end
  
  % if the mexopts struct was not set, then it could be because the
  % TemplateMakefile param was explicitly set to a specific TMF (trtwmakecfg).
  % Since the TMF is targeted for MSVC (that's why this function was called), 
  % try getting a struct for an installed Microsoft compiler.
  if isempty(args.mexOpts)
      args.mexOpts = coder.make.internal.getMexCompilerInfo(...
                                'manufacturer','Microsoft');
  end
  
  if ~isempty(args.mexOpts)
      oString = locWriteBatFile(args);

      msvcver.mexopts = ['"' args.mexOpts.comp.MexOpt '"'];
      msvcver.onesVer = args.mexOpts.verStr;
  

  else
      locErrorNoCompiler();
      return;
  end
  
  % Set up TMF token for compiler version
  if isempty(findTMFToken(args.buildInfo, '|>VISUAL_VER<|'))
      args.buildInfo.addTMFTokens('|>VISUAL_VER<|',...
                             msvcver.onesVer,...
                             'COMPILER_VERSION');
  end

%endfunction setup_for_visual_x64
    
%=============================================================================
% Function: locWriteBatFile 
%
% inputs:
%    comp
%
%
% returns:
%
%
%=============================================================================
function cmdFile = locWriteBatFile(args)

% Name of the .bat file
setupFile = coder.make.internal.msvcSetupFileName;
setupFile = fullfile(pwd, setupFile);

% Write out the setup file
coder.make.internal.writeMsvcSetup(setupFile, args.mexOpts.comp.Details);

% Create command file content
content = coder.make.internal.getVisualCommandFileContent(args);

% Write out command file
cmdFile = ['.\',args.modelName, '.bat'];
if coder.make.internal.needToUpdateFile(cmdFile, content)
    cmdFileFid = fopen(cmdFile,'w');
    fprintf(cmdFileFid, '%s', content);
    fclose(cmdFileFid);
end


%=============================================================================
% Function: locErrorNoCompiler 
%
% inputs:
%
%
% returns:
%    oString
%
%
%=============================================================================
function locErrorNoCompiler()

error(message('RTW:buildProcess:noSupportedCompiler'));

%End of Function locErrorNoCompiler

% LocalWords:  vcver toolset batfile ignorecase VSCMD
