Repository navigation
Expand file tree
/
Copy pathGenerateHeaderUnit.pas
More file actions
954 lines (862 loc) · 29.4 KB
/
Copy pathGenerateHeaderUnit.pas
File metadata and controls
954 lines (862 loc) · 29.4 KB
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
{
This file is part of the MWA Software Pascal API Code Generator for OpenSSL .
Copyright © MWA Software 2024
This program 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 3 of the License, or (at your option) any later version.
This program 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.
You should have received a copy of the GNU General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.
}
unit GenerateHeaderUnit;
{$IFDEF FPC}
{$mode Delphi}
{$ENDIF}
(* The TGenerateAPIUnit Class is a subclass of TAPIFileReader and provides the
overall logic for generating an API header unit from a template. It acts either
on a single template unit or all template units in the same directory. In the
latter case, support files, provided in a separate directory may be copied to
the same output directory as the API Header units.
This is an abstract class that must be overidden by subclasses for each function
load strategy.
For each template unit, TAPIFileReader.ReadAPIHeaderFile is called to analyse the
file into its different sections. The API Header unit is then generated by processing
the sections in turn, adding API function declarations for
- Static linking
- Dynamic Loading.
Any function declarations found in the implementation section are assumed to be
legacy support functions for API calls with the same name.
For static linking, these appear in the interface section (forward declaration
only) and with the function body in the implementation section.
In the dynamic linking case, a function variable for the API call is declared
in the interface section and a renamed (name preceded by COMPAT_) function body
in the implementation section. When the function call is loaded, if the API call
is not present in the library then the function variable is set to the address
of the legacy support function instead.
In support for dynamic loading, "Load" and "Unload" procedures are added to the
function body and which are intended to be called when the library is loaded/unloaded.
These procedures are registered with the library loader at unit initialisation time.
*)
interface
uses
Classes , SysUtils, APIFileReader, ProgramConstants;
const
AllHeadersUnitName = 'AllHeaders';
DefaultLibNamePrefix = 'C';
HeaderSrcFilter = '*.h2pas';
type
{ TGenerateAPIUnit }
TGenerateAPIUnit = class(TAPIFileReader)
private
FCopyFilePrefix : string;
FLibNamePrefix : string;
FRemovedFunctions: TStrings;
FCompatibilityFunctions: TStrings;
FLegacySupportFunctions: TStrings;
FLibName: string;
FUnitPrefix : string;
FUnitNames: TStrings;
FCopyUnitNames: TStrings;
FOutFile: TStrings;
FCopyingFiles: boolean;
FIncludeFiles: TStrings;
function GetLibNameForUnit: string;
procedure AddExFunctionVersionInfo(S: TStrings);
procedure AddIncludeList(S: TStrings);
procedure AnalyseExternalFunctions;
procedure CopyFile(aFileName,destFileName: string);
procedure DoCopyFile(aFileName: string; destDir: string);
procedure DoCopyFiles(copyFiles: TStrings; destDir: string); overload;
procedure DoCopyFiles(copyFilesFromDir: string; destDir: string); overload;
function FindFirstFunction(list: TList): integer;
procedure GetUnitNamesList(SourceDir: string; filter: string);
protected type
TLegacyCallType = (lcAllLegacy, lcAllowNilAndRemoved, lcRemovedNotAllowNil,
lcRemovedHasCompatibility, lcRemovedNoCompatibility);
protected
procedure AddErrorFunctions(S : TStrings); virtual; abstract;
procedure AddDynamicLoadInit(S: TStrings); virtual; abstract;
procedure AddDynamicInterfaceSection(S : TStrings; firstFuncProc : integer); virtual;
procedure AddDynamicLegacyCalls(S : TStrings; firstFuncProc : integer; callType: TLegacyCallType);
procedure AddStaticInterfaceSection(S : TStrings; firstFuncProc : integer); virtual;
procedure AddDynamicImplementationSection(S: TStrings; firstFuncProc: integer); virtual;
function DoFixUp(useUnit: string): string; override;
function GenerateInterfaceSection(S : TStrings) : boolean;
procedure GenerateImplementationSection(S: TStrings);
function GetImplementationUses: string; virtual;
function GetInitialiser(funcProc: IFuncProcInfo): string; virtual; abstract;
function GetUnitName(aUnitName: string):string; override;
procedure DoGenerateUnit(sourceFileName,destfileName: string);
procedure AddLoadFunctions(S : TStrings); virtual; abstract;
procedure AddUnLoadFunctions(S : TStrings); virtual; abstract;
procedure PeekSaveLine(var aLine: string); override;
procedure SetExternalLibName(aLibName: string); override;
public
constructor Create;
destructor Destroy; override;
procedure Clear; override;
procedure GenerateHeaderUnitFrom(sourceFilename, destDir:string; aLibName: string='');
procedure GenerateHeaderUnitFromDir(sourceDir, destDir: string; copyFiles: TStrings; aLibName: string= '');
property CopyFilePrefix: string read FCopyFilePrefix write FCopyFilePrefix;
property LibName: string read FLibName;
property LibNamePrefix: string read FLibNamePrefix write FLibNamePrefix;
property RemovedFunctions: TStrings read FRemovedFunctions;
property CompatibilityFunctions: TStrings read FCompatibilityFunctions;
property LegacySupportFunctions: TStrings read FLegacySupportFunctions;
property UnitPrefix: string read FUnitPrefix write FUnitPrefix;
end;
implementation
uses Tokeniser;
const
sUnitIntro = '(* This unit was generated from the source file %s ' + LineEnding +
'It should not be modified directly. All changes should be made to %s' + LineEnding +
'and this file regenerated *)' + LineEnding + LineEnding;
sExternalSymIntro = '{ The EXTERNALSYM directive is ignored by FPC, however, it is used by Delphi as follows: ' +
LineEnding + LineEnding +
'The EXTERNALSYM directive prevents the specified Delphi symbol from appearing in header ' + LineEnding +
'files generated for C++. }' + LineEnding + LineEnding;
resourcestring
RSOHelpersInConditional = 'Helper Function section cannot be within a conditional section';
RSOGeneratingUnit = 'Generating Unit File to %s';
RSOCopyFile = 'Copying %s to %s';
{ TGenerateAPIUnit }
function TGenerateAPIUnit.GetLibNameForUnit : string;
begin
Result := CryptoLibName
end;
procedure TGenerateAPIUnit.AddExFunctionVersionInfo(S : TStrings);
var needsConst: boolean;
i: integer;
begin
{const declarations for each External Function Introduced and Removed Versions}
needsConst := true;
with InterfaceSection do
begin
for i := 0 to Count - 1 do
if TObject(Items[i]) is IFuncProcInfo then
with TObject(Items[i]) as IFuncProcInfo do
begin
if NeedsConst and ((IntroducedVersion <> '') or (RemovedVersion <> '')) then
begin
S.Add('const');
NeedsConst := false;
end;
if IntroducedVersion <> '' then
S.Add(' ' + ProcName + '_introduced = ' + IntroducedVersion + '; {' + GetIntroducedIn + '}');
if RemovedVersion <> '' then
S.Add(' ' + ProcName + '_removed = ' + RemovedVersion + '; {' + GetRemovedIn + '}');
end;
if not needsConst then
S.Add('');
end;
end;
procedure TGenerateAPIUnit.AddIncludeList(S : TStrings);
var i: integer;
begin
for i := 0 to FIncludeFiles.Count - 1 do
S.Add('{$i ' + CopyFilePrefix + FIncludeFiles[i] + '}');
if FIncludeFiles.Count > 0 then
S.Add('');
end;
procedure TGenerateAPIUnit.AnalyseExternalFunctions;
var i: integer;
begin
with InterfaceSection do
begin
for i := 0 to Count - 1 do
begin
if TObject(Items[i]) is IFuncProcInfo then
with TObject(Items[i]) as IFuncProcInfo do
if Removed then
FRemovedFunctions.AddObject(ProcName,TObject(Items[i]))
end;
end;
with ImplementationSection do
begin
for i := 0 to Count - 1 do
begin
if TObject(Items[i]) is IFuncProcInfo then
with TObject(Items[i]) as IFuncProcInfo do
begin
if RemovedFunctions.IndexOf(ProcName) <> -1 then
FLegacySupportFunctions.AddObject(ProcName, TObject(Items[i]));
FCompatibilityFunctions.AddObject(ProcName,TObject(Items[i]));
end;
end;
end;
end;
procedure TGenerateAPIUnit.CopyFile(aFileName , destFileName : string);
var S, D: TStream;
begin
S := TFileStream.Create(aFileName,fmOpenRead);
D := TFileStream.Create(destFileName,fmCreate);
try
D.CopyFrom(S,0);
finally
S.Free;
D.Free;
end;
end;
procedure TGenerateAPIUnit.DoCopyFile(aFileName : string; destDir : string);
var destFileName: string;
begin
if UpperCase(ExtractFileExt(aFileName)) = '.PAS' then
begin
destFileName := destDir + DirectorySeparator + CopyFilePrefix + ExtractFileName(aFileName);
FOutFile := TStringList.Create;
try
if not FGetUnitNamesPass then
AddIncludeList(FOutFile);
ReadAPIHeaderFile(aFileName); {rely on PeekSaveLine to copy updated lines}
if not FGetUnitNamesPass then
begin
FOutFile.SaveToFile(destFileName
{$IFNDEF FPC},TEncoding.UTF8{$ENDIF});
DoWriteLine(Format(RSOCopyFile,[aFileName, destFileName]));
end
else
FCopyUnitNames.Add(Uppercase(UnitName));
finally
FreeAndNil(FOutFile);
end;
end
else
if FGetUnitNamesPass then
begin
if UpperCase(ExtractFileExt(aFileName)) ='.INC' then
FIncludeFiles.Add(ExtractFileName(aFileName));
end
else
begin
if UpperCase(ExtractFileExt(aFileName)) ='.INC' then
destFileName := destDir + DirectorySeparator + CopyFilePrefix + ExtractFileName(aFileName)
else
destFileName := destDir + DirectorySeparator + ExtractFileName(aFileName);
CopyFile(aFileName,destFileName);
DoWriteLine(Format(RSOCopyFile,[aFileName, destFileName]));
end;
end;
procedure TGenerateAPIUnit.DoCopyFiles(copyFiles : TStrings; destDir : string
);
var i: integer;
begin
FCopyingFiles := true;
try
{Get Unit Names for all files}
FGetUnitNamesPass := true;
try
for i := 0 to copyFiles.Count - 1 do
begin
if DirectoryExists(copyFiles[i]) then
DoCopyFiles(copyFiles[i],destDir)
else
DoCopyFile(copyFiles[i],destDir);
end;
finally
FGetUnitNamesPass := false;
end;
{Now do the actual copy}
for i := 0 to copyFiles.Count - 1 do
begin
if DirectoryExists(copyFiles[i]) then
DoCopyFiles(copyFiles[i],destDir)
else
DoCopyFile(copyFiles[i],destDir);
end;
finally
FCopyingFiles := false;
end;
end;
procedure TGenerateAPIUnit.DoCopyFiles(copyFilesFromDir : string;
destDir : string);
var token: TSearchRec;
begin
if FindFirst(copyFilesFromDir + DirectorySeparator + '*',0,token) = 0 then
try
repeat
if not DirectoryExists(copyFilesFromDir + DirectorySeparator + token.name) then
DoCopyFile(copyFilesFromDir + DirectorySeparator + token.name,destDir);
until FindNext(token) <> 0;
finally
FindClose(token);
end;
end;
function TGenerateAPIUnit.FindFirstFunction(list : TList) : integer;
var ConditionalLevel: integer;
LastConditionalLevel0Directive: integer;
i: integer;
begin
ConditionalLevel := 0;
LastConditionalLevel0Directive := -1;
with list do
begin
Result := Count;
{find first external function or opening conditional directive for first function}
for i := 0 to Count - 1 do
begin
// writeln(TObject(Items[i]).ClassName);
if TObject(Items[i]) is TDirective then
begin
with TObject(Items[i]) as TDirective do
if Pos('IF',UpperCase(Name)) = 1 then
begin
if ConditionalLevel = 0 then
LastConditionalLevel0Directive := i;
Inc(ConditionalLevel);
end
else
if (UpperCase(Name) = 'ENDIF') or (UpperCase(Name) = 'IFEND') then
Dec(ConditionalLevel);
end
else
if (TObject(Items[i]) is THelperFunctions) and (ConditionalLevel <> 0) then
raise Exception.Create(RSOHelpersInConditional)
else
if TObject(Items[i]) is IFuncProcInfo then
begin
if ConditionalLevel = 0 then
Result := i
else
Result := LastConditionalLevel0Directive;
break;
end;
end;
end;
end;
procedure TGenerateAPIUnit.GetUnitNamesList(SourceDir : string;
filter : string);
var token: TSearchRec;
begin
if FindFirst(SourceDir + DirectorySeparator + filter,0,token) = 0 then
try
FGetUnitNamesPass := true;
repeat
ReadAPIHeaderFile(SourceDir + DirectorySeparator + token.Name);
FUnitNames.Add(UpperCase(UnitName));
Clear;
until FindNext(token) <> 0;
finally
FindClose(token);
FGetUnitNamesPass := false;
end;
end;
procedure TGenerateAPIUnit.AddDynamicInterfaceSection(
S : TStrings; firstFuncProc : integer);
var i: integer;
needsVar: boolean;
AllowNilFunctionCount: integer;
begin
needsVar := true;
AllowNilFunctionCount := 0;
{Add functions for current version}
with InterfaceSection do
begin
for i := firstFuncProc to Count - 1 do
begin
if TObject(Items[i]) is THelperFunctions then
continue
else
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if TObject(Items[i]) is IFuncProcInfo then
begin
with TObject(Items[i]) as IFuncProcInfo do
begin
if not Removed then
begin
if needsVar then
begin
S.Add('var');
needsVar := false;
end;
S.Add(' ' + GetVarDeclaration(GetInitialiser(TObject(Items[i]) as IFuncProcInfo)));
end
else
if AllowNil then
Inc(AllowNilFunctionCount);
end;
end
else
if TObject(Items[i]) is TStrings then
S.AddStrings(TStrings(Items[i])) {types, vars, comments, etc embedded in API declarations}
end;
end;
if (LegacySupportFunctions.Count = 0) and (AllowNilFunctionCount = 0) then
Exit;
{Add Legacy Functions}
S.Add('');
S.Add('{Removed functions for which legacy support available - use is deprecated}');
S.Add('');
AddDynamicLegacyCalls(S,firstFuncProc,lcRemovedHasCompatibility);
end;
procedure TGenerateAPIUnit.AddDynamicLegacyCalls(S : TStrings;
firstFuncProc : integer; callType : TLegacyCallType);
function Selected(funcProc: IFuncProcInfo): boolean;
begin
with funcProc do
case callType of
lcAllLegacy:
Result := Removed;
lcAllowNilAndRemoved:
Result := (LegacySupportFunctions.IndexOf(ProcName) <> -1) or (AllowNil and Removed);
lcRemovedNotAllowNil:
Result := (LegacySupportFunctions.IndexOf(ProcName) = -1) and Removed and not AllowNil;
lcRemovedHasCompatibility:
Result := Removed and (LegacySupportFunctions.IndexOf(ProcName) <> -1);
lcRemovedNoCompatibility:
Result := Removed and (LegacySupportFunctions.IndexOf(ProcName) = -1);
end;
end;
var i: integer;
needsVar: boolean;
begin
needsVar := true;
S.Add('{$IFNDEF ' + NoLegacySupportSymbol + '}');
with InterfaceSection do
begin
for i := firstFuncProc to Count - 1 do
begin
if TObject(Items[i]) is THelperFunctions then
continue
else
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if TObject(Items[i]) is IFuncProcInfo then
begin
if Selected(TObject(Items[i]) as IFuncProcInfo) then
with TObject(Items[i]) as IFuncProcInfo do
begin
if needsVar then
begin
S.Add('var');
needsVar := false;
end;
S.Add(' ' + GetVarDeclaration(GetInitialiser(TObject(Items[i]) as IFuncProcInfo)) );
end;
end
end;
end;
S.Add('{$ENDIF} { End of ' + NoLegacySupportSymbol + '}');
end;
procedure TGenerateAPIUnit.AddStaticInterfaceSection(S : TStrings;
firstFuncProc : integer);
var i: integer;
begin
with InterfaceSection do
begin
for i := firstFuncProc to Count - 1 do
begin
// writeln(i,',', TObject(Items[i]).ClassName);
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if TObject(Items[i]) is IFuncProcInfo then
begin
with TObject(Items[i]) as IFuncProcInfo do
if not Removed then
S.Add(GetExternalDeclaration + ' external ' + LibNamePrefix + LibName + ';');
end
else
if TObject(Items[i]) is THelperFunctions then
continue
else
if TObject(Items[i]) is TStrings then
S.AddStrings(TStrings(Items[i])) {types, vars, comments, etc embedded in API declarations}
end;
S.Add('');
if LegacySupportFunctions.Count > 0 then
begin
S.Add('{Removed functions for which legacy support available - use is deprecated}');
S.Add('');
S.Add('{$IFNDEF ' + NoLegacySupportSymbol + '}');
for i := firstFuncProc to Count - 1 do
begin
if TObject(Items[i]) is THelperFunctions then
continue
else
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if TObject(Items[i]) is IFuncProcInfo then
begin
with TObject(Items[i]) as IFuncProcInfo do
begin
if Removed and (LegacySupportFunctions.IndexOf(ProcName) <> -1) then
S.Add(GetExternalDeclaration);
end;
end
end;
S.Add('{$ENDIF} { End of ' + NoLegacySupportSymbol + '}');
end;
end;
end;
procedure TGenerateAPIUnit.AddDynamicImplementationSection(S : TStrings;
firstFuncProc : integer);
var i, j: integer;
begin
{Now add each Compatibility Functions}
S.Add('{$IFNDEF ' + NoLegacySupportSymbol + '}');
with ImplementationSection do
for i := firstFuncProc to Count - 1 do
begin
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if (TObject(Items[i]) is IFuncProcInfo) then
begin
j := CompatibilityFunctions.IndexOfObject(TObject(Items[i]));
if j <> -1 then
with CompatibilityFunctions.Objects[j] as IFuncProcInfo do
begin
if isFunction then
S.Add('function COMPAT_' + ProcName + ProcHeader + ': ' + ResultType + '; cdecl;')
else
S.Add('procedure COMPAT_' + ProcName + ProcHeader + '; cdecl;');
S.AddStrings(FunctionBody);
S.Add('');
end;
end;
end;
S.Add('{$ENDIF} { End of ' + NoLegacySupportSymbol + '}');
end;
function TGenerateAPIUnit.DoFixUp(useUnit : string) : string;
begin
if FUnitNames.IndexOf(UpperCase(useUnit)) <> -1 then
Result := UnitPrefix + useUnit
else
if FCopyUnitNames.IndexOf(UpperCase(useUnit)) <> -1 then
Result := CopyFilePrefix + useUnit
else
Result := inherited DoFixUp(useUnit);
end;
function TGenerateAPIUnit.GenerateInterfaceSection(S : TStrings): boolean;
var i: integer;
firstFuncProc: integer;
HasHelpers: boolean;
begin
Result := true;
HasHelpers := true;
firstFuncProc := FindFirstFunction(InterfaceSection);
with InterfaceSection do
begin
{Copy everything up to first function/procedure}
for i := 0 to firstFuncProc - 1 do
begin
if TObject(Items[i]) is THelperFunctions then
HasHelpers := true;
if TObject(Items[i]) is TStrings then
S.AddStrings(TStrings(Items[i]))
else
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}');
end;
Result := (firstFuncProc < Count) or HasHelpers;
if not Result then
Exit; {otherwise ignore files without a procedure/function definition}
{copy any more helper functions from interface section}
for i := firstFuncProc to Count - 1 do
if TObject(Items[i]) is THelperFunctions then
S.AddStrings(THelperFunctions(Items[i]));
{Add EXTERNALSYM directive for each function/procedure not removed in most recent version of OpenSSL}
AddText(S,sExternalSymIntro);
for i := firstFuncProc to Count - 1 do
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if TObject(Items[i]) is IFuncProcInfo then
with TObject(Items[i]) as IFuncProcInfo do
if not Removed then
S.Add('{$EXTERNALSYM ' + ProcName + '}');
S.Add('');
{Generate static library interface}
S.Add('{$IFDEF ' + StaticLinkModel + '}');
AddStaticInterfaceSection(S,firstFuncProc);
S.Add('{$ELSE}');
{Generate Dynamic library interface}
AddDynamicInterfaceSection(S,firstFuncProc);
S.Add('{$ENDIF}');
AddExFunctionVersionInfo(S);
end;
end;
procedure TGenerateAPIUnit.GenerateImplementationSection(S : TStrings);
var i,j: integer;
hasUses: boolean;
firstFuncProc: integer;
begin
hasUses := false;
firstFuncProc := FindFirstFunction(ImplementationSection);
with ImplementationSection do
begin
{Is there a uses clause in the source?}
for i := 0 to firstFuncProc -1 do
if (TObject(Items[i]) is TUsesClause) then
begin
hasUses := true;
break;
end;
end;
{Find uses and compatibility functions and include all else other than Helper functions}
with ImplementationSection do
for i := 0 to firstFuncProc - 1 do
begin
// writeln(i,',',TObject(Items[i]).ClassName);
if TObject(Items[i]) is TPlainText then
S.AddStrings(TStrings(Items[i]))
else
if TObject(Items[i]) is TDirective then
with TDirective(Items[i]) do
S.Add('{$' + Name + Condition + '}')
else
if (TObject(Items[i]) is TUsesClause) then
begin
{Add extra uses to uses clause}
with TObject(Items[i]) as TUsesClause do
begin
for j := 0 to Count - 1 do
if Pos(';',Strings[j]) = 0 then
S.Add(Strings[j])
else
begin
S.Add(StringReplace(Strings[j],';',',',[]));
self.AddText(S,GetImplementationUses + ';');
end;
end;
hasUses := true;
end
else
begin
if not hasUses then
begin
AddText(S,'uses ' + GetImplementationUses + ';' + LineEnding + LineEnding);
hasUses := true;
end;
if TObject(Items[i]) is IFuncProcInfo then
continue
else
if TObject(Items[i]) is TStrings then {note includes helper functions}
S.AddStrings(TStrings(Items[i]))
end;
end;
if not hasUses then
AddText(S,'uses ' + GetImplementationUses + ';' + LineEnding + LineEnding);
S.Add('{$IFNDEF ' + StaticLinkModel + '}');
AddDynamicLegacyCalls(S,0,lcRemovedNoCompatibility);
S.Add('{$ENDIF}');
{Add any other const, var and type declarations}
with ImplementationSection do
for i := firstFuncProc to count - 1 do
begin
if (TObject(Items[i]) is TConstClause) or (TObject(Items[i]) is TVarClause) or
(TObject(Items[i]) is TTypeClause) then
S.AddStrings(TStrings(Items[i]))
else
if TObject(Items[i]) is THelperFunctions then
S.AddStrings(THelperFunctions(Items[i]))
end;
if LegacySupportFunctions.Count > 0 then
begin
S.Add('{$IFDEF ' + StaticLinkModel + '}');
S.Add('');
S.Add('{Legacy Support Functions}');
S.Add('');
S.Add('{$IFNDEF ' + NoLegacySupportSymbol + '}');
with ImplementationSection do
for i := 0 to Count - 1 do
begin
if TObject(Items[i]) is TDirective then
with TObject(Items[i]) as TDirective do
S.Add('{$' + Name + ' ' + Condition + '}')
else
if (TObject(Items[i]) is IFuncProcInfo) and (LegacySupportFunctions.IndexOfObject(TObject(Items[i])) <> -1) then
with TObject(Items[i]) as IFuncProcInfo do
begin
if isFunction then
S.Add('function ' + ProcName + ProcHeader + ': ' + ResultType + ';')
else
S.Add('procedure ' + ProcName + ProcHeader + ';');
S.AddStrings(FunctionBody);
S.Add('');
end;
end;
S.Add('{$ENDIF} { End of ' + NoLegacySupportSymbol + '}');
S.Add('{$ELSE}');
end
else
S.Add('{$IFNDEF ' + StaticLinkModel + '}');
{Everything else is only for dynamic loading}
AddDynamicImplementationSection(S,firstFuncProc);
{Add a type definition for all required error functions}
AddErrorFunctions(S);
AddLoadFunctions(S);
AddUnLoadFunctions(S);
S.Add('{$ENDIF}');
end;
function TGenerateAPIUnit.GetImplementationUses : string;
begin
Result := '';
end;
function TGenerateAPIUnit.GetUnitName(aUnitName : string) : string;
begin
if FGetUnitNamesPass then
Result := inherited
else
if FCopyingFiles then
Result := CopyFilePrefix + aUnitName
else
Result := UnitPrefix + aUnitName;
end;
procedure TGenerateAPIUnit.DoGenerateUnit(sourceFileName ,
destfileName : string);
var S: TStringList;
begin
WriteLine(Format(RSOGeneratingUnit,[destfileName]));
S := TStringList.Create;
try
AddText(S,Format(sUnitIntro,[ExtractFileName(sourceFileName),
ExtractFileName(sourceFileName)]));
AddIncludeList(S);
S.AddStrings(UnitHeader);
AnalyseExternalFunctions;
if GenerateInterfaceSection(S) then
begin
S.Add('');
GenerateImplementationSection(S);
S.Add('');
S.Add('initialization');
S.Add('');
S.AddStrings(InitializationSection);
AddDynamicLoadInit(S);
if FinalizationSection.Count > 0 then
S.Add('');
S.Add('finalization');
S.Add('');
S.AddStrings(FinalizationSection);
end
else
S.Add('implementation');
S.Add('');
S.Add('end.');
finally
S.SaveToFile(destfileName
{$IFNDEF FPC},TEncoding.UTF8{$ENDIF});
S.Free;
end;
end;
procedure TGenerateAPIUnit.PeekSaveLine(var aLine : string);
begin
if FOutFile <> nil then
begin
if aLine = '' then
FOutFile.Add('')
else
AddText(FOutFile,aLine);
end;
end;
procedure TGenerateAPIUnit.SetExternalLibName(aLibName : string);
begin
FLibName := Trim(aLibName);
end;
constructor TGenerateAPIUnit.Create;
begin
inherited Create;
FRemovedFunctions := TStringList.Create;
FCompatibilityFunctions := TStringList.Create;
FLegacySupportFunctions := TStringList.Create;
FLibNamePrefix := DefaultLibNamePrefix;
FUnitNames := TStringList.Create;
FCopyUnitNames := TStringList.Create;
FIncludeFiles := TStringList.Create;
end;
destructor TGenerateAPIUnit.Destroy;
begin
if FRemovedFunctions <> nil then
FreeAndNil(FRemovedFunctions);
if FCompatibilityFunctions <> nil then
FreeAndNil(FCompatibilityFunctions);
if FLegacySupportFunctions <> nil then
FreeAndNil(FLegacySupportFunctions);
if FUnitNames <> nil then
FUnitNames.Free;
if FCopyUnitNames <> nil then
FCopyUnitNames.Free;
if FIncludeFiles <> nil then
FIncludeFiles.Free;
FRemovedFunctions := nil;
inherited Destroy;
end;
procedure TGenerateAPIUnit.Clear;
begin
inherited;
FLibName := '';
if FRemovedFunctions <> nil then
FRemovedFunctions.Clear;
if FCompatibilityFunctions <> nil then
FCompatibilityFunctions.Clear;
if FLegacySupportFunctions <> nil then
FLegacySupportFunctions.Clear;
end;
procedure TGenerateAPIUnit.GenerateHeaderUnitFrom(sourceFilename ,
destDir : string; aLibName : string);
begin
FLibName := aLibName;
ReadAPIHeaderFile(sourceFilename);
if FLibName = '' then
FLibName := GetLibNameForUnit;
DoGenerateUnit(sourceFilename,destDir + DirectorySeparator + UnitName + '.pas');
FLibName := '';
end;
procedure TGenerateAPIUnit.GenerateHeaderUnitFromDir(sourceDir ,
destDir : string; copyFiles : TStrings; aLibName : string);
var token: TSearchRec;
AllHeaders: TStrings;
lastUnitName: string;
begin
GetUnitNamesList(sourceDir,HeaderSrcFilter);
DoCopyFiles(copyFiles,destDir);
lastUnitName := '';
AllHeaders := TStringList.Create;
try
AllHeaders.Add('unit ' + UnitPrefix + AllHeadersUnitName + ';');
AllHeaders.Add('');
AllHeaders.Add('interface');
AllHeaders.Add('');
AllHeaders.Add('uses');
if FindFirst(SourceDir + DirectorySeparator + HeaderSrcFilter,0,token) = 0 then
try
repeat
if lastUnitName <> '' then
AllHeaders.Add(' ' + lastUnitName + ',');
GenerateHeaderUnitFrom(SourceDir + DirectorySeparator + token.Name,destDir,aLibName);
lastUnitName := UnitName;
until FindNext(token) <> 0;
finally
FindClose(token);
end;
if lastUnitName <> '' then
AllHeaders.Add(' ' + lastUnitName + ';');
AllHeaders.Add('');
AllHeaders.Add('implementation');
AllHeaders.Add('');
AllHeaders.Add('end.');
AllHeaders.SaveToFile(destDir + DirectorySeparator + UnitPrefix + AllHeadersUnitName + '.pas'
{$IFNDEF FPC},TEncoding.UTF8{$ENDIF});
finally
FCopyUnitNames.Clear;
FUnitNames.Clear;
FIncludeFiles.Clear;
Allheaders.Free;
end;
end;
end.