comprsrc.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. {
  2. Copyright (c) 1998-2008 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, Script;
  22. type
  23. tresoutput = (roRES, roOBJ);
  24. tresourcefile = class(TAbstractResourceFile)
  25. private
  26. fname : ansistring;
  27. protected
  28. function SetupCompilerArguments(output: tresoutput; const OutName :
  29. ansistring; respath: ansistring; out ObjUsed : boolean) : ansistring; virtual;
  30. public
  31. constructor Create(const fn : ansistring);override;
  32. function Compile(output: tresoutput; const OutName: ansistring) : boolean; virtual;
  33. procedure PostProcessResourcefile(const s : ansistring);virtual;
  34. function IsCompiled(const fn : ansistring) : boolean;virtual;
  35. procedure Collect(const fn : ansistring);virtual;
  36. procedure EndCollect; virtual;
  37. end;
  38. TWinLikeResourceFile = class(tresourcefile)
  39. private
  40. fResScript : TScript;
  41. fScriptName : ansistring;
  42. fCollectCount : integer;
  43. protected
  44. function SetupCompilerArguments(output: tresoutput; const OutName :
  45. ansistring; respath: ansistring; out ObjUsed : boolean) : ansistring; override;
  46. public
  47. constructor Create(const fn : ansistring);override;
  48. destructor Destroy; override;
  49. function Compile(output: tresoutput; const OutName: ansistring) : boolean; override;
  50. function IsCompiled(const fn : ansistring) : boolean;override;
  51. procedure Collect(const fn : ansistring);override;
  52. procedure EndCollect; override;
  53. end;
  54. procedure CompileResourceFiles;
  55. procedure CollectResourceFiles;
  56. Var
  57. ResCompiler : String;
  58. RCCompiler : String;
  59. implementation
  60. uses
  61. SysUtils,
  62. cutils,cfileutl,cclasses,
  63. Globtype,Globals,Verbose,Fmodule, comphook;
  64. {****************************************************************************
  65. TRESOURCEFILE
  66. ****************************************************************************}
  67. constructor tresourcefile.create(const fn : ansistring);
  68. begin
  69. fname:=fn;
  70. end;
  71. procedure tresourcefile.PostProcessResourcefile(const s : ansistring);
  72. begin
  73. end;
  74. function tresourcefile.IsCompiled(const fn: ansistring): boolean;
  75. begin
  76. Result:=CompareText(ExtractFileExt(fn), target_info.resobjext) = 0;
  77. end;
  78. procedure tresourcefile.Collect(const fn: ansistring);
  79. begin
  80. if fn='' then
  81. exit;
  82. fname:=fn;
  83. Compile(roOBJ, ChangeFileExt(fn, target_info.resobjext));
  84. end;
  85. procedure tresourcefile.EndCollect;
  86. begin
  87. end;
  88. function tresourcefile.SetupCompilerArguments(output: tresoutput; const OutName
  89. : ansistring; respath: ansistring; out ObjUsed : boolean) : ansistring;
  90. var
  91. s : TCmdStr;
  92. begin
  93. if output=roRES then
  94. begin
  95. s:=target_res.rccmd;
  96. Replace(s,'$RES',maybequoted(OutName));
  97. Replace(s,'$RC',maybequoted(fname));
  98. ObjUsed:=False;
  99. end
  100. else
  101. begin
  102. s:=target_res.rescmd;
  103. ObjUsed:=(pos('$OBJ',s)>0);
  104. Replace(s,'$OBJ',maybequoted(OutName));
  105. Replace(s,'$RES',maybequoted(fname));
  106. end;
  107. Result:=s;
  108. end;
  109. function tresourcefile.compile(output: tresoutput; const OutName: ansistring)
  110. : boolean;
  111. Function SelectBin(Const Bin1,Bin2 : String) : String;
  112. begin
  113. If (Bin1<>'') then
  114. SelectBin:=Bin1
  115. else
  116. SelectBin:=Bin2;
  117. end;
  118. var
  119. respath,
  120. s,
  121. bin,
  122. resbin : TCmdStr;
  123. resfound,
  124. objused : boolean;
  125. begin
  126. Result:=true;
  127. if output=roRES then
  128. Bin:=SelectBin(RCCompiler,target_res.rcbin)
  129. else
  130. Bin:=SelectBin(ResCompiler,target_res.resbin);
  131. if bin='' then
  132. begin
  133. Result:=false;
  134. exit;
  135. end;
  136. resfound:=false;
  137. if utilsdirectory<>'' then
  138. resfound:=FindFile(utilsprefix+bin+source_info.exeext,utilsdirectory,false,resbin);
  139. if not resfound then
  140. resfound:=FindExe(utilsprefix+bin,false,resbin);
  141. { get also the path to be searched for the windres.h }
  142. respath:=ExtractFilePath(resbin);
  143. if (not resfound) and not(cs_link_nolink in current_settings.globalswitches) then
  144. begin
  145. Message1(exec_e_res_not_found, bin);
  146. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  147. Result:=false;
  148. end;
  149. s:=SetupCompilerArguments(output,OutName,respath,objused);
  150. { Execute the command }
  151. { Always try to compile resources. but don't complain if cs_link_nolink }
  152. if resfound then
  153. begin
  154. Message1(exec_i_compilingresource,fname);
  155. Message2(exec_d_resbin_params,resbin,s);
  156. FlushOutput;
  157. try
  158. if ExecuteProcess(resbin,s) <> 0 then
  159. begin
  160. if not (cs_link_nolink in current_settings.globalswitches) then
  161. Message(exec_e_error_while_compiling_resources);
  162. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  163. Result:=false;
  164. end;
  165. except
  166. on E:EOSError do
  167. begin
  168. if not (cs_link_nolink in current_settings.globalswitches) then
  169. Message1(exec_e_cant_call_resource_compiler, resbin);
  170. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  171. Result:=false;
  172. end
  173. end;
  174. end;
  175. { Update asmres when externmode is set and resource compiling failed }
  176. if (not Result) and (cs_link_nolink in current_settings.globalswitches) then
  177. AsmRes.AddLinkCommand(resbin,s,OutName);
  178. if Result and (output=roOBJ) and ObjUsed then
  179. current_module.linkunitofiles.add(OutName,link_always);
  180. end;
  181. constructor TWinLikeResourceFile.Create(const fn : ansistring);
  182. begin
  183. inherited Create(fn);
  184. fResScript:=nil;
  185. fCollectCount:=0;
  186. if (tf_use_8_3 in target_info.flags) then
  187. fScriptName:=ChangeFileExt(fn,'.rls')
  188. else
  189. fScriptName:=ChangeFileExt(fn,'.reslst');
  190. end;
  191. destructor TWinLikeResourceFile.Destroy;
  192. begin
  193. if fResScript<>nil then
  194. fResScript.Free;
  195. inherited;
  196. end;
  197. function TWinLikeResourceFile.SetupCompilerArguments(output: tresoutput; const
  198. OutName : ansistring; respath : ansistring; out ObjUsed : boolean) : ansistring;
  199. var
  200. srcfilepath,
  201. preprocessorbin,
  202. s : TCmdStr;
  203. arch : ansistring;
  204. begin
  205. srcfilepath:=ExtractFilePath(current_module.mainsource^);
  206. if output=roRES then
  207. begin
  208. s:=target_res.rccmd;
  209. Replace(s,'$RES',maybequoted(OutName));
  210. Replace(s,'$RC',maybequoted(fname));
  211. ObjUsed:=False;
  212. end
  213. else
  214. begin
  215. s:=target_res.rescmd;
  216. if (res_external_file in target_res.resflags) then
  217. ObjUsed:=false
  218. else
  219. ObjUsed:=(pos('$OBJ',s)>0);
  220. Replace(s,'$OBJ',maybequoted(OutName));
  221. arch:=cpu2str[target_cpu];
  222. //Differentiate between arm and armeb
  223. if (source_info.cpu=cpu_arm) and (source_info.endian=endian_big) then
  224. arch:=arch+'eb';
  225. Replace(s,'$ARCH',arch);
  226. case target_info.endian of
  227. endian_little : Replace(s,'$ENDIAN','littleendian');
  228. endian_big : Replace(s,'$ENDIAN','bigendian');
  229. end;
  230. //call resource compiler with debug switch
  231. if (status.verbosity and V_Debug)<>0 then
  232. Replace(s,'$DBG','-v')
  233. else
  234. Replace(s,'$DBG','');
  235. if fCollectCount=0 then
  236. s:=s+' '+maybequoted(fname)
  237. else
  238. s:=s+' @'+fScriptName;
  239. end;
  240. { windres doesn't like empty include paths }
  241. if respath='' then
  242. respath:='.';
  243. Replace(s,'$INC',maybequoted(respath));
  244. if (output=roRes) and (target_res.rcbin='windres') then
  245. begin
  246. if (srcfilepath<>'') then
  247. s:=s+' --include '+maybequoted(srcfilepath);
  248. { try to find a preprocessor }
  249. preprocessorbin := respath+'cpp'+source_info.exeext;
  250. if FileExists(preprocessorbin,true) then
  251. s:=s+' --preprocessor='+preprocessorbin;
  252. end;
  253. Result:=s;
  254. end;
  255. function TWinLikeResourceFile.compile(output: tresoutput;
  256. const OutName: ansistring) : boolean;
  257. begin
  258. Result:=inherited compile(output,OutName);
  259. //delete fpc-res.lst file if things went well
  260. if Result and (output=roOBJ) then
  261. DeleteFile(fScriptName);
  262. end;
  263. function TWinLikeResourceFile.IsCompiled(const fn: ansistring): boolean;
  264. const
  265. ResSignature : array [1..32] of byte =
  266. ($00,$00,$00,$00,$20,$00,$00,$00,$FF,$FF,$00,$00,$FF,$FF,$00,$00,
  267. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00);
  268. dfmexts : array[1..3] of string[4] = ('.lfm', '.dfm', '.xfm');
  269. var
  270. f : file;
  271. oldfmode : byte;
  272. buf: array[1..32] of byte;
  273. i: longint;
  274. ext : shortstring;
  275. begin
  276. ext:=lower(ExtractFileExt(fn));
  277. Result:=CompareText(ext, target_info.resext) = 0;
  278. if not Result then
  279. for i:=1 to high(dfmexts) do
  280. begin
  281. Result:=CompareText(ext, dfmexts[i]) = 0;
  282. if Result then break;
  283. end;
  284. if Result or not FileExists(fn, False) then exit;
  285. oldfmode:=Filemode;
  286. Filemode:=0;
  287. assign(f,fn);
  288. reset(f,1);
  289. BlockRead(f, buf, SizeOf(buf), i);
  290. close(f);
  291. Filemode:=oldfmode;
  292. if i<>SizeOf(buf) then
  293. exit;
  294. for i:=1 to 32 do
  295. if buf[i]<>ResSignature[i] then
  296. exit;
  297. Result:=True;
  298. end;
  299. procedure TWinLikeResourceFile.Collect(const fn: ansistring);
  300. begin
  301. if fResScript=nil then
  302. fResScript:=TScript.Create(fScriptName);
  303. fResScript.Add(MaybeQuoted(fn));
  304. inc(fCollectCount);
  305. end;
  306. procedure TWinLikeResourceFile.EndCollect;
  307. begin
  308. if fResScript<>nil then
  309. begin
  310. fResScript.WriteToDisk;
  311. FreeAndNil(fResScript);
  312. Compile(roOBJ,ChangeFileExt(fname,target_info.resobjext));
  313. end;
  314. end;
  315. function CopyResFile(inf,outf : TCmdStr) : boolean;
  316. var
  317. src,dst : TCFileStream;
  318. begin
  319. { Copy .res file to units output dir. }
  320. Result:=false;
  321. src:=TCFileStream.Create(inf,fmOpenRead or fmShareDenyNone);
  322. if CStreamError<>0 then
  323. begin
  324. Message1(exec_e_cant_open_resource_file, src.FileName);
  325. Include(current_settings.globalswitches, cs_link_nolink);
  326. exit;
  327. end;
  328. dst:=TCFileStream.Create(current_module.outputpath^+outf,fmCreate);
  329. if CStreamError<>0 then
  330. begin
  331. Message1(exec_e_cant_write_resource_file, dst.FileName);
  332. Include(current_settings.globalswitches, cs_link_nolink);
  333. exit;
  334. end;
  335. dst.CopyFrom(src,src.Size);
  336. dst.Free;
  337. src.Free;
  338. Result:=true;
  339. end;
  340. procedure CompileResourceFiles;
  341. var
  342. resourcefile : tresourcefile;
  343. res: TCmdStrListItem;
  344. p,s : TCmdStr;
  345. outfmt : tresoutput;
  346. begin
  347. { Don't do anything for systems supporting resources without using resource
  348. file classes (e.g. Mac OS). They process resources elsewhere. }
  349. if (target_info.res<>res_none) and (target_res.resourcefileclass=nil) then
  350. exit;
  351. p:=ExtractFilePath(current_module.mainsource^);
  352. res:=TCmdStrListItem(current_module.ResourceFiles.First);
  353. while res<>nil do
  354. begin
  355. if target_info.res=res_none then
  356. Message(scan_e_resourcefiles_not_supported);
  357. s:=res.FPStr;
  358. if not path_absolute(s) then
  359. s:=p+s;
  360. if not FileExists(s, True) then
  361. begin
  362. Message1(exec_e_cant_open_resource_file, s);
  363. Include(current_settings.globalswitches, cs_link_nolink);
  364. exit;
  365. end;
  366. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  367. if resourcefile.IsCompiled(s) then
  368. begin
  369. resourcefile.free;
  370. if AnsiCompareText(current_module.outputpath^, p) <> 0 then
  371. begin
  372. { Copy .res file to units output dir. Otherwise .res file will not be found
  373. when only compiled units path is available }
  374. res.FPStr:=ExtractFileName(res.FPStr); //store file name only in PPU.
  375. if not CopyResFile(s,res.FPStr) then exit;
  376. end;
  377. end
  378. else
  379. begin
  380. res.FPStr:=ExtractFileName(res.FPStr);
  381. if (target_res.rcbin='') and (RCCompiler='') then
  382. begin
  383. { if target does not have .rc to .res compiler, create obj }
  384. outfmt:=roOBJ;
  385. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resobjext);
  386. end
  387. else
  388. begin
  389. outfmt:=roRES;
  390. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resext);
  391. end;
  392. resourcefile.compile(outfmt, current_module.outputpath^+res.FPStr);
  393. resourcefile.free;
  394. end;
  395. res:=TCmdStrListItem(res.Next);
  396. end;
  397. end;
  398. procedure CollectResourceFiles;
  399. var
  400. resourcefile : tresourcefile;
  401. procedure ProcessModule(u : tmodule);
  402. var
  403. res : TCmdStrListItem;
  404. s : TCmdStr;
  405. begin
  406. res:=TCmdStrListItem(u.ResourceFiles.First);
  407. while assigned(res) do
  408. begin
  409. if path_absolute(res.FPStr) then
  410. s:=res.FPStr
  411. else
  412. begin
  413. s:=u.path^+res.FPStr;
  414. if not FileExists(s,True) then
  415. s:=u.outputpath^+res.FPStr;
  416. end;
  417. resourcefile.Collect(s);
  418. res:=TCmdStrListItem(res.Next);
  419. end;
  420. end;
  421. var
  422. hp : tused_unit;
  423. s : TCmdStr;
  424. begin
  425. if (target_info.res=res_none) or ((target_res.resbin='')
  426. and (ResCompiler='')) then
  427. exit;
  428. // if cs_link_nolink in current_settings.globalswitches then
  429. // exit;
  430. s:=ChangeFileExt(current_module.ppufilename^,target_info.resobjext);
  431. if (res_arch_in_file_name in target_res.resflags) then
  432. s:=ChangeFileExt(s,'.'+cpu2str[target_cpu]+target_info.resobjext);
  433. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  434. hp:=tused_unit(usedunits.first);
  435. while assigned(hp) do
  436. begin
  437. ProcessModule(hp.u);
  438. hp:=tused_unit(hp.next);
  439. end;
  440. ProcessModule(current_module);
  441. { Finish collection }
  442. resourcefile.EndCollect;
  443. resourcefile.free;
  444. end;
  445. end.