File: lazbuild.lpr

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (1937 lines) | stat: -rw-r--r-- 66,788 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
{***************************************************************************
 *                                                                         *
 *   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.        *
 *                                                                         *
 ***************************************************************************
 
  Command line utility to compile lazarus projects and packages.
}
program lazbuild;

{$mode objfpc}{$H+}

uses
  {$IF defined(HASAMIGA)}
  athreads,
  {$ENDIF}
  {$IF defined(UNIX)}
  cthreads,
  {$ENDIF}
  Classes, SysUtils, Math, CustApp, System.UITypes,
  Interfaces, // this includes the NoGUI widgetset
  // LazUtils
  Masks, LConvEncoding, FileUtil, LazFileUtils, LazLoggerBase, LazUtilities,
  LazUTF8, Laz2_XMLCfg, LazStringUtils, FPCAdds,
  // LCL
  LCLPlatformDef,
  // Codetools
  CodeCache, CodeToolManager, DefineTemplates, FileProcs,
  // BuildIntf
  BaseIDEIntf, MacroIntf, PackageIntf, LazMsgWorker, ProjectIntf, IDEExternToolIntf,
  CompOptsIntf, IDEOptionsIntf, PackageDependencyIntf,
  // IdeConfig
  LazConf, IDECmdLine, TransferMacros, EnvironmentOpts, ParsedCompilerOpts, CompilerOptions,
  // IdePackager
  IdePackagerStrConsts,
  // IDE
  InitialSetupProc, ExtToolsConsole, ApplicationBundle,
  IDETranslations, LazarusIDEStrConsts, MiscOptions, Project, PackageDefs,
  PackageLinks, PackageSystem, InterPkgConflictFiles, BuildLazDialog,
  BuildProfileManager, BuildManager, BaseBuildManager, ModeMatrixOpts,
  ColorTTY;

type
  TPkgAction = (
    lpaBuild, // build package, default
    lpaInstall, // install package
    lpaAddPkgLinks // register, no build
    );

  { TLazBuildApplication }

  TLazBuildApplication = class(TCustomApplication)
  private
    FBuildAll: boolean;
    FBuildIDE: boolean;
    FBuildIDEOptions: string;
    FBuildModeOverride: String;
    FBuildRecursive: boolean;
    fCompilerInCfg: string;
    fCompilerOverride: String;
    fCPUOverride: String;
    FCreateMakefile: boolean;
    fInitialized: boolean;
    fInitResult: boolean;
    fLazarusDirInCfg: string;
    fLazarusDirOverride : String;
    FMaxProcessCount: integer;
    FNoWriteProject: Boolean;
    fOSOverride: String;
    FPackageAction: TPkgAction;
    FPkgGraphVerbosity: TPkgVerbosityFlags;
    FSkipDependencies: boolean;
    FSubtargetOverride: boolean;
    FSubtargetOverrideValue: String;
    fWidgetsetOverride: String;

    function HasLongOptIgnoreCase(const S: String; out aValue: String): Boolean;
    function HasShortOrLongOpt(const C: Char; const S: String): Boolean;

    // codetools
    procedure CodeBufferDecodeLoaded({%H-}Code: TCodeBuffer;
         const {%H-}Filename: string; var Source, DiskEncoding, MemEncoding: string);
    procedure CodeBufferEncodeSaving(Code: TCodeBuffer;
                                    const {%H-}Filename: string; var Source: string);

    // global package functions
    procedure GetDependencyOwnerDescription(Dependency: TPkgDependency;
                                            out Description: string);
    procedure GetDependencyOwnerDirectory(Dependency: TPkgDependency;
                                          out Directory: string);
    // Event procedure that adds every package added to the package graph to the (user) package links
    procedure PackageGraphAddPackage(Pkg: TLazPackage);
    function PackageGraphCheckInterPkgFiles(IDEObject: TObject;
                          PkgList: TFPList; out FilesChanged: boolean): boolean;

    // project
    procedure ProjectInfoFileChanged(TheProject: TProject);

    // dialogs
    function IDEMessageDialogHandler(const aCaption, aMsg: string;
                         {%H-}DlgType: TMsgDlgType; {%H-}Buttons: TMsgDlgButtons;
                         const {%H-}HelpKeyword: string): Integer;
    function IDEQuestionDialogHandler(const aCaption, aMsg: string;
                         {%H-}DlgType: TMsgDlgType; {%H-}Buttons: array of const;
                         const {%H-}HelpKeyword: string): Integer;
  protected
    function GetParams(Index: Integer): String; override;
    function GetParamCount: Integer; override;
    function HasCustomCompilerOpts(out aValue: string): boolean;

    // Builds project or package, depending on extension.
    // Packages can also be specified by package name if they are known to the IDE.
    function BuildFile(Filename: string): boolean;

    // packages
    // Build a package identified by filename and return build result
    function BuildPackage(const AFilename: string): boolean;
    // Load package file into loaded packages (package graph), overwriting any package with the same name
    function LoadPackage(const AFilename: string): TLazPackage;
    procedure CompilePackage(APackage: TLazPackage; Flags: TPkgCompileFlags);
    procedure DoCreateMakefile(APackage: TLazPackage);
    procedure CheckPackageGraphForCompilation(APackage: TLazPackage;
                                 FirstDependency: TPkgDependency);

    // projects
    function BuildProject(const AFilename: string): boolean;
    function LoadProject(const AFilename: string): TProject;
    procedure CloseProject(var AProject: TProject);

    // Adding packages to list of to-be-installed packages in the IDE.
    // The packages can then be installed by recompiling the IDE (because we're using static packages)
    function AddPackagesToInstallList(const PackageNamesOrFiles: TStringList): boolean;
    function AddCmdLinePackageLinks(const PackageNamesOrFiles: TStringList): boolean;

    // IDE
    function BuildLazarusIDE: boolean;
    function CompileAutoInstallPackages(Clean: boolean): boolean;

    function Init: boolean;
    procedure LoadEnvironmentOptions;
    procedure LoadMiscellaneousOptions;
    procedure SetupMacros;
    procedure SetupCodetools;
    procedure SetupPackageSystem;
    procedure SetupDialogs;
    procedure StoreBaseSettings;
    function RepairedCheckOptions(Const ShortOptions : String;
                   Const Longopts : TStrings; Opts,NonOpts : TStrings) : String;
  public
    // Files (or package names) passed by the user to Lazbuild:
    Files: TStringList;
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;

    procedure Run;
    function ParseParameters: boolean;
    procedure WriteUsage;
    procedure PrintInfo(const Msg: string);
    procedure PrintHint(const Msg: string);
    procedure PrintWarning(const Msg: string);
    procedure PrintErrorAndHalt(Code: Byte; const Msg: string);

    property PackageAction: TPkgAction read FPackageAction write FPackageAction;
    property BuildAll: boolean read FBuildAll write FBuildAll;// build all files of project/package
    property BuildRecursive: boolean read FBuildRecursive // apply BuildAll flag to dependencies
                                     write FBuildRecursive;
    property SkipDependencies: boolean read FSkipDependencies
                                            write FSkipDependencies;
    property BuildIDE: boolean read FBuildIDE write FBuildIDE; // build IDE (as opposed to a project/package etc)
    property BuildIDEOptions: string read FBuildIDEOptions write FBuildIDEOptions;
    property CreateMakefile: boolean read FCreateMakefile write FCreateMakefile;
    property WidgetSetOverride: String read FWidgetsetOverride write FWidgetsetOverride;
    property OSOverride: String read fOSOverride write fOSOverride;
    property CPUOverride: String read fCPUOverride write fCPUOverride;
    property SubtargetOverride: boolean read FSubtargetOverride write FSubtargetOverride;
    property SubtargetOverrideValue: String read FSubtargetOverrideValue write FSubtargetOverrideValue;
    property CompilerOverride: String read fCompilerOverride write fCompilerOverride;
    property LazarusDirOverride: String read fLazarusDirOverride write fLazarusDirOverride;
    property BuildModeOverride: String read FBuildModeOverride write FBuildModeOverride;
    property MaxProcessCount: integer read FMaxProcessCount write FMaxProcessCount;
    property NoWriteProject: boolean read FNoWriteProject write FNoWriteProject;
    property PkgGraphVerbosity: TPkgVerbosityFlags read FPkgGraphVerbosity write FPkgGraphVerbosity;
  end;

var
  LazBuildApp: TLazBuildApplication = nil;

const
  ErrorFileNotFound       = 1;
  ErrorBuildFailed        = 2;
  ErrorLoadPackageFailed  = 3;
  ErrorPackageNameInvalid = 4;
  ErrorLoadProjectFailed  = 5;
  ErrorInvalidSyntax      = 6;
  ErrorInitialization     = 7;
  ErrorExpandMacro        = 8;
  VersionStr = {$I packages/ideconfig/version.inc};

procedure FilterConfigFileContent;
var
  l: TStrings;
  i: Integer;
  LowerOpt: String;
begin
  ResetParamsAndCfg;
  l := GetCfgFileContent;
  if l = nil then exit;
  i := l.Count - 1;
  while i >= 0 do begin
    LowerOpt := LowerCase(l[i]);
    if not(
        LazStartsStr('--primary-config-path=', LowerOpt) or
        LazStartsStr('--secondary-config-path=', LowerOpt) or
        LazStartsStr('--pcp=', LowerOpt) or
        LazStartsStr('--scp=', LowerOpt)
       )
    then
      l.Delete(i);
    dec(i);
  end;
end;

