2
0

comprsrc.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Handles the resource files handling
  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 comprsrc;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. Systems, cstreams;
  22. type
  23. tresoutput = (roRES, roOBJ);
  24. tresourcefile = class(TAbstractResourceFile)
  25. private
  26. fname : ansistring;
  27. public
  28. constructor Create(const fn : ansistring);override;
  29. procedure Compile(output: tresoutput; const OutName: ansistring);virtual;
  30. procedure PostProcessResourcefile(const s : ansistring);virtual;
  31. function IsCompiled(const fn : ansistring) : boolean;virtual;
  32. procedure Collect(const fn : ansistring);virtual;
  33. end;
  34. TWinLikeResourceFile = class(tresourcefile)
  35. private
  36. FOut: TCFileStream;
  37. FLastIconID: longint;
  38. FLastCursorID: longint;
  39. public
  40. function IsCompiled(const fn : ansistring) : boolean;override;
  41. procedure Collect(const fn : ansistring);override;
  42. end;
  43. procedure CompileResourceFiles;
  44. procedure CollectResourceFiles;
  45. Var
  46. ResCompiler : String;
  47. RCCompiler : String;
  48. implementation
  49. uses
  50. SysUtils,
  51. cutils,cfileutl,cclasses,
  52. Globtype,Globals,Verbose,Fmodule,
  53. Script;
  54. const
  55. GlobalResName = 'fpc-res';
  56. {****************************************************************************
  57. TRESOURCEFILE
  58. ****************************************************************************}
  59. constructor tresourcefile.create(const fn : ansistring);
  60. begin
  61. fname:=fn;
  62. end;
  63. procedure tresourcefile.PostProcessResourcefile(const s : ansistring);
  64. begin
  65. end;
  66. function tresourcefile.IsCompiled(const fn: ansistring): boolean;
  67. begin
  68. Result:=CompareText(ExtractFileExt(fn), target_info.resobjext) = 0;
  69. end;
  70. procedure tresourcefile.Collect(const fn: ansistring);
  71. begin
  72. if fn='' then
  73. exit;
  74. fname:=fn;
  75. Compile(roOBJ, ChangeFileExt(fn, target_info.resobjext));
  76. end;
  77. procedure tresourcefile.compile(output: tresoutput; const OutName: ansistring);
  78. Function SelectBin(Const Bin1,Bin2 : String) : String;
  79. begin
  80. If (Bin1<>'') then
  81. SelectBin:=Bin1
  82. else
  83. SelectBin:=Bin2;
  84. end;
  85. var
  86. respath,
  87. srcfilepath,
  88. s,
  89. bin,
  90. resbin : TCmdStr;
  91. resfound,
  92. objused : boolean;
  93. begin
  94. if output=roRES then
  95. Bin:=SelectBin(RCCompiler,target_res.rcbin)
  96. else
  97. Bin:=SelectBin(ResCompiler,target_res.resbin);
  98. if bin='' then
  99. exit;
  100. resfound:=false;
  101. if utilsdirectory<>'' then
  102. resfound:=FindFile(utilsprefix+bin+source_info.exeext,utilsdirectory,false,resbin);
  103. if not resfound then
  104. resfound:=FindExe(utilsprefix+bin,false,resbin);
  105. { get also the path to be searched for the windres.h }
  106. respath:=ExtractFilePath(resbin);
  107. if (not resfound) and not(cs_link_nolink in current_settings.globalswitches) then
  108. begin
  109. Message(exec_e_res_not_found);
  110. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  111. end;
  112. srcfilepath:=ExtractFilePath(current_module.mainsource^);
  113. if output=roRES then
  114. begin
  115. s:=target_res.rccmd;
  116. Replace(s,'$RES',maybequoted(OutName));
  117. Replace(s,'$RC',maybequoted(fname));
  118. ObjUsed:=False;
  119. end
  120. else
  121. begin
  122. s:=target_res.rescmd;
  123. ObjUsed:=(pos('$OBJ',s)>0);
  124. Replace(s,'$OBJ',maybequoted(OutName));
  125. Replace(s,'$RES',maybequoted(fname));
  126. end;
  127. { windres doesn't like empty include paths }
  128. if respath='' then
  129. respath:='.';
  130. Replace(s,'$INC',maybequoted(respath));
  131. if (target_res.resbin='windres') and
  132. (srcfilepath<>'') then
  133. s:=s+' --include '+maybequoted(srcfilepath);
  134. { Execute the command }
  135. if not (cs_link_nolink in current_settings.globalswitches) then
  136. begin
  137. Message1(exec_i_compilingresource,fname);
  138. Message2(exec_d_resbin_params,resbin,s);
  139. FlushOutput;
  140. try
  141. if ExecuteProcess(resbin,s) <> 0 then
  142. begin
  143. Message(exec_e_error_while_linking);
  144. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  145. end;
  146. except
  147. on E:EOSError do
  148. begin
  149. Message(exec_e_cant_call_linker);
  150. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  151. end
  152. end;
  153. end;
  154. if output=roOBJ then
  155. PostProcessResourcefile(OutName);
  156. { Update asmres when externmode is set }
  157. if cs_link_nolink in current_settings.globalswitches then
  158. AsmRes.AddLinkCommand(resbin,s,'');
  159. if (output=roOBJ) and ObjUsed then
  160. current_module.linkunitofiles.add(OutName,link_always);
  161. end;
  162. function TWinLikeResourceFile.IsCompiled(const fn: ansistring): boolean;
  163. const
  164. ResSignature : array [1..32] of byte =
  165. ($00,$00,$00,$00,$20,$00,$00,$00,$FF,$FF,$00,$00,$FF,$FF,$00,$00,
  166. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00);
  167. var
  168. f : file;
  169. oldfmode : byte;
  170. buf: array[1..32] of byte;
  171. i: longint;
  172. begin
  173. Result:=CompareText(ExtractFileExt(fn), target_info.resext) = 0;
  174. if Result or not FileExists(fn, False) then exit;
  175. oldfmode:=Filemode;
  176. Filemode:=0;
  177. assign(f,fn);
  178. reset(f,1);
  179. BlockRead(f, buf, SizeOf(buf), i);
  180. close(f);
  181. Filemode:=oldfmode;
  182. if i<>SizeOf(buf) then
  183. exit;
  184. for i:=1 to 32 do
  185. if buf[i]<>ResSignature[i] then
  186. exit;
  187. Result:=True;
  188. end;
  189. procedure TWinLikeResourceFile.Collect(const fn: ansistring);
  190. const
  191. zeroes: array[1..3] of byte = (0,0,0);
  192. type
  193. TResHeader = packed record
  194. DataSize: dword;
  195. HeaderSize: dword;
  196. ResTypeFlag: word;
  197. ResTypeID: word;
  198. end;
  199. PIconHeader = ^TIconHeader;
  200. TIconHeader = packed record
  201. Reserved: word;
  202. wType: word;
  203. wCount: word;
  204. end;
  205. PIconDir = ^TIconDir;
  206. TIconDir = packed record
  207. bWidth: byte;
  208. bHeight: byte;
  209. bColorCount: byte;
  210. bReserved: byte;
  211. wPlanes: word;
  212. wBitCount: word;
  213. lBytesInRes: dword;
  214. wNameOrdinal: word;
  215. end;
  216. var
  217. fs: TCFileStream;
  218. i, sz, rsz, IconCount, CursorCount: longint;
  219. hdr: TResHeader;
  220. P: pointer;
  221. PData: PIconHeader;
  222. PDir: PIconDir;
  223. ResNameBuf: array[0..1] of word;
  224. begin
  225. if fn='' then
  226. begin
  227. if FOut<>nil then
  228. begin
  229. FOut.Free;
  230. Compile(roOBJ,ChangeFileExt(fname,target_info.resobjext));
  231. end;
  232. end
  233. else
  234. try
  235. fs:=TCFileStream.Create(fn,fmOpenRead or fmShareDenyNone);
  236. if CStreamError<>0 then
  237. begin
  238. fs.Free;
  239. Comment(V_Error,'Can''t open resource file: '+fn);
  240. Include(current_settings.globalswitches, cs_link_nolink);
  241. exit;
  242. end;
  243. if FOut=nil then
  244. begin
  245. FOut:=TCFileStream.Create(fname,fmCreate);
  246. { writing res signature }
  247. FOut.CopyFrom(fs, 32);
  248. end
  249. else
  250. fs.Seek(32, soFromBeginning);
  251. sz:=fs.Size;
  252. IconCount := 0;
  253. CursorCount := 0;
  254. repeat
  255. fs.ReadBuffer(hdr, SizeOf(hdr));
  256. FOut.WriteBuffer(hdr, SizeOf(hdr));
  257. rsz:=hdr.HeaderSize + hdr.DataSize - SizeOf(hdr);
  258. if fs.Position + rsz > sz then
  259. begin
  260. Comment(V_Error,'Invalid resource file: '+fn);
  261. Include(current_settings.globalswitches, cs_link_nolink);
  262. fs.Free;
  263. exit;
  264. end;
  265. { Adjusting cursor and icon IDs }
  266. if hdr.ResTypeFlag = $FFFF then { resource type is ordinal }
  267. case hdr.ResTypeID of
  268. 1, 3:
  269. { cursor or icon resource }
  270. begin
  271. fs.ReadBuffer(ResNameBuf, SizeOf(ResNameBuf));
  272. if ResNameBuf[0] = $FFFF then { resource name is ordinal }
  273. if hdr.ResTypeID = 1 then
  274. begin
  275. Inc(ResNameBuf[1], FLastCursorID);
  276. Inc(CursorCount);
  277. end
  278. else
  279. begin
  280. Inc(ResNameBuf[1], FLastIconID);
  281. Inc(IconCount);
  282. end;
  283. FOut.WriteBuffer(ResNameBuf, SizeOf(ResNameBuf));
  284. Dec(rsz, SizeOf(ResNameBuf));
  285. end;
  286. 12, 14:
  287. { cursor or icon group resource }
  288. begin
  289. GetMem(P, rsz);
  290. fs.ReadBuffer(P^, rsz);
  291. PData := PIconHeader(P + hdr.HeaderSize - sizeof(hdr));
  292. PDir := PIconDir(Pointer(PData) + sizeof(TIconHeader));
  293. for i := 0 to PData^.wCount-1 do
  294. begin
  295. if hdr.ResTypeID = 12 then
  296. Inc(PDir^.wNameOrdinal, FLastCursorID)
  297. else
  298. Inc(PDir^.wNameOrdinal, FLastIconID);
  299. Inc(PDir);
  300. end;
  301. FOut.WriteBuffer(P^, rsz);
  302. rsz:=0;
  303. FreeMem(P);
  304. end;
  305. end;
  306. { copy rest of the resource data }
  307. FOut.CopyFrom(fs, rsz);
  308. { align resource to dword }
  309. i:=4 - FOut.Position mod 4;
  310. if i<4 then
  311. FOut.WriteBuffer(zeroes, i);
  312. { position to the next resource }
  313. i:=4 - fs.Position mod 4;
  314. if i<4 then
  315. fs.Seek(i, soFromCurrent);
  316. until fs.Position + SizeOf(hdr) >= sz;
  317. fs.Free;
  318. Inc(FLastCursorID, CursorCount);
  319. Inc(FLastIconID, IconCount);
  320. except
  321. on E:EOSError do begin
  322. Comment(V_Error,'Error processing resource file: '+fn+': '+E.Message);
  323. Include(current_settings.globalswitches, cs_link_nolink);
  324. end;
  325. end;
  326. end;
  327. procedure CompileResourceFiles;
  328. var
  329. resourcefile : tresourcefile;
  330. res: TCmdStrListItem;
  331. p,s : TCmdStr;
  332. src,dst : TCFileStream;
  333. outfmt : tresoutput;
  334. begin
  335. { OS/2 (EMX) must be processed elsewhere (in the linking/binding stage).
  336. same with MacOS}
  337. if target_info.system in [system_i386_os2,system_i386_emx,system_powerpc_macos] then exit;
  338. p:=ExtractFilePath(current_module.mainsource^);
  339. res:=TCmdStrListItem(current_module.ResourceFiles.First);
  340. while res<>nil do
  341. begin
  342. if target_info.res=res_none then
  343. Message(scan_e_resourcefiles_not_supported);
  344. s:=res.FPStr;
  345. if not path_absolute(s) then
  346. s:=p+s;
  347. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  348. if resourcefile.IsCompiled(s) then
  349. begin
  350. resourcefile.free;
  351. if CompareText(current_module.outputpath^, p) <> 0 then
  352. begin
  353. { Copy .res file to units output dir }
  354. res.FPStr:=ExtractFileName(res.FPStr);
  355. src:=TCFileStream.Create(s,fmOpenRead or fmShareDenyNone);
  356. if CStreamError<>0 then
  357. begin
  358. Comment(V_Error,'Can''t open resource file: '+src.FileName);
  359. Include(current_settings.globalswitches, cs_link_nolink);
  360. exit;
  361. end;
  362. dst:=TCFileStream.Create(current_module.outputpath^+res.FPStr,fmCreate);
  363. if CStreamError<>0 then
  364. begin
  365. Comment(V_Error,'Can''t create resource file: '+dst.FileName);
  366. Include(current_settings.globalswitches, cs_link_nolink);
  367. exit;
  368. end;
  369. dst.CopyFrom(src,src.Size);
  370. dst.Free;
  371. src.Free;
  372. end;
  373. end
  374. else
  375. begin
  376. res.FPStr:=ExtractFileName(res.FPStr);
  377. if target_res.rcbin='' then
  378. begin
  379. { if target does not have .rc to .res compiler, create obj }
  380. outfmt:=roOBJ;
  381. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resobjext);
  382. end
  383. else
  384. begin
  385. outfmt:=roRES;
  386. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resext);
  387. end;
  388. resourcefile.compile(outfmt, current_module.outputpath^+res.FPStr);
  389. resourcefile.free;
  390. end;
  391. res:=TCmdStrListItem(res.Next);
  392. end;
  393. end;
  394. procedure CollectResourceFiles;
  395. var
  396. resourcefile : tresourcefile;
  397. procedure ProcessModule(u : tmodule);
  398. var
  399. res : TCmdStrListItem;
  400. s : TCmdStr;
  401. begin
  402. res:=TCmdStrListItem(u.ResourceFiles.First);
  403. while assigned(res) do
  404. begin
  405. if path_absolute(res.FPStr) then
  406. s:=res.FPStr
  407. else
  408. begin
  409. s:=u.path^+res.FPStr;
  410. if not FileExists(s,True) then
  411. s:=u.outputpath^+res.FPStr;
  412. end;
  413. resourcefile.Collect(s);
  414. res:=TCmdStrListItem(res.Next);
  415. end;
  416. end;
  417. var
  418. hp : tused_unit;
  419. s : TCmdStr;
  420. begin
  421. if (target_info.res=res_none) or (target_res.rcbin='') then
  422. exit;
  423. if cs_link_nolink in current_settings.globalswitches then
  424. exit;
  425. s:=main_module.outputpath^+GlobalResName+target_info.resext;
  426. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  427. hp:=tused_unit(usedunits.first);
  428. while assigned(hp) do
  429. begin
  430. ProcessModule(hp.u);
  431. hp:=tused_unit(hp.next);
  432. end;
  433. ProcessModule(current_module);
  434. { Finish collection }
  435. resourcefile.Collect('');
  436. resourcefile.free;
  437. end;
  438. end.