| 1 |
// -*- compile-command: "make" -*- |
| 2 |
{ |
| 3 |
Copyright 2021-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
uses Classes, SysUtils, |
| 18 |
CastleUtils, CastleParameters, CastleStringUtils; |
| 19 |
|
| 20 |
{ Is it OK to depend from unit in CurrentCategory on a unit in DependencyCategory. } |
| 21 |
function DependencyOk(CurrentCategory, DependencyCategory: string): boolean; |
| 22 |
|
| 23 |
{ Enable depending of a unit from ValidCategory on a unit |
| 24 |
in ValidDependencyCategory. |
| 25 |
Works recursively, so any unit in a category that can depend on |
| 26 |
ValidCategory can also depend on ValidDependencyCategory. } |
| 27 |
function IndirectDependencyCheck(const ValidCategory, ValidDependencyCategory: string): boolean; |
| 28 |
begin |
| 29 |
Result := |
| 30 |
( (CurrentCategory = ValidCategory) and (DependencyCategory = ValidDependencyCategory) ) or |
| 31 |
( (CurrentCategory = ValidCategory) and (DependencyOk(ValidDependencyCategory, DependencyCategory)) ); |
| 32 |
end; |
| 33 |
|
| 34 |
begin |
| 35 |
CurrentCategory := LowerCase(CurrentCategory); |
| 36 |
DependencyCategory := LowerCase(DependencyCategory); |
| 37 |
|
| 38 |
Result := |
| 39 |
(CurrentCategory = DependencyCategory) or |
| 40 |
IndirectDependencyCheck('base', 'physics') or |
| 41 |
IndirectDependencyCheck('base', 'compatibility') or |
| 42 |
IndirectDependencyCheck('base', 'deprecated_units') or // everything can use deprecated_units, ignore |
| 43 |
IndirectDependencyCheck('files', 'base') or |
| 44 |
IndirectDependencyCheck('audio', 'files') or |
| 45 |
IndirectDependencyCheck('images', 'files') or |
| 46 |
IndirectDependencyCheck('images', 'vampyre_imaginglib') or |
| 47 |
IndirectDependencyCheck('base_rendering', 'images') or |
| 48 |
IndirectDependencyCheck('fonts', 'base_rendering') or |
| 49 |
IndirectDependencyCheck('ui', 'audio') or |
| 50 |
IndirectDependencyCheck('ui', 'fonts') or |
| 51 |
IndirectDependencyCheck('services', 'ui') or |
| 52 |
IndirectDependencyCheck('transform', 'services') or |
| 53 |
IndirectDependencyCheck('castlescript', 'transform') or |
| 54 |
IndirectDependencyCheck('scene', 'castlescript') or |
| 55 |
IndirectDependencyCheck('window', 'castlescript') or |
| 56 |
IndirectDependencyCheck('lcl', 'ui'); |
| 57 |
end; |
| 58 |
|
| 59 |
var |
| 60 |
AllCgeUnits: TStringList; |
| 61 |
DependenciesToCheck: TStringList; |
| 62 |
|
| 63 |
procedure ExtractCategory(UnitPath: string; out Category: string); |
| 64 |
var |
| 65 |
TokenPos: Integer; |
| 66 |
//PartAfterCategory: string; |
| 67 |
begin |
| 68 |
while SCharIs(UnitPath, 1, '.') or |
| 69 |
SCharIs(UnitPath, 1, '/') do |
| 70 |
UnitPath := SEnding(UnitPath, 2); // cut off initial ./ from UnitPath |
| 71 |
TokenPos := 1; |
| 72 |
Category := NextToken(UnitPath, TokenPos, ['/', '\']); |
| 73 |
//PartAfterCategory := NextToken(UnitPath, TokenPos, ['/', '\']); |
| 74 |
end; |
| 75 |
|
| 76 |
function FindCategory(const DependencyBaseName: string; |
| 77 |
out Category: string): boolean; |
| 78 |
var |
| 79 |
UnitPath: string; |
| 80 |
begin |
| 81 |
for UnitPath in AllCgeUnits do |
| 82 |
if SameText(ChangeFileExt(ExtractFileName(UnitPath), ''), DependencyBaseName) then |
| 83 |
begin |
| 84 |
ExtractCategory(UnitPath, Category); |
| 85 |
Exit(true); |
| 86 |
end; |
| 87 |
Result := false; |
| 88 |
end; |
| 89 |
|
| 90 |
const |
| 91 |
AllowedExceptions: array [0..8] of String = ( |
| 92 |
// Because TCurve.GeometryNode uses X3D nodes. Possible to overcome (just move it somewhere else) but weird (it is natural to have this in TCurve). |
| 93 |
'Category castlescript uses scene. Unit ./castlescript/castlecurves.pas uses X3DNodes', |
| 94 |
|
| 95 |
// Because TCastleXxxNavigation access InternalViewport and some it's TCastleViewport-specific props. |
| 96 |
'Category transform uses scene. Unit ./transform/castlecameras.pas uses CastleViewport', |
| 97 |
|
| 98 |
// Because TCastleCamera displays it's own visualization at design-time. Possible to overcome, but would complicate code. |
| 99 |
'Category transform uses scene. Unit ./transform/castletransform.pas uses CastleScene', |
| 100 |
'Category transform uses scene. Unit ./transform/castletransform.pas uses X3DNodes', |
| 101 |
|
| 102 |
// Because TCastleJointTool uses CastleScene |
| 103 |
'Category transform uses scene. Unit ./transform/castleinternalphysicsvisualization.pas uses CastleScene', |
| 104 |
|
| 105 |
// Because TCastleInspector accesses some key engine classes for some functionality. |
| 106 |
'Category ui uses transform. Unit ./ui/castleinternalinspector.pas uses CastleCameras', |
| 107 |
'Category ui uses scene. Unit ./ui/castleinternalinspector.pas uses CastleScene', |
| 108 |
'Category ui uses transform. Unit ./ui/castleinternalinspector.pas uses CastleTransform', |
| 109 |
'Category ui uses scene. Unit ./ui/castleinternalinspector.pas uses CastleViewport' |
| 110 |
); |
| 111 |
|
| 112 |
var |
| 113 |
CurrentUnit, DependencyToCheck, CurrentCategory, DependencyCategory, DependencyDescribe: string; |
| 114 |
begin |
| 115 |
Parameters.CheckHigh(3); |
| 116 |
AllCgeUnits := TStringList.Create; |
| 117 |
AllCgeUnits.LoadFromFile(Parameters[1]); |
| 118 |
CurrentUnit := Parameters[2]; |
| 119 |
DependenciesToCheck := TStringList.Create; |
| 120 |
DependenciesToCheck.Text := Parameters[3]; |
| 121 |
|
| 122 |
try |
| 123 |
for DependencyToCheck in DependenciesToCheck do |
| 124 |
begin |
| 125 |
if FindCategory(DependencyToCheck, DependencyCategory) then |
| 126 |
begin |
| 127 |
ExtractCategory(CurrentUnit, CurrentCategory); |
| 128 |
DependencyDescribe := Format('Category %s uses %s. Unit %s uses %s', [ |
| 129 |
CurrentCategory, |
| 130 |
DependencyCategory, |
| 131 |
CurrentUnit, |
| 132 |
DependencyToCheck |
| 133 |
]); |
| 134 |
// Writeln('Checking: ', DependencyDescribe); |
| 135 |
if not DependencyOk(CurrentCategory, DependencyCategory) then |
| 136 |
begin |
| 137 |
if ArrayPosStr(DependencyDescribe, AllowedExceptions) <> -1 then |
| 138 |
begin |
| 139 |
Writeln(ErrOutput, 'WARNING: Not allowed dependency, but it is listed as exception to temporarily allow: ', DependencyDescribe); |
| 140 |
end else |
| 141 |
begin |
| 142 |
Writeln(ErrOutput, 'ERROR: Not allowed dependency: ', DependencyDescribe); |
| 143 |
ExitCode := 1; |
| 144 |
end; |
| 145 |
end; |
| 146 |
end; |
| 147 |
end; |
| 148 |
finally |
| 149 |
FreeAndNil(AllCgeUnits); |
| 150 |
FreeAndNil(DependenciesToCheck); |
| 151 |
end; |
| 152 |
end. |