function makeCmdOut = setup_for_lcc(args)
%
% Function: SetupForLcc ========================================================
% Abstract:
%       Configure the build process for Lcc
%
%       This function wraps the raw make command, args.makeCmd (e.g. 'gmake -f
%       model.mk MAT_FILE=1') in a batch (.bat) file that sets up some
%       environment variables needed during the build process.
%

% Copyright 2002-2019 The MathWorks, Inc.
  
  makeCmd        = args.makeCmd;
  modelName      = args.modelName;
  verbose        = args.verbose;
  
  lcc_root = coder.make.internal.parsestrforvar(makeCmd, 'LCC_LOC');
  if isempty(lcc_root)
    if(strcmp(computer,'PCWIN'))
        lcc_root = [matlabroot '\sys\lcc'];
    else
        lcc_root = [matlabroot '\sys\lcc64\lcc64'];
    end
  end

  if ~isempty(lcc_root)
    if(strcmp(computer,'PCWIN'))
        if isempty(dir([lcc_root,'\bin\lcc.exe']))
            DAStudio.error('RTW:compilerConfig:compilerNotFound',...
                                'LCC',lcc_root,'LCC');
        end
    else
        if isempty(dir([lcc_root,'\bin\lcc64.exe']))
            DAStudio.error('RTW:compilerConfig:compilerNotFound',...
                                'LCC',lcc_root,'LCC');
        end        
    end
    
    % if the mexopts struct was not set, try getting a struct for an installed LCC
    % compiler since the TMF is targeted for LCC (that's why this function was
    % called).
    if isempty(args.mexOpts)
        args.mexOpts = coder.make.internal.getMexCompInfoFromKey('LCC-x');
    end
    
    if ~isempty(args.mexOpts)
        if isempty(args.buildInfo.InstallDirmexopts)
            args.buildInfo.InstallDirmexopts = ...
                coder.make.internal.transformPaths(args.mexOpts.comp.MexOpt);
        end
    else % error out if no supported compilers are installed
        DAStudio.error('RTW:buildProcess:noSupportedCompiler');
    end
    
    cmdFile = ['.\',modelName, '.bat'];
    cmdFileFid = fopen(cmdFile,'wt');
    if ~verbose
      fprintf(cmdFileFid, '@echo off\n');
    end
    fprintf(cmdFileFid, 'set MATLAB=%s\n', matlabroot);

    [mapMLR, forceMap,mlrToUse] = coder.make.internal.checkMLRDriveMap(args.buildInfo);
  
    if mapMLR
        cmd = coder.make.internal.getMLRDriveMapCmd(mlrToUse, forceMap);
        fprintf(cmdFileFid, '\n%s\n', cmd); 
    end
    
    fprintf(cmdFileFid, '%s\n', makeCmd );
    fclose(cmdFileFid);
    makeCmdOut = cmdFile;
  else
    if isempty(lcc_root)
        DAStudio.error('RTW:compilerConfig:compilerNotFound',...
                       'LCC',lcc_root,'LCC');
    end
    makeCmdOut = makeCmd;  % No change
  end

%endfunction setup_for_lcc
