ppuparser.pas 23 KB

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