comprsrc.pas 9.8 KB

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