Source

fpcup / installermanager.pas

The default branch has multiple heads

Full commit
  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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
unit installerManager;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils,installerCore,installerFpc,installerLazarus,installerHelp,installerUniversal,fpcuputil,fileutil
  {$ifdef linux}
  ,dynlibs
  {$endif linux}
  ;

Const
  Sequences=
//default sequence
    'Declare default;'+
    {$ifdef linux}
    'Exec CheckDevLibs;'+
    {$endif linux}
    'Exec CreateFpcupScript;'+
    'Do fpc;'+
    //Get bigide so we at least have a compiler:
    'Do BIGIDE;'+
    'Exec CreateLazarusScript;'+
    'Do helplazarus;'+
    'Do LAZDATADESKTOP;'+
    'Do DOCEDITOR;'+
    //Get external packages/universal modules
    'Do UniversalDefault;'+
    //Recompile user IDE so any packages selected by the
    //universal installer are compiled into the IDE:
    'Do USERIDE;'+
    'End;'+
//default sequence for win32
    'Declare defaultwin32;'+
    'Exec CreateFpcupScript;'+
    'Do fpc;'+
    //Get bigide so we at least have a compiler:
    'Do BIGIDE;'+
    'Exec CreateLazarusScript;'+
    'Do helplazarus;'+
    'Do LAZDATADESKTOP;'+
    'Do DOCEDITOR;'+
    //Get external packages/universal modules
    'Do UniversalDefault;'+
    //Recompile user IDE so any packages selected by the
    //universal installer are compiled into the IDE:
    'Do USERIDE;'+
    'Do crosswin32-64;'+  //this has to be the last. All TExecState reset!
    'End;'+
//cross sequence for win32. Note: if changing this name,
    //also change checks for this in skipmodules etc.
    'Declare crosswin32-64;'+
    'SetCPU x86_64;'+
    'SetOS win64;'+
    'Cleanmodule fpc;'+
    'Buildmodule fpc;'+
    //Getmodule has already been done
    'Cleanmodule LCL;'+
    'Buildmodule LCL;'+
    'End;'+
//default clean sequence
    'Declare defaultclean;'+
    'Do fpcclean;'+
    'Do lazarusclean;'+
    'Do helplazarusclean;'+
    'CleanModule LAZDATADESKTOP;'+
    'CleanModule DOCEDITOR;'+
    'Do UniversalDefaultClean;'+
    'End;'+
//default clean sequence for win32
    'Declare defaultwin32clean;'+
    'Do fpcclean;'+
    'Do lazarusclean;'+
    'Do helplazarusclean;'+
    'CleanModule LAZDATADESKTOP;'+
    'CleanModule DOCEDITOR;'+
    'Do UniversalDefaultClean;'+
    'Do crosswin32-64Clean;'+   //this has to be the last. All TExecState reset!
    'End;'+
//default cross clean sequence for win32
    'Declare crosswin32-64Clean;'+
    'SetCPU x86_64;'+
    'SetOS win64;'+
    'Cleanmodule fpc;'+
    'Cleanmodule lazarus;'+
    'End;'+

//default uninstall sequence
    'Declare defaultuninstall;'+
    'Do fpcuninstall;'+
    'Do lazarusuninstall;'+
    'Do helpuninstall;'+
    'UninstallModule LAZDATADESKTOP;'+
    'UninstallModule DOCEDITOR;'+
    'Do UniversalDefaultUnInstall;'+
    'End;'+
//default uninstall sequence for win32
    'Declare defaultwin32uninstall;'+
    'Do defaultuninstall;'+
    'End;';

