Skip to content
Snippets Groups Projects
writegitinfoheader.m 4.07 KiB
function writegitinfoheader()
   % max code description string length
   maxdesclen=1024;

   % scd expcode info retrieval
   try
       scd=Simulink.data.evalinGlobal('tcv','scd');
       expcode = scd.expcode;
       expcodeset=true;
   catch ME
       warning('No configured expcode found, code description string will not have this info.');
       expcodeset=false;
   end
  
   % Git checks 
   
   % 1) check repo cleanlyness
   % TODO: check that the output of 'git status -procelain' is empty
   [~,result] = system('git status --porcelain');
   if ~isempty(result)
       error('the working copy is not clean, please commit changes first.');
   end
   
   % 2) check local and remote hash are matching (in synch working copy)
   [~,localhash]  = system('git rev-parse @');
   [~,remotehash] = system('git rev-parse @{u}');
   if ~strcmp(localhash, remotehash)
       error('the working copy is not in sync with its origin, please push first.');
   end
   
   % first we get TCV current shot number, to be used for tagging
   mdsconnect('tcvdata');
   curshot=mdsvalue('current_shot("tcv_shot")');
   
   % then we try to tag the current rtccode repo 
      
   tagname = ['builds/TCV' num2str(curshot)];
   str=sprintf('Going to try this git tag: %s [(Y)es/(N)o/A(p)pend]?',tagname);
   answ=input(str,'s'); 
   switch upper(answ)
       case 'P'
            nameapp = input('Type what to append to tag name: ','s');
            tagname = [tagname nameapp];
       case 'Y'
       otherwise
           return
   end
   
   tagmsg = input('Type git tag message here: ','s');
   tagcmd = ['git tag -f ' tagname ' -m "' tagmsg '"'];
   
   answ = input('Ok to tag [(Y)es/(N)o]?','s');
   switch upper(answ)
       case 'Y'
       otherwise
            return
   end

   [status,result] = system(tagcmd);       
   
   if status~=0
      fprintf('could not tag, git error: \n%s\n', result);
      return
   end
   
   taginfocmd = ['git for-each-ref refs/tags/' tagname ' --format="RTCCODE TAG HASH: %(objectname:short) CONTENTS: %(contents:subject) TAGGER: %(taggername) %(taggerdate) FULL TAG INFO: %(objecttype) %(refname) %(objectname)"'];
   [status,result] = system(taginfocmd); 
   
   if status~=0
      fprintf('could not retrieve tag info, git error: \n%s\n', result);
      return
   end
   
   descstr=result;

   if expcodeset
       descstr = ['EXPCODE: ' num2str(expcode) ' ' descstr];
   else
       descstr = ['EXPCODE: n/a ' descstr];
   end
   
   if numel(descstr)>maxdesclen
       descstr=descstr(1:numel(descstr));
       fprintf('WARNING: code description string truncated!\n');
   end
   
   fprintf('Code decription string will be set to:\n%s\n', descstr);
      
   [path,~,~]=fileparts(mfilename('fullpath'));
   
   fd=fopen([path '/scdalgoinfo.h'],'w');
   fprintf(fd, '/* scdalgoinfo.h */\n');
   fprintf(fd, '/* generated on: %s */\n',char(datetime));
   fprintf(fd, '\n');
   fprintf(fd, '#ifndef SCDALGOINFO\n');
   fprintf(fd, '#define SCDALGOINFO\n');
   fprintf(fd, ['#define ALGOINFOSTR "' descstr(1:end-1) '"\n']);
   fprintf(fd, ['#define ALGOINFOLEN ' num2str(numel(result)) '\n']);
   fprintf(fd, ['#define ALGOINFOMAXLEN ' num2str(maxdesclen) '\n']);
   fprintf(fd, '\n');
   fprintf(fd, 'struct algoinfo {\n');
   fprintf(fd, ' char text[ALGOINFOMAXLEN];\n');
   fprintf(fd, ' unsigned int len;\n');
   fprintf(fd, '} __attribute__((packed));\n');
   fprintf(fd, '\n');
   fprintf(fd, '#endif\n');
   
   fclose(fd);
   
   % clean build folders, the generated .h is not
   % checked for changes by the standard generated makefile
   % it is enough to remove the primary model build folder
   % (i.e. rm -rf SCD_rtccode_xx_xx*) so that submodels builds
   % can be kept 
   
   try
      cfg=Simulink.fileGenControl('getConfig');
      codegenfolder=cfg.CodeGenFolder;
      cleancmd = ['rm -f ' codegenfolder '/*.so'];
      [status,result]  = system(cleancmd);
      
      if result~=0
          warning('Build folder could not be cleaned properly with the command: %s',cleancmd);
          status
      end
   catch ME
       warning('Exception cleaning expcode build folder %s', codegenfolder);
   end
      
end