Function ToolParamCount: Integer;
begin
  Result := GetParamsAndCfgFile.Count - 1;
end;

Function ToolParamStr(Param : Integer) : Ansistring;
begin
  if Param >= GetParamsAndCfgFile.Count then
    Result := ''
  else
    Result := GetParamsAndCfgFile[Param];
end;

procedure GetDescriptionOfDependencyOwner(Dependency: TPkgDependency;
  out Description: string);
var
  DepOwner: TObject;
begin
  DepOwner:=Dependency.Owner;
  if (DepOwner<>nil) then begin
    if DepOwner is TLazPackage then begin
      Description:=Format(lisPkgMangPackage, [TLazPackage(DepOwner).IDAsString]);
    end else if DepOwner is TProject then begin
      Description:=Format(lisPkgMangProject,
                          [ExtractFileNameOnly(TProject(DepOwner).ProjectInfoFile)]);
    end else begin
      Description:=dbgsName(DepOwner)
    end;
  end else begin
    Description:=Format(lisPkgMangDependencyWithoutOwner, [Dependency.AsString]);
  end;
end;

procedure GetDirectoryOfDependencyOwner(Dependency: TPkgDependency;
  out Directory: string);
var
  DepOwner: TObject;
begin
  DepOwner:=Dependency.Owner;
  if (DepOwner<>nil) then begin
    if DepOwner is TLazPackage then begin
      Directory:=TLazPackage(DepOwner).Directory;
    end else if DepOwner is TProject then begin
      Directory:=TProject(DepOwner).Directory;
    end else begin
      Directory:=''
    end;
  end else begin
    Directory:=''
  end;
end;

procedure ShowBuildModes;
var
  i: Integer;
begin
  // first print the active build mode
  for i := 0 to Project1.BuildModes.Count - 1 do
    if Project1.BuildModes[i] = Project1.ActiveBuildMode then
      WriteLn(Project1.BuildModes[i].Identifier);
  // print other build modes
  for i := 0 to Project1.BuildModes.Count - 1 do
    if Project1.BuildModes[i] <> Project1.ActiveBuildMode then
      WriteLn(Project1.BuildModes[i].Identifier);
end;

procedure ShowBuildModeError(const aBuildModeOverride: string);
begin
  WriteLn(Format(lisERRORInvalidBuildMode, [aBuildModeOverride]));
  if Project1.BuildModes.Count>1 then
  begin
    WriteLn(lisAvailableProjectBuildModes);
    ShowBuildModes;
  end else
    WriteLn(lisThisProjectHasOnlyTheDefaultBuildMode);
  WriteLn;
  Halt(ErrorBuildFailed);
end;

{ TLazBuildApplication }

procedure TLazBuildApplication.CodeBufferEncodeSaving(Code: TCodeBuffer;
  const Filename: string; var Source: string);
begin
  if (Code.DiskEncoding<>'') and (Code.MemEncoding<>'')
  and (Code.DiskEncoding<>Code.MemEncoding) then begin
    {$IFDEF VerboseIDEEncoding}
    DebugLn(['TLazBuildApplication.OnCodeBufferEncodeSaving Filename=',Code.Filename,' Mem=',Code.MemEncoding,' to Disk=',Code.DiskEncoding]);
    {$ENDIF}
    Source:=ConvertEncoding(Source,Code.MemEncoding,Code.DiskEncoding);
  end;
end;

procedure TLazBuildApplication.CodeBufferDecodeLoaded(Code: TCodeBuffer;
  const Filename: string; var Source, DiskEncoding, MemEncoding: string);
begin
  //DebugLn(['TLazBuildApplication.OnCodeBufferDecodeLoaded Filename=',Filename,' Encoding=',GuessEncoding(Source)]);
  DiskEncoding:='';
  if DiskEncoding='' then
    DiskEncoding:=GuessEncoding(Source);
  MemEncoding:=EncodingUTF8;
  if (DiskEncoding<>MemEncoding) then begin
    {$IFDEF VerboseIDEEncoding}
    DebugLn(['TLazBuildApplication.OnCodeBufferDecodeLoaded Filename=',Filename,' Disk=',DiskEncoding,' to Mem=',MemEncoding]);
    {$ENDIF}
    Source:=ConvertEncoding(Source,DiskEncoding,MemEncoding);
    //DebugLn(['TLazBuildApplication.OnCodeBufferDecodeLoaded ',Source]);
  end;
end;

procedure TLazBuildApplication.GetDependencyOwnerDescription(
  Dependency: TPkgDependency; out Description: string);
begin
  GetDescriptionOfDependencyOwner(Dependency,Description);
end;

procedure TLazBuildApplication.GetDependencyOwnerDirectory(
  Dependency: TPkgDependency; out Directory: string);
begin
  GetDirectoryOfDependencyOwner(Dependency,Directory);
end;

procedure TLazBuildApplication.PackageGraphAddPackage(Pkg: TLazPackage);
begin
  if FileExistsUTF8(Pkg.FileName) then LazPackageLinks.AddUserLink(Pkg);
end;

function TLazBuildApplication.PackageGraphCheckInterPkgFiles(
  IDEObject: TObject; PkgList: TFPList; out FilesChanged: boolean): boolean;
begin
  Result:=CheckInterPkgFiles(IDEObject,PkgList,FilesChanged);
end;

procedure TLazBuildApplication.ProjectInfoFileChanged(TheProject: TProject);
begin
  if TheProject<>Project1 then exit;
  if TheProject.IsVirtual then
    CodeToolBoss.SetGlobalValue(ExternalMacroStart+'ProjPath',VirtualDirectory)
  else
    CodeToolBoss.SetGlobalValue(ExternalMacroStart+'ProjPath',Project1.Directory)
end;

function TLazBuildApplication.IDEMessageDialogHandler(const aCaption, aMsg: string;
  DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; const HelpKeyword: string): Integer;
begin
  DumpStack;
  PrintErrorAndHalt(ErrorBuildFailed, Format(lisLazbuildIsNonInteractiveAbortingNow, [
    aCaption, LineEnding, aMsg, LineEnding]));
  Result:=mrCancel;
end;

function TLazBuildApplication.IDEQuestionDialogHandler(const aCaption, aMsg: string;
  DlgType: TMsgDlgType; Buttons: array of const; const HelpKeyword: string): Integer;
begin
  DumpStack;
  PrintErrorAndHalt(ErrorBuildFailed, Format(lisLazbuildIsNonInteractiveAbortingNow, [
    aCaption, LineEnding, aMsg, LineEnding]));
  Result:=mrCancel;
end;

function TLazBuildApplication.GetParams(Index: Integer): String;
begin
  Result := ToolParamStr(Index);
end;

function TLazBuildApplication.GetParamCount: Integer;
begin
  Result := ToolParamCount;
end;

function TLazBuildApplication.HasCustomCompilerOpts(out aValue: string): boolean;
var
  p: string; // current parameter
  v: string; // value of current parameter
  i: Integer;