type
  TSequencer=class; //forward


  { TFPCupManager }

  TFPCupManager=class(Tobject)
  private
    FAllOptions: string;
    FBootstrapCompiler: string;
    FBootstrapCompilerDirectory: string;
    FBootstrapCompilerURL: string;
    FClean: boolean;
    FCompilerName: string;
    FConfigFile: string;
    FCrossCPU_Target: string;
    FCrossLCL_Platform: string;
    FCrossOS_Target: string;
    FFPCDesiredRevision: string;
    FFPCDirectory: string;
    FFPCOPT: string;
    FFPCURL: string;
    FKeepLocalDiffs: boolean;
    FLazarusDesiredRevision: string;
    FLazarusDirectory: string;
    FLazarusOPT: string;
    FLazarusPrimaryConfigPath: string;
    FLazarusURL: string;
    FMakeDirectory: string;
    FOnlyModules: string;
    FShortCutName: string;
    FShortCutNameFpcup: string;
    FSkipModules: string;
    FUninstall:boolean;
    FVerbose: boolean;
    Sequencer: TSequencer;
    function GetLazarusPrimaryConfigPath: string;
    procedure SetBootstrapCompilerDirectory(AValue: string);
    procedure SetFPCDirectory(AValue: string);
    procedure SetLazarusDirectory(AValue: string);
    procedure SetMakeDirectory(AValue: string);
  protected
    LogFile:Text;
    VerboseLog:Text;
    FModuleList:TStringList;
    FModuleEnabledList:TStringList;
    FModulePublishedList:TStringList;
    // write verbatim to log and possibly console
    procedure WriteLog(msg:string;ToConsole:boolean=true);
    // append line ending and write to log and possibly console
    procedure WritelnLog(msg:string;ToConsole:boolean=true);
 public
    property ShortCutName: string read FShortCutName write FShortCutName;
    property ShortCutNameFpcup:string read FShortCutNameFpcup write FShortCutNameFpcup;
    property CompilerName: string read FCompilerName write FCompilerName;
    property AllOptions:string read FAllOptions write FAllOptions;
    property BootstrapCompiler: string read FBootstrapCompiler write FBootstrapCompiler;
    property BootstrapCompilerDirectory: string read FBootstrapCompilerDirectory write SetBootstrapCompilerDirectory;
    property BootstrapCompilerURL: string read FBootstrapCompilerURL write FBootstrapCompilerURL;
    property Clean: boolean read FClean write FClean;
    property ConfigFile: string read FConfigFile write FConfigFile;
    property CrossCPU_Target:string read FCrossCPU_Target write FCrossCPU_Target;
    property CrossLCL_Platform:string read FCrossLCL_Platform write FCrossLCL_Platform;
    property CrossOS_Target:string read FCrossOS_Target write FCrossOS_Target;
    property FPCDirectory: string read FFPCDirectory write SetFPCDirectory;
    property FPCURL: string read FFPCURL write FFPCURL;
    property FPCOPT: string read FFPCOPT write FFPCOPT;
    property FPCDesiredRevision:string read FFPCDesiredRevision write FFPCDesiredRevision;
    property KeepLocalChanges: boolean read FKeepLocalDiffs write FKeepLocalDiffs;
    property LazarusDirectory: string read FLazarusDirectory write SetLazarusDirectory;
    property LazarusPrimaryConfigPath: string read GetLazarusPrimaryConfigPath write FLazarusPrimaryConfigPath ;
    property LazarusURL: string read FLazarusURL write FLazarusURL;
    property LazarusOPT:string read FLazarusOPT write FLazarusOPT;
    property LazarusDesiredRevision:string read FLazarusDesiredRevision write FLazarusDesiredRevision;
    property MakeDirectory: string read FMakeDirectory write SetMakeDirectory;
    //List of all default enabled sequences available
    property ModuleEnabledList: TStringList read FModuleEnabledList;
    //List of all publicly visible sequences
    property ModulePublishedList: TStringList read FModulePublishedList;
    property SkipModules:string read FSkipModules write FSkipModules;
    property OnlyModules:string read FOnlyModules write FOnlyModules;
    property Uninstall: boolean read FUninstall write FUninstall;
    property Verbose:boolean read FVerbose write FVerbose;
    // Fill in ModulePublishedList and ModuleEnabledList
    function LoadModuleList:boolean;
    // Stop talking. Do it!
    function Run: boolean;
    constructor Create;
    destructor Destroy; override;
  end;


  TExecState=(ESNever,ESFailed,ESSucceeded);

  TSequenceAttributes=record
    EntryPoint:integer;  //instead of rescanning the sequence table everytime, we can as well store the index in the table
    Executed:TExecState; //  Reset to ESNever at sequencer start up
  end;
  PSequenceAttributes=^TSequenceAttributes;

  TKeyword=(SMdeclare, SMdeclareHidden, SMdo, SMrequire, SMexec, SMend, SMcleanmodule, SMgetmodule, SMbuildmodule,
    SMuninstallmodule, SMconfigmodule, SMSetLCL, SMSetOS, SMSetCPU,SMInvalid);

  TState=record
    instr:TKeyword;
    param:string;
    end;

  { TSequencer }

  TSequencer=class(TObject)
    protected
      FParent:TFPCupManager;
      CurrentModule:String;
      Installer:TInstaller;  //current installer
      SkipList:TStringList;
      StateMachine:array of TState;
      procedure AddToModuleList(ModuleName:string;EntryPoint:integer);
      function DoBuildModule(ModuleName:string):boolean;
      function DoCleanModule(ModuleName:string):boolean;
      function DoConfigModule(ModuleName:string):boolean;
      function DoExec(FunctionName:string):boolean;
      function DoGetModule(ModuleName:string):boolean;
      function DoSetCPU(CPU:string):boolean;
      function DoSetOS(OS:string):boolean;
      function DoSetLCL(LCL:string):boolean;
      function DoUnInstallModule(ModuleName:string):boolean;
      function GetInstaller(ModuleName:string):boolean;
      function IsSkipped(ModuleName:string):boolean;
      procedure ResetAllExecuted(SkipFPC:boolean=false);
    public
      property Parent:TFPCupManager write Fparent;
      // parse a sequence source code and append to the statemachine
      function AddSequence(Sequence:string):boolean;
      // Add the "only" sequence from the statemachine based on the --only list
      function CreateOnly(OnlyModules:string):boolean;
      // deletes the "only" sequence from the statemachine
      function DeleteOnly:boolean;
      // run the statemachine starting at SequenceName
      function Run(SequenceName:string):boolean;
      constructor Create;
      destructor Destroy; override;
    end;

