ppuparser.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. {
  2. pas2jni - JNI bridge generator for Pascal.
  3. Copyright (c) 2013 by Yury Sidorov.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. unit ppuparser;
  17. {$mode objfpc}{$H+}
  18. interface
  19. uses
  20. Classes, SysUtils, def;
  21. type
  22. TCheckItemResult = (crDefault, crInclude, crExclude);
  23. TOnCheckItem = function (const ItemName: string): TCheckItemResult of object;
  24. { TPPUParser }
  25. TPPUParser = class
  26. private
  27. FOnCheckItem: TOnCheckItem;
  28. FDefaultSearchPathAdded: boolean;
  29. function FindUnit(const AName: string): string;
  30. function ReadUnit(const AName: string): string;
  31. function InternalParse(const AUnitName: string): TUnitDef;
  32. procedure AddSearchPath(const ASearchPath: string);
  33. function ReadProcessOutput(const AExeName, AParams: string; var AOutput, AError: string): integer;
  34. procedure AddDefaultSearchPath(const ACPU, AOS: string);
  35. public
  36. SearchPath: TStringList;
  37. Units: TDef;
  38. OnExceptionProc: TProcDef;
  39. constructor Create(const ASearchPath: string);
  40. destructor Destroy; override;
  41. procedure Parse(const AUnitName: string);
  42. property OnCheckItem: TOnCheckItem read FOnCheckItem write FOnCheckItem;
  43. end;
  44. var
  45. ppudumpprog: string;
  46. implementation
  47. uses process, pipes, fpjson, jsonparser, jsonscanner;
  48. const
  49. OnExceptionProcName = 'JNI_OnException';
  50. type
  51. TCharSet = set of char;
  52. function WordPosition(const N: Integer; const S: string;
  53. const WordDelims: TCharSet): Integer;
  54. var
  55. Count, I: Integer;
  56. begin
  57. Count := 0;
  58. I := 1;
  59. Result := 0;
  60. while (I <= Length(S)) and (Count <> N) do
  61. begin
  62. { skip over delimiters }
  63. while (I <= Length(S)) and (S[I] in WordDelims) do
  64. Inc(I);
  65. { if we're not beyond end of S, we're at the start of a word }
  66. if I <= Length(S) then
  67. Inc(Count);
  68. { if not finished, find the end of the current word }
  69. if Count <> N then
  70. while (I <= Length(S)) and not (S[I] in WordDelims) do
  71. Inc(I)
  72. else
  73. Result := I;
  74. end;
  75. end;
  76. function ExtractWord(N: Integer; const S: string;
  77. const WordDelims: TCharSet): string;
  78. var
  79. I: Integer;
  80. Len: Integer;
  81. begin
  82. Len := 0;
  83. I := WordPosition(N, S, WordDelims);
  84. if I <> 0 then
  85. { find the end of the current word }
  86. while (I <= Length(S)) and not (S[I] in WordDelims) do
  87. begin
  88. { add the I'th character to result }
  89. Inc(Len);
  90. SetLength(Result, Len);
  91. Result[Len] := S[I];
  92. Inc(I);
  93. end;
  94. SetLength(Result, Len);
  95. end;
  96. { TPPUParser }
  97. constructor TPPUParser.Create(const ASearchPath: string);
  98. begin
  99. SearchPath:=TStringList.Create;
  100. AddSearchPath(ASearchPath);
  101. Units:=TDef.Create;
  102. end;
  103. destructor TPPUParser.Destroy;
  104. begin
  105. Units.Free;
  106. SearchPath.Free;
  107. inherited Destroy;
  108. end;
  109. procedure TPPUParser.Parse(const AUnitName: string);
  110. begin
  111. InternalParse(AUnitName);
  112. end;
  113. function TPPUParser.FindUnit(const AName: string): string;
  114. var
  115. i: integer;
  116. fn: string;
  117. begin
  118. fn:=ChangeFileExt(LowerCase(AName), '.ppu');
  119. if FileExists(fn) then begin
  120. Result:=fn;
  121. exit;
  122. end;
  123. for i:=0 to SearchPath.Count - 1 do begin
  124. Result:=IncludeTrailingPathDelimiter(SearchPath[i]) + fn;
  125. if FileExists(Result) then
  126. exit;
  127. end;
  128. raise Exception.CreateFmt('Unable to find PPU file for unit "%s".', [AName]);
  129. end;
  130. function TPPUParser.ReadUnit(const AName: string): string;
  131. var
  132. s, un, err: ansistring;
  133. ec: integer;
  134. begin
  135. un:=FindUnit(AName);
  136. if ppudumpprog = '' then begin
  137. ppudumpprog:='ppudump';
  138. // Check for ppudump in the same folder as pas2jni
  139. s:=ExtractFilePath(ParamStr(0));
  140. if s <> '' then begin
  141. s:=s + ppudumpprog + ExtractFileExt(ParamStr(0));
  142. if FileExists(s) then
  143. ppudumpprog:=s;
  144. end;
  145. end;
  146. ec:=ReadProcessOutput(ppudumpprog, '-Fj' + LineEnding + un, s, err);
  147. if Copy(s, 1, 1) <> '[' then begin
  148. ec:=-1;
  149. err:='Output of ppudump is not in JSON format.' + LineEnding + 'Probably old version of ppudump has been used.';
  150. end;
  151. if ec <> 0 then begin
  152. if err = '' then
  153. if Length(s) < 300 then
  154. err:=s;
  155. raise Exception.CreateFmt('Error reading contents of unit "%s" using "%s".'+LineEnding+'Error code: %d'+LineEnding+'%s', [un, ppudumpprog, ec, err]);
  156. end;
  157. Result:=s;
  158. {$ifopt D+}
  159. // Lines.SaveToFile(AName + '-dump.txt');
  160. {$endif}
  161. end;
  162. function TPPUParser.InternalParse(const AUnitName: string): TUnitDef;
  163. var
  164. junit: TJSONObject;
  165. deref: array of TUnitDef;
  166. CurUnit: TUnitDef;
  167. IsSystemUnit: boolean;
  168. AMainUnit: boolean;
  169. CurObjName: string;
  170. function _GetRef(Ref: TJSONObject; ExpectedClass: TDefClass = nil): TDef;
  171. var
  172. j: integer;
  173. u: TUnitDef;
  174. begin
  175. Result:=nil;
  176. if Ref = nil then
  177. exit;
  178. u:=CurUnit;
  179. j:=Ref.Get('Unit', -1);
  180. if j >= 0 then begin
  181. u:=deref[j];
  182. if u.DefType = dtNone then begin
  183. // Reading unit
  184. u:=InternalParse(LowerCase(u.Name));
  185. if u = nil then
  186. exit;
  187. if u.CPU <> CurUnit.CPU then
  188. raise Exception.CreateFmt('Invalid target CPU of unit "%s": %s', [u.Name, u.CPU]);
  189. if u.OS <> CurUnit.OS then
  190. raise Exception.CreateFmt('Invalid target OS of unit "%s": %s', [u.Name, u.OS]);
  191. if u.PPUVer <> CurUnit.PPUVer then
  192. raise Exception.CreateFmt('Invalid PPU version of unit "%s": %s', [u.Name, u.PPUVer]);
  193. deref[j].Free;
  194. deref[j]:=u;
  195. end;
  196. end;
  197. j:=Ref.Integers['Id'];
  198. Result:=u.FindDef(j);
  199. if Result = nil then begin
  200. if ExpectedClass <> nil then
  201. Result:=ExpectedClass.Create(u, dtNone)
  202. else
  203. Result:=TDef.Create(u, dtNone);
  204. Result.DefId:=j;
  205. end;
  206. if (ExpectedClass <> nil) and (Result <> nil) then
  207. if (Result.DefType <> dtNone) and not (Result is ExpectedClass) then
  208. raise Exception.CreateFmt('Unexpected class. Expected: %s, got: %s', [ExpectedClass.ClassName, Result.ClassName]);
  209. end;
  210. procedure _ReadDefs(CurDef: TDef; jobj: TJSONObject; const ItemsName: string);
  211. var
  212. i, j: integer;
  213. jt, s: string;
  214. d: TDef;
  215. it: TJSONObject;
  216. jarr, arr: TJSONArray;
  217. ct: TClassType;
  218. begin
  219. jarr:=jobj.Get(ItemsName, TJSONArray(nil));
  220. if jarr = nil then
  221. exit;
  222. with jarr do
  223. for i:=0 to Count - 1 do begin
  224. it:=Objects[i];
  225. CurObjName:=it.Get('Name', '');
  226. jt:=it.Strings['Type'];
  227. if jt = 'obj' then begin
  228. s:=it.Strings['ObjType'];
  229. if s = 'class' then
  230. ct:=ctClass
  231. else
  232. if s = 'interface' then
  233. ct:=ctInterface
  234. else
  235. if s = 'object' then
  236. ct:=ctObject
  237. else
  238. continue;
  239. d:=TClassDef.Create(CurDef, dtClass);
  240. TClassDef(d).CType:=ct;
  241. end
  242. else
  243. if jt = 'rec' then begin
  244. if IsSystemUnit and (CompareText(CurObjName, 'tguid') = 0) then begin
  245. d:=TTypeDef.Create(CurDef, dtType);
  246. TTypeDef(d).BasicType:=btGuid;
  247. end
  248. else begin
  249. d:=TClassDef.Create(CurDef, dtClass);
  250. TClassDef(d).CType:=ctRecord;
  251. end;
  252. end
  253. else
  254. if jt = 'proc' then
  255. d:=TProcDef.Create(CurDef, dtProc)
  256. else
  257. if jt = 'proctype' then begin
  258. d:=TProcDef.Create(CurDef, dtProcType);
  259. TProcDef(d).ProcType:=ptProcedure;
  260. end
  261. else
  262. if jt = 'param' then begin
  263. d:=TVarDef.Create(CurDef, dtParam);
  264. TVarDef(d).VarOpt:=[voRead];
  265. end
  266. else
  267. if jt = 'prop' then begin
  268. d:=TVarDef.Create(CurDef, dtProp);
  269. TVarDef(d).VarOpt:=[];
  270. end
  271. else
  272. if jt = 'field' then
  273. d:=TVarDef.Create(CurDef, dtField)
  274. else
  275. if jt = 'var' then
  276. d:=TVarDef.Create(CurDef, dtVar)
  277. else
  278. if jt = 'ord' then begin
  279. d:=TTypeDef.Create(CurDef, dtType);
  280. with TTypeDef(d) do begin
  281. s:=it.Strings['OrdType'];
  282. j:=it.Get('Size', 0);
  283. if s = 'void' then
  284. BasicType:=btVoid
  285. else
  286. if s = 'uint' then begin
  287. case j of
  288. 1: BasicType:=btByte;
  289. 2: BasicType:=btWord;
  290. 4: BasicType:=btLongWord;
  291. else BasicType:=btInt64;
  292. end;
  293. end
  294. else
  295. if s = 'sint' then begin
  296. case j of
  297. 1: BasicType:=btShortInt;
  298. 2: BasicType:=btSmallInt;
  299. 4: BasicType:=btLongInt;
  300. else BasicType:=btInt64;
  301. end;
  302. end
  303. else
  304. if (s = 'pasbool') or (s = 'bool') then
  305. BasicType:=btBoolean
  306. else
  307. if s = 'char' then begin
  308. if j = 1 then
  309. BasicType:=btChar
  310. else
  311. BasicType:=btWideChar;
  312. end
  313. else
  314. if s = 'currency' then
  315. BasicType:=btDouble;
  316. end;
  317. end
  318. else
  319. if jt = 'float' then begin
  320. d:=TTypeDef.Create(CurDef, dtType);
  321. with TTypeDef(d) do
  322. if it.Strings['FloatType'] = 'single' then
  323. BasicType:=btSingle
  324. else
  325. BasicType:=btDouble;
  326. end
  327. else
  328. if jt = 'string' then begin
  329. d:=TTypeDef.Create(CurDef, dtType);
  330. s:=it.Strings['StrType'];
  331. with TTypeDef(d) do
  332. if (s = 'wide') or (s = 'unicode') or (s = 'long') then
  333. BasicType:=btWideString
  334. else
  335. BasicType:=btString;
  336. if not (IsSystemUnit and (CompareText(CurObjName, 'rawbytestring') = 0)) then
  337. CurObjName:=s + 'string';
  338. end
  339. else
  340. if jt = 'enum' then begin
  341. d:=TTypeDef.Create(CurDef, dtEnum);
  342. TTypeDef(d).BasicType:=btEnum;
  343. end
  344. else
  345. if jt = 'set' then
  346. d:=TSetDef.Create(CurDef, dtSet)
  347. else
  348. if jt = 'ptr' then begin
  349. d:=TPointerDef.Create(CurDef, dtPointer);
  350. end
  351. else
  352. if jt = 'const' then
  353. d:=TConstDef.Create(CurDef, dtConst)
  354. else
  355. if jt = 'array' then
  356. d:=TArrayDef.Create(CurDef, dtArray)
  357. else
  358. continue;
  359. if (CurObjName = '') and not (d.DefType in [dtEnum, dtArray]) then begin
  360. d.Free;
  361. continue;
  362. end;
  363. // Common def attributes
  364. d.Name:=CurObjName;
  365. d.DefId:=it.Get('Id', -1);
  366. d.SymId:=it.Get('SymId', -1);
  367. s:=it.Get('Visibility', '');
  368. d.IsPrivate:=(s <> '') and (s <> 'public') and (s <> 'published');
  369. if Copy(d.Name, 1, 1) = '$' then
  370. d.IsPrivate:=True;
  371. // Specific def attributes
  372. case d.DefType of
  373. dtClass:
  374. with TClassDef(d) do begin
  375. if CType <> ctRecord then
  376. AncestorClass:=TClassDef(_GetRef(it.Get('Ancestor', TJSONObject(nil)), TClassDef));
  377. if CType in [ctObject, ctRecord] then
  378. Size:=it.Integers['Size'];
  379. _ReadDefs(d, it, 'Fields');
  380. end;
  381. dtProc, dtProcType:
  382. with TProcDef(d) do begin
  383. arr:=it.Get('Options', TJSONArray(nil));
  384. if arr <> nil then
  385. for j:=0 to arr.Count - 1 do begin
  386. s:=arr.Strings[j];
  387. if s = 'procedure' then
  388. ProcType:=ptProcedure
  389. else
  390. if s = 'function' then
  391. ProcType:=ptFunction
  392. else
  393. if s = 'constructor' then begin
  394. ProcType:=ptConstructor;
  395. if CompareText(Name, 'create') = 0 then
  396. Name:='Create'; // fix char case for standard constructors
  397. end
  398. else
  399. if s = 'destructor' then
  400. ProcType:=ptDestructor
  401. else
  402. if s = 'overriding' then begin
  403. ProcType:=ptDestructor;
  404. ProcOpt:=ProcOpt + [poOverride];
  405. if ProcType <> ptConstructor then
  406. IsPrivate:=True;
  407. end
  408. else
  409. if s = 'overload' then
  410. ProcOpt:=ProcOpt + [poOverload]
  411. else
  412. if s = 'abstract' then
  413. TClassDef(Parent).HasAbstractMethods:=True;
  414. end;
  415. ReturnType:=_GetRef(it.Get('RetType', TJSONObject(nil)));
  416. if (DefType = dtProcType) and not ( (ReturnType is TTypeDef) and (TTypeDef(ReturnType).BasicType = btVoid) ) then
  417. ProcType:=ptFunction;
  418. if it.Get('MethodPtr', False) then
  419. ProcOpt:=ProcOpt + [poMethodPtr];
  420. if IsSystemUnit and (ProcType = ptFunction) and (Name = 'int') then
  421. Name:='Int';
  422. _ReadDefs(d, it, 'Params');
  423. for j:=0 to d.Count - 1 do
  424. with d[j] do begin
  425. if DefType <> dtParam then
  426. continue;
  427. s:=Name;
  428. Name:=Format('p%d', [j + 1]);
  429. AliasName:=s;
  430. end;
  431. // Check for user exception handler proc
  432. if AMainUnit and (Parent = CurUnit) and (OnExceptionProc = nil) and (AnsiCompareText(Name, OnExceptionProcName) = 0) then
  433. OnExceptionProc:=TProcDef(d);
  434. end;
  435. dtVar, dtField, dtParam:
  436. with TVarDef(d) do begin
  437. VarType:=_GetRef(it.Objects['VarType']);
  438. s:=it.Get('Spez', '');
  439. if s = 'out' then
  440. VarOpt:=[voWrite, voOut]
  441. else
  442. if s = 'var' then
  443. VarOpt:=[voRead, voWrite, voVar]
  444. else
  445. if s = 'const' then
  446. VarOpt:=[voRead, voConst];
  447. end;
  448. dtProp:
  449. with TVarDef(d) do begin
  450. VarType:=_GetRef(it.Objects['PropType']);
  451. if it.Get('Getter', TJSONObject(nil)) <> nil then
  452. VarOpt:=VarOpt + [voRead];
  453. if it.Get('Setter', TJSONObject(nil)) <> nil then
  454. VarOpt:=VarOpt + [voWrite];
  455. _ReadDefs(d, it, 'Params');
  456. end;
  457. dtEnum:
  458. _ReadDefs(d, it, 'Elements');
  459. dtSet:
  460. with TSetDef(d) do begin
  461. Size:=it.Integers['Size'];
  462. Base:=it.Integers['Base'];
  463. ElMax:=it.Integers['Max'];
  464. ElType:=TTypeDef(_GetRef(it.Objects['ElType'], TTypeDef));
  465. if (ElType <> nil) and (ElType.Name = '') then
  466. ElType.Name:=CurObjName + 'El';
  467. end;
  468. dtConst:
  469. with TConstDef(d) do begin
  470. VarType:=_GetRef(it.Get('TypeRef', TJSONObject(nil)));
  471. s:=it.Strings['ValType'];
  472. if s = 'int' then
  473. Value:=IntToStr(it.Int64s['Value'])
  474. else
  475. if s = 'float' then begin
  476. Str(it.Floats['Value'], s);
  477. Value:=s;
  478. end
  479. else
  480. if s = 'string' then begin
  481. s:=it.Strings['Value'];
  482. s:=StringReplace(s, '\', '\\', [rfReplaceAll]);
  483. s:=StringReplace(s, '"', '\"', [rfReplaceAll]);
  484. s:=StringReplace(s, #9, '\t', [rfReplaceAll]);
  485. s:=StringReplace(s, #10, '\n', [rfReplaceAll]);
  486. s:=StringReplace(s, #13, '\r', [rfReplaceAll]);
  487. Value:='"' + s + '"';
  488. end
  489. else
  490. FreeAndNil(d);
  491. end;
  492. dtPointer:
  493. with TPointerDef(d) do begin
  494. PtrType:=_GetRef(it.Get('Ptr', TJSONObject(nil)));;
  495. if AMainUnit and (Parent = CurUnit) and (CompareText(Name, 'TJavaObject') = 0) then
  496. DefType:=dtJniObject;
  497. end;
  498. dtArray:
  499. with TArrayDef(d) do begin
  500. _ReadDefs(d, it, 'Types');
  501. RangeLow:=it.Get('Low', -1);
  502. RangeHigh:=it.Get('High', -1);
  503. RangeType:=_GetRef(it.Get('RangeType', TJSONObject(nil)));
  504. ElType:=_GetRef(it.Get('ElType', TJSONObject(nil)));
  505. end;
  506. end;
  507. end;
  508. end;
  509. var
  510. i, j: integer;
  511. s: string;
  512. chkres: TCheckItemResult;
  513. jp: TJSONParser;
  514. jdata: TJSONData;
  515. begin
  516. Result:=nil;
  517. for i:=0 to Units.Count - 1 do
  518. if CompareText(Units[i].Name, AUnitName) = 0 then begin
  519. Result:=TUnitDef(Units[i]);
  520. exit;
  521. end;
  522. chkres:=FOnCheckItem(AUnitName);
  523. if chkres = crExclude then
  524. exit;
  525. AMainUnit:=chkres = crInclude;
  526. if not AMainUnit and ( (CompareText(AUnitName, 'windows') = 0) or (CompareText(AUnitName, 'unix') = 0) ) then
  527. exit;
  528. s:=ReadUnit(AUnitName);
  529. try
  530. jdata:=nil;
  531. try
  532. jp:=TJSONParser.Create(s, [joUTF8]);
  533. try
  534. s:='';
  535. jdata:=jp.Parse;
  536. junit:=TJSONObject(jdata.Items[0]);
  537. finally
  538. jp.Free;
  539. end;
  540. IsSystemUnit:=CompareText(AUnitName, 'system') = 0;
  541. Result:=TUnitDef.Create(nil, dtUnit);
  542. Units.Add(Result);
  543. Result.Name:=junit.Strings['Name'];
  544. Result.PPUVer:=junit.Integers['Version'];
  545. Result.CPU:=junit.Strings['TargetCPU'];
  546. Result.OS:=junit.Strings['TargetOS'];
  547. j:=Length(Result.CPU);
  548. if AnsiLowerCase(Copy(Result.OS, Length(Result.OS) - j, j + 1)) = AnsiLowerCase('-' + Result.CPU) then
  549. Result.OS:=Copy(Result.OS, 1, Length(Result.OS) - j - 1);
  550. Result.IntfCRC:=junit.Strings['InterfaceCRC'];
  551. if IsSystemUnit then
  552. Result.IsUsed:=True;
  553. if not FDefaultSearchPathAdded then begin
  554. FDefaultSearchPathAdded:=True;
  555. AddDefaultSearchPath(AnsiLowerCase(Result.CPU), AnsiLowerCase(Result.OS));
  556. end;
  557. if junit.Find('Units') <> nil then
  558. with junit.Arrays['Units'] do begin
  559. SetLength(deref, Count);
  560. for i:=0 to Count - 1 do begin
  561. deref[i]:=TUnitDef.Create(nil, dtNone);
  562. deref[i].Name:=Strings[i];
  563. end;
  564. end;
  565. CurUnit:=Result;
  566. _ReadDefs(CurUnit, junit, 'Interface');
  567. Result.ResolveDefs;
  568. if CompareText(AUnitName, 'jni') = 0 then begin
  569. for i:=0 to Result.Count - 1 do
  570. with Result[i] do
  571. if CompareText(Name, 'PJNIEnv') = 0 then
  572. DefType:=dtJniEnv;
  573. end;
  574. if AMainUnit then
  575. Result.IsUsed:=True;
  576. SetLength(Result.UsedUnits, Length(deref));
  577. j:=0;
  578. for i:=0 to High(deref) do
  579. if deref[i].DefType = dtNone then
  580. deref[i].Free
  581. else begin
  582. Result.UsedUnits[j]:=deref[i];
  583. Inc(j);
  584. end;
  585. SetLength(Result.UsedUnits, j);
  586. finally
  587. jdata.Free;
  588. end;
  589. except
  590. if CurObjName <> '' then
  591. CurObjName:=Format('; Object: "%s"', [CurObjName]);
  592. raise Exception.CreateFmt('%s' + LineEnding + 'Unit: "%s"%s', [Exception(ExceptObject).Message, AUnitName, CurObjName]);
  593. end;
  594. end;
  595. procedure TPPUParser.AddSearchPath(const ASearchPath: string);
  596. var
  597. i, j: integer;
  598. s, d: string;
  599. sr: TSearchRec;
  600. sl: TStringList;
  601. begin
  602. sl:=TStringList.Create;
  603. try
  604. sl.Delimiter:=';';
  605. sl.DelimitedText:=ASearchPath;
  606. i:=0;
  607. while i < sl.Count do begin
  608. s:=sl[i];
  609. if (Pos('*', s) > 0) or (Pos('?', s) > 0) then begin
  610. d:=ExtractFilePath(s);
  611. j:=FindFirst(s, faDirectory, sr);
  612. while j = 0 do begin
  613. if (sr.Name <> '.') and (sr.Name <> '..') then
  614. sl.Add(d + sr.Name);
  615. j:=FindNext(sr);
  616. end;
  617. FindClose(sr);
  618. sl.Delete(i);
  619. end
  620. else
  621. Inc(i);
  622. end;
  623. SearchPath.AddStrings(sl);
  624. finally
  625. sl.Free;
  626. end;
  627. end;
  628. function TPPUParser.ReadProcessOutput(const AExeName, AParams: string; var AOutput, AError: string): integer;
  629. procedure _ReadOutput(o: TInputPipeStream; var s: string; var idx: integer);
  630. var
  631. i: integer;
  632. begin
  633. with o do
  634. while NumBytesAvailable > 0 do begin
  635. i:=NumBytesAvailable;
  636. if idx + i > Length(s) then
  637. SetLength(s, idx + i*10 + idx div 10);
  638. ReadBuffer(s[idx + 1], i);
  639. Inc(idx, i);
  640. end;
  641. end;
  642. var
  643. p: TProcess;
  644. oidx, eidx: integer;
  645. begin
  646. AOutput:='';
  647. AError:='';
  648. oidx:=0;
  649. eidx:=0;
  650. p:=TProcess.Create(nil);
  651. try
  652. p.Executable:=AExeName;
  653. p.Parameters.Text:=AParams;
  654. p.Options:=[poUsePipes, poNoConsole];
  655. p.ShowWindow:=swoHIDE;
  656. p.StartupOptions:=[suoUseShowWindow];
  657. try
  658. p.Execute;
  659. except
  660. raise Exception.CreateFmt('Unable to run "%s".'+LineEnding+'%s', [p.Executable, Exception(ExceptObject).Message]);
  661. end;
  662. repeat
  663. if p.Output.NumBytesAvailable = 0 then
  664. TThread.Yield;
  665. _ReadOutput(p.Output, AOutput, oidx);
  666. _ReadOutput(p.Stderr, AError, eidx);
  667. until not p.Running and (p.Output.NumBytesAvailable = 0) and (p.Stderr.NumBytesAvailable = 0);
  668. SetLength(AOutput, oidx);
  669. SetLength(AError, eidx);
  670. Result:=p.ExitStatus;
  671. finally
  672. p.Free;
  673. end;
  674. end;
  675. procedure TPPUParser.AddDefaultSearchPath(const ACPU, AOS: string);
  676. var
  677. fpc, s, e: string;
  678. sl: TStringList;
  679. i, j: integer;
  680. begin
  681. try
  682. fpc:=ExtractFilePath(ppudumpprog) + 'fpc' + ExtractFileExt(ppudumpprog);
  683. if not FileExists(fpc) then
  684. exit;
  685. // Find the compiler binary
  686. if ReadProcessOutput(fpc, '-P' + ACPU + LineEnding + '-T' + AOS + LineEnding + '-PB', s, e) <> 0 then
  687. exit;
  688. fpc:=Trim(s);
  689. // Get units path from the compiler output
  690. ReadProcessOutput(fpc, '-P' + ACPU + LineEnding + '-T' + AOS + LineEnding + '-vt' + LineEnding + '.', s, e);
  691. sl:=TStringList.Create;
  692. try
  693. sl.Text:=s;
  694. s:='';
  695. for i:=0 to sl.Count - 1 do begin
  696. s:=sl[i];
  697. j:=Pos(':', s);
  698. if j > 0 then begin
  699. s:=Trim(Copy(s, j + 1, MaxInt));
  700. s:=ExcludeTrailingPathDelimiter(s);
  701. if (Copy(s, Length(s) - 3, 4) = DirectorySeparator + 'rtl') and DirectoryExists(s) then begin
  702. AddSearchPath(ExtractFilePath(s) + '*');
  703. exit;
  704. end;
  705. end;
  706. end;
  707. finally
  708. sl.Free;
  709. end;
  710. except
  711. // Ignore exceptions
  712. end;
  713. end;
  714. end.