function oString = setup_for_visual(args)
%
% Function: setup_for_visual =================================================
% 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-2021 The MathWorks, Inc.

  % If the mexopts struct was not set, then it could be because the
  % TemplateMakefile param was set explicitly.  Since the
  % template makefile is targeted for MSVC (that's why this function was called), 
  % obtain a struct for an installed Microsoft compiler.
  if isempty(args.mexOpts)
      args.mexOpts = coder.make.internal.getMexCompilerInfo(...
                                'manufacturer','Microsoft');
  end
  
  % If the mexOpts struct is set, and it's in the 'new style' compiler
  % support, then call the new bat file setup
  if ~isempty(args.mexOpts)
      oString = locWriteBatFile(args);
  else
      error(message('RTW:buildProcess:noSupportedCompiler'));
  end
  
  % Set up template makefile token for compiler version
  if isempty(findTMFToken(args.buildInfo, '|>VISUAL_VER<|'))
      args.buildInfo.addTMFTokens('|>VISUAL_VER<|', args.mexOpts.verStr, ...
                                  'COMPILER_VERSION');
  end
  


%=============================================================================
% Function: locWriteBatFile 
%=============================================================================
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,'wt');
      fprintf(cmdFileFid, '%s', content);
      fclose(cmdFileFid);
  end