implementation

{ TFPCupManager }

function TFPCupManager.GetLazarusPrimaryConfigPath: string;
const
  DefaultPCPSubdir='lazarusdevsettings'; //Include the name lazarus for easy searching Caution: shouldn't be the same name as Lazarus dir itself.
begin
  if FLazarusPrimaryConfigPath='' then
    begin
      {$IFDEF MSWINDOWS}
      // Somewhere in local appdata special folder
      FLazarusPrimaryConfigPath := IncludeTrailingPathDelimiter(GetLocalAppDataPath())+DefaultPCPSubdir;
      {$ELSE}
      //Note: normsl GetAppConfigDir gets ~/.config/fpcup/.lazarusdev or something
      LazarusPrimaryConfigPath:=IncludeTrailingPathDelimiter(XdgConfigHome)+DefaultPCPSubdir;
      {$ENDIF MSWINDOWS}
    end;
  result:=FLazarusPrimaryConfigPath;
end;

procedure TFPCupManager.SetBootstrapCompilerDirectory(AValue: string);
begin
FBootstrapCompilerDirectory:=IncludeTrailingPathDelimiter(ExpandFileName(AValue));
end;

procedure TFPCupManager.SetFPCDirectory(AValue: string);
begin
  FFPCDirectory:=ExpandFileName(AValue);
end;

procedure TFPCupManager.SetLazarusDirectory(AValue: string);
begin
  FLazarusDirectory:=ExpandFileName(AValue);
end;

procedure TFPCupManager.SetMakeDirectory(AValue: string);
begin
  // Make directory can be empty (e.g. in Linux). In this case
  // expanding '' gives the user's home directory, which is not what we want.
  if AValue='' then
    FMakeDirectory:=''
  else
    FMakeDirectory:=ExpandFileName(AValue);
end;

procedure TFPCupManager.WriteLog(msg: string; ToConsole: boolean);
begin
Write(LogFile,msg);
if ToConsole then
  InfoLn(msg,info);
end;

procedure TFPCupManager.WritelnLog(msg: string; ToConsole: boolean);
begin
WriteLog(msg+LineEnding,false); //infoln adds already a lf
if ToConsole then
  InfoLn(msg,info);
end;

function TFPCupManager.LoadModuleList: boolean;
var i:integer;
begin
Sequencer.AddSequence(Sequences);
Sequencer.AddSequence(installerFPC.Sequences);
Sequencer.AddSequence(installerLazarus.Sequences);
Sequencer.AddSequence(installerHelp.Sequences);
Sequencer.AddSequence(installerUniversal.Sequences);
//append universal modules to the lists
Sequencer.AddSequence(installerUniversal.GetModuleList(FConfigFile));
installerUniversal.GetModuleEnabledList(FModuleEnabledList);
end;

function TFPCupManager.Run: boolean;

begin
  result:=false;
  if SkipModules<>'' then
    begin
    Sequencer.SkipList:=TStringList.Create;
    Sequencer.SkipList.Delimiter:=',';
    Sequencer.SkipList.DelimitedText:=SkipModules;
    end;
  if FOnlyModules<>'' then
    begin
    Sequencer.CreateOnly(FOnlyModules);
    result:=Sequencer.Run('Only');
    Sequencer.DeleteOnly;
    end
  else
    {$ifdef win32}
    // Run Windows specific cross compiler or regular version
    if pos('CROSSWIN32-64',UpperCase(SkipModules))>0 then
      result:=Sequencer.Run('Default')
    else
      result:=Sequencer.Run('DefaultWin32');
    {$else}
    result:=Sequencer.Run('Default');
    {$endif win32}
  if assigned(Sequencer.SkipList) then
    Sequencer.SkipList.Free;
end;

constructor TFPCupManager.Create;
var
  LogFileName: string;
