summaryrefslogtreecommitdiff
path: root/refreshexecutableunit.pas
blob: 7c046dccdbea5cf8c79cd9ac2f08872fd837a456 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
unit refreshexecutableunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

procedure refreshExecutable(myDir: string);

implementation

uses baseunix, systemunit, process, lowLevelUnit;

procedure refreshExecutable(myDir: string);
var
  tmpDir,output,summeEins: string;
  args:                    array of string;
begin
  output:='';
  tmpDir:=mkTemp('-d /tmp/fpc.XXXXXX');
  mkdir(tmpDir+'/lib');
  setlength(args,15);
  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../units';
  args[11]:='-Fu.';
  args[12]:='-FU'+tmpDir+'/lib';
  args[13]:=myDir+extractFileName(paramstr(0));
  if fileexists(args[13]+'.lpr') then
    args[13]:=args[13]+'.lpr'
  else
    args[13]:=args[13]+'.pas';
  args[14]:='-o'+tmpDir+'/output';
  if runCommandInDir(myDir,'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;
end;

end.