comprsrc.pas 14 KB

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