begin
FModuleList:=TStringList.Create;
FModuleEnabledList:=TStringList.Create;
FModulePublishedList:=TStringList.Create;
Sequencer:=TSequencer.create;
Sequencer.Parent:=Self;
{$IFDEF MSWINDOWS}
LogFileName:='fpcup.log'; //current directory
{$ELSE}
LogFileName:=ExpandFileNameUTF8('~')+DirectorySeparator+'fpcup.log'; //In home directory
{$ENDIF MSWINDOWS}
try
 AssignFile(LogFile,LogFileName);
 if FileExistsUTF8(LogFileName) then
   Append(LogFile)
 else
   Rewrite(LogFile);
except
  infoln('Error: could not open log file '+LogFileName+' for writing.'+LineEnding+
  'This may be caused by another fpcup currently running.'+LineEnding+
  'Aborting.',error);
  halt(2); //Is there a nicer way to do this?
end;
WritelnLog(DateTimeToStr(now)+': fpcup started.',true);
end;

destructor TFPCupManager.Destroy;
var i:integer;
begin
  for i:=0 to FModuleList.Count-1 do
    Freemem(FModuleList.Objects[i]);
  FModuleList.Free;
  FModulePublishedList.Free;
  FModuleEnabledList.Free;
  Sequencer.free;
  WritelnLog(DateTimeToStr(now)+': fpcup finished.',true);
  WritelnLog('------------------------------------------------',false);
  CloseFile(LogFile);
  inherited Destroy;
end;

{ TSequencer }

procedure TSequencer.AddToModuleList(ModuleName: string; EntryPoint: integer);
var
  SeqAttr:PSequenceAttributes;
begin
getmem(SeqAttr,sizeof(TSequenceAttributes));
SeqAttr^.EntryPoint:=EntryPoint;
SeqAttr^.Executed:=ESNever;
FParent.FModuleList.AddObject(ModuleName,TObject(SeqAttr));
end;

function TSequencer.DoBuildModule(ModuleName: string): boolean;
begin
  result:= GetInstaller(ModuleName) and Installer.BuildModule(ModuleName);
end;

function TSequencer.DoCleanModule(ModuleName: string): boolean;
begin
  result:= GetInstaller(ModuleName) and Installer.CleanModule(ModuleName);
end;

function TSequencer.DoConfigModule(ModuleName: string): boolean;
begin
  result:= GetInstaller(ModuleName) and Installer.ConfigModule(ModuleName);
end;

function TSequencer.DoExec(FunctionName: string): boolean;

  function CreateFpcupScript:boolean;
  begin
    result:=true;
    // Link to fpcup itself, with all options as passed when invoking it:
    if FParent.ShortCutNameFpcup<>EmptyStr then
    begin
     {$IFDEF MSWINDOWS}
      CreateDesktopShortCut(paramstr(0),FParent.AllOptions,FParent.ShortCutNameFpcup);
     {$ELSE}
      FParent.AllOptions:=FParent.AllOptions+' $*';
      CreateHomeStartLink('"'+paramstr(0)+'"',FParent.AllOptions,FParent.ShortCutNameFpcup);
     {$ENDIF MSWINDOWS}
    end;
  end;


  function CreateLazarusScript:boolean;
  //calculate InstalledLazarus. Don't use this function when lazarus is not installed.
  var
    InstalledLazarus:string;
  begin
  result:=true;
  if FParent.ShortCutName<>EmptyStr then
  begin
    infoln('Lazarus: creating desktop shortcut:',info);
    try
      //Create shortcut; we don't care very much if it fails=>don't mess with OperationSucceeded
      InstalledLazarus:=IncludeTrailingPathDelimiter(FParent.LazarusDirectory)+'lazarus'+GetExeExt;
      {$IFDEF MSWINDOWS}
      CreateDesktopShortCut(InstalledLazarus,'--pcp="'+FParent.LazarusPrimaryConfigPath+'"',FParent.ShortCutName);
      {$ENDIF MSWINDOWS}
      {$IFDEF UNIX}
      {$IFDEF DARWIN}
      CreateHomeStartLink(InstalledLazarus+'.app/Contents/MacOS/lazarus','--pcp="'+FParent.LazarusPrimaryConfigPath+'"',FParent.ShortcutName);
      {$ELSE}
      CreateHomeStartLink(InstalledLazarus,'--pcp="'+FParent.LazarusPrimaryConfigPath+'"',FParent.ShortcutName);
      {$ENDIF DARWIN}
      {$ENDIF UNIX}
    finally
      //Ignore problems creating shortcut
    end;
  end;
  end;

  function DeleteLazarusScript:boolean;
  //calculate InstalledLazarus. Don't use this function when lazarus is not installed.
  var
    InstalledLazarus:string;
  begin
  result:=true;
  if FParent.ShortCutName<>EmptyStr then
  begin
    infoln('Lazarus: deleting desktop shortcut:',info);
    try
      //Delete shortcut; we don't care very much if it fails=>don't mess with OperationSucceeded
      InstalledLazarus:=IncludeTrailingPathDelimiter(FParent.LazarusDirectory)+'lazarus'+GetExeExt;
      {$IFDEF MSWINDOWS}
      DeleteDesktopShortCut(FParent.ShortCutName);
      {$ENDIF MSWINDOWS}
      {$IFDEF UNIX}
      DeleteFileUTF8(FParent.ShortcutName);
      {$ENDIF UNIX}
    finally
      //Ignore problems creating shortcut
    end;
  end;
  end;


  {$ifdef linux}
  function CheckDevLibs(LCLPlatform: string): boolean;
  const
    LIBSCNT=4;
  type
    TLibList=array[1..LIBSCNT] of string;
  const
    LCLLIBS:TLibList = ('libX11.so','libgdk_pixbuf-2.0.so','libpango-1.0.so','libgdk-x11-2.0.so');
    QTLIBS:TLibList = ('libQt4Pas.so','','','');
  var
    i:integer;
    pll:^TLibList;

    function TestLib(LibName:string):boolean;
    var
      Lib : TLibHandle;
    begin
      result:=true;
      if LibName<>'' then
        begin
        Lib:=LoadLibrary(LibName);
        result:=Lib<>0;
        if result then
          UnloadLibrary(Lib);
        end;
    end;

  begin
    result:=true;
    if (LCLPlatform='') or (Uppercase(LCLPlatform)='GTK2') then
      pll:=@LCLLIBS
    else if Uppercase(LCLPlatform)='QT' then
      pll:=@QTLIBS;
    for i:=1 to LIBSCNT do
      begin
      if not TestLib(pll^[i]) then
        begin
        FParent.WritelnLog('Required -dev packages are not installed for Lazarus: '+pll^[i], true);
        result:=false;
        end;
      end;
  end;
  {$endif linux}

