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-2020 The MathWorks, Inc.
% 

  if isfield(args, 'EnvVarSuggestions')

      locErrorNoCompiler();
      return;
  end
    
  % first check for DEVSTUDIO_LOC on the make command-------------------
  DSLoc = coder.make.internal.parsestrforvar(args.makeCmd, 'DEVSTUDIO_LOC');
  if ~isempty(DSLoc)
      args.mexOpts =  coder.make.internal.getMexCompilerInfo('location',DSLoc);
  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
  
  % Set up mex options location
  if isempty(args.buildInfo.InstallDirmexopts)
      args.buildInfo.InstallDirmexopts = msvcver.mexopts;
  end

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


  % some versions, notably the express versions don't always ship with the
  % vcvars bat file.  in that case we can generate a local mexopts into the
  % build folder and use that instead.
  %
  % if the Location of the compiler is in a known Shared folder, then this 
  % is an installed toolset which the vcvars batfile does not work properly
  % for.
  if isempty(args.mexOpts.batFileLoc) || ~isempty(regexp(args.mexOpts.comp.Location,'Microsoft Visual Studio[\\/]shared','ignorecase'))
      vcver = lower(args.mexOpts.comp.ShortName);
      batFileName = ['setup_' vcver '.bat'];

       batFileStr = args.mexOpts.comp.Details.SetEnv;
       
       if strcmp(args.mexOpts.batFileArgs,'x86')
           % the env setup string needs some additional massaging to work
           % right for 32 bit binary generation
           batFileStr = regexprep(batFileStr,'bin[\\/]amd64','bin','ignorecase');
           batFileStr = regexprep(batFileStr,'lib[\\/]amd64','lib','ignorecase');

           batFileStr = strrep(batFileStr,'amd64','x86');
           batFileStr = strrep(batFileStr,'x64','x86');
       end
       
       fid = fopen( batFileName, 'w+' );
       fprintf(fid, '%s', batFileStr);
       fclose( fid );
      vcvars = batFileName;
  else
      vcvars = strtrim(args.mexOpts.comp.Details.CommandLineShell);
      if (isempty(vcvars) == 0 && (vcvars(1) ~= '"' || vcvars(end) ~= '"'))
          vcvars = ['"' vcvars '"'];
      end
      vcvars = [vcvars ' ' args.mexOpts.batFileArgs];

  end

  % Create command file content
  content = coder.make.internal.getVisualCommandFileContent(args, vcvars);
  
  % Write out command file
  cmdFile = ['.\',args.modelName, '.bat'];
  cmdFileFid = fopen(cmdFile,'wt');
  fprintf(cmdFileFid, '%s', content);
  fclose(cmdFileFid);
  oString = cmdFile;

return;
%End of Function locWriteBatFile

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

DAStudio.error('RTW:buildProcess:noSupportedCompiler');

%End of Function locErrorNoCompiler

% LocalWords:  vcver toolset batfile ignorecase VSCMD
