unit refreshexecutableunit; {$mode objfpc}{$H+} interface uses Classes, SysUtils, lowlevelunit; procedure refreshExecutable(dirs: tStringArray); // dirs[0] = Verzeichnis des Hauptprogramms, dirs[1] = Verzeichnis dieser Unit function sourceSha512Sum(dir: string): string; function binarySha512Sum(index: longint): string; inline; implementation uses baseunix, systemunit, process; {$INCLUDE refreshexecutableunit_sums.inc} procedure refreshExecutable(dirs: tStringArray); var tmpDir,output,summeEins: string; args: tStringArray; i: longint; f: textFile; begin output:=''; fileMode:=fmOpenWrite; assignFile(f,dirs[1]+'refreshexecutableunit_sums.inc'); rewrite(f); writeln(f,'const'); writeln(f,' sha512sums: array[0..1] of string = ('); for i:=0 to length(dirs)-1 do begin write(f,' '''+sourceSha512Sum(dirs[i])+''''); if i=length(dirs)-1 then writeln(f) else writeln(f,','); end; writeln(f,' );'); closeFile(f); tmpDir:=mkTemp('-d /tmp/fpc.'+extractFileName(paramstr(0))+'.XXXXXX'); mkdir(tmpDir+'/lib'); setLength(args,18+length(dirs)); args[0]:='-MObjFPC'; args[1]:='-Scghi'; args[2]:='-Cg'; args[3]:='-CirotR'; args[4]:='-O3'; args[5]:='-g'; args[6]:='-gl'; args[7]:='-l'; args[8]:='-vewnhibq'; args[9]:='-Fi'+tmpDir+'/lib'; args[10]:='-Fu/usr/lib/lazarus/lcl'; args[11]:='-Fu/usr/lib/lazarus/components/*'; args[12]:='-Fu/usr/lib/lazarus/lcl/*'; args[13]:='-Fi/usr/lib/lazarus/lcl/include'; args[14]:='-Fl/opt/gnome/lib'; args[15]:='-FU'+tmpDir+'/lib'; args[16]:=dirs[0]+extractFileName(paramstr(0)); if fileExists(args[16]+'.lpr') then args[16]:=args[16]+'.lpr' else args[16]:=args[16]+'.pas'; args[17]:='-o'+tmpDir+'/output'; for i:=0 to length(dirs)-1 do args[18+i]:='-Fu'+dirs[i]; if runCommandInDir(dirs[0],'fpc',args,output) then begin setLength(args,2); args[0]:=tmpDir+'/output'; args[1]:=paramstr(0); if runCommand('sha512sum',args,output) then begin summeEins:=erstesArgument(output,#10); if erstesArgument(summeEins)<>erstesArgument(output) then begin deleteFile(paramstr(0)); if runCommand('mv',args,output) then begin setLength(args,2); args[0]:='-rf'; args[1]:=tmpDir; if runCommand('rm',args,output) then begin fpExecVe(paramstr(0),argv,envp); raise exception.create('Fehler beim Ersetzen der Executable!'); end; end; end; end; end; if (tmpDir<>'') and directoryexists(tmpDir) then begin setLength(args,2); args[0]:='-rf'; args[1]:=tmpDir; runCommand('rm',args,output); end; setLength(args,0); end; function sourceSha512Sum(dir: string): string; var args: tStringArray; begin result:=''; setLength(args,3); args[0]:='describe'; args[1]:='--always'; args[2]:='--abbrev=0'; if runCommandInDir(dir,'git',args,result) then result:=trim(result) else result:='ungültig'; end; function binarySha512Sum(index: longint): string; begin result:=sha512sums[index]; end; end.