begin
  if UpperCase(FunctionName)='CREATEFPCUPSCRIPT' then
    result:=CreateFpcupScript
  else if UpperCase(FunctionName)='CREATELAZARUSSCRIPT' then
    result:=CreateLazarusScript
  else if UpperCase(FunctionName)='DELETELAZARUSSCRIPT' then
    result:=DeleteLazarusScript
  {$ifdef linux}
  else if UpperCase(FunctionName)='CHECKDEVLIBS' then
    result:=CheckDevLibs(FParent.CrossLCL_Platform)
  {$endif linux}
  else
    begin
    result:=false;
    FParent.WritelnLog('Error: Trying to execute a non existing function: ' + FunctionName);
    end;
end;

function TSequencer.DoGetModule(ModuleName: string): boolean;
begin
  result:= GetInstaller(ModuleName) and Installer.GetModule(ModuleName);
end;

function TSequencer.DoSetCPU(CPU: string): boolean;
begin
  FParent.CrossCPU_Target:=CPU;
  ResetAllExecuted;
end;

function TSequencer.DoSetOS(OS: string): boolean;
begin
  FParent.CrossOS_Target:=OS;
  ResetAllExecuted;
end;

function TSequencer.DoSetLCL(LCL: string): boolean;
begin
  FParent.CrossLCL_Platform:=LCL;
  ResetAllExecuted(true);
end;

function TSequencer.DoUnInstallModule(ModuleName: string): boolean;
begin
  result:= GetInstaller(ModuleName) and Installer.UnInstallModule(ModuleName);
end;

{GetInstaller gets a new installer for ModuleName and initialises parameters unless one exist already.}

function TSequencer.GetInstaller(ModuleName: string): boolean;
var
  CrossCompiling:boolean;
