ppuparser.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. arr:=it.Get('Options', TJSONArray(nil));
  385. if arr <> nil then
  386. for j:=0 to arr.Count - 1 do begin
  387. s:=arr.Strings[j];
  388. if s = 'abstract_methods' then
  389. HasAbstractMethods:=True;
  390. end;
  391. _ReadDefs(d, it, 'Fields');
  392. end;
  393. dtProc, dtProcType:
  394. with TProcDef(d) do begin
  395. arr:=it.Get('Options', TJSONArray(nil));
  396. if arr <> nil then
  397. for j:=0 to arr.Count - 1 do begin
  398. s:=arr.Strings[j];
  399. if s = 'procedure' then
  400. ProcType:=ptProcedure
  401. else
  402. if s = 'function' then
  403. ProcType:=ptFunction
  404. else
  405. if s = 'constructor' then begin
  406. ProcType:=ptConstructor;
  407. if CompareText(Name, 'create') = 0 then
  408. Name:='Create'; // fix char case for standard constructors
  409. end
  410. else
  411. if s = 'destructor' then
  412. ProcType:=ptDestructor
  413. else
  414. if s = 'overriding' then begin
  415. ProcType:=ptDestructor;
  416. ProcOpt:=ProcOpt + [poOverride];
  417. if ProcType <> ptConstructor then
  418. IsPrivate:=True;
  419. end
  420. else
  421. if s = 'overload' then
  422. ProcOpt:=ProcOpt + [poOverload]
  423. else
  424. if s = 'abstract' then
  425. TClassDef(Parent).HasAbstractMethods:=True
  426. else
  427. if s = 'classmethod' then
  428. ProcOpt:=ProcOpt + [poClassMethod];
  429. end;
  430. ReturnType:=_GetRef(it.Get('RetType', TJSONObject(nil)));
  431. if (DefType = dtProcType) and not ( (ReturnType is TTypeDef) and (TTypeDef(ReturnType).BasicType = btVoid) ) then
  432. ProcType:=ptFunction;
  433. if it.Get('MethodPtr', False) then
  434. ProcOpt:=ProcOpt + [poMethodPtr];
  435. if IsSystemUnit and (ProcType = ptFunction) and (Name = 'int') then
  436. Name:='Int';
  437. _ReadDefs(d, it, 'Params');
  438. for j:=0 to d.Count - 1 do
  439. with d[j] do begin
  440. if DefType <> dtParam then
  441. continue;
  442. s:=Name;
  443. Name:=Format('p%d', [j + 1]);
  444. AliasName:=s;
  445. end;
  446. // Check for user exception handler proc
  447. if AMainUnit and (Parent = CurUnit) and (OnExceptionProc = nil) and (AnsiCompareText(Name, OnExceptionProcName) = 0) then
  448. OnExceptionProc:=TProcDef(d);
  449. end;
  450. dtVar, dtField, dtParam:
  451. with TVarDef(d) do begin
  452. VarType:=_GetRef(it.Objects['VarType']);
  453. s:=it.Get('Spez', '');
  454. if s = 'out' then
  455. VarOpt:=[voWrite, voOut]
  456. else
  457. if s = 'var' then
  458. VarOpt:=[voRead, voWrite, voVar]
  459. else
  460. if s = 'const' then
  461. VarOpt:=[voRead, voConst];
  462. end;
  463. dtProp:
  464. with TVarDef(d) do begin
  465. VarType:=_GetRef(it.Objects['PropType']);
  466. if it.Get('Getter', TJSONObject(nil)) <> nil then
  467. VarOpt:=VarOpt + [voRead];
  468. if it.Get('Setter', TJSONObject(nil)) <> nil then
  469. VarOpt:=VarOpt + [voWrite];
  470. _ReadDefs(d, it, 'Params');
  471. end;
  472. dtEnum:
  473. _ReadDefs(d, it, 'Elements');
  474. dtSet:
  475. with TSetDef(d) do begin
  476. Size:=it.Integers['Size'];
  477. Base:=it.Integers['Base'];
  478. ElMax:=it.Integers['Max'];
  479. ElType:=TTypeDef(_GetRef(it.Objects['ElType'], TTypeDef));
  480. if (ElType <> nil) and (ElType.Name = '') then
  481. ElType.Name:=CurObjName + 'El';
  482. end;
  483. dtConst:
  484. with TConstDef(d) do begin
  485. VarType:=_GetRef(it.Get('TypeRef', TJSONObject(nil)));
  486. s:=it.Strings['ValType'];
  487. if s = 'int' then
  488. Value:=IntToStr(it.Int64s['Value'])
  489. else
  490. if s = 'float' then begin
  491. Str(it.Floats['Value'], s);
  492. Value:=s;
  493. end
  494. else
  495. if s = 'string' then begin
  496. s:=it.Strings['Value'];
  497. s:=StringReplace(s, '\', '\\', [rfReplaceAll]);
  498. s:=StringReplace(s, '"', '\"', [rfReplaceAll]);
  499. s:=StringReplace(s, #9, '\t', [rfReplaceAll]);
  500. s:=StringReplace(s, #10, '\n', [rfReplaceAll]);
  501. s:=StringReplace(s, #13, '\r', [rfReplaceAll]);
  502. Value:='"' + s + '"';
  503. end
  504. else
  505. FreeAndNil(d);
  506. end;
  507. dtPointer:
  508. with TPointerDef(d) do begin
  509. PtrType:=_GetRef(it.Get('Ptr', TJSONObject(nil)));;
  510. if AMainUnit and (Parent = CurUnit) and (CompareText(Name, 'TJavaObject') = 0) then
  511. DefType:=dtJniObject;
  512. end;
  513. dtArray:
  514. with TArrayDef(d) do begin
  515. _ReadDefs(d, it, 'Types');
  516. RangeLow:=it.Get('Low', -1);
  517. RangeHigh:=it.Get('High', -1);
  518. RangeType:=_GetRef(it.Get('RangeType', TJSONObject(nil)));
  519. ElType:=_GetRef(it.Get('ElType', TJSONObject(nil)));
  520. end;
  521. dtClassRef:
  522. with TClassRefDef(d) do begin
  523. ClassRef:=_GetRef(it.Get('Ref', TJSONObject(nil)));;
  524. end;
  525. end;
  526. end;
  527. end;
  528. var
  529. i, j: integer;
  530. s: string;
  531. chkres: TCheckItemResult;
  532. jp: TJSONParser;
  533. jdata: TJSONData;
  534. begin
  535. Result:=nil;
  536. for i:=0 to Units.Count - 1 do
  537. if CompareText(Units[i].Name, AUnitName) = 0 then begin
  538. Result:=TUnitDef(Units[i]);
  539. exit;
  540. end;
  541. chkres:=FOnCheckItem(AUnitName);
  542. if chkres = crExclude then
  543. exit;
  544. AMainUnit:=chkres = crInclude;
  545. if not AMainUnit and ( (CompareText(AUnitName, 'windows') = 0) or (CompareText(AUnitName, 'unix') = 0) ) then
  546. exit;
  547. s:=ReadUnit(AUnitName);
  548. try
  549. jdata:=nil;
  550. try
  551. jp:=TJSONParser.Create(s, [joUTF8]);
  552. try
  553. s:='';
  554. jdata:=jp.Parse;
  555. junit:=TJSONObject(jdata.Items[0]);
  556. finally
  557. jp.Free;
  558. end;
  559. IsSystemUnit:=CompareText(AUnitName, 'system') = 0;
  560. Result:=TUnitDef.Create(nil, dtUnit);
  561. Units.Add(Result);
  562. Result.Name:=junit.Strings['Name'];
  563. Result.PPUVer:=junit.Integers['Version'];
  564. Result.CPU:=junit.Strings['TargetCPU'];
  565. Result.OS:=junit.Strings['TargetOS'];
  566. j:=Length(Result.CPU);
  567. if AnsiLowerCase(Copy(Result.OS, Length(Result.OS) - j, j + 1)) = AnsiLowerCase('-' + Result.CPU) then
  568. Result.OS:=Copy(Result.OS, 1, Length(Result.OS) - j - 1);
  569. Result.IntfCRC:=junit.Strings['InterfaceCRC'];
  570. if IsSystemUnit then
  571. Result.IsUsed:=True;
  572. if not FDefaultSearchPathAdded then begin
  573. FDefaultSearchPathAdded:=True;
  574. AddDefaultSearchPath(AnsiLowerCase(Result.CPU), AnsiLowerCase(Result.OS));
  575. end;
  576. if junit.Find('Units') <> nil then
  577. with junit.Arrays['Units'] do begin
  578. SetLength(deref, Count);
  579. for i:=0 to Count - 1 do begin
  580. deref[i]:=TUnitDef.Create(nil, dtNone);
  581. deref[i].Name:=Strings[i];
  582. end;
  583. end;
  584. CurUnit:=Result;
  585. _ReadDefs(CurUnit, junit, 'Interface');
  586. Result.ResolveDefs;
  587. if CompareText(AUnitName, 'jni') = 0 then begin
  588. for i:=0 to Result.Count - 1 do
  589. with Result[i] do
  590. if CompareText(Name, 'PJNIEnv') = 0 then
  591. DefType:=dtJniEnv;
  592. end;
  593. if AMainUnit then
  594. Result.IsUsed:=True;
  595. SetLength(Result.UsedUnits, Length(deref));
  596. j:=0;
  597. for i:=0 to High(deref) do
  598. if deref[i].DefType = dtNone then
  599. deref[i].Free
  600. else begin
  601. Result.UsedUnits[j]:=deref[i];
  602. Inc(j);
  603. end;
  604. SetLength(Result.UsedUnits, j);
  605. finally
  606. jdata.Free;
  607. end;
  608. except
  609. if CurObjName <> '' then
  610. CurObjName:=Format('; Object: "%s"', [CurObjName]);
  611. raise Exception.CreateFmt('%s' + LineEnding + 'Unit: "%s"%s', [Exception(ExceptObject).Message, AUnitName, CurObjName]);
  612. end;
  613. end;
  614. procedure TPPUParser.AddSearchPath(const ASearchPath: string);
  615. var
  616. i, j: integer;
  617. s, d: string;
  618. sr: TSearchRec;
  619. sl: TStringList;
  620. begin
  621. sl:=TStringList.Create;
  622. try
  623. sl.Delimiter:=';';
  624. sl.DelimitedText:=ASearchPath;
  625. i:=0;
  626. while i < sl.Count do begin
  627. s:=sl[i];
  628. if (Pos('*', s) > 0) or (Pos('?', s) > 0) then begin
  629. d:=ExtractFilePath(s);
  630. j:=FindFirst(s, faDirectory, sr);
  631. while j = 0 do begin
  632. if (sr.Name <> '.') and (sr.Name <> '..') then
  633. sl.Add(d + sr.Name);
  634. j:=FindNext(sr);
  635. end;
  636. FindClose(sr);
  637. sl.Delete(i);
  638. end
  639. else
  640. Inc(i);
  641. end;
  642. SearchPath.AddStrings(sl);
  643. finally
  644. sl.Free;
  645. end;
  646. end;
  647. function TPPUParser.ReadProcessOutput(const AExeName, AParams: string; var AOutput, AError: string): integer;
  648. procedure _ReadOutput(o: TInputPipeStream; var s: string; var idx: integer);
  649. var
  650. i: integer;
  651. begin
  652. with o do
  653. while NumBytesAvailable > 0 do begin
  654. i:=NumBytesAvailable;
  655. if idx + i > Length(s) then
  656. SetLength(s, idx + i*10 + idx div 10);
  657. ReadBuffer(s[idx + 1], i);
  658. Inc(idx, i);
  659. end;
  660. end;
  661. var
  662. p: TProcess;
  663. oidx, eidx: integer;
  664. begin
  665. AOutput:='';
  666. AError:='';
  667. oidx:=0;
  668. eidx:=0;
  669. p:=TProcess.Create(nil);
  670. try
  671. p.Executable:=AExeName;
  672. p.Parameters.Text:=AParams;
  673. p.Options:=[poUsePipes, poNoConsole];
  674. p.ShowWindow:=swoHIDE;
  675. p.StartupOptions:=[suoUseShowWindow];
  676. try
  677. p.Execute;
  678. except
  679. raise Exception.CreateFmt('Unable to run "%s".'+LineEnding+'%s', [p.Executable, Exception(ExceptObject).Message]);
  680. end;
  681. repeat
  682. if p.Output.NumBytesAvailable = 0 then
  683. TThread.Yield;
  684. _ReadOutput(p.Output, AOutput, oidx);
  685. _ReadOutput(p.Stderr, AError, eidx);
  686. until not p.Running and (p.Output.NumBytesAvailable = 0) and (p.Stderr.NumBytesAvailable = 0);
  687. SetLength(AOutput, oidx);
  688. SetLength(AError, eidx);
  689. Result:=p.ExitStatus;
  690. finally
  691. p.Free;
  692. end;
  693. end;
  694. procedure TPPUParser.AddDefaultSearchPath(const ACPU, AOS: string);
  695. var
  696. fpc, s, e: string;
  697. sl: TStringList;
  698. i, j: integer;
  699. begin
  700. try
  701. fpc:=ExtractFilePath(ppudumpprog) + 'fpc' + ExtractFileExt(ParamStr(0));
  702. if not FileExists(fpc) then
  703. exit;
  704. // Find the compiler binary
  705. if ReadProcessOutput(fpc, '-P' + ACPU + LineEnding + '-T' + AOS + LineEnding + '-PB', s, e) <> 0 then
  706. exit;
  707. fpc:=Trim(s);
  708. // Get units path from the compiler output
  709. ReadProcessOutput(fpc, '-P' + ACPU + LineEnding + '-T' + AOS + LineEnding + '-vt' + LineEnding + '.', s, e);
  710. sl:=TStringList.Create;
  711. try
  712. sl.Text:=s;
  713. s:='';
  714. for i:=0 to sl.Count - 1 do begin
  715. s:=sl[i];
  716. j:=Pos(':', s);
  717. if j > 0 then begin
  718. s:=Trim(Copy(s, j + 1, MaxInt));
  719. s:=ExcludeTrailingPathDelimiter(s);
  720. if (Copy(s, Length(s) - 3, 4) = DirectorySeparator + 'rtl') and DirectoryExists(s) then begin
  721. AddSearchPath(ExtractFilePath(s) + '*');
  722. exit;
  723. end;
  724. end;
  725. end;
  726. finally
  727. sl.Free;
  728. end;
  729. except
  730. // Ignore exceptions
  731. end;
  732. end;
  733. end.