begin
  aValue := '';
  for i := 1 to GetParamCount do
  begin
    p := GetParams(i);
    if LazStartsText('--opt=', p) then
    begin
      // get value
      v := copy(p, length('--opt=') + 1, length(p));
      // remove quotes
      if length(v) >= 2 then
        if ((v[1] = '"' ) and (v[length(v)] = '"' )) or
           ((v[1] = '''') and (v[length(v)] = ''''))
        then
          v := copy(v, 2, length(v) - 2);
      // append
      aValue := MergeCustomOptions(aValue, v);
    end;
  end;
  result := aValue <> '';
end;

function TLazBuildApplication.BuildFile(Filename: string): boolean;
var
  OriginalFilename: string;
  Package: TLazPackageLink;
begin
  Result:=false;
  OriginalFilename:=FileName;

  Filename:=CleanAndExpandFilename(Filename);
  if not FileExistsUTF8(Filename) then
  begin
    // File doesn't exist.

    // Check for packages if the specified name is a valid identifier
    //debugln(['TLazBuildApplication.BuildFile ',OriginalFilename]);
    if IsValidPkgName(OriginalFileName) then begin
      if PackageAction=lpaAddPkgLinks then
        PrintErrorAndHalt(ErrorFileNotFound, 'lpk file expected, but "' + OriginalFilename + '" found');
      // Initialize package graph with base packages etc:
      if not Init then exit;
      // Could be a known but not installed package
      // so try and get package filename from all other known packages
      Package:=TLazPackageLink(LazPackageLinks.FindLinkWithPkgName(OriginalFileName));
      if Package=nil then begin
        // Not found after everything we tried
        if FilenameExtIs(Filename,'lpi', false) then
          PrintErrorAndHalt(ErrorFileNotFound, 'File not found: "' + OriginalFilename + '"')
        else
          PrintErrorAndHalt(ErrorFileNotFound, 'Package not found: "' + OriginalFilename + '"');
      end
      else begin
        // We found a package link
        case PackageAction of
        lpaBuild:
          exit(BuildPackage(Package.LPKFilename));
        lpaInstall:
          exit(true); // this is handled in AddPackagesToInstallList
        end;
      end;
    end;

    PrintErrorAndHalt(ErrorFileNotFound, 'File not found: "' + OriginalFilename + '"');
  end
  else if DirPathExists(Filename) then
    PrintErrorAndHalt(ErrorFileNotFound, '"' + Filename + '" is a directory')
  else begin
    // File exists:
    if FilenameExtIs(Filename,'lpk',true) then begin
      case PackageAction of
      lpaBuild: Result:=BuildPackage(Filename);
      lpaInstall: Result:=true; // this is handled in AddPackagesToInstallList
      lpaAddPkgLinks: Result:=true;
      end;
    end else if FilenameExtIs(Filename,'lpi',false) then
      Result:=BuildProject(Filename)
    else if FilenameExtIs(Filename,'lpr',false) then begin
      Filename:=ChangeFileExt(Filename,'.lpi');
      if FileExists(Filename) then
        Result:=BuildProject(Filename)
      else
        PrintErrorAndHalt(ErrorFileNotFound, 'File not found: "' + Filename + '"');
    end else
      PrintErrorAndHalt(ErrorBuildFailed, 'Don''t know how to build: "' + Filename + '"');
  end;
end;

function TLazBuildApplication.BuildPackage(const AFilename: string): boolean;
var
  APackage: TLazPackage;
  Flags: TPkgCompileFlags;
  S: String;
begin
  Result:=false;
  
  if not Init then exit;

  PrintHint('Compile package "' + AFilename + '"');

  APackage:=LoadPackage(AFilename);
  if APackage=nil then
    PrintErrorAndHalt(ErrorLoadPackageFailed, 'Unable to load package "' + AFilename + '"');
    
  Flags:=[];
  if BuildAll then
    Include(Flags,pcfCleanCompile)
  else
    Include(Flags,pcfOnlyIfNeeded);
  if BuildRecursive and BuildAll then
    Include(Flags,pcfCompileDependenciesClean);
  if SkipDependencies then
    Include(Flags,pcfDoNotCompileDependencies);

  if (Length(OSOverride) <> 0) then
    APackage.CompilerOptions.TargetOS:=OSOverride;
  if (Length(CPUOverride) <> 0) then
    APackage.CompilerOptions.TargetCPU:=CPUOverride;
  if SubtargetOverride then
    APackage.CompilerOptions.Subtarget:=SubtargetOverrideValue;
  if HasCustomCompilerOpts(S) then
    with APackage.CompilerOptions do
      CustomOptions := MergeCustomOptions(CustomOptions, S);

  if CreateMakefile then
    DoCreateMakefile(APackage)
  else
    CompilePackage(APackage,Flags);

  LazPackageLinks.SaveUserLinks(true);

  Result:=true;
end;

function TLazBuildApplication.LoadPackage(const AFilename: string): TLazPackage;
var
  XMLConfig: TXMLConfig;
  ConflictPkg: TLazPackage;
begin
  // check if package is already loaded
  Result:=PackageGraph.FindPackageWithFilename(AFilename);
  if (Result<>nil) then exit;
  if not FileExists(AFilename) then
    PrintErrorAndHalt(ErrorLoadPackageFailed, 'Package file not found: "' + AFilename + '"');

  Result:=TLazPackage.Create;
  // load the package file
  XMLConfig:=TXMLConfig.Create(AFilename);
  try
    Result.Filename:=AFilename;
    Result.LoadFromXMLConfig(XMLConfig,'Package/');
  finally
    XMLConfig.Free;
  end;
  // check Package Name
  if not IsValidPkgName(Result.Name) then
    PrintErrorAndHalt(ErrorPackageNameInvalid,
      Format(lisPkgMangThePackageNameOfTheFileIsInvalid, [Result.Name, LineEnding, Result.Filename]));

  // check if Package with same name is already loaded
  ConflictPkg:=PackageGraph.FindPackageWithName(Result.Name,nil);
  if ConflictPkg<>nil then begin
    // replace package
    if not PackageGraph.PackageCanBeReplaced(ConflictPkg,Result) then
      PrintErrorAndHalt(ErrorLoadPackageFailed,
        'Cannot replace loaded package '+ConflictPkg.IDAsString+' with '+Result.IDAsString+' from "'+Result.Filename+'"');
    PackageGraph.ReplacePackage(ConflictPkg,Result);
  end else begin
    // add to graph
    PackageGraph.AddPackage(Result);
  end;
  // save package file links
  LazPackageLinks.SaveUserLinks;
end;

function TLazBuildApplication.BuildLazarusIDE: boolean;
var
  Flags: TBuildLazarusFlags;
  CurResult: TModalResult;
  BuildLazProfiles: TBuildLazarusProfiles;
  CurProf: TBuildLazarusProfile;
  InheritedOptionStrings: TInheritedCompOptsStrings;
  TargetDir: String;
  i: Integer;
  s: String;
  Builder: TLazarusBuilder;
begin
  Result:=false;
  if not Init then exit;

  LoadMiscellaneousOptions;
  BuildLazProfiles:=MiscellaneousOptions.BuildLazProfiles;
  CurProf:=BuildLazProfiles.Current;
  if BuildModeOverride<>'' then
  begin
    i:=BuildLazProfiles.IndexByName(BuildModeOverride);
    if i<0 then
    begin
      debugln(['Error: (lazbuild) IDE build mode "' + BuildModeOverride + '" not found']);
      if ConsoleVerbosity>=0 then begin
        debugln;
        debugln('Available IDE build modes:');
        for i:=0 to BuildLazProfiles.Count-1 do
        begin
          if BuildLazProfiles[i]=CurProf then
            dbgout('* ')
          else
            dbgout('  ');
          debugln(BuildLazProfiles[i].Name);
        end;
      end;
      Halt(ErrorBuildFailed);
    end;
    CurProf:=BuildLazProfiles[i];
    BuildLazProfiles.CurrentIndex:=i;
  end;
  PrintHint('Building Lazarus IDE with profile "' + CurProf.Name + '"');

  if (Length(OSOverride) <> 0) then
    CurProf.TargetOS:=OSOverride;
  if (Length(CPUOverride) <> 0) then
    CurProf.TargetCPU:=CPUOverride;
  if SubtargetOverride then
    CurProf.Subtarget:=SubtargetOverrideValue;

  if WidgetSetOverride<>'' then
    CurProf.TargetPlatform:=DirNameToLCLPlatform(WidgetSetOverride)
  else
    CurProf.TargetPlatform:=GetBuildLCLWidgetType;

  // add custom options from --build-ide after build mode options
  if BuildIDEOptions <> '' then
    CurProf.ExtraOptions := MergeCustomOptions(CurProf.ExtraOptions, BuildIDEOptions);
  // add parameters from the --opt option after --build-ide, as higher priority
  if HasCustomCompilerOpts(s) then
    CurProf.ExtraOptions := MergeCustomOptions(CurProf.ExtraOptions, s);

  if BuildAll then
    CurProf.IdeBuildMode:=bmCleanAllBuild;
  MainBuildBoss.SetBuildTargetIDE;
  Flags:=[];

  // try loading install packages
  PackageGraph.LoadAutoInstallPackages(BuildLazProfiles.StaticAutoInstallPackages);

  // create target directory
  TargetDir:=CurProf.TargetDirectory;
  IDEMacros.SubstituteMacros(TargetDir);
  if not ForceDirectory(TargetDir) then
    PrintErrorAndHalt(ErrorBuildFailed, 'Failed creating IDE target directory "' + TargetDir + '"');

  // clean
  Builder:=TLazarusBuilder.Create;
  try
    Builder.ProfileChanged:=false;

    if BuildLazProfiles.Current.IdeBuildMode=bmCleanAllBuild then begin
      Builder.PackageOptions:='';
      CurResult:=Builder.MakeLazarus(BuildLazProfiles.Current,
                  Flags+[blfDontBuild]);
      if CurResult<>mrOk then
        PrintErrorAndHalt(ErrorBuildFailed, 'Building IDE: Clean all failed');
    end;

    // save list of install packages
    CurResult:=PackageGraph.SaveAutoInstallConfig;
    if CurResult<>mrOk then
      PrintErrorAndHalt(ErrorBuildFailed, 'Building IDE: Failed saving IDE make config files');

    // compile auto install static packages
    if not CompileAutoInstallPackages(BuildLazProfiles.Current.IdeBuildMode<>bmBuild) then
      PrintErrorAndHalt(ErrorBuildFailed, 'Building IDE: Compile AutoInstall Packages failed');

    // create inherited compiler options
    Builder.PackageOptions:=PackageGraph.GetIDEInstallPackageOptions(InheritedOptionStrings{%H-});

    // save idemake.cfg
    CurResult:=Builder.SaveIDEMakeOptions(BuildLazProfiles.Current,Flags+[blfBackupOldExe]);
    if CurResult<>mrOk then
      PrintErrorAndHalt(ErrorBuildFailed, 'Building IDE: Failed saving "idemake.cfg"');

    // compile IDE
    CurResult:=Builder.MakeLazarus(BuildLazProfiles.Current,
                           Flags+[blfUseMakeIDECfg,blfOnlyIDE]);
    if CurResult<>mrOk then
      PrintErrorAndHalt(ErrorBuildFailed, 'Building IDE: Building IDE failed');

    Result:=true;
  finally
    Builder.Free;
  end;
end;

function TLazBuildApplication.CompileAutoInstallPackages(Clean: boolean): boolean;
var
  Dependency: TPkgDependency;
  OldDependency: TPkgDependency;
  CurResult: TModalResult;
  CompilePolicy: TPackageUpdatePolicy;
begin
  Result:=false;
  PackageGraph.BeginUpdate(false);
  try
    Dependency:=PackageGraph.FirstInstallDependency;
    while Dependency<>nil do begin
      OldDependency:=Dependency;
      Dependency:=Dependency.NextRequiresDependency;
      if OldDependency.LoadPackageResult<>lprSuccess then begin
        raise Exception.Create(Format(
            lisPkgMangThePackageIsMarkedForInstallationButCanNotBeFound, [
            OldDependency.AsString, LineEnding]));
      end;
    end;

    // check consistency
    CheckPackageGraphForCompilation(nil,
                      PackageGraph.FirstInstallDependency);

    // compile all auto install dependencies
    CompilePolicy:=pupAsNeeded;
    if (BuildRecursive and BuildAll) or Clean then
      CompilePolicy:=pupOnRebuildingAll;
    CurResult:=PackageGraph.CompileRequiredPackages(nil,
                   PackageGraph.FirstInstallDependency,false,CompilePolicy);
    if CurResult<>mrOk then exit;

  finally
    PackageGraph.EndUpdate;
  end;
  Result:=true;
end;

procedure TLazBuildApplication.CompilePackage(APackage: TLazPackage;
  Flags: TPkgCompileFlags);
begin
  if APackage.Missing then
    PrintErrorAndHalt(ErrorBuildFailed, '"' + APackage.IDAsString + '": lpk file missing');

  // check graph for circles and broken dependencies
  if not (pcfDoNotCompileDependencies in Flags) then begin
    CheckPackageGraphForCompilation(APackage,nil);
  end;

  if PackageGraph.CompilePackage(APackage,Flags,false)<>mrOk then
    PrintErrorAndHalt(ErrorBuildFailed, '"' + APackage.IDAsString + '": compilation failed');
end;

procedure TLazBuildApplication.DoCreateMakefile(APackage: TLazPackage);
begin
  PrintHint('Create makefile for package "' + APackage.Filename + '"');
  PackageGraph.WriteMakeFile(APackage);
end;

procedure TLazBuildApplication.CheckPackageGraphForCompilation(
  APackage: TLazPackage; FirstDependency: TPkgDependency);
  
  function PathListToString(PathList: TFPList): string;
  var
    i: Integer;
    Item: TObject;
  begin
    Result:='';
    for i:=0 to PathList.Count-1 do begin
      Item:=TObject(PathList[i]);
      if Result<>'' then
        Result:=Result+'->';
      if Item is TPkgDependency then begin
        Result:=Result+TPkgDependency(Item).AsString;
      end else if Item is TProject then begin
        Result:=Result
                +'Project:'+ExtractFileNameOnly(TProject(Item).ProjectInfoFile);
      end else if Item is TLazPackage then begin
        Result:=Result+TLazPackage(Item).IDAsString;
      end else begin
        Result:=Result+'Unknown:'+dbgsName(Item);
      end;
    end;
  end;
  
var
  PathList: TFPList;
begin
  PathList:=nil;
  try
    // check for broken dependencies
    PathList:=PackageGraph.FindBrokenDependencyPath(APackage,FirstDependency);
    if PathList<>nil then
      PrintErrorAndHalt(ErrorLoadPackageFailed,'Broken dependency: '+PathListToString(PathList));

    // check for circle dependencies
    PathList:=PackageGraph.FindCycleDependencyPath(APackage,FirstDependency);
    if PathList<>nil then
      PrintErrorAndHalt(ErrorLoadPackageFailed,'Circle dependency: '+PathListToString(PathList));
  finally
    PathList.Free;
  end;
end;

function TLazBuildApplication.BuildProject(const AFilename: string): boolean;
var
  SrcFilename: String;
  CompReason: TCompileReason;

  function StartBuilding : boolean;
  var
    i: integer;
    HasMacro: boolean;
    NeedBuildAllFlag: Boolean;
    CfgCode: TCodeBuffer;
    CfgFilename: String;
    CompilerParams, CmdLineParams: TStrings;
    CompilerFilename, CompileHint: String;
    S, TargetExeName, WorkingDir: String;
    MatrixOption: TBuildMatrixOption;
    CompilePolicy: TPackageUpdatePolicy;
    ToolBefore, ToolAfter: TProjectCompilationToolOptions;
  begin
    Result := false;

    // override specific options
    if (OSOverride<>'') then
      Project1.CompilerOptions.TargetOS:=OSOverride;
    if (CPUOverride<>'') then
      Project1.CompilerOptions.TargetCPU:=CPUOverride;
    if SubtargetOverride then
      Project1.CompilerOptions.Subtarget:=SubtargetOverrideValue;
    if (WidgetSetOverride<>'') then begin
      MatrixOption:=Project1.BuildModes.SessionMatrixOptions.Add(bmotIDEMacro);
      MatrixOption.Modes:=Project1.ActiveBuildMode.Identifier;
      MatrixOption.MacroName:='LCLWidgetType';
      MatrixOption.Value:=WidgetSetOverride;
    end;
    if HasCustomCompilerOpts(S) then
      with Project1.CompilerOptions do
        CustomOptions := MergeCustomOptions(CustomOptions, S);

    // apply options
    MainBuildBoss.SetBuildTargetProject1(true,smsfsSkip);

    if HasLongOptIgnoreCase('get',S) or
       HasLongOptIgnoreCase('get-expand-text',S) then begin
      // check for empty text
      if S = '' then
      begin
        writeln('');            // print empty text as well
        halt(ErrorExpandMacro); // exit with error
      end;

      // check for macros
      HasMacro := false;
      for i := 1 to length(S) - 1 do // skip last char
        if (S[i] = '$') and (S[i + 1] <> '$') then begin // skip escaped '$'
          HasMacro := true;
          break;
        end;
      // if a macro is not specified, its name is assumed
      if not HasMacro then
        S := '$(' + S + ')';
      // expand
      Project1.MacroEngine.MarkUnhandledMacros := false;
      if not Project1.MacroEngine.SubstituteStr(S) then
      begin
        writeln(S);             // print partially expanded text
        halt(ErrorExpandMacro); // exit with error
      end;
      // print result
      WriteLn(S);
      exit(true);
    end;

    CompilerParams:=nil;
    CmdLineParams:=nil;
    try
      if not SkipDependencies then
      begin
        // compile required packages
        CheckPackageGraphForCompilation(nil,Project1.FirstRequiredDependency);
        PackageGraph.BeginUpdate(false);
        // automatically compile required packages
        CompilePolicy:=pupAsNeeded;
        if BuildRecursive and BuildAll then
          CompilePolicy:=pupOnRebuildingAll;
        if PackageGraph.CompileRequiredPackages(nil,Project1.FirstRequiredDependency,
                                  not (pfUseDesignTimePackages in Project1.Flags),
                                  CompilePolicy)<>mrOk
        then
          PrintErrorAndHalt(ErrorBuildFailed, 'Project dependencies of "' + AFilename + '"');
      end;

      WorkingDir:=Project1.Directory;
      SrcFilename:=CreateRelativePath(Project1.MainUnitInfo.Filename,WorkingDir);

      NeedBuildAllFlag:=false;
      CompileHint:='';
      if (CompReason in Project1.CompilerOptions.CompileReasons) then begin
        // only check if NeedBuildAllFlag will be set
        MainBuildBoss.DoCheckIfProjectNeedsCompilation(Project1, NeedBuildAllFlag,CompileHint);
      end;

      // execute compilation tool 'Before'
      ToolBefore:=TProjectCompilationToolOptions(Project1.CompilerOptions.ExecuteBefore);
      if (CompReason in ToolBefore.CompileReasons) then begin
        if ToolBefore.Execute(Project1.Directory,
          lisProject2+lisExecutingCommandBefore, CompileHint)<>mrOk
        then
          PrintErrorAndHalt(ErrorBuildFailed, 'Failed "tool before" of project "' + AFilename + '"');
      end;

      // create unit output directory
      S:=Project1.CompilerOptions.GetUnitOutPath(false);
      if not ForceDirectory(S) then
        PrintErrorAndHalt(ErrorBuildFailed, 'Unable to create project unit output directory "' + S + '"');

      // create target output directory
      TargetExeName := Project1.CompilerOptions.CreateTargetFilename;
      S := ExtractFileDir(TargetExeName);
      if not ForceDirectory(S) then
        PrintErrorAndHalt(ErrorBuildFailed, 'Unable to create project target directory "' + S + '"');

      // create LazBuildApp bundle
      if Project1.UseAppBundle and (Project1.MainUnitID>=0)
      and ((MainBuildBoss.GetLCLWidgetType=LCLPlatformDirNames[lpCarbon])
        or (MainBuildBoss.GetLCLWidgetType=LCLPlatformDirNames[lpCocoa]))
      then begin
        if not (CreateApplicationBundle(TargetExeName, Project1.Title) in [mrOk,mrIgnore]) then
          PrintErrorAndHalt(ErrorBuildFailed, 'Unable to create application bundle for "' + TargetExeName + '"');
        if not (CreateAppBundleSymbolicLink(TargetExeName) in [mrOk,mrIgnore]) then
          PrintErrorAndHalt(ErrorBuildFailed, 'Unable to create application bundle symbolic link for "' + TargetExeName + '"');
      end;

      // update all lrs files
      MainBuildBoss.UpdateProjectAutomaticFiles('');

      // regenerate resources
      if not Project1.ProjResources.Regenerate(SrcFileName, False, True, '') then
        PrintWarning('Project1.Resources.Regenerate failed of "' + SrcFilename + '"');

      // get compiler parameters
      if CompilerOverride <> '' then
        CompilerFilename := CompilerOverride
      else
        CompilerFilename:=Project1.GetCompilerFilename;
      if CompilerFilename='' then
        PrintErrorAndHalt(ErrorBuildFailed, 'Invalid compiler "' + Project1.CompilerOptions.CompilerPath + '"');

      //DebugLn(['TLazBuildApplication.BuildProject CompilerFilename="',CompilerFilename,'" CompilerPath="',Project1.CompilerOptions.CompilerPath,'"']);
      // CompileHint: use absolute paths, same as TBuildManager.DoCheckIfProjectNeedsCompilation
      CompilerParams:=Project1.CompilerOptions.MakeCompilerParams([ccloAbsolutePaths]);
      CompilerParams.Add(SrcFilename);
      CmdLineParams:=CompilerParams;

      if (CompReason in Project1.CompilerOptions.CompileReasons) then begin
        // compile

        if Project1.CompilerOptions.WriteConfigFile then
        begin
          CfgFilename:=Project1.GetWriteConfigFilePath;
          CfgCode:=WriteCompilerCfgFile(CfgFilename,CompilerParams,CmdLineParams);
          if CfgCode=nil then
            PrintErrorAndHalt(ErrorBuildFailed, 'Unable to read "' + CfgFilename + '"');
          if CfgCode.FileOnDiskNeedsUpdate and (not CfgCode.Save) then
            PrintErrorAndHalt(ErrorBuildFailed, 'Unable to write "' + CfgFilename + '"');
        end;

        // write state file to avoid building clean every time
        if Project1.SaveStateFile(CompilerFilename,CompilerParams,false)<>mrOk then
          PrintErrorAndHalt(ErrorBuildFailed, 'Failed saving statefile of project "' + AFilename + '"');
        if TheCompiler.Compile(Project1,WorkingDir,CompilerFilename,CmdLineParams,
                               BuildAll or NeedBuildAllFlag,false,false,false,
                               CompileHint)<>mrOk
        then
          PrintErrorAndHalt(ErrorBuildFailed, 'Failed compiling of project "' + AFilename + '"');
        // compilation succeded -> write state file
        if Project1.SaveStateFile(CompilerFilename,CompilerParams,true)<>mrOk then
          PrintErrorAndHalt(ErrorBuildFailed, 'Failed saving statefile of project "' + AFilename + '"');
      end;

      // execute compilation tool 'After'
      ToolAfter:=TProjectCompilationToolOptions(Project1.CompilerOptions.ExecuteAfter);
      if (CompReason in ToolAfter.CompileReasons) then begin
        if ToolAfter.Execute(Project1.Directory,
          lisProject2+lisExecutingCommandAfter,CompileHint)<>mrOk
        then
          PrintErrorAndHalt(ErrorBuildFailed, 'Failed "tool after" of project "' + AFilename + '"');
      end;

      // no need to check for mrOk, we are exit if it wasn't
      Result:=true;
    finally
      if CmdLineParams<>CompilerParams then
        CmdLineParams.Free;
      CompilerParams.Free;
      if not SkipDependencies then
        PackageGraph.EndUpdate;
    end;
  end;

var
  i, MatchCount: Integer;
  ModeMask: TMask;
  CurResult: Boolean;
  S: string;
begin
  Result:=false;
  CloseProject(Project1);

  if not Init then exit;

  Project1:=LoadProject(AFilename);
  
  if Project1.MainUnitInfo=nil then
    PrintErrorAndHalt(ErrorBuildFailed, 'Project has no main unit');

  if BuildAll then
    CompReason:= crBuild
  else
    CompReason:= crCompile;

  if HasLongOptIgnoreCase('get-build-modes',S) then begin
    ShowBuildModes;
    exit(true);
  end;

  // first override build mode
  if BuildModeOverride<>'' then
  begin
    CurResult := true;
    MatchCount := 0;
    ModeMask := TMask.Create(BuildModeOverride);
    for i := 0 to Project1.BuildModes.Count-1 do
    begin
      if ModeMask.Matches(Project1.BuildModes[i].Identifier) then
      begin
        inc(MatchCount);
        Project1.ActiveBuildMode := Project1.BuildModes[i];
        CurResult := CurResult and StartBuilding;
      end;
    end;
    ModeMask.Free;
    if MatchCount=0 then // No matches
      ShowBuildModeError(BuildModeOverride);
    Result := CurResult;
  end
  else
    Result := StartBuilding;

  // Auto increment build number
  if Result and BuildAll and not NoWriteProject
  and Project1.ProjResources.VersionInfo.UseVersionInfo
  and Project1.ProjResources.VersionInfo.AutoIncrementBuild
  then begin
    if FileIsWritable(AFilename) then begin
      Project1.ProjResources.DoAfterBuild(CompReason, Project1.IsVirtual);
      Project1.WriteProject(Project1.PublishOptions.WriteFlags,AFileName,EnvironmentOptions.BuildMatrixOptions)
    end else
      PrintWarning('Project1.WriteProject skipped for read-only "' + SrcFilename + '"');
  end;
end;

function TLazBuildApplication.LoadProject(const AFilename: string): TProject;
var
  ProjectDesc: TProjectDescriptor;
begin
  ProjectDesc:=TProjectDescriptor.Create;
  try
    Result:=TProject.Create(ProjectDesc);
    // custom initialization
    Result.BeginUpdate(true);
    if ProjectDesc.InitProject(Result)<>mrOk then begin
      Result.EndUpdate;
      Result.Free;
      exit(nil);
    end;
    Result.EndUpdate;

    Result.MainProject:=true;
    Result.OnFileBackup:=@BuildBoss.BackupFileForWrite;
    Result.OnChangeProjectInfoFile:=@ProjectInfoFileChanged;

  finally
    ProjectDesc.Free;
  end;

  Result.BeginUpdate(true);
  try
    // read project info file
    if Result.ReadProject(AFilename,EnvironmentOptions.BuildMatrixOptions)<>mrOk then
      PrintErrorAndHalt(ErrorLoadProjectFailed,'Project '+AFilename);
    //BuildBoss.RescanCompilerDefines(true);

    // load required packages
    PackageGraph.OpenRequiredDependencyList(Result.FirstRequiredDependency);
  finally
    Result.EndUpdate;
  end;
  IncreaseCompilerParseStamp;
end;

procedure TLazBuildApplication.CloseProject(var AProject: TProject);
begin
  // free project, if it is still there
  FreeThenNil(AProject);
end;

function TLazBuildApplication.AddPackagesToInstallList(
  const PackageNamesOrFiles: TStringList): boolean;
var
  i: integer;
  Package: TLazPackage;
  PackageLink: TLazPackageLink;
  PackageName: String;
  PkgFilename: String;
  ErrorMsg: String;
  ErrCode: Byte;
begin
  Result:=false;
  if not Init then exit;

  LoadMiscellaneousOptions;

  ErrorMsg:='';
  ErrCode:=ErrorPackageNameInvalid;
  for i:=0 to PackageNamesOrFiles.Count -1 do
  begin
    // Look for package name in all known packages
    PackageName:='';
    PkgFilename:='';
    if pvPkgSearch in fPkgGraphVerbosity then
      debugln(['Info: (lazbuild) [TLazBuildApplication.AddPackagesToInstallList] "',PackageNamesOrFiles[i],'"']);
    if FilenameExtIs(PackageNamesOrFiles[i],'lpk',true) then
      PkgFilename:=ExpandFileNameUTF8(PackageNamesOrFiles[i])
    else if IsValidPkgName(PackageNamesOrFiles[i]) then begin
      PackageLink:=TLazPackageLink(LazPackageLinks.FindLinkWithPkgName(PackageNamesOrFiles[i]));
      if PackageLink=nil then
      begin
        ErrorMsg+='Can not find package "'+PackageNamesOrFiles[i]+'", so it is not marked for installation.'+LineEnding;
        continue;
      end;
      PkgFilename:=PackageLink.LPKFilename;
    end else begin
      ErrorMsg+='"'+PackageNamesOrFiles[i]+'" is not a package, so it is not marked for installation.'+LineEnding;
      continue;
    end;
    Package:=LoadPackage(PkgFilename);
    if Package=nil then
    begin
      ErrorMsg+='Could not load "'+PackageNamesOrFiles[i]+'", so it is not marked for installation.'+LineEnding;
      ErrCode:=ErrorLoadPackageFailed;
      continue;
    end;
    if Package.PackageType in [lptRunTime,lptRunTimeOnly] then
    begin
      ErrorMsg+='Package "'+PackageNamesOrFiles[i]+'" is only for runtime.'+LineEnding;
      continue;
    end;
    PackageName:=Package.Name;
    // set it as (static) autoinstall: select for installation
    PrintHint('Adding package "' + PkgFilename + '" to install list of IDE');
    if MiscellaneousOptions.BuildLazProfiles.StaticAutoInstallPackages.IndexOf(PackageName)<0 then
      MiscellaneousOptions.BuildLazProfiles.StaticAutoInstallPackages.Add(PackageName);
  end;
  if ErrorMsg<>'' then
    PrintErrorAndHalt(ErrCode,UTF8Trim(ErrorMsg));

  // save list
  MiscellaneousOptions.Save;
  LazPackageLinks.SaveUserLinks(true);

  Result:=true;
end;

function TLazBuildApplication.AddCmdLinePackageLinks(
  const PackageNamesOrFiles: TStringList): boolean;
var
  ErrorMsg, PkgFilename: String;
  i, ErrCode: Integer;
  Package: TLazPackage;
begin
  Result:=false;
  if not Init then exit;

  ErrorMsg:='';
  ErrCode:=ErrorLoadPackageFailed;
  for i:=0 to PackageNamesOrFiles.Count -1 do
  begin
    // Look for package name in all known packages
    PkgFilename:=PackageNamesOrFiles[i];
    if not FilenameExtIs(PkgFilename,'lpk',true) then begin
      ErrorMsg+='"' + PkgFilename+'" is not a package (.lpk), so it is not registered.'+LineEnding;
      continue;
    end;
    PkgFilename:=ExpandFileNameUTF8(PkgFilename);

    Package:=LoadPackage(PkgFilename);
    if Package=nil then
    begin
      ErrorMsg+='Could not load "'+PkgFilename+'", so it is not registered.'+LineEnding;
      continue;
    end;
    PrintHint('Registering package link "' + PkgFilename + '"');
    LazPackageLinks.AddUserLink(Package);
  end;
  if ErrorMsg<>'' then
    PrintErrorAndHalt(ErrCode,UTF8Trim(ErrorMsg));

  LazPackageLinks.SaveUserLinks(true);
  Result:=true;
end;

function TLazBuildApplication.Init: boolean;
begin
  if fInitialized then exit(fInitResult);
  fInitResult:=false;
  fInitialized:=true;

  try
    PrintHint('Primary config path: "' + GetPrimaryConfigPath + '"');
    CreatePrimaryConfigPath;

    MainBuildBoss:=TBuildManager.Create(nil);
    SetupMacros;
    LoadEnvironmentOptions;
    if Terminated then exit(false);
    LoadMiscellaneousOptions;
    SetupLazarusDirectory;
    SetupCodetools;
    SetupFPCExeFilename;
    SetupPackageSystem;
    MainBuildBoss.SetupExternalTools(TExternalToolsConsole);
    ExtToolConsole:=TLazExtToolConsole.Create(nil);
    MainBuildBoss.SetupCompilerInterface;

    StoreBaseSettings;

    // load static base packages
    PackageGraph.LoadStaticBasePackages;

    MainBuildBoss.SetBuildTarget(OSOverride,CPUOverride,SubtargetOverrideValue,
                                 WidgetSetOverride,smsfsSkip,true);
  except
    PrintErrorAndHalt(ErrorInitialization, 'Initialization error');
  end;

  fInitResult:=true;
  Result:=fInitResult;
end;

procedure TLazBuildApplication.LoadEnvironmentOptions;
var
  Note, Lang: string;
begin
  EnvironmentOptions.CreateConfig;
  EnvironmentOptions.Load(false);
  fCompilerInCfg:=EnvironmentOptions.CompilerFilename;
  fLazarusDirInCfg:=EnvironmentOptions.LazarusDirectory;

  if HasLongOptIgnoreCase('language',Lang) then
    EnvironmentOptions.LanguageID:=Lang;
  TranslateResourceStrings(EnvironmentOptions.GetParsedLazarusDirectory,
                           EnvironmentOptions.LanguageID);
  if CompilerOverride<>'' then
    EnvironmentOptions.CompilerFilename:=CompilerOverride;
  //debugln(['TLazBuildApplication.LoadEnvironmentOptions LazarusDirectory="',LazarusDirectory,'"']);
  if LazarusDirOverride<>'' then
    EnvironmentOptions.LazarusDirectory:=CleanAndExpandDirectory(LazarusDirOverride);
  if MaxProcessCount>=0 then
    // set command line override
    EnvironmentOptions.MaxExtToolsInParallel:=MaxProcessCount;
  if not FileExistsUTF8(EnvironmentOptions.GetParsedLazarusDirectory
                        +GetForcedPathDelims('packager/registration/fcl.lpk'))
  then begin
    CheckLazarusDirectoryQuality(EnvironmentOptions.GetParsedLazarusDirectory,Note);
    PrintErrorAndHalt(ErrorInitialization, 'Invalid Lazarus directory "' + EnvironmentOptions.LazarusDirectory + '": ' + Note);
  end;
end;

procedure TLazBuildApplication.LoadMiscellaneousOptions;
begin
  if MiscellaneousOptions<>nil then exit;
  MiscellaneousOptions:=TMiscellaneousOptions.Create;
  MiscellaneousOptions.Load;
end;

procedure TLazBuildApplication.SetupMacros;
begin
  MainBuildBoss.SetupTransferMacros;
  (IDEMacros as TLazIDEMacros).LoadLazbuildMacros;
end;

procedure TLazBuildApplication.SetupCodetools;
begin
  // create a test unit needed to get from the compiler all macros and search paths
  CodeToolBoss.CompilerDefinesCache.TestFilename:=CreateCompilerTestPascalFilename;
  CodeToolBoss.SourceCache.OnEncodeSaving:=@CodeBufferEncodeSaving;
  CodeToolBoss.SourceCache.OnDecodeLoaded:=@CodeBufferDecodeLoaded;
  CodeToolBoss.SourceCache.DefaultEncoding:=EncodingUTF8;

  MainBuildBoss.LoadCompilerDefinesCaches;
  // create a test unit needed to get from the compiler all macros and search paths
  CodeToolBoss.CompilerDefinesCache.TestFilename:=CreateCompilerTestPascalFilename;
end;

procedure TLazBuildApplication.SetupPackageSystem;
begin
  OnGetDependencyOwnerDescription:=@GetDependencyOwnerDescription;
  OnGetDependencyOwnerDirectory:=@GetDependencyOwnerDirectory;

  // package links
  LazPackageLinks:=TLazPackageLinks.Create;
  LazPackageLinks.UpdateAll;

  // package graph
  PackageGraph:=TLazPackageGraph.Create;
  PackageGraphInterface:=PackageGraph;
  PackageGraph.OnAddPackage:=@PackageGraphAddPackage;
  PackageGraph.OnCheckInterPkgFiles:=@PackageGraphCheckInterPkgFiles;
  PackageGraph.Verbosity:=PkgGraphVerbosity;
end;

procedure TLazBuildApplication.SetupDialogs;
begin
  LazMessageWorker:=@IDEMessageDialogHandler;
  LazQuestionWorker:=@IDEQuestionDialogHandler;
end;

procedure TLazBuildApplication.StoreBaseSettings;
var
  StoreLazDir: Boolean;
  StoreCompPath: Boolean;
  Cfg: TXMLConfig;
begin
  StoreLazDir:=(fLazarusDirInCfg='') and (EnvironmentOptions.LazarusDirectory<>'');
  StoreCompPath:=(fCompilerInCfg='') and (EnvironmentOptions.CompilerFilename<>'');
  if (not StoreLazDir) and (not StoreCompPath) then exit;

  try
    PrintInfo('Storing in "' + EnvironmentOptions.Filename + '":');
    if StoreLazDir then
      PrintInfo('  Lazarus directory: "' + EnvironmentOptions.LazarusDirectory + '"');
    if StoreCompPath then
      PrintInfo('  Compiler path: "' + EnvironmentOptions.CompilerFilename + '"');

    Cfg:=TXMLConfig.Create(EnvironmentOptions.Filename);
    try
      if StoreLazDir then
        Cfg.SetValue('EnvironmentOptions/LazarusDirectory/Value',
                     EnvironmentOptions.LazarusDirectory);
      if StoreCompPath then
        Cfg.SetValue('EnvironmentOptions/CompilerFilename/Value',
                     EnvironmentOptions.CompilerFilename);
      Cfg.Flush;
    finally
      Cfg.Free;
    end;
  except
    PrintWarning('Unable to edit file "' + EnvironmentOptions.Filename + '"');
  end;
end;

function TLazBuildApplication.RepairedCheckOptions(const ShortOptions: String;
  const Longopts: TStrings; Opts, NonOpts: TStrings): String;

  Function FindLongOpt(S : String) : boolean;
  Var
    I : integer;
  begin
    I:=LongOpts.Count-1;
    While (I>=0) and (LongOpts[I]<>S) do
      Dec(I);
    Result:=(I<>-1);
  end;

Var
  I,J,L,P : Integer;
  O,OV : String;
  HaveArg : Boolean;
  NeedArg: Boolean;
begin
  Result:='';
  I:=1;
  While (I<=ToolParamCount) and (Result='') do
    begin
    O:=ToolParamStr(I);
    If (Length(O)=0) or (O[1]<>OptionChar) then
      begin
      If Assigned(NonOpts) then
        NonOpts.Add(O)
      end
    else
      begin
      If (Length(O)<2) then
        Result:=Format(lisErrInvalidOption,[i,O])
      else
        begin
        HaveArg:=False;
        OV:='';
        // Long option ?
        If (O[2]=OptionChar) then
          begin
          Delete(O,1,2);
          J:=Pos('=',O);
          If J<>0 then
            begin
            HaveArg:=true;
            OV:=O;
            Delete(OV,1,J);
            O:=Copy(O,1,J-1);
            end;
          O:=LowerCase(O); // Long options are case-insensitive.
          // Switch Option
          If FindLongopt(O) then
            begin
            If HaveArg then
              Result:=Format(lisErrNoOptionAllowed,[I,O]);
            end
          else
            begin // Required argument
            If FindLongOpt(O+':') then
              begin
              If Not HaveArg then
                Result:=Format(lisErrOptionNeeded,[I,O]);
              end
            else
              begin // Optional Argument.
              If not FindLongOpt(O+'::') then
                Result:=Format(lisErrInvalidOption,[I,O]);
              end;
            end;
          end
        else // Short Option.
          begin
          HaveArg:=(I<ToolParamCount) and (Length(ToolParamStr(I+1))>0)
                   and (ToolParamStr(I+1)[1]<>OptionChar);
          If HaveArg then
            OV:=ToolParamStr(I+1);
          L:=Length(O);
          J:=2;
          NeedArg:=false;
          While (result='') and (J<=L) do
            begin
            P:=Pos(O[J],ShortOptions);
            If (P=0) or (O[j]=':') then
              Result:=Format(lisErrInvalidOption,[I,O[J]])
            else
              begin
              If (P<Length(ShortOptions)) and (Shortoptions[P+1]=':') then
                begin
                // Required argument
                NeedArg:=true;
                PrintInfo( Format('P %d J %d %s %d Havearg %s', [P, J, O[J], l, BoolToStr(HaveArg, true)]) );
                If ((P+1)=Length(ShortOptions)) or (Shortoptions[P+2]<>':') Then
                  If (J<L) or not haveArg then // Must be last in multi-opt !!
                    Result:=Format(lisErrOptionNeeded,[I,O[J]]);
                O:=O[j]; // O is added to arguments.
                end;
              end;
            Inc(J);
            end;
          if not NeedArg then HaveArg:=false;
          If HaveArg then
            begin
            Inc(I); // Skip argument.
            O:=O[Length(O)]; // O is added to arguments !
            end;
          end;
        If HaveArg and (Result='') then
          If Assigned(Opts) then
            Opts.Add(O+'='+OV);
        end;
      end;
    Inc(I);
    end;
end;

constructor TLazBuildApplication.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  SetupDialogs;
  Files:=TStringList.Create;
  fMaxProcessCount:=-1;
end;

destructor TLazBuildApplication.Destroy;
begin
  CloseProject(Project1);

  if Assigned(PackageGraph) then
  begin
    PackageGraph.FreeAutoInstallDependencies;
    FreeThenNil(PackageGraph);
  end;

  FreeThenNil(LazPackageLinks);
  FreeThenNil(TheCompiler);
  FreeAndNil(ExtToolConsole);
  FreeThenNil(GlobalMacroList);
  FreeThenNil(IDEMacros);
  FreeThenNil(MiscellaneousOptions);
  FreeThenNil(EnvironmentOptions);
  FreeThenNil(MainBuildBoss);

  FreeAndNil(Files);
  inherited Destroy;
end;

procedure TLazBuildApplication.Run;
var
  i: Integer;
begin
  if not ParseParameters then exit;

  // Build all projects/packages specified by the user...
  // except packages to be added the IDE install list.
  for i:=0 to Files.Count-1 do
    if not BuildFile(Files[i]) then
      PrintErrorAndHalt(ErrorBuildFailed, 'Building failed: "' + Files[i] + '"');

  // Add user-requested packages to IDE install list:
  case PackageAction of
  lpaInstall:
    if not AddPackagesToInstallList(Files) then
      PrintErrorAndHalt(ErrorBuildFailed, 'Installing package(s) failed: "' + Files.Text + '"');
  lpaAddPkgLinks:
    if not AddCmdLinePackageLinks(Files) then
      PrintErrorAndHalt(ErrorBuildFailed, 'Adding package(s) links failed: "' + Files.Text + '"');
  end;

  if BuildIDE then
    if not BuildLazarusIDE then
      PrintErrorAndHalt(ErrorBuildFailed, '');
end;

function TLazBuildApplication.HasLongOptIgnoreCase(const S: String; out
  aValue: String): Boolean;
// Check existence of a long option case-insensitively.
begin
  CaseSensitiveOptions:=False;
  Result:=HasOption(S);
  if Result then
    aValue:=GetOptionValue(S)
  else
    aValue:='';
  CaseSensitiveOptions:=True;
end;

function TLazBuildApplication.HasShortOrLongOpt(const C: Char; const S: String
  ): Boolean;
// Check existence of a short option casesensitively and long option case-insensitively.
begin
  Result:=HasOption(C);
  CaseSensitiveOptions:=False;
  Result:=Result or HasOption(S);
  CaseSensitiveOptions:=True;
end;

function TLazBuildApplication.ParseParameters: boolean;
var
  Options: TStringList;
  NonOptions: TStringList;
  LongOptions: TStringList;
  i: Integer;
  p, ErrorMsg: String;
  FilesNeeded: Boolean;
begin
  Result:=False;
  if (ToolParamCount<=0)
   or (CompareText(ToolParamStr(1),'--help')=0)
   or (CompareText(ToolParamStr(1),'-help')=0)
   or (CompareText(ToolParamStr(1),'-?')=0)
   or (CompareText(ToolParamStr(1),'-h')=0)
  then begin
    WriteUsage;
    exit;
  end;
  if HasShortOrLongOpt('h','help') or HasOption('?') then begin
    WriteUsage;
    exit;
  end;
  if HasShortOrLongOpt('v','version') then begin
    writeln(VersionStr);
    exit;
  end;

  // ConsoleVerbosity
  if HasLongOptIgnoreCase('get-build-modes', p) or
     HasLongOptIgnoreCase('get-expand-text', p) or
     HasLongOptIgnoreCase('get', p)
  then
    ConsoleVerbosity := -100 // do not output anything other than the result
  else
    for i:=1 to ToolParamCount do begin
      p:=ToolParamStr(i);
      if CompareText(p, '--verbose') = 0 then
        ConsoleVerbosity:=Max(+1,ConsoleVerbosity+1)
      else if (p='-q') or (CompareText(p, '--quiet') = 0) then
        ConsoleVerbosity:=Min(-1,ConsoleVerbosity-1);
    end;
  CTConsoleVerbosity:=ConsoleVerbosity;

  Options:=TStringList.Create;
  NonOptions:=TStringList.Create;
  LongOptions:=TStringList.Create;
  try
    LongOptions.Add('quiet');
    LongOptions.Add('verbose');
    LongOptions.Add('verbose-pkgsearch');
    LongOptions.Add('primary-config-path:');
    LongOptions.Add('pcp:');
    LongOptions.Add('secondary-config-path:');
    LongOptions.Add('scp:');
    LongOptions.Add('language:');
    LongOptions.Add('add-package');
    LongOptions.Add('add-package-link');
    LongOptions.Add('build-all');
    LongOptions.Add('build-ide::');
    LongOptions.Add('recursive');
    LongOptions.Add('skip-dependencies');
    LongOptions.Add('widgetset:');
    LongOptions.Add('ws:');
    LongOptions.Add('operating-system:');
    LongOptions.Add('os:');
    LongOptions.Add('cpu:');
    LongOptions.Add('subtarget:');
    LongOptions.Add('bm:');
    LongOptions.Add('build-mode:');
    LongOptions.Add('opt:');
    LongOptions.Add('compiler:');
    LongOptions.Add('lazarusdir:');
    LongOptions.Add('create-makefile');
    LongOptions.Add('max-process-count:');
    LongOptions.Add('no-write-project');
    LongOptions.Add('get-expand-text:');
    LongOptions.Add('get:');
    LongOptions.Add('get-build-modes');
    ErrorMsg:=RepairedCheckOptions('lBrdq',LongOptions,Options,NonOptions);
    if ErrorMsg<>'' then
      PrintErrorAndHalt(ErrorInvalidSyntax, ErrorMsg);

    // lazarus config
    if FileExistsUTF8(GetCfgFileName) then
      PrintInfo('Using config file: "' + GetCfgFileName + '"');

    FilesNeeded:=true;

    if HasLongOptIgnoreCase('verbose-pkgsearch',p) then
      Include(fPkgGraphVerbosity,pvPkgSearch);

    // PackageAction: register lpk files
    if HasLongOptIgnoreCase('add-package-link',p) then begin
      PrintInfo('Parameter: --add-package-link');
      if PackageAction<>lpaBuild then
        PrintErrorAndHalt(ErrorInvalidSyntax, 'Invalid combination of package actions');
      FilesNeeded:=false;
      PackageAction:=lpaAddPkgLinks;
    end;

    // PackageAction: install lpk files
    if HasLongOptIgnoreCase('add-package',p) then begin
      PrintInfo('Parameter: --add-package');
      if PackageAction<>lpaBuild then
        PrintErrorAndHalt(ErrorInvalidSyntax, 'Invalid combination of package actions');
      PackageAction:=lpaInstall;
      FilesNeeded:=false;
    end;

    // building IDE
    if HasLongOptIgnoreCase('build-ide', FBuildIDEOptions) then begin
      BuildIDE:=true;
      FilesNeeded:=false;
      PrintInfo('Parameter: --build-ide="' + BuildIDEOptions + '"');
    end;

    // files
    Files.Assign(NonOptions);
    if FilesNeeded and (Files.Count=0) then
      PrintErrorAndHalt(ErrorInvalidSyntax, 'Missing file');

    // primary config path
    if HasLongOptIgnoreCase('primary-config-path',p) then begin
      SetPrimaryConfigPath(p);
      PrintInfo('Parameter: --primary-config-path="' + GetPrimaryConfigPath + '"');
    end else if HasLongOptIgnoreCase('pcp',p) then begin
      SetPrimaryConfigPath(p);
      PrintInfo('Parameter: --pcp="' + GetPrimaryConfigPath + '"');
    end;

    // secondary config path
    if HasLongOptIgnoreCase('secondary-config-path',p) then begin
      SetPrimaryConfigPath(p);
      PrintInfo('Parameter: --secondary-config-path="' + GetSecondaryConfigPath + '"');
    end else if HasLongOptIgnoreCase('scp',p) then begin
      SetSecondaryConfigPath(p);
      PrintInfo('Parameter: --scp="' + GetSecondaryConfigPath + '"');
    end;

    // build all
    if HasShortOrLongOpt('B','build-all') then begin
      BuildAll:=true;
      PrintInfo('Parameter: --build-all');
    end;
    if HasShortOrLongOpt('r','recursive') then begin
      BuildAll:=true;
      BuildRecursive:=true;
      PrintInfo('Parameter: --recursive');
    end;
    if HasShortOrLongOpt('d','skip-dependencies') then begin
      SkipDependencies:=true;
      PrintInfo('Parameter: --skip-dependencies');
    end;
    if BuildRecursive and SkipDependencies then
      PrintErrorAndHalt(ErrorInvalidSyntax, '"--recursive" and "--skip-dependencies" options are incompatible');

    { Overrides }

    // widgetset
    if HasLongOptIgnoreCase('ws',FWidgetSetOverride) then
      PrintInfo('Parameter: --ws=' + WidgetSetOverride)
    else if HasLongOptIgnoreCase('widgetset',FWidgetSetOverride) then
      PrintInfo('Parameter: --widgetset=' + WidgetSetOverride);

    // operating system
    if HasLongOptIgnoreCase('os',FOSOverride) then
      PrintInfo('Parameter: --os=' + OSOverride)
    else if HasLongOptIgnoreCase('operating-system',FOSOverride) then
      PrintInfo('Parameter: --operating-system=' + OSOverride);

    // cpu
    if HasLongOptIgnoreCase('cpu',FCPUOverride) then
      PrintInfo('Parameter: --cpu=' + CPUOverride);

    // subtarget
    if HasLongOptIgnoreCase('subtarget',FSubtargetOverrideValue) then begin
      FSubtargetOverride:=true;
      PrintInfo('Parameter: --subtarget=' + FSubtargetOverrideValue);
    end;

    // build mode
    if HasLongOptIgnoreCase('bm',FBuildModeOverride) then
      PrintInfo('Parameter: --bm="' + BuildModeOverride + '"')
    else if HasLongOptIgnoreCase('build-mode',FBuildModeOverride) then
      PrintInfo('Parameter: --build-mode="' + BuildModeOverride + '"');

    // compiler
    if HasLongOptIgnoreCase('compiler',FCompilerOverride) then
      PrintInfo('Parameter: --compiler="' + CompilerOverride + '"');

    // lazarusdir
    if HasLongOptIgnoreCase('lazarusdir',FLazarusDirOverride) then
      PrintInfo('Parameter: --lazarusdir="' + LazarusDirOverride + '"');

    // language
    if HasLongOptIgnoreCase('language',p) then
      PrintInfo('Parameter: --language=' + p);

    // max-process-count
    if HasLongOptIgnoreCase('max-process-count',p) then begin
      MaxProcessCount:=StrToInt(p);
      PrintInfo('Parameter: --max-process-count=' + p);
    end;

    if HasLongOptIgnoreCase('no-write-project',p) then begin
      NoWriteProject := true;
      PrintInfo('Parameter: --no-write-project');
    end;

    if HasLongOptIgnoreCase('create-makefile',p) then
    begin
      CreateMakefile := true;
      PrintInfo('Parameter: --create-makefile');
      if PackageAction<>lpaBuild then
        PrintErrorAndHalt(ErrorInvalidSyntax, '"--create-makefile" and "--add-package" options are incompatible');
    end;
  finally
    Options.Free;
    NonOptions.Free;
    LongOptions.Free;
  end;
  Result:=true;
end;

procedure TLazBuildApplication.WriteUsage;
var
  CustomLang: string;
const
  cDescrIndent = 16;
  cMaxLength = 80;

  procedure w(Msg: string);
  begin
    writeln(UTF8ToConsole(StringOfChar(' ', cDescrIndent) + UTF8WrapText(Msg, LineEnding, [' ', #9], cMaxLength, cDescrIndent)));
  end;

begin
  // Obtain 'language' option value if present.
  // HasLongOptIgnoreCase correctly handles all cases, so no need to check its result:
  // empty CustomLang means using default language.
  HasLongOptIgnoreCase('language',CustomLang);
  TranslateResourceStrings(ProgramDirectoryWithBundle,CustomLang);
  writeln('');
  writeln(lisLazbuildOptionsSyntax);
  writeln('');
  writeln(lisEdtExtToolParameters);
  writeln('');
  writeln('-?, -h, --help');
  w(lisThisHelpMessage);
  writeln('');
  writeln('-B, --build-all');
  w(lisBuildAllFilesOfProjectPackageIDE);
  writeln('');
  writeln('-r, --recursive');
  w(lisApplyBuildFlagsBToDependenciesToo);
  writeln('');
  writeln('-d, --skip-dependencies');
  w(lisDoNotCompileDependencies);
  writeln('');
  writeln('--build-ide=<options>');
  w(lisBuildIDEWithPackages);
  writeln('');
  writeln('-v, --version');
  w(lisShowVersionAndExit);
  writeln('');
  writeln('-q, --quiet');
  w(lisBeLessVerboseCanBeGivenMultipleTimes);
  w(lisPassingQuietTwoTimesWillP);
  writeln('');
  writeln('--verbose');
  w(lisBeMoreVerboseCanBeGivenMultipleTimes);
  writeln('');
  writeln('--verbose-pkgsearch');
  w(lisWriteWhatPackageFilesAreS);
  writeln('');

  writeln('--add-package');
  w(lisAddPackageSToListOfInstalledPackagesCombineWithBui);
  writeln('');
  writeln('--add-package-link=<.lpk file>');
  w(lisOnlyRegisterTheLazarusPackageFilesLpkDoNotBuild);
  writeln('');
  writeln('--create-makefile');
  w(lisInsteadOfCompilePackageCreateASimpleMakefile);
  writeln('');

  writeln(PrimaryConfPathOptShort,'<path>, ',PrimaryConfPathOptLong,'<path>');
  w(Format(lisprimaryConfigDirectoryWhereLazarusStoresItsConfig, [LazConf.GetPrimaryConfigPath]));
  writeln('');
  writeln(SecondaryConfPathOptShort,'<path>, ',SecondaryConfPathOptLong,'<path>');
  w(Format(lissecondaryConfigDirectoryWhereLazarusSearchesFor, [LazConf.GetSecondaryConfigPath]));
  writeln('');
  writeln('--os=<operating-system>, --operating-system=<operating-system>');
  w(Format(lisOverrideTheProjectOperatingSystemEGWin32LinuxDefau, [FPCAdds.GetCompiledTargetOS]));
  writeln('');
  writeln('--ws=<widgetset>, --widgetset=<widgetset>');
  w(Format(lisOverrideTheProjectWidgetsetEGGtkGtk2QtWin32CarbonD, [LCLPlatformDirNames[GetBuildLCLWidgetType]]));
  writeln('');
  writeln('--cpu=<cpu>');
  w(Format(lisOverrideTheProjectCpuEGI386X86_64PowerpcPowerpc_64, [FPCAdds.GetCompiledTargetCPU]));
  writeln('');
  writeln('--subtarget=<subtarget>');
  w(lisOverrideTheProjectSubtarg);
  writeln('');
  writeln('--bm=<project/IDE build mode>, --build-mode=<project/IDE build mode>');
  w(lisOverrideTheProjectBuildMode);
  writeln('');
  writeln('--opt=<extra-options>');
  w(lisExtraOpts);
  writeln('');
  writeln('--compiler=<ppcXXX>');
  w(lisOverrideTheDefaultCompilerEGPpc386Ppcx64PpcppcEtcD);
  writeln('');
  writeln(LanguageOpt,'<Language ID>');
  w(lisOverrideLanguage);
  writeln('');
  writeln('--lazarusdir=<Lazarus directory>');
  w(lisLazarusDirOverride);
  writeln('');
  writeln('--max-process-count=<count>');
  w(lisMaximumNumberOfThreadsForCompilingInParallelDefaul);
  writeln('');
  writeln('--no-write-project');
  w(lisDoNotWriteUpdatedProjectInfoAfterBuild);
  writeln('');
  writeln('--get=<text>, --get-expand-text=<text>');
  w(lisGetExpandText);
  writeln('');
  writeln('--get-build-modes');
  w(lisGetBuildModes);
  writeln('');
end;

procedure TLazBuildApplication.PrintInfo(const Msg: string);
begin
  if ConsoleVerbosity >= 1 then
    debugln('Info: (lazbuild) ', LineBreaksToSystemLineBreaks(Msg));
end;

procedure TLazBuildApplication.PrintHint(const Msg: string);
begin
  if ConsoleVerbosity >= 0 then
    debugln('Hint: (lazbuild) ', LineBreaksToSystemLineBreaks(Msg));
end;

procedure TLazBuildApplication.PrintWarning(const Msg: string);
begin
  if ConsoleVerbosity >= -1 then
    debugln('Warning: (lazbuild) ', LineBreaksToSystemLineBreaks(Msg));
end;

procedure TLazBuildApplication.PrintErrorAndHalt(Code: Byte; const Msg: string);
begin
  if Msg <> '' then
    debugln('Error: (lazbuild) ', LineBreaksToSystemLineBreaks(Msg));
  halt(Code);
end;

begin
  // When quick rebuilding lazbuild, FPC rebuilds only the lazbuild.lpr, so any
  // flag that should work with quick build must be set here.
  // At the moment there is no flag

  HasGUI:=false;
  FilterConfigFileContent;
  // start our own LazBuildApp
  LazBuildApp:=TLazBuildApplication.Create(nil);
  LazBuildApp.Run;
  LazBuildApp.Free;
end.