comprsrc.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. begin
  141. resfound:=FindExe(utilsprefix+bin,false,resbin);
  142. if not resfound and (utilsprefix<>'') and ( (output=roRES) or (Pos('$ARCH', target_res.rescmd)<>0) ) then
  143. { Search for resource compiler without utilsprefix, if RC->RES compiler is called }
  144. { or RES->OBJ compiler supports different architectures. }
  145. resfound:=FindExe(bin,false,resbin);
  146. end;
  147. { get also the path to be searched for the windres.h }
  148. respath:=ExtractFilePath(resbin);
  149. if (not resfound) and not(cs_link_nolink in current_settings.globalswitches) then
  150. begin
  151. Message1(exec_e_res_not_found, utilsprefix+bin+source_info.exeext);
  152. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  153. Result:=false;
  154. end;
  155. s:=SetupCompilerArguments(output,OutName,respath,objused);
  156. { Execute the command }
  157. { Always try to compile resources. but don't complain if cs_link_nolink }
  158. if resfound then
  159. begin
  160. Message1(exec_i_compilingresource,fname);
  161. Message2(exec_d_resbin_params,resbin,s);
  162. FlushOutput;
  163. try
  164. if ExecuteProcess(resbin,s) <> 0 then
  165. begin
  166. if not (cs_link_nolink in current_settings.globalswitches) then
  167. Message(exec_e_error_while_compiling_resources);
  168. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  169. Result:=false;
  170. end;
  171. except
  172. on E:EOSError do
  173. begin
  174. if not (cs_link_nolink in current_settings.globalswitches) then
  175. Message1(exec_e_cant_call_resource_compiler, resbin);
  176. current_settings.globalswitches:=current_settings.globalswitches+[cs_link_nolink];
  177. Result:=false;
  178. end
  179. end;
  180. end;
  181. { Update asmres when externmode is set and resource compiling failed }
  182. if (not Result) and (cs_link_nolink in current_settings.globalswitches) then
  183. AsmRes.AddLinkCommand(resbin,s,OutName);
  184. if Result and (output=roOBJ) and ObjUsed then
  185. current_module.linkunitofiles.add(OutName,link_always);
  186. end;
  187. constructor TWinLikeResourceFile.Create(const fn : ansistring);
  188. begin
  189. inherited Create(fn);
  190. fResScript:=nil;
  191. fCollectCount:=0;
  192. if (tf_use_8_3 in target_info.flags) then
  193. fScriptName:=ChangeFileExt(fn,'.rls')
  194. else
  195. fScriptName:=ChangeFileExt(fn,'.reslst');
  196. end;
  197. destructor TWinLikeResourceFile.Destroy;
  198. begin
  199. if fResScript<>nil then
  200. fResScript.Free;
  201. inherited;
  202. end;
  203. function TWinLikeResourceFile.SetupCompilerArguments(output: tresoutput; const
  204. OutName : ansistring; respath : ansistring; out ObjUsed : boolean) : ansistring;
  205. var
  206. srcfilepath,
  207. preprocessorbin,
  208. s : TCmdStr;
  209. arch : ansistring;
  210. function WindresFileName(filename: TCmdStr): TCmdStr;
  211. // to be on the safe side, only give short file names with forward slashes to
  212. // windres
  213. var
  214. i: longint;
  215. begin
  216. Result := GetShortName(filename);
  217. for I:=1 to Length(Result) do
  218. if Result[I] in AllowDirectorySeparators then
  219. Result[i]:='/';
  220. end;
  221. begin
  222. srcfilepath:=ExtractFilePath(current_module.mainsource^);
  223. if output=roRES then
  224. begin
  225. s:=target_res.rccmd;
  226. if target_res.rcbin = 'windres' then
  227. begin
  228. Replace(s,'$RES',WindresFileName(OutName));
  229. Replace(s,'$RC',WindresFileName(fname));
  230. end
  231. else
  232. begin
  233. Replace(s,'$RES',maybequoted(OutName));
  234. Replace(s,'$RC',maybequoted(fname));
  235. end;
  236. ObjUsed:=False;
  237. end
  238. else
  239. begin
  240. s:=target_res.rescmd;
  241. if (res_external_file in target_res.resflags) then
  242. ObjUsed:=false
  243. else
  244. ObjUsed:=(pos('$OBJ',s)>0);
  245. Replace(s,'$OBJ',maybequoted(OutName));
  246. arch:=cpu2str[target_cpu];
  247. //Differentiate between arm and armeb
  248. if (source_info.cpu=cpu_arm) and (source_info.endian=endian_big) then
  249. arch:=arch+'eb';
  250. Replace(s,'$ARCH',arch);
  251. case target_info.endian of
  252. endian_little : Replace(s,'$ENDIAN','littleendian');
  253. endian_big : Replace(s,'$ENDIAN','bigendian');
  254. end;
  255. //call resource compiler with debug switch
  256. if (status.verbosity and V_Debug)<>0 then
  257. Replace(s,'$DBG','-v')
  258. else
  259. Replace(s,'$DBG','');
  260. if fCollectCount=0 then
  261. s:=s+' '+maybequoted(fname)
  262. else
  263. s:=s+' @'+maybequoted(fScriptName);
  264. end;
  265. { windres doesn't like empty include paths }
  266. if respath='' then
  267. respath:='.';
  268. Replace(s,'$INC',maybequoted(respath));
  269. if (output=roRes) and (target_res.rcbin='windres') then
  270. begin
  271. { try to find a preprocessor }
  272. preprocessorbin := respath+'cpp'+source_info.exeext;
  273. if FileExists(preprocessorbin,true) then
  274. s:='--preprocessor='+preprocessorbin+' '+s;
  275. if (srcfilepath<>'') then
  276. s:='--include '+WindresFileName(srcfilepath)+' '+s;
  277. end;
  278. Result:=s;
  279. end;
  280. function TWinLikeResourceFile.compile(output: tresoutput;
  281. const OutName: ansistring) : boolean;
  282. begin
  283. Result:=inherited compile(output,OutName);
  284. //delete fpc-res.lst file if things went well
  285. if Result and (output=roOBJ) then
  286. DeleteFile(fScriptName);
  287. end;
  288. function TWinLikeResourceFile.IsCompiled(const fn: ansistring): boolean;
  289. const
  290. ResSignature : array [1..32] of byte =
  291. ($00,$00,$00,$00,$20,$00,$00,$00,$FF,$FF,$00,$00,$FF,$FF,$00,$00,
  292. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00);
  293. dfmexts : array[1..3] of string[4] = ('.lfm', '.dfm', '.xfm');
  294. var
  295. f : file;
  296. oldfmode : byte;
  297. buf: array[1..32] of byte;
  298. i: longint;
  299. ext : shortstring;
  300. begin
  301. ext:=lower(ExtractFileExt(fn));
  302. Result:=CompareText(ext, target_info.resext) = 0;
  303. if not Result then
  304. for i:=1 to high(dfmexts) do
  305. begin
  306. Result:=CompareText(ext, dfmexts[i]) = 0;
  307. if Result then break;
  308. end;
  309. if Result or not FileExists(fn, False) then exit;
  310. oldfmode:=Filemode;
  311. Filemode:=0;
  312. assign(f,fn);
  313. reset(f,1);
  314. BlockRead(f, buf, SizeOf(buf), i);
  315. close(f);
  316. Filemode:=oldfmode;
  317. if i<>SizeOf(buf) then
  318. exit;
  319. for i:=1 to 32 do
  320. if buf[i]<>ResSignature[i] then
  321. exit;
  322. Result:=True;
  323. end;
  324. procedure TWinLikeResourceFile.Collect(const fn: ansistring);
  325. begin
  326. if fResScript=nil then
  327. fResScript:=TScript.Create(fScriptName);
  328. fResScript.Add(MaybeQuoted(fn));
  329. inc(fCollectCount);
  330. end;
  331. procedure TWinLikeResourceFile.EndCollect;
  332. begin
  333. if fResScript<>nil then
  334. begin
  335. fResScript.WriteToDisk;
  336. FreeAndNil(fResScript);
  337. Compile(roOBJ,ChangeFileExt(fname,target_info.resobjext));
  338. end;
  339. end;
  340. function CopyResFile(inf,outf : TCmdStr) : boolean;
  341. var
  342. src,dst : TCFileStream;
  343. begin
  344. { Copy .res file to units output dir. }
  345. Result:=false;
  346. src:=TCFileStream.Create(inf,fmOpenRead or fmShareDenyNone);
  347. if CStreamError<>0 then
  348. begin
  349. Message1(exec_e_cant_open_resource_file, src.FileName);
  350. Include(current_settings.globalswitches, cs_link_nolink);
  351. exit;
  352. end;
  353. dst:=TCFileStream.Create(current_module.outputpath^+outf,fmCreate);
  354. if CStreamError<>0 then
  355. begin
  356. Message1(exec_e_cant_write_resource_file, dst.FileName);
  357. Include(current_settings.globalswitches, cs_link_nolink);
  358. exit;
  359. end;
  360. dst.CopyFrom(src,src.Size);
  361. dst.Free;
  362. src.Free;
  363. Result:=true;
  364. end;
  365. procedure CompileResourceFiles;
  366. var
  367. resourcefile : tresourcefile;
  368. res: TCmdStrListItem;
  369. p,s : TCmdStr;
  370. outfmt : tresoutput;
  371. begin
  372. { Don't do anything for systems supporting resources without using resource
  373. file classes (e.g. Mac OS). They process resources elsewhere. }
  374. if (target_info.res<>res_none) and (target_res.resourcefileclass=nil) then
  375. exit;
  376. p:=ExtractFilePath(ExpandFileName(current_module.mainsource^));
  377. res:=TCmdStrListItem(current_module.ResourceFiles.First);
  378. while res<>nil do
  379. begin
  380. if target_info.res=res_none then
  381. Message(scan_e_resourcefiles_not_supported);
  382. s:=res.FPStr;
  383. if not path_absolute(s) then
  384. s:=p+s;
  385. if not FileExists(s, True) then
  386. begin
  387. Message1(exec_e_cant_open_resource_file, s);
  388. Include(current_settings.globalswitches, cs_link_nolink);
  389. exit;
  390. end;
  391. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  392. if resourcefile.IsCompiled(s) then
  393. begin
  394. resourcefile.free;
  395. if AnsiCompareFileName(IncludeTrailingPathDelimiter(ExpandFileName(current_module.outputpath^)), p) <> 0 then
  396. begin
  397. { Copy .res file to units output dir. Otherwise .res file will not be found
  398. when only compiled units path is available }
  399. res.FPStr:=ExtractFileName(res.FPStr); //store file name only in PPU.
  400. if not CopyResFile(s,res.FPStr) then exit;
  401. end;
  402. end
  403. else
  404. begin
  405. res.FPStr:=ExtractFileName(res.FPStr);
  406. if (target_res.rcbin='') and (RCCompiler='') then
  407. begin
  408. { if target does not have .rc to .res compiler, create obj }
  409. outfmt:=roOBJ;
  410. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resobjext);
  411. end
  412. else
  413. begin
  414. outfmt:=roRES;
  415. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resext);
  416. end;
  417. resourcefile.compile(outfmt, current_module.outputpath^+res.FPStr);
  418. resourcefile.free;
  419. end;
  420. res:=TCmdStrListItem(res.Next);
  421. end;
  422. end;
  423. procedure CollectResourceFiles;
  424. var
  425. resourcefile : tresourcefile;
  426. procedure ProcessModule(u : tmodule);
  427. var
  428. res : TCmdStrListItem;
  429. s : TCmdStr;
  430. begin
  431. res:=TCmdStrListItem(u.ResourceFiles.First);
  432. while assigned(res) do
  433. begin
  434. if path_absolute(res.FPStr) then
  435. s:=res.FPStr
  436. else
  437. begin
  438. s:=u.path^+res.FPStr;
  439. if not FileExists(s,True) then
  440. s:=u.outputpath^+res.FPStr;
  441. end;
  442. resourcefile.Collect(s);
  443. res:=TCmdStrListItem(res.Next);
  444. end;
  445. end;
  446. var
  447. hp : tused_unit;
  448. s : TCmdStr;
  449. begin
  450. if (target_info.res=res_none) or ((target_res.resbin='')
  451. and (ResCompiler='')) then
  452. exit;
  453. // if cs_link_nolink in current_settings.globalswitches then
  454. // exit;
  455. s:=ChangeFileExt(current_module.ppufilename^,target_info.resobjext);
  456. if (res_arch_in_file_name in target_res.resflags) then
  457. s:=ChangeFileExt(s,'.'+cpu2str[target_cpu]+target_info.resobjext);
  458. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  459. hp:=tused_unit(usedunits.first);
  460. while assigned(hp) do
  461. begin
  462. ProcessModule(hp.u);
  463. hp:=tused_unit(hp.next);
  464. end;
  465. ProcessModule(current_module);
  466. { Finish collection }
  467. resourcefile.EndCollect;
  468. resourcefile.free;
  469. end;
  470. end.