begin
  result:=true;
  CrossCompiling:= (FParent.CrossCPU_Target<>'') or (FParent.CrossOS_Target<>'');
  //check if this is a known module
  if uppercase(ModuleName)='FPC' then
    begin
    if assigned(Installer) then
      begin
      if (not crosscompiling and (Installer is TFPCNativeInstaller)) or
        ( crosscompiling and (Installer is TFPCCrossInstaller)) then
        begin
        exit; //all fine, continue with current installer
        end
      else
        Installer.free; // get rid of old installer
      end;
    if CrossCompiling then
      begin
      Installer:=TFPCCrossInstaller.Create;
      Installer.CrossOS_Target:=FParent.CrossOS_Target;
      Installer.CrossCPU_Target:=FParent.CrossCPU_Target;
      end
    else
      Installer:=TFPCNativeInstaller.Create;
    Installer.BaseDirectory:=FParent.FPCDirectory;
    (Installer as TFPCInstaller).BootstrapCompilerDirectory:=FParent.BootstrapCompilerDirectory;
    (Installer as TFPCInstaller).BootstrapCompilerURL:=FParent.BootstrapCompilerURL;
    Installer.Compiler:='';  //bootstrap used
    Installer.CompilerOptions:=FParent.FPCOPT;
    Installer.DesiredRevision:=FParent.FPCDesiredRevision;
    Installer.URL:=FParent.FPCURL;
    end

  else if (uppercase(ModuleName)='LAZARUS') or (uppercase(ModuleName)='LAZBUILD') or (uppercase(ModuleName)='LCL') or
    (uppercase(ModuleName)='BIGIDE') or (uppercase(ModuleName)='USERIDE') then
    begin
    if assigned(Installer) then
      begin
      if (not crosscompiling and (Installer is TLazarusNativeInstaller)) or
        ( crosscompiling and (Installer is TLazarusCrossInstaller)) then
        begin
        exit; //all fine, continue with current installer
        end
      else
        Installer.free; // get rid of old installer
      end;
    if CrossCompiling then
      begin
      Installer:=TLazarusCrossInstaller.Create;
      Installer.CrossOS_Target:=FParent.CrossOS_Target;
      Installer.CrossCPU_Target:=FParent.CrossCPU_Target;
      end
    else
      Installer:=TLazarusNativeInstaller.Create;
    Installer.BaseDirectory:=FParent.LazarusDirectory ;
    if FParent.CompilerName='' then
      Installer.Compiler:=Installer.GetCompilerInDir(FParent.FPCDirectory)
    else
      Installer.Compiler:=FParent.CompilerName;
    Installer.CompilerOptions:=FParent.LazarusOPT;
    Installer.DesiredRevision:=FParent.LazarusDesiredRevision;
    (Installer As TLazarusInstaller).FPCDir:=FParent.FPCDirectory;
    (Installer As TLazarusInstaller).PrimaryConfigPath:=FParent.LazarusPrimaryConfigPath;
    Installer.URL:=FParent.LazarusURL;
    end

  //Convention: help modules start with HelpFPC
  //or HelpLazarus
  else if uppercase(ModuleName)='HELPFPC' then
      begin
      if assigned(Installer) then
        begin
        if (Installer is THelpFPCInstaller) then
          begin
          exit; //all fine, continue with current installer
          end
        else
          Installer.free; // get rid of old installer
        end;
      Installer:=THelpFPCInstaller.Create;
      Installer.BaseDirectory:=FParent.FPCDirectory;
      if FParent.CompilerName='' then
        Installer.Compiler:=Installer.GetCompilerInDir(FParent.FPCDirectory)
      else
        Installer.Compiler:=FParent.CompilerName;
      end

  else if uppercase(ModuleName)='HELPLAZARUS' then
      begin
      if assigned(Installer) then
        begin
       if (Installer is THelpLazarusInstaller) then
          begin
          exit; //all fine, continue with current installer
          end
        else
          Installer.free; // get rid of old installer
        end;
      Installer:=THelpLazarusInstaller.Create;
      Installer.BaseDirectory:=FParent.LazarusDirectory ;
      if FParent.CompilerName='' then
        Installer.Compiler:=Installer.GetCompilerInDir(FParent.FPCDirectory)
      else
        Installer.Compiler:=FParent.CompilerName;
      (Installer as THelpLazarusInstaller).FPCDirectory:=FParent.FPCDirectory;
      (Installer as THelpLazarusInstaller).LazarusPrimaryConfigPath:=FParent.LazarusPrimaryConfigPath;
      end

  else       // this is a universal module
    begin
      if assigned(Installer) then
        begin
        if (Installer is TUniversalInstaller) and
          (CurrentModule= ModuleName) then
          begin
          exit; //all fine, continue with current installer
          end
        else
          Installer.free; // get rid of old installer
        end;
      Installer:=TUniversalInstaller.Create;
      CurrentModule:=ModuleName;
      //assign properties
      (Installer as TUniversalInstaller).FPCDir:=FParent.FFPCDirectory;
      (Installer as TUniversalInstaller).LazarusDir:=FParent.FLazarusDirectory;
      (Installer as TUniversalInstaller).LazarusPrimaryConfigPath:=FParent.LazarusPrimaryConfigPath;
      if FParent.CompilerName='' then
        Installer.Compiler:=Installer.GetCompilerInDir(FParent.FPCDirectory)
      else
        Installer.Compiler:=FParent.CompilerName;
    end;

  Installer.KeepLocalChanges:=FParent.KeepLocalChanges;
  Installer.Verbose:=FParent.Verbose;
  Installer.LogFile:=FParent.LogFile;
  {$IFDEF MSWINDOWS}
  Installer.MakeDirectory:=FParent.MakeDirectory;
  {$ENDIF}
