fpcp.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. {
  2. Copyright (c) 2013-2016 by Free Pascal development team
  3. This unit implements the loading and searching of package files
  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. }
  17. unit fpcp;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,cstreams,
  22. globtype,
  23. pcp,finput,fpkg;
  24. type
  25. tpcppackage=class(tpackage)
  26. private
  27. loaded : boolean;
  28. pcpfile : tpcpfile;
  29. private
  30. function openpcp:boolean;
  31. function search_package(ashortname:boolean):boolean;
  32. function search_package_file:boolean;
  33. procedure setfilename(const fn:string;allowoutput:boolean);
  34. procedure writecontainernames;
  35. procedure writecontainedunits;
  36. procedure writerequiredpackages;
  37. procedure writepputable;
  38. procedure writeppudata;
  39. procedure readcontainernames;
  40. procedure readcontainedunits;
  41. procedure readrequiredpackages;
  42. procedure readpputable;
  43. public
  44. constructor create(const pn:string);
  45. destructor destroy; override;
  46. procedure loadpcp;
  47. procedure savepcp;
  48. function getmodulestream(module:tmodulebase):tcstream;
  49. procedure initmoduleinfo(module:tmodulebase);
  50. procedure addunit(module:tmodulebase);
  51. end;
  52. implementation
  53. uses
  54. sysutils,
  55. cfileutl,cutils,
  56. systems,globals,version,
  57. verbose,
  58. entfile,fppu,ppu,pkgutil;
  59. { tpcppackage }
  60. function tpcppackage.openpcp: boolean;
  61. var
  62. pcpfiletime : longint;
  63. begin
  64. result:=false;
  65. Message1(package_t_pcp_loading,pcpfilename);
  66. { Get pcpfile time (also check if the file exists) }
  67. pcpfiletime:=getnamedfiletime(pcpfilename);
  68. if pcpfiletime=-1 then
  69. exit;
  70. { Open the pcpfile }
  71. Message1(package_u_pcp_name,pcpfilename);
  72. pcpfile:=tpcpfile.create(pcpfilename);
  73. if not pcpfile.openfile then
  74. begin
  75. pcpfile.free;
  76. pcpfile:=nil;
  77. Message(package_u_pcp_file_too_short);
  78. exit;
  79. end;
  80. { check for a valid PPU file }
  81. if not pcpfile.checkpcpid then
  82. begin
  83. pcpfile.free;
  84. pcpfile:=nil;
  85. Message(package_u_pcp_invalid_header);
  86. exit;
  87. end;
  88. { check for allowed PCP versions }
  89. if not (pcpfile.getversion=CurrentPCPVersion) then
  90. begin
  91. Message1(package_u_pcp_invalid_version,tostr(pcpfile.getversion));
  92. pcpfile.free;
  93. pcpfile:=nil;
  94. exit;
  95. end;
  96. { check the target processor }
  97. if tsystemcpu(pcpfile.header.common.cpu)<>target_cpu then
  98. begin
  99. pcpfile.free;
  100. pcpfile:=nil;
  101. Message(package_u_pcp_invalid_processor);
  102. exit;
  103. end;
  104. { check target }
  105. if tsystem(pcpfile.header.common.target)<>target_info.system then
  106. begin
  107. pcpfile.free;
  108. pcpfile:=nil;
  109. Message(package_u_pcp_invalid_target);
  110. exit;
  111. end;
  112. {$ifdef cpufpemu}
  113. { check if floating point emulation is on?
  114. fpu emulation isn't unit levelwise because it affects calling convention }
  115. if ((pcpfile.header.common.flags and uf_fpu_emulation)<>0) xor
  116. (cs_fp_emulation in current_settings.moduleswitches) then
  117. begin
  118. pcpfile.free;
  119. pcpfile:=nil;
  120. Message(package_u_pcp_invalid_fpumode);
  121. exit;
  122. end;
  123. {$endif cpufpemu}
  124. { Load values to be access easier }
  125. //flags:=pcpfile.header.common.flags;
  126. //crc:=pcpfile.header.checksum;
  127. { Show Debug info }
  128. Message1(package_u_pcp_time,filetimestring(pcpfiletime));
  129. Message1(package_u_pcp_flags,tostr(pcpfile.header.common.flags{flags}));
  130. Message1(package_u_pcp_crc,hexstr(pcpfile.header.checksum,8));
  131. (*Message1(package_u_pcp_crc,hexstr(ppufile.header.interface_checksum,8)+' (intfc)');
  132. Message1(package_u_pcp_crc,hexstr(ppufile.header.indirect_checksum,8)+' (indc)');
  133. Comment(V_used,'Number of definitions: '+tostr(ppufile.header.deflistsize));
  134. Comment(V_used,'Number of symbols: '+tostr(ppufile.header.symlistsize));
  135. do_compile:=false;*)
  136. result:=true;
  137. end;
  138. function tpcppackage.search_package(ashortname:boolean):boolean;
  139. var
  140. singlepathstring,
  141. filename : TCmdStr;
  142. function package_exists(const ext:string;var foundfile:TCmdStr):boolean;
  143. begin
  144. if CheckVerbosity(V_Tried) then
  145. Message1(package_t_packagesearch,Singlepathstring+filename+ext);
  146. result:=FindFile(filename+ext,singlepathstring,true,foundfile);
  147. end;
  148. function package_search_path(const s:TCmdStr):boolean;
  149. var
  150. found : boolean;
  151. hs : TCmdStr;
  152. begin
  153. found:=false;
  154. singlepathstring:=FixPath(s,false);
  155. { Check for package file }
  156. { TODO }
  157. found:=package_exists({target_info.pkginfoext}'.pcp',hs);
  158. if found then
  159. begin
  160. setfilename(hs,false);
  161. found:=openpcp;
  162. end;
  163. result:=found;
  164. end;
  165. function search_path_list(list:TSearchPathList):boolean;
  166. var
  167. hp : TCmdStrListItem;
  168. found : boolean;
  169. begin
  170. found:=false;
  171. hp:=TCmdStrListItem(list.First);
  172. while assigned(hp) do
  173. begin
  174. found:=package_search_path(hp.Str);
  175. if found then
  176. break;
  177. hp:=TCmdStrListItem(hp.next);
  178. end;
  179. result:=found;
  180. end;
  181. begin
  182. filename:=packagename^;
  183. result:=search_path_list(packagesearchpath);
  184. end;
  185. function tpcppackage.search_package_file: boolean;
  186. var
  187. found : boolean;
  188. begin
  189. found:=false;
  190. if search_package(false) then
  191. found:=true;
  192. if not found and
  193. (length(packagename^)>8) and
  194. search_package(true) then
  195. found:=true;
  196. result:=found;
  197. end;
  198. procedure tpcppackage.setfilename(const fn:string;allowoutput:boolean);
  199. var
  200. p,n : tpathstr;
  201. begin
  202. p:=FixPath(ExtractFilePath(fn),false);
  203. n:=FixFileName(ChangeFileExt(ExtractFileName(fn),''));
  204. { pcp name }
  205. if allowoutput then
  206. if (OutputUnitDir<>'') then
  207. p:=OutputUnitDir
  208. else
  209. if (OutputExeDir<>'') then
  210. p:=OutputExeDir;
  211. pcpfilename:=p+n+{target_info.pkginfoext}'.pcp';
  212. end;
  213. procedure tpcppackage.writecontainernames;
  214. begin
  215. pcpfile.putstring(pplfilename);
  216. //pcpfile.putstring(ppafilename);
  217. pcpfile.writeentry(ibpackagefiles);
  218. end;
  219. procedure tpcppackage.writecontainedunits;
  220. var
  221. p : pcontainedunit;
  222. i : longint;
  223. begin
  224. pcpfile.putlongint(containedmodules.count);
  225. pcpfile.writeentry(ibstartcontained);
  226. { for now we write the unit name and the ppu file name }
  227. for i:=0 to containedmodules.count-1 do
  228. begin
  229. p:=pcontainedunit(containedmodules.items[i]);
  230. pcpfile.putstring(p^.module.modulename^);
  231. pcpfile.putstring(p^.ppufile);
  232. end;
  233. pcpfile.writeentry(ibendcontained);
  234. end;
  235. procedure tpcppackage.writerequiredpackages;
  236. var
  237. i : longint;
  238. begin
  239. pcpfile.putlongint(requiredpackages.count);
  240. pcpfile.writeentry(ibstartrequireds);
  241. for i:=0 to requiredpackages.count-1 do
  242. begin
  243. pcpfile.putstring(requiredpackages.NameOfIndex(i));
  244. end;
  245. pcpfile.writeentry(ibendrequireds);
  246. end;
  247. procedure tpcppackage.writepputable;
  248. var
  249. module : pcontainedunit;
  250. i : longint;
  251. begin
  252. { no need to write the count again; it's the same as for the contained units }
  253. for i:=0 to containedmodules.count-1 do
  254. begin
  255. module:=pcontainedunit(containedmodules[i]);
  256. pcpfile.putlongint(module^.offset);
  257. pcpfile.putlongint(module^.size);
  258. end;
  259. pcpfile.writeentry(ibpputable);
  260. end;
  261. procedure tpcppackage.writeppudata;
  262. const
  263. align: array[0..15] of byte = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
  264. var
  265. i,j,
  266. pos,
  267. rem : longint;
  268. module : pcontainedunit;
  269. stream : TCStream;
  270. begin
  271. pcpfile.flush;
  272. for i:=0 to containedmodules.count-1 do
  273. begin
  274. module:=pcontainedunit(containedmodules[i]);
  275. pos:=pcpfile.position;
  276. { align to 16 byte so that it can be nicely viewed in hex editors;
  277. maybe we could also use 512 byte alignment instead }
  278. rem:=$f-(pos and $f);
  279. pcpfile.stream.write(align[0],rem+1);
  280. pcpfile.flush;
  281. module^.offset:=pcpfile.position;
  282. { retrieve substream for the current position }
  283. stream:=pcpfile.substream(module^.offset,-1);
  284. rewriteppu(module^.module.ppufilename,stream);
  285. module^.size:=stream.position;
  286. stream.free;
  287. end;
  288. pos:=pcpfile.position;
  289. { align to 16 byte so that it can be nicely viewed in hex editors;
  290. maybe we could also use 512 byte alignment instead }
  291. rem:=$f-(pos and $f);
  292. pcpfile.stream.write(align[0],rem+1);
  293. end;
  294. procedure tpcppackage.readcontainernames;
  295. begin
  296. if pcpfile.readentry<>ibpackagefiles then
  297. begin
  298. message(package_f_pcp_read_error);
  299. internalerror(424242);
  300. end;
  301. pplfilename:=pcpfile.getstring;
  302. writeln('PPL filename: ',pplfilename);
  303. end;
  304. procedure tpcppackage.readcontainedunits;
  305. var
  306. cnt,i : longint;
  307. name,path : string;
  308. p : pcontainedunit;
  309. begin
  310. if pcpfile.readentry<>ibstartcontained then
  311. begin
  312. message(package_f_pcp_read_error);
  313. internalerror(424242);
  314. end;
  315. cnt:=pcpfile.getlongint;
  316. if pcpfile.readentry<>ibendcontained then
  317. begin
  318. message(package_f_pcp_read_error);
  319. internalerror(424242);
  320. end;
  321. for i:=0 to cnt-1 do
  322. begin
  323. name:=pcpfile.getstring;
  324. path:=pcpfile.getstring;
  325. new(p);
  326. p^.module:=nil;
  327. p^.ppufile:=path;
  328. p^.offset:=0;
  329. p^.size:=0;
  330. containedmodules.add(name,p);
  331. message1(package_u_contained_unit,name);
  332. end;
  333. end;
  334. procedure tpcppackage.readrequiredpackages;
  335. var
  336. cnt,i : longint;
  337. name : string;
  338. begin
  339. if pcpfile.readentry<>ibstartrequireds then
  340. begin
  341. Writeln('Error reading pcp file');
  342. internalerror(2014110901);
  343. end;
  344. cnt:=pcpfile.getlongint;
  345. if pcpfile.readentry<>ibendrequireds then
  346. begin
  347. Writeln('Error reading pcp file');
  348. internalerror(2014110902);
  349. end;
  350. for i:=0 to cnt-1 do
  351. begin
  352. name:=pcpfile.getstring;
  353. requiredpackages.add(name,nil);
  354. Writeln('Found required package ',name);
  355. end;
  356. end;
  357. procedure tpcppackage.readpputable;
  358. var
  359. module : pcontainedunit;
  360. i : longint;
  361. begin
  362. if pcpfile.readentry<>ibpputable then
  363. begin
  364. message(package_f_pcp_read_error);
  365. internalerror(2015103001);
  366. end;
  367. for i:=0 to containedmodules.count-1 do
  368. begin
  369. module:=pcontainedunit(containedmodules[i]);
  370. module^.offset:=pcpfile.getlongint;
  371. module^.size:=pcpfile.getlongint;
  372. end;
  373. end;
  374. constructor tpcppackage.create(const pn: string);
  375. begin
  376. inherited create(pn);
  377. setfilename(pn,true);
  378. end;
  379. destructor tpcppackage.destroy;
  380. begin
  381. pcpfile.free;
  382. inherited destroy;
  383. end;
  384. procedure tpcppackage.loadpcp;
  385. var
  386. newpackagename : string;
  387. begin
  388. if loaded then
  389. exit;
  390. if not search_package_file then
  391. begin
  392. Message1(package_f_cant_find_pcp,realpackagename^);
  393. exit;
  394. end
  395. else
  396. Message1(package_u_pcp_found,realpackagename^);
  397. if not assigned(pcpfile) then
  398. internalerror(2013053101);
  399. if pcpfile.readentry<>ibpackagename then
  400. Message1(package_f_cant_read_pcp,realpackagename^);
  401. newpackagename:=pcpfile.getstring;
  402. if upper(newpackagename)<>packagename^ then
  403. Comment(V_Error,'Package was renamed: '+realpackagename^);
  404. readcontainernames;
  405. readrequiredpackages;
  406. readcontainedunits;
  407. readpputable;
  408. end;
  409. procedure tpcppackage.savepcp;
  410. var
  411. tablepos,
  412. oldpos : longint;
  413. begin
  414. { create new ppufile }
  415. pcpfile:=tpcpfile.create(pcpfilename);
  416. if not pcpfile.createfile then
  417. Message2(package_f_cant_create_pcp,realpackagename^,pcpfilename);
  418. pcpfile.putstring(realpackagename^);
  419. pcpfile.writeentry(ibpackagename);
  420. writecontainernames;
  421. writerequiredpackages;
  422. writecontainedunits;
  423. { the offsets and the contents of the ppus are not crc'd }
  424. pcpfile.do_crc:=false;
  425. pcpfile.flush;
  426. tablepos:=pcpfile.position;
  427. { this will write a table with empty entries }
  428. writepputable;
  429. pcpfile.do_crc:=true;
  430. { the last entry ibend is written automatically }
  431. { flush to be sure }
  432. pcpfile.flush;
  433. { create and write header }
  434. pcpfile.header.common.size:=pcpfile.size;
  435. pcpfile.header.checksum:=pcpfile.crc;
  436. pcpfile.header.common.compiler:=wordversion;
  437. pcpfile.header.common.cpu:=word(target_cpu);
  438. pcpfile.header.common.target:=word(target_info.system);
  439. //pcpfile.header.flags:=flags;
  440. pcpfile.header.ppulistsize:=containedmodules.count;
  441. pcpfile.header.requiredlistsize:=requiredpackages.count;
  442. pcpfile.writeheader;
  443. { write the ppu table which will also fill the offsets/sizes }
  444. writeppudata;
  445. pcpfile.flush;
  446. oldpos:=pcpfile.position;
  447. { now write the filled PPU table at the previously stored position }
  448. pcpfile.position:=tablepos;
  449. writepputable;
  450. pcpfile.position:=oldpos;
  451. { save crc in current module also }
  452. //crc:=pcpfile.crc;
  453. pcpfile.closefile;
  454. pcpfile.free;
  455. pcpfile:=nil;
  456. end;
  457. function tpcppackage.getmodulestream(module:tmodulebase):tcstream;
  458. var
  459. i : longint;
  460. contained : pcontainedunit;
  461. begin
  462. for i:=0 to containedmodules.count-1 do
  463. begin
  464. contained:=pcontainedunit(containedmodules[i]);
  465. if contained^.module=module then
  466. begin
  467. result:=pcpfile.substream(contained^.offset,contained^.size);
  468. exit;
  469. end;
  470. end;
  471. result:=nil;
  472. end;
  473. procedure tpcppackage.initmoduleinfo(module: tmodulebase);
  474. begin
  475. pplfilename:=extractfilename(module.sharedlibfilename);
  476. end;
  477. procedure tpcppackage.addunit(module: tmodulebase);
  478. var
  479. containedunit : pcontainedunit;
  480. begin
  481. new(containedunit);
  482. containedunit^.module:=module;
  483. containedunit^.ppufile:=extractfilename(module.ppufilename);
  484. containedunit^.offset:=0;
  485. containedunit^.size:=0;
  486. containedmodules.add(module.modulename^,containedunit);
  487. end;
  488. end.