This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
/
buildlazdialog.pas
1210 lines (1101 loc) · 43.5 KB
/
buildlazdialog.pas
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
992
993
994
995
996
997
998
999
1000
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Converted to lfm by: Matthijs Willemstein
Quickoptions added by: Giuliano Colla
Then extensively modified by: Juha Manninen
- added support for Build Profiles which extend the idea of Quick Options.
- changed UI to be less weird and comply better with UI design norms.
- changed object structure to keep it logical and to avoid duplicate data.
Abstract:
Defines settings for the "Build Lazarus" function of the IDE.
TConfigureBuildLazarusDlg is used to edit the build options.
The BuildLazarus function will build the lazarus parts.
Building occurs only with options defined in the Detail Page.
Profiles are just used to set options there. Therefore beginners can
use default profiles and don't need to touch the Detail Page.
Advanced users can define their own profiles for example for cross compiling.
}
unit BuildLazDialog;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc, LConvEncoding, Forms, Controls, LCLType, LCLIntf,
Graphics, GraphType, StdCtrls, ExtCtrls, Buttons, FileUtil, LazUTF8,
LazLogger, Dialogs, InterfaceBase, Themes, CheckLst, Menus, ComCtrls,
DividerBevel, DefineTemplates,
// IDEIntf
LazIDEIntf, IDEMsgIntf, IDEHelpIntf, IDEImagesIntf, IDEWindowIntf, IDEDialogs,
// IDE
LazarusIDEStrConsts, TransferMacros, LazConf, IDEProcs, DialogProcs, MainBar,
InputHistory, ExtToolDialog, ExtToolEditDlg, EnvironmentOpts,
{$IFDEF win32}
CodeToolManager, // added for windres workaround
{$ENDIF}
ApplicationBundle, CompilerOptions, BuildProfileManager,
GenericListEditor, GenericCheckList, PackageSystem, PackageDefs;
type
TBuildLazarusFlag = (
blfDontBuild, // skip all building, only cleaning
blfOnlyIDE, // skip all but IDE (for example build IDE, but not packages, not lazbuild, ...)
blfDontClean, // ignore clean up option in profile
blfUseMakeIDECfg, // append @idemake.cfg
blfReplaceExe, // ignore OSLocksExecutables and do not create lazarus.new.exe
blfBackupOldExe // rename existing lazarus exe to lazarus.old
);
TBuildLazarusFlags = set of TBuildLazarusFlag;
{ TConfigureBuildLazarusDlg }
TConfigureBuildLazarusDlg = class(TForm)
CleanAllRadioButton: TRadioButton;
CleanAutoRadioButton: TRadioButton;
CleanCommonRadioButton: TRadioButton;
CleanOnceCheckBox: TCheckBox;
CommonsDividerBevel: TDividerBevel;
ConfirmBuildCheckBox: TCheckBox;
DefinesButton: TButton;
DefinesLabel: TLabel;
DefinesListBox: TCheckListBox;
CancelButton: TBitBtn;
CBLDBtnPanel: TPanel;
BuildProfileComboBox: TComboBox;
CompileButton: TBitBtn;
CompileAdvancedButton: TBitBtn;
InhTreeView: TTreeView;
InhNoteLabel: TLabel;
LCLWidgetTypeLabel: TLabel;
LCLWidgetTypeComboBox: TComboBox;
OptionsLabel: TLabel;
OptionsMemo: TMemo;
CleanUpGroupBox: TGroupBox;
PageControl1: TPageControl;
RestartAfterBuildCheckBox: TCheckBox;
ShowOptsMenuItem: TMenuItem;
DetailsPanel: TPanel;
HelpButton: TBitBtn;
BuildProfileLabel: TLabel;
OptionsPopupMenu: TPopupMenu;
Panel2: TPanel;
SaveSettingsButton: TBitBtn;
BuildProfileButton: TButton;
BuildTabSheet: TTabSheet;
InfoTabSheet: TTabSheet;
TargetCPUComboBox: TComboBox;
TargetCPULabel: TLabel;
TargetDirectoryButton: TButton;
TargetDirectoryComboBox: TComboBox;
TargetDirectoryLabel: TLabel;
TargetOSComboBox: TComboBox;
TargetOSLabel: TLabel;
UpdateRevisionIncCheckBox: TCheckBox;
procedure BuildProfileButtonClick(Sender: TObject);
procedure BuildProfileComboBoxSelect(Sender: TObject);
procedure CompileAdvancedButtonClick(Sender: TObject);
procedure CompileButtonClick(Sender: TObject);
procedure DefinesButtonClick(Sender: TObject);
procedure FormClose(Sender: TObject; var {%H-}CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure HelpButtonClick(Sender: TObject);
procedure ShowOptsMenuItemClick(Sender: TObject);
procedure SaveSettingsButtonClick(Sender: TObject);
procedure TargetDirectoryButtonClick(Sender: TObject);
private
// Data is copied by caller before and after opening this dialog.
fProfiles: TBuildLazarusProfiles;
fUpdatingProfileCombo: Boolean;
fImageIndexPackage: Integer;
fImageIndexRequired: Integer;
fImageIndexInherited: Integer;
procedure PrepareClose;
procedure SetupInfoPage;
procedure UpdateInheritedTree;
public
constructor Create(TheOwner: TComponent); overload; reintroduce;
destructor Destroy; override;
procedure CopyProfileToUI(AProfile: TBuildLazarusProfile);
procedure CopyUIToProfile(AProfile: TBuildLazarusProfile);
procedure UpdateProfileNamesUI;
public
property Profiles: TBuildLazarusProfiles read fProfiles;
end;
function ShowConfigureBuildLazarusDlg(AProfiles: TBuildLazarusProfiles): TModalResult;
{ Clean all
clean ide
ide
clean ide + ide
}
function MakeLazarus(Profile: TBuildLazarusProfile;
ExternalTools: TBaseExternalToolList; Macros: TTransferMacroList;
const PackageOptions, CompilerPath, MakePath: string;
Flags: TBuildLazarusFlags; var ProfileChanged: boolean): TModalResult;
function CreateIDEMakeOptions(Profile: TBuildLazarusProfile;
Macros: TTransferMacroList; const PackageOptions: string;
Flags: TBuildLazarusFlags; var AExOptions: string;
out UpdateRevisionInc: boolean; out OutputDirRedirected: boolean): TModalResult;
function SaveIDEMakeOptions(Profile: TBuildLazarusProfile;
Macros: TTransferMacroList;
const PackageOptions: string; Flags: TBuildLazarusFlags): TModalResult;
function GetMakeIDEConfigFilename: string;
implementation
{$R *.lfm}
const
DefaultIDEMakeOptionFilename = 'idemake.cfg';
function ShowConfigureBuildLazarusDlg(AProfiles: TBuildLazarusProfiles): TModalResult;
// mrOk=save
// mrYes=save and compile
// mrAll=save and compile all selected profiles
var
ConfigBuildLazDlg: TConfigureBuildLazarusDlg;
begin
Result := mrCancel;
ConfigBuildLazDlg := TConfigureBuildLazarusDlg.Create(nil);
try
ConfigBuildLazDlg.Profiles.Assign(AProfiles); // Copy profiles to dialog.
Result := ConfigBuildLazDlg.ShowModal;
if Result in [mrOk,mrYes,mrAll] then
AProfiles.Assign(ConfigBuildLazDlg.Profiles); // Copy profiles back from dialog.
finally
ConfigBuildLazDlg.Free;
end;
end;
function MakeLazarus(Profile: TBuildLazarusProfile;
ExternalTools: TBaseExternalToolList; Macros: TTransferMacroList;
const PackageOptions, CompilerPath, MakePath: string;
Flags: TBuildLazarusFlags; var ProfileChanged: boolean): TModalResult;
procedure ApplyCleanOnce;
begin
if not Profile.CleanOnce then exit;
if Profile.IdeBuildMode=bmBuild then exit;
Profile.IdeBuildMode:=bmBuild;
ProfileChanged:=true;
end;
function CheckDirectoryWritable(Dir: string): boolean;
begin
if DirectoryIsWritableCached(Dir) then exit(true);
Result:=false;
IDEMessageDialog(lisBuildingLazarusFailed,
Format(lisThisSetOfOptionsToBuildLazarusIsNotSupportedByThis,
[#13, '"', Dir, '"', #13]),
mtError,[mbCancel]);
end;
procedure CleanLazarusSrcDir(Dir: string; Recursive: boolean = true);
var
FileInfo: TSearchRec;
Ext: String;
Filename: TFilename;
begin
Dir:=AppendPathDelim(TrimFilename(Dir));
if FindFirstUTF8(Dir+AllFilesMask,faAnyFile,FileInfo)=0 then begin
repeat
if (FileInfo.Name='') or (FileInfo.Name='.') or (FileInfo.Name='..')
or (FileInfo.Name='.svn') or (FileInfo.Name='.git')
then
continue;
Filename:=Dir+FileInfo.Name;
if faDirectory and FileInfo.Attr>0 then
begin
if Recursive then
CleanLazarusSrcDir(Filename)
end
else begin
Ext:=LowerCase(ExtractFileExt(FileInfo.Name));
if (Ext='.ppu') or (Ext='.o') or (Ext='.rst') then begin
if not DeleteFileUTF8(Filename) then
debugln(['CleanLazarusSrcDir failed to delete file "',Filename,'"']);
end;
end;
until FindNextUTF8(FileInfo)<>0;
end;
FindCloseUTF8(FileInfo);
end;
procedure CheckRevisionInc;
var
RevisionIncFile: String;
sl: TStringList;
begin
RevisionIncFile:=AppendPathDelim(EnvironmentOptions.GetParsedLazarusDirectory)+'ide'+PathDelim+'revision.inc';
if not FileExistsUTF8(RevisionIncFile) then begin
debugln(['Note: revision.inc file missing: ',RevisionIncFile]);
sl:=TStringList.Create;
sl.Add('// Created by lazbuild');
sl.Add('const RevisionStr = '''+LazarusVersionStr+''';');
try
sl.SaveToFile(RevisionIncFile);
except
on E: Exception do begin
debugln(['Note: can not write ',RevisionIncFile,': ',E.Message]);
end;
end;
sl.Free;
end;
end;
var
Tool: TExternalToolOptions;
ExOptions: String;
WorkingDirectory: String;
OutputDirRedirected, UpdateRevisionInc: boolean;
IdeBuildMode: TIdeBuildMode;
CmdLineParams: String;
Dir: String;
begin
Result:=mrCancel;
if LazarusIDE<>nil then
LazarusIDE.MainBarSubTitle:=Profile.Name;
IdeBuildMode:=Profile.IdeBuildMode;
Tool:=TExternalToolOptions.Create;
try
// setup external tool
Tool.Filename:=MakePath;
Tool.EnvironmentOverrides.Values['LCL_PLATFORM']:=
LCLPlatformDirNames[Profile.TargetPlatform];
Tool.EnvironmentOverrides.Values['LANG']:= 'en_US';
if CompilerPath<>'' then
Tool.EnvironmentOverrides.Values['PP']:=CompilerPath;
if (Tool.Filename<>'') and (not FileExistsUTF8(Tool.Filename)) then
Tool.Filename:=FindDefaultExecutablePath(Tool.Filename);
if (Tool.Filename='') or (not FileExistsUTF8(Tool.Filename)) then begin
Tool.Filename:=FindDefaultMakePath;
if (Tool.Filename='') or (not FileExistsUTF8(Tool.Filename)) then begin
IDEMessageDialog(lisMakeNotFound,
Format(lisTheProgramMakeWasNotFoundThisToolIsNeededToBuildLa,
['"', '"', #13, #13]),
mtError,[mbCancel]);
exit;
end;
end;
// add -w option to print leaving/entering messages of "make"
CmdLineParams:=' -w';
// append target OS
if Profile.TargetOS<>'' then
CmdLineParams+=' OS_TARGET='+Profile.FPCTargetOS;
// append target CPU
if Profile.TargetCPU<>'' then
CmdLineParams+=' CPU_TARGET='+Profile.FPCTargetCPU;
Tool.ScanOutputForFPCMessages:=true;
Tool.ScanOutputForMakeMessages:=true;
// clean up
if (IdeBuildMode<>bmBuild) and (not (blfDontClean in Flags)) then begin
WorkingDirectory:=EnvironmentOptions.GetParsedLazarusDirectory;
if not CheckDirectoryWritable(WorkingDirectory) then exit(mrCancel);
if (IdeBuildMode=bmCleanAllBuild) and (not (blfOnlyIDE in Flags)) then begin
// clean all lazarus source directories
// Note: Some installations put the fpc units into the lazarus directory
// => clean only the known directories
CleanLazarusSrcDir(WorkingDirectory,false);
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'examples');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'components');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'units');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'ide');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'packager');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'lcl');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'ideintf');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'tools');
CleanLazarusSrcDir(WorkingDirectory+PathDelim+'test');
// clean config directory
CleanLazarusSrcDir(GetPrimaryConfigPath+PathDelim+'units');
// clean custom target directory
if Profile.TargetDirectory<>'' then begin
Dir:=Profile.GetParsedTargetDirectory(Macros);
if (Dir<>'') and DirPathExists(Dir) then
CleanLazarusSrcDir(Dir);
end;
end;
// call make to clean up
Tool.Title:=lisCleanLazarusSource;
Tool.WorkingDirectory:=WorkingDirectory;
if (IdeBuildMode=bmCleanBuild) or (blfOnlyIDE in Flags) then
Tool.CmdLineParams:='cleanide'
else
Tool.CmdLineParams:='cleanlaz';
Tool.CmdLineParams:=Tool.CmdLineParams+CmdLineParams;
Result:=ExternalTools.Run(Tool,Macros,false);
if Result<>mrOk then exit;
ApplyCleanOnce;
end;
// build IDE
if not (blfDontBuild in Flags) then begin
WorkingDirectory:=EnvironmentOptions.GetParsedLazarusDirectory;
if blfDontClean in Flags then
IdeBuildMode:=bmBuild;
Tool.Title:=lisIDE;
Tool.WorkingDirectory:=WorkingDirectory;
if IdeBuildMode=bmBuild then
Tool.CmdLineParams:='ide'
else
Tool.CmdLineParams:='cleanide ide';
// append extra Profile
ExOptions:='';
Result:=CreateIDEMakeOptions(Profile,Macros,PackageOptions,Flags,
ExOptions,UpdateRevisionInc,OutputDirRedirected);
if Result<>mrOk then exit;
if (not OutputDirRedirected)
and (not CheckDirectoryWritable(WorkingDirectory)) then
exit(mrCancel);
if ExOptions<>'' then
Tool.EnvironmentOverrides.Values['OPT'] := ExOptions;
if not UpdateRevisionInc then begin
CheckRevisionInc;
Tool.EnvironmentOverrides.Values['USESVN2REVISIONINC'] := '0';
end;
Tool.CmdLineParams:=Tool.CmdLineParams+CmdLineParams;
// run
Result:=ExternalTools.Run(Tool,Macros,false);
if Result<>mrOk then exit;
ApplyCleanOnce;
end;
Result:=mrOk;
finally
Tool.Free;
if LazarusIDE<>nil then
LazarusIDE.MainBarSubTitle:='';
end;
end;
function CreateIDEMakeOptions(Profile: TBuildLazarusProfile;
Macros: TTransferMacroList; const PackageOptions: string;
Flags: TBuildLazarusFlags; var AExOptions: string;
out UpdateRevisionInc: boolean; out OutputDirRedirected: boolean): TModalResult;
procedure BackupExe(var ExeFilename: string);
var
Ext: String;
BackupFilename: String;
Backup2Filename: String;
AltFilename: String;
begin
if not FileExistsUTF8(ExeFilename) then exit;
// the exe already exists
Ext:=ExtractFileExt(ExeFilename);
AltFilename:=LeftStr(ExeFilename,length(ExeFilename)-length(Ext))+'.new'+Ext;
if blfBackupOldExe in Flags then begin
// always delete the lazarus.new exe, so that users/startlazarus are not
// confused which one is the newest
if FileExistsUTF8(AltFilename) then begin
if DeleteFileUTF8(AltFilename) then
debugln(['Note: deleted file "',AltFilename,'"'])
else
debugln(['WARNING: unable to delete file "',AltFilename,'"']);
end;
// try to rename the old exe
BackupFilename:=LeftStr(ExeFilename,length(ExeFilename)-length(Ext))+'.old'+Ext;
if FileExistsUTF8(BackupFilename) then
if DeleteFileUTF8(BackupFilename) then begin
debugln(['Note: deleted backup "',BackupFilename,'"']);
end else begin
// unable to delete old backup file, maybe an old IDE is still running
// => try to backup the backup
Backup2Filename:=LeftStr(ExeFilename,length(ExeFilename)-length(Ext))+'.old2'+Ext;
if FileExistsUTF8(Backup2Filename) then begin
if DeleteFileUTF8(Backup2Filename) then
debugln(['Note: deleted backup "',Backup2Filename,'"'])
else
debugln(['WARNING: unable to delete old backup file "'+Backup2Filename+'"']);
end;
if not FileExistsUTF8(Backup2Filename) then begin
if RenameFileUTF8(BackupFilename,Backup2Filename) then
debugln(['Note: renamed old backup file "'+BackupFilename+'" to "',Backup2Filename,'"'])
else
debugln(['WARNING: unable to rename old backup file "'+BackupFilename+'" to "',Backup2Filename,'"']);
end;
end;
if not FileExistsUTF8(BackupFilename) then begin
if RenameFileUTF8(ExeFilename,BackupFilename) then
debugln(['Note: renamed file "'+ExeFilename+'" to "',BackupFilename,'"'])
else
debugln(['WARNING: unable to rename file "'+ExeFilename+'" to "',BackupFilename,'"']);
end;
end;
if (not (blfReplaceExe in Flags)) and FileExistsUTF8(ExeFilename) then begin
// backup didn't work => use another file name
ExeFilename:=AltFilename;
end;
end;
procedure AppendExtraOption(const AddOption: string; EncloseIfSpace: boolean);
begin
if AddOption='' then exit;
if AExOptions<>'' then
AExOptions:=AExOptions+' ';
if EncloseIfSpace and (Pos(' ',AddOption)>0) then
AExOptions:=AExOptions+'"'+AddOption+'"'
else
AExOptions:=AExOptions+AddOption;
//DebugLn(['AppendExtraOption ',AExOptions]);
end;
procedure AppendExtraOption(const AddOption: string);
begin
AppendExtraOption(AddOption,true);
end;
var
MakeIDECfgFilename: String;
NewTargetFilename: String;
NewTargetDirectory: String;
NewUnitDirectory: String;
DefaultTargetOS: string;
DefaultTargetCPU: string;
NewTargetOS: String;
NewTargetCPU: String;
CrossCompiling: Boolean;
CurTargetFilename: String;
BundleDir: String;
NewTargetDirectoryIsDefault: Boolean;
DefaultTargetFilename: String;
NewTargetFilenameIsDefault: Boolean;
begin
Result:=mrOk;
OutputDirRedirected:=false;
UpdateRevisionInc:=Profile.UpdateRevisionInc;
// create extra Profile
AExOptions:=Profile.ExtraOptions;
// check for special IDE config file
if (blfUseMakeIDECfg in Flags) then begin
MakeIDECfgFilename:=GetMakeIDEConfigFilename;
//DebugLn(['CreateBuildLazarusOptions MAKE MakeIDECfgFilename=',MakeIDECfgFilename,' ',FileExistsUTF8(MakeIDECfgFilename)]);
if (FileExistsUTF8(MakeIDECfgFilename)) then begin
// If a file name contains spaces, a file name whould need to be quoted.
// Using a single quote is not possible, it is used already in the
// makefile to group all Profile in OPT='bla bla'.
// using " implicates that make uses a shell to execute the command of
// that line. But using shells (i.e. command.com, cmd.exe, etc) is so
// fragile (see bug 11362), that is better to avoid this.
// Therefore we use a short 8.3 file and path name, so we don't need to
// use quotes at all.
// On platforms other than windows, ExtractShortPathName is implemented
// too and simply returns the passed file name, so there is no need
// for $IFDEF.
if pos(' ',MakeIDECfgFilename)>0 then
MakeIDECfgFilename:=ExtractShortPathNameUTF8(MakeIDECfgFilename);
AppendExtraOption('@'+MakeIDECfgFilename);
end;
end;
// set target filename and target directory:
// 1. the user has set a target directory
// 2. For crosscompiling the IDE needs a different directory
// 3. If lazarus is installed as root/administrator, the lazarus directory
// is readonly and needs a different name and directory
// (e.g. ~/.lazarus/bin/lazarus).
// 4. Platforms like windows locks executables, so lazarus can not replace
// itself. The IDE will try to rename the file or fallback to another name
// (e.g. lazarus.new.exe).
// The target directory is writable, the lazarus.o file can be created.
// Otherwise: Don't touch the target filename.
NewTargetFilename:='';
NewUnitDirectory:='';
NewTargetDirectory:='';
DefaultTargetOS:=GetDefaultTargetOS;
DefaultTargetCPU:=GetDefaultTargetCPU;
NewTargetOS:=Profile.FPCTargetOS;
NewTargetCPU:=Profile.FPCTargetCPU;
if NewTargetOS='' then NewTargetOS:=DefaultTargetOS;
if NewTargetCPU='' then NewTargetCPU:=DefaultTargetCPU;
DefaultTargetFilename:='lazarus'+GetExecutableExt(NewTargetOS);
CrossCompiling:=(CompareText(NewTargetOS,DefaultTargetOS)<>0) or (CompareText(NewTargetCPU,DefaultTargetCPU)<>0);
//DebugLn(['CreateBuildLazarusOptions NewTargetOS=',NewTargetOS,' NewTargetCPU=',NewTargetCPU]);
if (Profile.TargetDirectory<>'') then begin
// Case 1. the user has set a target directory
NewTargetDirectory:=Profile.GetParsedTargetDirectory(Macros);
if NewTargetDirectory='' then begin
debugln('CreateBuildLazarusOptions macro aborted Options.TargetDirectory=',Profile.TargetDirectory);
Result:=mrAbort;
exit;
end;
NewUnitDirectory:=AppendPathDelim(NewTargetDirectory)+'units';
debugln('CreateBuildLazarusOptions TargetDirectory=',NewTargetDirectory);
debugln('CreateBuildLazarusOptions UnitsTargetDirectory=',NewUnitDirectory);
end else begin
// no user defined target directory
// => find it automatically
if CrossCompiling then
begin
// Case 2. crosscompiling the IDE
// create directory <primary config dir>/bin/<TargetCPU>-<TargetOS>
NewTargetDirectory:=AppendPathDelim(GetPrimaryConfigPath)+'bin'
+PathDelim+NewTargetCPU+'-'+NewTargetOS;
NewUnitDirectory:=AppendPathDelim(GetPrimaryConfigPath)+'units'
+PathDelim+NewTargetCPU+'-'+NewTargetOS;
debugln('CreateBuildLazarusOptions Options.TargetOS=',Profile.FPCTargetOS,' Options.TargetCPU=',
Profile.FPCTargetCPU,' DefaultOS=',DefaultTargetOS,' DefaultCPU=',DefaultTargetCPU);
end else begin
// -> normal compile for this platform
// get lazarus directory
NewTargetDirectory:=EnvironmentOptions.GetParsedLazarusDirectory;
if (NewTargetDirectory<>'') and DirPathExists(NewTargetDirectory) then
begin
if not DirectoryIsWritableCached(NewTargetDirectory) then begin
// Case 3. the lazarus directory is not writable
// create directory <primary config dir>/bin/
UpdateRevisionInc:=false;
NewTargetDirectory:=AppendPathDelim(GetPrimaryConfigPath)+'bin';
debugln('CreateBuildLazarusOptions LazDir readonly NewTargetDirectory=',NewTargetDirectory);
NewUnitDirectory:=AppendPathDelim(GetPrimaryConfigPath)+'units'
+PathDelim+NewTargetCPU+'-'+NewTargetOS;
end else begin
// Case 4. the lazarus directory is writable
end;
end else begin
// lazarus dir is not valid (probably someone is experimenting)
// -> just compile to current directory
NewTargetDirectory:='';
end;
end;
end;
// compute targetfilename
if not FilenameIsAbsolute(NewTargetDirectory) then
NewTargetDirectory:=
TrimFilename(AppendPathDelim(EnvironmentOptions.GetParsedLazarusDirectory)+NewTargetDirectory);
if NewTargetFilename='' then
NewTargetFilename:='lazarus'+GetExecutableExt(NewTargetOS);
if not FilenameIsAbsolute(NewTargetFilename) then
NewTargetFilename:=TrimFilename(AppendPathDelim(NewTargetDirectory)+NewTargetFilename);
// backup old exe
BackupExe(NewTargetFilename);
// check if target file is default
NewTargetDirectoryIsDefault:=
CompareFilenames(ChompPathDelim(EnvironmentOptions.GetParsedLazarusDirectory),
ChompPathDelim(NewTargetDirectory))=0;
NewTargetFilenameIsDefault:=NewTargetDirectoryIsDefault;
if NewTargetFilenameIsDefault then begin
CurTargetFilename:=CreateRelativePath(NewTargetFilename,NewTargetDirectory);
NewTargetFilenameIsDefault:=CurTargetFilename=DefaultTargetFilename;
end;
// create output directories
if not NewTargetDirectoryIsDefault then begin
Result:=ForceDirectoryInteractive(NewTargetDirectory,[]);
if Result<>mrOk then exit;
end;
if NewUnitDirectory<>'' then begin
Result:=ForceDirectoryInteractive(NewUnitDirectory,[]);
if Result<>mrOk then exit;
end;
OutputDirRedirected:=NewTargetDirectory<>'';
// create apple bundle if needed
//debugln(['CreateBuildLazarusOptions NewTargetDirectory=',NewTargetDirectory]);
if (Profile.TargetPlatform in [lpCarbon,lpCocoa])
and (not NewTargetDirectoryIsDefault)
and (DirectoryIsWritableCached(NewTargetDirectory)) then begin
CurTargetFilename:=NewTargetFilename;
BundleDir:=ChangeFileExt(CurTargetFilename,'.app');
//debugln(['CreateBuildLazarusOptions checking bundle ',BundleDir]);
if not FileExistsCached(BundleDir) then begin
//debugln(['CreateBuildLazarusOptions CurTargetFilename=',CurTargetFilename]);
Result:=CreateApplicationBundle(CurTargetFilename, 'Lazarus');
if not (Result in [mrOk,mrIgnore]) then begin
debugln(['CreateBuildLazarusOptions CreateApplicationBundle failed']);
if IDEMessagesWindow<>nil then
IDEMessagesWindow.AddMsg('Error: failed to create application bundle '+BundleDir,NewTargetDirectory,-1);
exit;
end;
Result:=CreateAppBundleSymbolicLink(CurTargetFilename);
if not (Result in [mrOk,mrIgnore]) then begin
debugln(['CreateBuildLazarusOptions CreateAppBundleSymbolicLink failed']);
if IDEMessagesWindow<>nil then
IDEMessagesWindow.AddMsg('Error: failed to create application bundle symlink to '+CurTargetFilename,NewTargetDirectory,-1);
exit;
end;
end;
end;
if NewUnitDirectory<>'' then
// FPC interpretes '\ ' as an escape for a space in a path,
// so make sure the directory doesn't end with the path delimeter.
AppendExtraOption('-FU'+ChompPathDelim(NewUnitDirectory));
if not NewTargetDirectoryIsDefault then
// FPC interpretes '\ ' as an escape for a space in a path,
// so make sure the directory doesn't end with the path delimeter.
AppendExtraOption('-FE'+ChompPathDelim(NewTargetDirectory));
if not NewTargetFilenameIsDefault then begin
// FPC automatically changes the last extension (append or replace)
// For example under linux, where executables don't need any extension
// fpc removes the last extension of the -o option.
// Trick fpc:
AppendExtraOption('-o'+NewTargetFilename);
end;
// add package Profile for IDE
//DebugLn(['CreateBuildLazarusOptions blfUseMakeIDECfg=',blfUseMakeIDECfg in FLags,' ExtraOptions="',AExOptions,'" ',PackageOptions]);
if not (blfUseMakeIDECfg in Flags) then
AppendExtraOption(PackageOptions,false);
//DebugLn(['CreateBuildLazarusOptions ',MMDef.Name,' ',AExOptions]);
end;
function SaveIDEMakeOptions(Profile: TBuildLazarusProfile;
Macros: TTransferMacroList;
const PackageOptions: string; Flags: TBuildLazarusFlags): TModalResult;
function BreakOptions(const OptionString: string): string;
var
StartPos: Integer;
EndPos: Integer;
c: Char;
CurLine: String;
begin
Result:='';
// write each option into a line of its own
StartPos:=1;
repeat
while (StartPos<=length(OptionString)) and (OptionString[StartPos]=' ') do
inc(StartPos);
EndPos:=StartPos;
while EndPos<=length(OptionString) do begin
c:=OptionString[EndPos];
case c of
' ': break;
'''','"','`':
begin
repeat
inc(EndPos);
if (OptionString[EndPos]=c) then begin
inc(EndPos);
break;
end;
until (EndPos>length(OptionString));
end;
else
inc(EndPos);
end;
end;
if (EndPos>StartPos) then begin
CurLine:=Trim(copy(OptionString,StartPos,EndPos-StartPos));
if (length(CurLine)>2) and (CurLine[1] in ['''','"','`'])
and (CurLine[1]=CurLine[length(CurLine)]) then begin
// whole line enclosed in quotation marks
// in fpc config this is forbidden and gladfully unncessary
CurLine:=copy(CurLine,2,length(CurLine)-2);
end;
Result:=Result+CurLine+LineEnding;
end;
StartPos:=EndPos;
until StartPos>length(OptionString);
end;
var
ExOptions: String;
Filename: String;
fs: TFileStream;
OptionsAsText: String;
UpdateRevisionInc: boolean;
OutputDirRedirected: boolean;
begin
ExOptions:='';
Result:=CreateIDEMakeOptions(Profile, Macros, PackageOptions,
Flags, ExOptions, UpdateRevisionInc, OutputDirRedirected);
if Result<>mrOk then exit;
Filename:=GetMakeIDEConfigFilename;
try
InvalidateFileStateCache;
fs:=TFileStream.Create(UTF8ToSys(Filename),fmCreate);
try
if ExOptions<>'' then begin
OptionsAsText:=BreakOptions(ExOptions);
fs.Write(OptionsAsText[1],length(OptionsAsText));
end;
finally
fs.Free;
end;
except
on E: Exception do begin
Result:=IDEMessageDialog(lisLazBuildErrorWritingFile,
Format(lisLazBuildUnableToWriteFile, [Filename, #13])
+E.Message,
mtError,[mbCancel,mbAbort]);
exit;
end;
end;
Result:=mrOk;
end;
function GetMakeIDEConfigFilename: string;
begin
Result:=AppendPathDelim(GetPrimaryConfigPath)+DefaultIDEMakeOptionFilename;
end;
{ TConfigureBuildLazarusDlg }
constructor TConfigureBuildLazarusDlg.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
fProfiles:=TBuildLazarusProfiles.Create;
fUpdatingProfileCombo:=False;
end;
destructor TConfigureBuildLazarusDlg.Destroy;
begin
fProfiles.Free;
inherited Destroy;
end;
procedure TConfigureBuildLazarusDlg.FormCreate(Sender: TObject);
var
LCLInterface: TLCLPlatform;
begin
IDEDialogLayoutList.ApplyLayout(Self,700,480);
Caption := Format(lisConfigureBuildLazarus, ['"', '"']);
PageControl1.ActivePage:=BuildTabSheet;
BuildTabSheet.Caption:=lisBuildCaption;
// Show Build target names in combobox.
LCLWidgetTypeLabel.Caption := lisLCLWidgetType;
for LCLInterface:=Low(TLCLPlatform) to High(TLCLPlatform) do
LCLWidgetTypeComboBox.Items.Add(LCLPlatformDisplayNames[LCLInterface]);
BuildProfileLabel.Caption:=lisLazBuildProfile;
BuildProfileButton.Hint := lisLazBuildManageProfiles2;
BuildProfileComboBox.Hint := lisLazBuildNameOfTheActiveProfile;
OptionsLabel.Caption := lisLazBuildOptions;
TargetOSLabel.Caption := lisLazBuildTargetOS;
TargetCPULabel.Caption := lisLazBuildTargetCPU;
TargetDirectoryLabel.Caption := lisLazBuildTargetDirectory;
DefinesListBox.Hint := lisLazBuildDefinesWithoutD;
DefinesLabel.Caption := lisLazBuildDefines;
DefinesButton.Caption := lisLazBuildEditDefines;
DefinesButton.Hint := lisLazBuildEditListOfDefinesWhichCanBeUsedByAnyProfile;
CleanUpGroupBox.Caption:=lisCleanUp;
CleanAutoRadioButton.Caption:=lisAutomatically;
CleanCommonRadioButton.Caption:=lisCleanCommonFiles;
CleanAllRadioButton.Caption:=lisCleanAll;
CleanOnceCheckBox.Caption:=lisCleanOnlyOnce;
CleanOnceCheckBox.Hint:=lisAfterCleaningUpSwitchToAutomaticClean;
UpdateRevisionIncCheckBox.Caption := lisLazBuildUpdateRevInc;
UpdateRevisionIncCheckBox.Hint := lisLazBuildUpdateRevisionInfoInAboutLazarusDialog;
CommonsDividerBevel.Caption := lisLazBuildCommonSettings;
RestartAfterBuildCheckBox.Caption := lisLazBuildRestartAfterBuild;
RestartAfterBuildCheckBox.Hint := lisLazBuildRestartLazarusAutomatically;
ConfirmBuildCheckBox.Caption := lisLazBuildConfirmBuild;
ConfirmBuildCheckBox.Hint := lisLazBuildShowConfirmationDialogWhenBuilding;
CompileButton.Caption := lisBuild;
CompileButton.LoadGlyphFromLazarusResource('menu_build');
CompileAdvancedButton.Caption := lisLazBuildBuildMany;
CompileAdvancedButton.LoadGlyphFromLazarusResource('menu_build_all');
SaveSettingsButton.Caption := lisSaveSettings;
SaveSettingsButton.LoadGlyphFromStock(idButtonSave);
if SaveSettingsButton.Glyph.Empty then
SaveSettingsButton.LoadGlyphFromLazarusResource('laz_save');
CancelButton.Caption := lisCancel;
HelpButton.Caption := lisMenuHelp;
OptionsMemo.Hint := lisLazBuildOptionsPassedToCompiler;
with TargetOSComboBox do
begin
with Items do begin
Add(''); //('+rsiwpDefault+')');
Add('Darwin');
Add('FreeBSD');
Add('Linux');
Add('NetBSD');
Add('OpenBSD');
Add('Solaris');
Add('Win32');
Add('Win64');
Add('WinCE');
Add('go32v2');
Add('os2');
Add('beos');
Add('haiku');
Add('qnx');
Add('netware');
Add('wdosx');
Add('emx');
Add('watcom');
Add('netwlibc');
Add('amiga');
Add('atari');
Add('palmos');
Add('gba');
Add('nds');
Add('macos');
Add('morphos');
Add('embedded');
Add('symbian');
end;
ItemIndex:=0;
end;
with TargetCPUComboBox do begin
with Items do begin
Add(''); //('+rsiwpDefault+')');
Add('arm');
Add('i386');
Add('m68k');
Add('powerpc');
Add('sparc');
Add('x86_64');
end;
ItemIndex:=0;
end;
SetupInfoPage;
end;
procedure TConfigureBuildLazarusDlg.FormResize(Sender: TObject);
begin
LCLWidgetTypeComboBox.Width:=(OptionsMemo.Width - 12) div 3;
TargetOSComboBox.Width:=LCLWidgetTypeComboBox.Width;
DefinesListBox.Width:=(OptionsMemo.Width - 6) div 2;
end;
procedure TConfigureBuildLazarusDlg.FormShow(Sender: TObject);
begin
UpdateProfileNamesUI;
end;
procedure TConfigureBuildLazarusDlg.HelpButtonClick(Sender: TObject);
begin
LazarusHelp.ShowHelpForIDEControl(Self);
end;
procedure TConfigureBuildLazarusDlg.ShowOptsMenuItemClick(Sender: TObject);
begin
CopyUIToProfile(Profiles.Current);
ShowMessage(fProfiles.Current.ExtraOptions);
end;
procedure TConfigureBuildLazarusDlg.TargetDirectoryButtonClick(Sender: TObject);
var
AFilename: String;
DirDialog: TSelectDirectoryDialog;
begin
DirDialog:=TSelectDirectoryDialog.Create(nil);
try
DirDialog.Options:=DirDialog.Options+[ofPathMustExist];
DirDialog.Title:=lisLazBuildABOChooseOutputDir+'(lazarus'+
GetExecutableExt(Profiles.Current.FPCTargetOS)+')';
if DirDialog.Execute then begin
AFilename:=CleanAndExpandDirectory(DirDialog.Filename);
TargetDirectoryComboBox.AddHistoryItem(AFilename,10,true,true);
end;
finally
DirDialog.Free;
end;
end;
procedure TConfigureBuildLazarusDlg.CopyProfileToUI(AProfile: TBuildLazarusProfile);
var
i: Integer;
begin
LCLWidgetTypeComboBox.ItemIndex :=ord(AProfile.TargetPlatform);
UpdateRevisionIncCheckBox.Checked :=AProfile.UpdateRevisionInc;
TargetOSComboBox.Text :=AProfile.TargetOS;
TargetDirectoryComboBox.Text :=AProfile.TargetDirectory;
TargetCPUComboBox.Text :=AProfile.TargetCPU;
case AProfile.IdeBuildMode of
bmBuild: CleanAutoRadioButton.Checked:=true;
bmCleanBuild: CleanCommonRadioButton.Checked:=true;
bmCleanAllBuild: CleanAllRadioButton.Checked:=true;
end;
CleanOnceCheckBox.Checked:=AProfile.CleanOnce;
OptionsMemo.Lines.Assign(AProfile.OptionsLines);
for i:=0 to DefinesListBox.Items.Count-1 do
DefinesListBox.Checked[i]:=AProfile.Defines.IndexOf(DefinesListBox.Items[i]) > -1;
end;
procedure TConfigureBuildLazarusDlg.CopyUIToProfile(AProfile: TBuildLazarusProfile);
var
i: Integer;
begin
AProfile.TargetPlatform :=TLCLPlatform(LCLWidgetTypeComboBox.ItemIndex);
AProfile.UpdateRevisionInc :=UpdateRevisionIncCheckBox.Checked;
AProfile.TargetOS :=TargetOSComboBox.Text;
AProfile.TargetDirectory :=TargetDirectoryComboBox.Text;
AProfile.TargetCPU :=TargetCPUComboBox.Text;
if CleanAllRadioButton.Checked then
AProfile.IdeBuildMode := bmCleanAllBuild
else if CleanCommonRadioButton.Checked then
AProfile.IdeBuildMode := bmCleanBuild
else
AProfile.IdeBuildMode := bmBuild;
AProfile.CleanOnce:=CleanOnceCheckBox.Checked;
AProfile.OptionsLines.Assign(OptionsMemo.Lines);
AProfile.Defines.Clear;
for i:=0 to DefinesListBox.Items.Count-1 do
if DefinesListBox.Checked[i] then
AProfile.Defines.Add(DefinesListBox.Items[i]);
end;
procedure TConfigureBuildLazarusDlg.UpdateProfileNamesUI;
var
i: Integer;