end;

function TSequencer.IsSkipped(ModuleName: string): boolean;
begin
  result:=assigned(SkipList) and (SkipList.IndexOf(Uppercase(ModuleName))>=0);
end;

procedure TSequencer.ResetAllExecuted(SkipFPC: boolean);
var
  idx:integer;
begin
for idx:=0 to FParent.FModuleList.Count -1 do
  // convention: FPC sequences that are to be skipped start with 'FPC'. Used in SetLCL.
  // todo: skip also help???? Who would call several help installs in one sequence? SubSequences?
  if not SkipFPC or (pos('FPC',Uppercase(FParent.FModuleList[idx]))<>1) then
    PSequenceAttributes(FParent.FModuleList.Objects[idx])^.Executed:=ESNever;
end;

function TSequencer.AddSequence(Sequence: string): boolean;
//our mini parser
var
  line,key,param:string;
  i:integer;
  instr:TKeyword;
  sequencename:string;

  function KeyStringToKeyword(Key:string):TKeyword;

  begin
    if key='DECLARE' then result:=SMdeclare
    else if key='DECLAREHIDDEN' then result:=SMdeclareHidden
    else if key='DO' then result:=SMdo
    else if key='REQUIRES' then result:=SMrequire
    else if key='EXEC' then result:=SMexec
    else if key='END' then result:=SMend
    else if key='CLEANMODULE' then result:=SMcleanmodule
    else if key='GETMODULE' then result:=SMgetmodule
    else if key='BUILDMODULE' then result:=SMbuildmodule
    else if key='UNINSTALLMODULE' then result:=SMuninstallmodule
    else if key='CONFIGMODULE' then result:=SMconfigmodule
    else if key='SETLCL' then result:=SMSetLCL
    else if key='SETOS' then result:=SMSetOS
    else if key='SETCPU' then result:=SMSetCPU
    else result:=SMInvalid;
  end;

  //remove white space and line terminator
  function NoWhite(s:string):string;
  begin
    while (s[1]<=' ') or (s[1]=';') do delete(s,1,1);
    while (s[length(s)]<=' ') or (s[length(s)]=';') do delete(s,length(s),1);
    result:=s;
  end;

begin
while Sequence<>'' do
  begin
  i:=pos(';',Sequence);
  if i>0 then
    line:=copy(Sequence,1,i-1)
  else
    line:=Sequence;
  delete(Sequence,1,length(line)+1);
  line:=NoWhite(line);
  if line<>'' then
    begin
    i:=pos(' ',line);
    if i>0 then
      begin
      key:=copy(line,1,i-1);
      param:=NoWhite(copy(line,i,length(line)));
      end
    else
      begin
      key:=line;
      param:='';
      end;
    key:=NoWhite(key);
    if key<>'' then
      begin
      i:=Length(StateMachine);
      SetLength(StateMachine,i+1);
      instr:=KeyStringToKeyword(Uppercase(Key));
      StateMachine[i].instr:=instr;
      if instr=SMInvalid then
        FParent.WritelnLog('Invalid instruction '+Key+' in sequence '+sequencename);
      StateMachine[i].param:=param;
      if instr in [SMdeclare,SMdeclareHidden] then
        begin
        AddToModuleList(uppercase(param),i);
        sequencename:=param;
        end;
      if instr = SMdeclare then
        FParent.FModulePublishedList.Add(param);
      end;
    end;
  end;
result:=true;
end;


// create the sequence corresponding with the only parameters
function TSequencer.CreateOnly(OnlyModules: string): boolean;
var
  i:integer;
  seq:string;

begin
AddToModuleList('ONLY',Length(StateMachine));
while Onlymodules<>'' do
  begin
  i:=pos(',',Onlymodules);
  if i>0 then
    seq:=copy(Onlymodules,1,i-1)
  else
    seq:=Onlymodules;
  delete(Onlymodules,1,length(seq)+1);
  // We could build a sequence string and have it parsed by AddSequence.
  // Pro: no dependency on statemachine structure
  // Con: dependency on sequence format; double parsing
  if seq<>'' then
    begin
    i:=Length(StateMachine);
    SetLength(StateMachine,i+1);
    StateMachine[i].instr:=SMdo;
    StateMachine[i].param:=seq;
    end;
  end;
i:=Length(StateMachine);
SetLength(StateMachine,i+1);
StateMachine[i].instr:=SMend;
StateMachine[i].param:='';
result:=true;
end;

