ppuparser.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. if ct = ctInterface then
  242. TClassDef(d).IID:=it.Get('IID', '');
  243. end
  244. else
  245. if jt = 'rec' then begin
  246. if IsSystemUnit and (CompareText(CurObjName, 'tguid') = 0) then begin
  247. d:=TTypeDef.Create(CurDef, dtType);
  248. TTypeDef(d).BasicType:=btGuid;
  249. end
  250. else begin
  251. d:=TClassDef.Create(CurDef, dtClass);
  252. TClassDef(d).CType:=ctRecord;
  253. end;
  254. end
  255. else
  256. if jt = 'proc' then
  257. d:=TProcDef.Create(CurDef, dtProc)
  258. else
  259. if jt = 'proctype' then begin
  260. d:=TProcDef.Create(CurDef, dtProcType);
  261. TProcDef(d).ProcType:=ptProcedure;
  262. end
  263. else
  264. if jt = 'param' then begin
  265. d:=TVarDef.Create(CurDef, dtParam);
  266. TVarDef(d).VarOpt:=[voRead];
  267. end
  268. else
  269. if jt = 'prop' then begin
  270. d:=TVarDef.Create(CurDef, dtProp);
  271. TVarDef(d).VarOpt:=[];
  272. end
  273. else
  274. if jt = 'field' then
  275. d:=TVarDef.Create(CurDef, dtField)
  276. else
  277. if jt = 'var' then
  278. d:=TVarDef.Create(CurDef, dtVar)
  279. else
  280. if jt = 'ord' then begin
  281. d:=TTypeDef.Create(CurDef, dtType);
  282. with TTypeDef(d) do begin
  283. s:=it.Strings['OrdType'];
  284. j:=it.Get('Size', 0);
  285. if s = 'void' then
  286. BasicType:=btVoid
  287. else
  288. if s = 'uint' then begin
  289. case j of
  290. 1: BasicType:=btByte;
  291. 2: BasicType:=btWord;
  292. 4: BasicType:=btLongWord;
  293. else BasicType:=btInt64;
  294. end;
  295. end
  296. else
  297. if s = 'sint' then begin
  298. case j of
  299. 1: BasicType:=btShortInt;
  300. 2: BasicType:=btSmallInt;
  301. 4: BasicType:=btLongInt;
  302. else BasicType:=btInt64;
  303. end;
  304. end
  305. else
  306. if (s = 'pasbool') or (s = 'bool') then
  307. BasicType:=btBoolean
  308. else
  309. if s = 'char' then begin
  310. if j = 1 then
  311. BasicType:=btChar
  312. else
  313. BasicType:=btWideChar;
  314. end
  315. else
  316. if s = 'currency' then
  317. BasicType:=btDouble;
  318. end;
  319. end
  320. else
  321. if jt = 'float' then begin
  322. d:=TTypeDef.Create(CurDef, dtType);
  323. with TTypeDef(d) do
  324. if it.Strings['FloatType'] = 'single' then
  325. BasicType:=btSingle
  326. else
  327. BasicType:=btDouble;
  328. end
  329. else
  330. if jt = 'string' then begin
  331. d:=TTypeDef.Create(CurDef, dtType);
  332. s:=it.Strings['StrType'];
  333. with TTypeDef(d) do
  334. if (s = 'wide') or (s = 'unicode') or (s = 'long') then
  335. BasicType:=btWideString
  336. else
  337. BasicType:=btString;
  338. if not (IsSystemUnit and (CompareText(CurObjName, 'rawbytestring') = 0)) then
  339. CurObjName:=s + 'string';
  340. end
  341. else
  342. if jt = 'enum' then begin
  343. d:=TTypeDef.Create(CurDef, dtEnum);
  344. TTypeDef(d).BasicType:=btEnum;
  345. end
  346. else
  347. if jt = 'set' then
  348. d:=TSetDef.Create(CurDef, dtSet)
  349. else
  350. if jt = 'ptr' then begin
  351. d:=TPointerDef.Create(CurDef, dtPointer);
  352. end
  353. else
  354. if jt = 'const' then
  355. d:=TConstDef.Create(CurDef, dtConst)
  356. else
  357. if jt = 'array' then
  358. d:=TArrayDef.Create(CurDef, dtArray)
  359. else
  360. if jt = 'classref' then
  361. d:=TClassRefDef.Create(CurDef, dtClassRef)
  362. else
  363. continue;
  364. if (CurObjName = '') and not (d.DefType in [dtEnum, dtArray]) then begin
  365. d.Free;
  366. continue;
  367. end;
  368. // Common def attributes
  369. d.Name:=CurObjName;
  370. d.DefId:=it.Get('Id', -1);
  371. d.SymId:=it.Get('SymId', -1);
  372. s:=it.Get('Visibility', '');
  373. d.IsPrivate:=(s <> '') and (s <> 'public') and (s <> 'published');
  374. if Copy(d.Name, 1, 1) = '$' then
  375. d.IsPrivate:=True;
  376. // Specific def attributes
  377. case d.DefType of
  378. dtClass:
  379. with TClassDef(d) do begin
  380. if CType <> ctRecord then
  381. AncestorClass:=TClassDef(_GetRef(it.Get('Ancestor', TJSONObject(nil)), TClassDef));
  382. if CType in [ctObject, ctRecord] then
  383. Size:=it.Integers['Size'];
  384. _ReadDefs(d, it, 'Fields');
  385. end;
  386. dtProc, dtProcType:
  387. with TProcDef(d) do begin
  388. arr:=it.Get('Options', TJSONArray(nil));
  389. if arr <> nil then
  390. for j:=0 to arr.Count - 1 do begin
  391. s:=arr.Strings[j];
  392. if s = 'procedure' then
  393. ProcType:=ptProcedure
  394. else
  395. if s = 'function' then
  396. ProcType:=ptFunction
  397. else
  398. if s = 'constructor' then begin
  399. ProcType:=ptConstructor;
  400. if CompareText(Name, 'create') = 0 then
  401. Name:='Create'; // fix char case for standard constructors
  402. end
  403. else
  404. if s = 'destructor' then
  405. ProcType:=ptDestructor
  406. else
  407. if s = 'overriding' then begin
  408. ProcType:=ptDestructor;
  409. ProcOpt:=ProcOpt + [poOverride];
  410. if ProcType <> ptConstructor then
  411. IsPrivate:=True;
  412. end
  413. else
  414. if s = 'overload' then
  415. ProcOpt:=ProcOpt + [poOverload]
  416. else
  417. if s = 'abstract' then
  418. TClassDef(Parent).HasAbstractMethods:=True
  419. else
  420. if s = 'classmethod' then
  421. ProcOpt:=ProcOpt + [poClassMethod];
  422. end;
  423. ReturnType:=_GetRef(it.Get('RetType', TJSONObject(nil)));
  424. if (DefType = dtProcType) and not ( (ReturnType is TTypeDef) and (TTypeDef(ReturnType).BasicType = btVoid) ) then
  425. ProcType:=ptFunction;
  426. if it.Get('MethodPtr', False) then
  427. ProcOpt:=ProcOpt + [poMethodPtr];
  428. if IsSystemUnit and (ProcType = ptFunction) and (Name = 'int') then
  429. Name:='Int';
  430. _ReadDefs(d, it, 'Params');
  431. for j:=0 to d.Count - 1 do
  432. with d[j] do begin
  433. if DefType <> dtParam then
  434. continue;
  435. s:=Name;
  436. Name:=Format('p%d', [j + 1]);
  437. AliasName:=s;
  438. end;
  439. // Check for user exception handler proc
  440. if AMainUnit and (Parent = CurUnit) and (OnExceptionProc = nil) and (AnsiCompareText(Name, OnExceptionProcName) = 0) then
  441. OnExceptionProc:=TProcDef(d);
  442. end;
  443. dtVar, dtField, dtParam:
  444. with TVarDef(d) do begin
  445. VarType:=_GetRef(it.Objects['VarType']);
  446. s:=it.Get('Spez', '');
  447. if s = 'out' then
  448. VarOpt:=[voWrite, voOut]
  449. else
  450. if s = 'var' then
  451. VarOpt:=[voRead, voWrite, voVar]
  452. else
  453. if s = 'const' then
  454. VarOpt:=[voRead, voConst];
  455. end;
  456. dtProp:
  457. with TVarDef(d) do begin
  458. VarType:=_GetRef(it.Objects['PropType']);
  459. if it.Get('Getter', TJSONObject(nil)) <> nil then
  460. VarOpt:=VarOpt + [voRead];
  461. if it.Get('Setter', TJSONObject(nil)) <> nil then
  462. VarOpt:=VarOpt + [voWrite];
  463. _ReadDefs(d, it, 'Params');
  464. end;
  465. dtEnum:
  466. _ReadDefs(d, it, 'Elements');
  467. dtSet:
  468. with TSetDef(d) do begin
  469. Size:=it.Integers['Size'];
  470. Base:=it.Integers['Base'];
  471. ElMax:=it.Integers['Max'];
  472. ElType:=TTypeDef(_GetRef(it.Objects['ElType'], TTypeDef));
  473. if (ElType <> nil) and (ElType.Name = '') then
  474. ElType.Name:=CurObjName + 'El';
  475. end;
  476. dtConst:
  477. with TConstDef(d) do begin
  478. VarType:=_GetRef(it.Get('TypeRef', TJSONObject(nil)));
  479. s:=it.Strings['ValType'];
  480. if s = 'int' then
  481. Value:=IntToStr(it.Int64s['Value'])
  482. else
  483. if s = 'float' then begin
  484. Str(it.Floats['Value'], s);
  485. Value:=s;
  486. end
  487. else
  488. if s = 'string' then begin
  489. s:=it.Strings['Value'];
  490. s:=StringReplace(s, '\', '\\', [rfReplaceAll]);
  491. s:=StringReplace(s, '"', '\"', [rfReplaceAll]);
  492. s:=StringReplace(s, #9, '\t', [rfReplaceAll]);
  493. s:=StringReplace(s, #10, '\n', [rfReplaceAll]);
  494. s:=StringReplace(s, #13, '\r', [rfReplaceAll]);
  495. Value:='"' + s + '"';
  496. end
  497. else
  498. FreeAndNil(d);
  499. end;
  500. dtPointer:
  501. with TPointerDef(d) do begin
  502. PtrType:=_GetRef(it.Get('Ptr', TJSONObject(nil)));;
  503. if AMainUnit and (Parent = CurUnit) and (CompareText(Name, 'TJavaObject') = 0) then
  504. DefType:=dtJniObject;
  505. end;
  506. dtArray:
  507. with TArrayDef(d) do begin
  508. _ReadDefs(d, it, 'Types');
  509. RangeLow:=it.Get('Low', -1);
  510. RangeHigh:=it.Get('High', -1);
  511. RangeType:=_GetRef(it.Get('RangeType', TJSONObject(nil)));
  512. ElType:=_GetRef(it.Get('ElType', TJSONObject(nil)));
  513. end;
  514. dtClassRef:
  515. with TClassRefDef(d) do begin
  516. ClassRef:=_GetRef(it.Get('Ref', TJSONObject(nil)));;
  517. end;
  518. end;
  519. end;
  520. end;
  521. var
  522. i, j: integer;
  523. s: string;
  524. chkres: TCheckItemResult;
  525. jp: TJSONParser;
  526. jdata: TJSONData;
  527. begin
  528. Result:=nil;
  529. for i:=0 to Units.Count - 1 do
  530. if CompareText(Units[i].Name, AUnitName) = 0 then begin
  531. Result:=TUnitDef(Units[i]);
  532. exit;
  533. end;
  534. chkres:=FOnCheckItem(AUnitName);
  535. if chkres = crExclude then
  536. exit;
  537. AMainUnit:=chkres = crInclude;
  538. if not AMainUnit and ( (CompareText(AUnitName, 'windows') = 0) or (CompareText(AUnitName, 'unix') = 0) ) then
  539. exit;
  540. s:=ReadUnit(AUnitName);
  541. try
  542. jdata:=nil;
  543. try
  544. jp:=TJSONParser.Create(s, [joUTF8]);
  545. try
  546. s:='';
  547. jdata:=jp.Parse;
  548. junit:=TJSONObject(jdata.Items[0]);
  549. finally
  550. jp.Free;
  551. end;
  552. IsSystemUnit:=CompareText(AUnitName, 'system') = 0;
  553. Result:=TUnitDef.Create(nil, dtUnit);
  554. Units.Add(Result);
  555. Result.Name:=junit.Strings['Name'];
  556. Result.PPUVer:=junit.Integers['Version'];
  557. Result.CPU:=junit.Strings['TargetCPU'];
  558. Result.OS:=junit.Strings['TargetOS'];
  559. j:=Length(Result.CPU);
  560. if AnsiLowerCase(Copy(Result.OS, Length(Result.OS) - j, j + 1)) = AnsiLowerCase('-' + Result.CPU) then
  561. Result.OS:=Copy(Result.OS, 1, Length(Result.OS) - j - 1);
  562. Result.IntfCRC:=junit.Strings['InterfaceCRC'];
  563. if IsSystemUnit then
  564. Result.IsUsed:=True;
  565. if not FDefaultSearchPathAdded then begin
  566. FDefaultSearchPathAdded:=True;
  567. AddDefaultSearchPath(AnsiLowerCase(Result.CPU), AnsiLowerCase(Result.OS));
  568. end;
  569. if junit.Find('Units') <> nil then
  570. with junit.Arrays['Units'] do begin
  571. SetLength(deref, Count);
  572. for i:=0 to Count - 1 do begin
  573. deref[i]:=TUnitDef.Create(nil, dtNone);
  574. deref[i].Name:=Strings[i];
  575. end;
  576. end;
  577. CurUnit:=Result;
  578. _ReadDefs(CurUnit, junit, 'Interface');
  579. Result.ResolveDefs;
  580. if CompareText(AUnitName, 'jni') = 0 then begin
  581. for i:=0 to Result.Count - 1 do
  582. with Result[i] do
  583. if CompareText(Name, 'PJNIEnv') = 0 then
  584. DefType:=dtJniEnv;
  585. end;
  586. if AMainUnit then
  587. Result.IsUsed:=True;
  588. SetLength(Result.UsedUnits, Length(deref));
  589. j:=0;
  590. for i:=0 to High(deref) do
  591. if deref[i].DefType = dtNone then
  592. deref[i].Free
  593. else begin
  594. Result.UsedUnits[j]:=deref[i];
  595. Inc(j);
  596. end;
  597. SetLength(Result.UsedUnits, j);
  598. finally
  599. jdata.Free;
  600. end;
  601. except
  602. if CurObjName <> '' then
  603. CurObjName:=Format('; Object: "%s"', [CurObjName]);
  604. raise Exception.CreateFmt('%s' + LineEnding + 'Unit: "%s"%s', [Exception(ExceptObject).Message, AUnitName, CurObjName]);
  605. end;
  606. end;
  607. procedure TPPUParser.AddSearchPath(const ASearchPath: string);
  608. var
  609. i, j: integer;
  610. s, d: string;
  611. sr: TSearchRec;
  612. sl: TStringList;
  613. begin
  614. sl:=TStringList.Create;
  615. try
  616. sl.Delimiter:=';';
  617. sl.DelimitedText:=ASearchPath;
  618. i:=0;
  619. while i < sl.Count do begin
  620. s:=sl[i];
  621. if (Pos('*', s) > 0) or (Pos('?', s) > 0) then begin
  622. d:=ExtractFilePath(s);
  623. j:=FindFirst(s, faDirectory, sr);
  624. while j = 0 do begin
  625. if (sr.Name <> '.') and (sr.Name <> '..') then
  626. sl.Add(d + sr.Name);
  627. j:=FindNext(sr);
  628. end;
  629. FindClose(sr);
  630. sl.Delete(i);
  631. end
  632. else
  633. Inc(i);
  634. end;
  635. SearchPath.AddStrings(sl);
  636. finally
  637. sl.Free;
  638. end;
  639. end;
  640. function TPPUParser.ReadProcessOutput(const AExeName, AParams: string; var AOutput, AError: string): integer;
  641. procedure _ReadOutput(o: TInputPipeStream; var s: string; var idx: integer);
  642. var
  643. i: integer;
  644. begin
  645. with o do
  646. while NumBytesAvailable > 0 do begin
  647. i:=NumBytesAvailable;
  648. if idx + i > Length(s) then
  649. SetLength(s, idx + i*10 + idx div 10);
  650. ReadBuffer(s[idx + 1], i);
  651. Inc(idx, i);
  652. end;
  653. end;
  654. var
  655. p: TProcess;
  656. oidx, eidx: integer;
  657. begin
  658. AOutput:='';
  659. AError:='';
  660. oidx:=0;
  661. eidx:=0;
  662. p:=TProcess.Create(nil);
  663. try
  664. p.Executable:=AExeName;
  665. p.Parameters.Text:=AParams;
  666. p.Options:=[poUsePipes, poNoConsole];
  667. p.ShowWindow:=swoHIDE;
  668. p.StartupOptions:=[suoUseShowWindow];
  669. try
  670. p.Execute;
  671. except
  672. raise Exception.CreateFmt('Unable to run "%s".'+LineEnding+'%s', [p.Executable, Exception(ExceptObject).Message]);
  673. end;
  674. repeat
  675. if p.Output.NumBytesAvailable = 0 then
  676. TThread.Yield;
  677. _ReadOutput(p.Output, AOutput, oidx);
  678. _ReadOutput(p.Stderr, AError, eidx);
  679. until not p.Running and (p.Output.NumBytesAvailable = 0) and (p.Stderr.NumBytesAvailable = 0);
  680. SetLength(AOutput, oidx);
  681. SetLength(AError, eidx);
  682. Result:=p.ExitStatus;
  683. finally
  684. p.Free;
  685. end;
  686. end;
  687. procedure TPPUParser.AddDefaultSearchPath(const ACPU, AOS: string);
  688. var
  689. fpc, s, e: string;
  690. sl: TStringList;
  691. i, j: integer;
  692. begin
  693. try
  694. fpc:=ExtractFilePath(ppudumpprog) + 'fpc' + ExtractFileExt(ParamStr(0));
  695. if not FileExists(fpc) then
  696. exit;
  697. // Find the compiler binary
  698. if ReadProcessOutput(fpc, '-P' + ACPU + LineEnding + '-T' + AOS + LineEnding + '-PB', s, e) <> 0 then
  699. exit;
  700. fpc:=Trim(s);
  701. // Get units path from the compiler output
  702. ReadProcessOutput(fpc, '-P' + ACPU + LineEnding + '-T' + AOS + LineEnding + '-vt' + LineEnding + '.', s, e);
  703. sl:=TStringList.Create;
  704. try
  705. sl.Text:=s;
  706. s:='';
  707. for i:=0 to sl.Count - 1 do begin
  708. s:=sl[i];
  709. j:=Pos(':', s);
  710. if j > 0 then begin
  711. s:=Trim(Copy(s, j + 1, MaxInt));
  712. s:=ExcludeTrailingPathDelimiter(s);
  713. if (Copy(s, Length(s) - 3, 4) = DirectorySeparator + 'rtl') and DirectoryExists(s) then begin
  714. AddSearchPath(ExtractFilePath(s) + '*');
  715. exit;
  716. end;
  717. end;
  718. end;
  719. finally
  720. sl.Free;
  721. end;
  722. except
  723. // Ignore exceptions
  724. end;
  725. end;
  726. end.