comprsrc.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. public
  38. function IsCompiled(const fn : ansistring) : boolean;override;
  39. procedure Collect(const fn : ansistring);override;
  40. end;
  41. procedure CompileResourceFiles;
  42. procedure CollectResourceFiles;
  43. Var
  44. ResCompiler : String;
  45. RCCompiler : String;
  46. implementation
  47. uses
  48. SysUtils,
  49. cutils,cfileutl,cclasses,
  50. Globtype,Globals,Verbose,Fmodule,
  51. Script;
  52. const
  53. GlobalResName = 'fpc-res';
  54. {****************************************************************************
  55. TRESOURCEFILE
  56. ****************************************************************************}
  57. constructor tresourcefile.create(const fn : ansistring);
  58. begin
  59. fname:=fn;
  60. end;
  61. procedure tresourcefile.PostProcessResourcefile(const s : ansistring);
  62. begin
  63. end;
  64. function tresourcefile.IsCompiled(const fn: ansistring): boolean;
  65. begin
  66. Result:=CompareText(ExtractFileExt(fn), target_info.resobjext) = 0;
  67. end;
  68. procedure tresourcefile.Collect(const fn: ansistring);
  69. begin
  70. if fn='' then
  71. exit;
  72. fname:=fn;
  73. Compile(roOBJ, ChangeFileExt(fn, target_info.resobjext));
  74. end;
  75. procedure tresourcefile.compile(output: tresoutput; const OutName: ansistring);
  76. Function SelectBin(Const Bin1,Bin2 : String) : String;
  77. begin
  78. If (Bin1<>'') then
  79. SelectBin:=Bin1
  80. else
  81. SelectBin:=Bin2;
  82. end;
  83. var
  84. respath,
  85. srcfilepath,
  86. s,
  87. bin,
  88. resbin : TCmdStr;
  89. resfound,
  90. objused : boolean;
  91. begin
  92. if output=roRES then
  93. Bin:=SelectBin(RCCompiler,target_res.rcbin)
  94. else
  95. Bin:=SelectBin(ResCompiler,target_res.resbin);
  96. if bin='' then
  97. exit;
  98. resfound:=false;
  99. if utilsdirectory<>'' then
  100. resfound:=FindFile(utilsprefix+bin+source_info.exeext,utilsdirectory,false,resbin);
  101. if not resfound then
  102. resfound:=FindExe(utilsprefix+bin,false,resbin);
  103. { get also the path to be searched for the windres.h }
  104. respath:=ExtractFilePath(resbin);
  105. if (not resfound) and not(cs_link_nolink in current_settings.globalswitches) then
  106. begin
  107. Message(exec_e_res_not_found);
  108. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  109. end;
  110. srcfilepath:=ExtractFilePath(current_module.mainsource^);
  111. if output=roRES then
  112. begin
  113. s:=target_res.rccmd;
  114. Replace(s,'$RES',maybequoted(OutName));
  115. Replace(s,'$RC',maybequoted(fname));
  116. ObjUsed:=False;
  117. end
  118. else
  119. begin
  120. s:=target_res.rescmd;
  121. ObjUsed:=(pos('$OBJ',s)>0);
  122. Replace(s,'$OBJ',maybequoted(OutName));
  123. Replace(s,'$RES',maybequoted(fname));
  124. end;
  125. { windres doesn't like empty include paths }
  126. if respath='' then
  127. respath:='.';
  128. Replace(s,'$INC',maybequoted(respath));
  129. if (target_res.resbin='windres') and
  130. (srcfilepath<>'') then
  131. s:=s+' --include '+maybequoted(srcfilepath);
  132. { Execute the command }
  133. if not (cs_link_nolink in current_settings.globalswitches) then
  134. begin
  135. Message1(exec_i_compilingresource,fname);
  136. Message2(exec_d_resbin_params,resbin,s);
  137. FlushOutput;
  138. try
  139. if ExecuteProcess(resbin,s) <> 0 then
  140. begin
  141. Message(exec_e_error_while_linking);
  142. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  143. end;
  144. except
  145. on E:EOSError do
  146. begin
  147. Message(exec_e_cant_call_linker);
  148. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  149. end
  150. end;
  151. end;
  152. if output=roOBJ then
  153. PostProcessResourcefile(OutName);
  154. { Update asmres when externmode is set }
  155. if cs_link_nolink in current_settings.globalswitches then
  156. AsmRes.AddLinkCommand(resbin,s,'');
  157. if (output=roOBJ) and ObjUsed then
  158. current_module.linkunitofiles.add(OutName,link_always);
  159. end;
  160. function TWinLikeResourceFile.IsCompiled(const fn: ansistring): boolean;
  161. const
  162. ResSignature : array [1..32] of byte =
  163. ($00,$00,$00,$00,$20,$00,$00,$00,$FF,$FF,$00,$00,$FF,$FF,$00,$00,
  164. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00);
  165. var
  166. f : file;
  167. oldfmode : byte;
  168. buf: array[1..32] of byte;
  169. i: longint;
  170. begin
  171. Result:=CompareText(ExtractFileExt(fn), target_info.resext) = 0;
  172. if Result or not FileExists(fn, False) then exit;
  173. oldfmode:=Filemode;
  174. Filemode:=0;
  175. assign(f,fn);
  176. reset(f,1);
  177. BlockRead(f, buf, SizeOf(buf), i);
  178. close(f);
  179. Filemode:=oldfmode;
  180. if i<>SizeOf(buf) then
  181. exit;
  182. for i:=1 to 32 do
  183. if buf[i]<>ResSignature[i] then
  184. exit;
  185. Result:=True;
  186. end;
  187. procedure TWinLikeResourceFile.Collect(const fn: ansistring);
  188. const
  189. zeroes: array[1..3] of byte = (0,0,0);
  190. type
  191. TResHeader = packed record
  192. DataSize: dword;
  193. HeaderSize: dword;
  194. end;
  195. var
  196. fs: TCFileStream;
  197. i, sz: longint;
  198. hdr: TResHeader;
  199. begin
  200. if fn='' then
  201. begin
  202. if FOut<>nil then
  203. begin
  204. FOut.Free;
  205. Compile(roOBJ,ChangeFileExt(fname,target_info.resobjext));
  206. end;
  207. end
  208. else
  209. try
  210. fs:=TCFileStream.Create(fn,fmOpenRead or fmShareDenyNone);
  211. if CStreamError<>0 then
  212. begin
  213. fs.Free;
  214. Comment(V_Error,'Can''t open resource file: '+fn);
  215. Include(current_settings.globalswitches, cs_link_nolink);
  216. exit;
  217. end;
  218. if FOut=nil then
  219. begin
  220. FOut:=TCFileStream.Create(fname,fmCreate);
  221. { writing res signature }
  222. FOut.CopyFrom(fs, 32);
  223. end
  224. else
  225. fs.Seek(32, soFromBeginning);
  226. sz:=fs.Size;
  227. repeat
  228. fs.ReadBuffer(hdr, SizeOf(hdr));
  229. FOut.WriteBuffer(hdr, SizeOf(hdr));
  230. i:=hdr.HeaderSize + hdr.DataSize - SizeOf(hdr);
  231. if fs.Position + i > sz then
  232. begin
  233. Comment(V_Error,'Invalid resource file: '+fn);
  234. Include(current_settings.globalswitches, cs_link_nolink);
  235. fs.Free;
  236. exit;
  237. end;
  238. FOut.CopyFrom(fs, i);
  239. { align resource to dword }
  240. i:=4 - FOut.Position mod 4;
  241. if i<4 then
  242. FOut.WriteBuffer(zeroes, i);
  243. { position to the next resource }
  244. i:=4 - fs.Position mod 4;
  245. if i<4 then
  246. fs.Seek(i, soFromCurrent);
  247. until fs.Position + SizeOf(hdr) >= sz;
  248. fs.Free;
  249. except
  250. on E:EOSError do begin
  251. Comment(V_Error,'Error processing resource file: '+fn+': '+E.Message);
  252. Include(current_settings.globalswitches, cs_link_nolink);
  253. end;
  254. end;
  255. end;
  256. procedure CompileResourceFiles;
  257. var
  258. resourcefile : tresourcefile;
  259. res: TCmdStrListItem;
  260. p,s : TCmdStr;
  261. src,dst : TCFileStream;
  262. outfmt : tresoutput;
  263. begin
  264. { OS/2 (EMX) must be processed elsewhere (in the linking/binding stage).
  265. same with MacOS}
  266. if target_info.system in [system_i386_os2,system_i386_emx,system_powerpc_macos] then exit;
  267. p:=ExtractFilePath(current_module.mainsource^);
  268. res:=TCmdStrListItem(current_module.ResourceFiles.First);
  269. while res<>nil do
  270. begin
  271. if target_info.res=res_none then
  272. Message(scan_e_resourcefiles_not_supported);
  273. s:=res.FPStr;
  274. if not path_absolute(s) then
  275. s:=p+s;
  276. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  277. if resourcefile.IsCompiled(s) then
  278. begin
  279. resourcefile.free;
  280. if CompareText(current_module.outputpath^, p) <> 0 then
  281. begin
  282. { Copy .res file to units output dir }
  283. res.FPStr:=ExtractFileName(res.FPStr);
  284. src:=TCFileStream.Create(s,fmOpenRead or fmShareDenyNone);
  285. if CStreamError<>0 then
  286. begin
  287. Comment(V_Error,'Can''t open resource file: '+src.FileName);
  288. Include(current_settings.globalswitches, cs_link_nolink);
  289. exit;
  290. end;
  291. dst:=TCFileStream.Create(current_module.outputpath^+res.FPStr,fmCreate);
  292. if CStreamError<>0 then
  293. begin
  294. Comment(V_Error,'Can''t create resource file: '+dst.FileName);
  295. Include(current_settings.globalswitches, cs_link_nolink);
  296. exit;
  297. end;
  298. dst.CopyFrom(src,src.Size);
  299. dst.Free;
  300. src.Free;
  301. end;
  302. end
  303. else
  304. begin
  305. res.FPStr:=ExtractFileName(res.FPStr);
  306. if target_res.rcbin='' then
  307. begin
  308. { if target does not have .rc to .res compiler, create obj }
  309. outfmt:=roOBJ;
  310. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resobjext);
  311. end
  312. else
  313. begin
  314. outfmt:=roRES;
  315. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resext);
  316. end;
  317. resourcefile.compile(outfmt, current_module.outputpath^+res.FPStr);
  318. resourcefile.free;
  319. end;
  320. res:=TCmdStrListItem(res.Next);
  321. end;
  322. end;
  323. procedure CollectResourceFiles;
  324. var
  325. resourcefile : tresourcefile;
  326. procedure ProcessModule(u : tmodule);
  327. var
  328. res : TCmdStrListItem;
  329. s : TCmdStr;
  330. begin
  331. res:=TCmdStrListItem(u.ResourceFiles.First);
  332. while assigned(res) do
  333. begin
  334. if path_absolute(res.FPStr) then
  335. s:=res.FPStr
  336. else
  337. begin
  338. s:=u.path^+res.FPStr;
  339. if not FileExists(s,True) then
  340. s:=u.outputpath^+res.FPStr;
  341. end;
  342. resourcefile.Collect(s);
  343. res:=TCmdStrListItem(res.Next);
  344. end;
  345. end;
  346. var
  347. hp : tused_unit;
  348. s : TCmdStr;
  349. begin
  350. if (target_info.res=res_none) or (target_res.rcbin='') then
  351. exit;
  352. if cs_link_nolink in current_settings.globalswitches then
  353. exit;
  354. s:=main_module.outputpath^+GlobalResName+target_info.resext;
  355. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  356. hp:=tused_unit(usedunits.first);
  357. while assigned(hp) do
  358. begin
  359. ProcessModule(hp.u);
  360. hp:=tused_unit(hp.next);
  361. end;
  362. ProcessModule(current_module);
  363. { Finish collection }
  364. resourcefile.Collect('');
  365. resourcefile.free;
  366. end;
  367. end.