function TSequencer.DeleteOnly: boolean;
var i,idx:integer;
  SeqAttr:^TSequenceAttributes;
begin
idx:=FParent.FModuleList.IndexOf('ONLY');
if (idx >0) then
  begin
  SeqAttr:=PSequenceAttributes(pointer(FParent.FModuleList.Objects[idx]));
  i:=SeqAttr^.EntryPoint;
  while i<length(StateMachine) do
    begin
    StateMachine[i].param:='';
    i:=i+1;
    end;
  SetLength(StateMachine,SeqAttr^.EntryPoint);
  Freemem(FParent.FModuleList.Objects[idx]);
  FParent.FModuleList.Delete(idx);
  end;
end;

function TSequencer.Run(SequenceName: string): boolean;
var
  EntryPoint,InstructionPointer:integer;
  idx:integer;
  SeqAttr:^TSequenceAttributes;

  Procedure CleanUpInstaller;
  begin
    if assigned(Installer) then
      begin
      Installer.Free;
      Installer:=nil;
      end;
  end;

begin
  if not assigned(FParent.FModuleList) then
    begin
    result:=false;
    FParent.WritelnLog('Error: No sequences loaded when trying to find' + SequenceName);
    exit;
    end;
  // --clean or --install ??
  if FParent.Uninstall then  // uninstall overrides clean
    begin
    if (UpperCase(SequenceName)<>'ONLY') and (uppercase(copy(SequenceName,length(SequenceName)-8,9))<>'UNINSTALL') then
      SequenceName:=SequenceName+'uninstall';
    end
  else if FParent.Clean  then
    begin
    if (UpperCase(SequenceName)<>'ONLY') and (uppercase(copy(SequenceName,length(SequenceName)-4,5))<>'CLEAN') then
      SequenceName:=SequenceName+'clean';
    end;
  // find sequence
  idx:=FParent.FModuleList.IndexOf(Uppercase(SequenceName));
  if (idx>=0) then
    begin
    result:=true;
    SeqAttr:=PSequenceAttributes(pointer(FParent.FModuleList.Objects[idx]));
    // Don't run if already run
    case SeqAttr^.Executed of
      ESFailed   : begin
                     result:=false;
                     exit;
                   end;
      ESSucceeded : exit;
      end;
    // Get entry point in statemachine
    InstructionPointer:=SeqAttr^.EntryPoint;
    EntryPoint:=InstructionPointer;
    // run sequence until end or failure
    while true do
      begin
      case StateMachine[InstructionPointer].instr of
        SMdeclare     :;
        SMdeclareHidden :;
        SMdo          : if not IsSkipped(StateMachine[InstructionPointer].param) then
                          result:=Run(StateMachine[InstructionPointer].param);
        SMrequire     : result:=Run(StateMachine[InstructionPointer].param);
        SMexec        : result:=DoExec(StateMachine[InstructionPointer].param);
        SMend         : begin
                          SeqAttr^.Executed:=ESSucceeded;
                          CleanUpInstaller;
                          exit; //success
                        end;
        SMcleanmodule : result:=DoCleanModule(StateMachine[InstructionPointer].param);
        SMgetmodule   : result:=DoGetModule(StateMachine[InstructionPointer].param);
        SMbuildmodule : result:=DoBuildModule(StateMachine[InstructionPointer].param);
        SMuninstallmodule: result:=DoUnInstallModule(StateMachine[InstructionPointer].param);
        SMconfigmodule: result:=DoConfigModule(StateMachine[InstructionPointer].param);
        SMSetLCL      : DoSetLCL(StateMachine[InstructionPointer].param);
        SMSetOS       : DoSetOS(StateMachine[InstructionPointer].param);
        SMSetCPU      : DoSetCPU(StateMachine[InstructionPointer].param);
        end;
      if not result then
        begin
        SeqAttr^.Executed:=ESFailed;
        FParent.WritelnLog('Error running fpcup. Technical details: error executing sequence '+SequenceName+
          '; line: '+IntTostr(InstructionPointer - EntryPoint+1)+
          ', param: '+StateMachine[InstructionPointer].param);
        CleanUpInstaller;
        exit; //failure, bail out
        end;
      InstructionPointer:=InstructionPointer+1;
      if InstructionPointer>=length(StateMachine) then  //somebody forgot end
        begin
        SeqAttr^.Executed:=ESSucceeded;
        CleanUpInstaller;
        exit; //success
        end;
      end;
    end
  else
    begin
    result:=false;  // sequence not found
    FParent.WritelnLog('Error: Failed to load sequence :' + SequenceName);
    end;
end;

constructor TSequencer.Create;
begin

end;

destructor TSequencer.Destroy;
begin
  inherited Destroy;
end;

end.