summaryrefslogtreecommitdiff
path: root/refreshexecutableunit.pas
blob: 272c10807ba3b7b99aa90699c8344b76d05be5db (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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/share/lazarus/lcl/units/x86_64-linux/gtk2';
  args[11]:='-Fu/usr/share/lazarus/lcl/units/x86_64-linux';
  args[12]:='-Fu/usr/share/lazarus/components/lazutils/lib/x86_64-linux';
  args[13]:='-Fu/usr/share/lazarus/packager/units/x86_64-linux';
  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.