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.XXXXXX'); mkdir(tmpDir+'/lib'); setlength(args,13+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'+tmpDir+'/lib'; args[11]:=dirs[0]+extractFileName(paramstr(0)); if fileexists(args[11]+'.lpr') then args[11]:=args[11]+'.lpr' else args[11]:=args[11]+'.pas'; args[12]:='-o'+tmpDir+'/output'; for i:=0 to length(dirs)-1 do args[13+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.