comprsrc.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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, for files that are passed to the preprocessor,
  212. // only give short file names with forward slashes to 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. Replace(s,'$RC',WindresFileName(fname))
  228. else
  229. Replace(s,'$RC',maybequoted(fname));
  230. Replace(s,'$RES',maybequoted(OutName));
  231. ObjUsed:=False;
  232. end
  233. else
  234. begin
  235. s:=target_res.rescmd;
  236. if (res_external_file in target_res.resflags) then
  237. ObjUsed:=false
  238. else
  239. ObjUsed:=(pos('$OBJ',s)>0);
  240. Replace(s,'$OBJ',maybequoted(OutName));
  241. arch:=cpu2str[target_cpu];
  242. //Differentiate between arm and armeb
  243. if (source_info.cpu=cpu_arm) and (source_info.endian=endian_big) then
  244. arch:=arch+'eb';
  245. Replace(s,'$ARCH',arch);
  246. case target_info.endian of
  247. endian_little : Replace(s,'$ENDIAN','littleendian');
  248. endian_big : Replace(s,'$ENDIAN','bigendian');
  249. end;
  250. //call resource compiler with debug switch
  251. if (status.verbosity and V_Debug)<>0 then
  252. Replace(s,'$DBG','-v')
  253. else
  254. Replace(s,'$DBG','');
  255. if fCollectCount=0 then
  256. s:=s+' '+maybequoted(fname)
  257. else
  258. s:=s+' @'+maybequoted(fScriptName);
  259. end;
  260. { windres doesn't like empty include paths }
  261. if respath='' then
  262. respath:='.';
  263. Replace(s,'$INC',maybequoted(respath));
  264. if (output=roRes) and (target_res.rcbin='windres') then
  265. begin
  266. { try to find a preprocessor }
  267. preprocessorbin := respath+'cpp'+source_info.exeext;
  268. if FileExists(preprocessorbin,true) then
  269. s:='--preprocessor='+preprocessorbin+' '+s;
  270. if (srcfilepath<>'') then
  271. s:='--include '+WindresFileName(srcfilepath)+' '+s;
  272. end;
  273. Result:=s;
  274. end;
  275. function TWinLikeResourceFile.compile(output: tresoutput;
  276. const OutName: ansistring) : boolean;
  277. begin
  278. Result:=inherited compile(output,OutName);
  279. //delete fpc-res.lst file if things went well
  280. if Result and (output=roOBJ) then
  281. DeleteFile(fScriptName);
  282. end;
  283. function TWinLikeResourceFile.IsCompiled(const fn: ansistring): boolean;
  284. const
  285. ResSignature : array [1..32] of byte =
  286. ($00,$00,$00,$00,$20,$00,$00,$00,$FF,$FF,$00,$00,$FF,$FF,$00,$00,
  287. $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00);
  288. dfmexts : array[1..3] of string[4] = ('.lfm', '.dfm', '.xfm');
  289. var
  290. f : file;
  291. oldfmode : byte;
  292. buf: array[1..32] of byte;
  293. i: longint;
  294. ext : shortstring;
  295. begin
  296. ext:=lower(ExtractFileExt(fn));
  297. Result:=CompareText(ext, target_info.resext) = 0;
  298. if not Result then
  299. for i:=1 to high(dfmexts) do
  300. begin
  301. Result:=CompareText(ext, dfmexts[i]) = 0;
  302. if Result then break;
  303. end;
  304. if Result or not FileExists(fn, False) then exit;
  305. oldfmode:=Filemode;
  306. Filemode:=0;
  307. assign(f,fn);
  308. reset(f,1);
  309. BlockRead(f, buf, SizeOf(buf), i);
  310. close(f);
  311. Filemode:=oldfmode;
  312. if i<>SizeOf(buf) then
  313. exit;
  314. for i:=1 to 32 do
  315. if buf[i]<>ResSignature[i] then
  316. exit;
  317. Result:=True;
  318. end;
  319. procedure TWinLikeResourceFile.Collect(const fn: ansistring);
  320. begin
  321. if fResScript=nil then
  322. fResScript:=TScript.Create(fScriptName);
  323. fResScript.Add(MaybeQuoted(fn));
  324. inc(fCollectCount);
  325. end;
  326. procedure TWinLikeResourceFile.EndCollect;
  327. begin
  328. if fResScript<>nil then
  329. begin
  330. fResScript.WriteToDisk;
  331. FreeAndNil(fResScript);
  332. Compile(roOBJ,ChangeFileExt(fname,target_info.resobjext));
  333. end;
  334. end;
  335. function CopyResFile(inf,outf : TCmdStr) : boolean;
  336. var
  337. src,dst : TCFileStream;
  338. begin
  339. { Copy .res file to units output dir. }
  340. Result:=false;
  341. src:=TCFileStream.Create(inf,fmOpenRead or fmShareDenyNone);
  342. if CStreamError<>0 then
  343. begin
  344. Message1(exec_e_cant_open_resource_file, src.FileName);
  345. Include(current_settings.globalswitches, cs_link_nolink);
  346. exit;
  347. end;
  348. dst:=TCFileStream.Create(current_module.outputpath^+outf,fmCreate);
  349. if CStreamError<>0 then
  350. begin
  351. Message1(exec_e_cant_write_resource_file, dst.FileName);
  352. Include(current_settings.globalswitches, cs_link_nolink);
  353. exit;
  354. end;
  355. dst.CopyFrom(src,src.Size);
  356. dst.Free;
  357. src.Free;
  358. Result:=true;
  359. end;
  360. procedure CompileResourceFiles;
  361. var
  362. resourcefile : tresourcefile;
  363. res: TCmdStrListItem;
  364. p,s : TCmdStr;
  365. outfmt : tresoutput;
  366. begin
  367. { Don't do anything for systems supporting resources without using resource
  368. file classes (e.g. Mac OS). They process resources elsewhere. }
  369. if (target_info.res<>res_none) and (target_res.resourcefileclass=nil) then
  370. exit;
  371. p:=ExtractFilePath(ExpandFileName(current_module.mainsource^));
  372. res:=TCmdStrListItem(current_module.ResourceFiles.First);
  373. while res<>nil do
  374. begin
  375. if target_info.res=res_none then
  376. Message(scan_e_resourcefiles_not_supported);
  377. s:=res.FPStr;
  378. if not path_absolute(s) then
  379. s:=p+s;
  380. if not FileExists(s, True) then
  381. begin
  382. Message1(exec_e_cant_open_resource_file, s);
  383. Include(current_settings.globalswitches, cs_link_nolink);
  384. exit;
  385. end;
  386. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  387. if resourcefile.IsCompiled(s) then
  388. begin
  389. resourcefile.free;
  390. if AnsiCompareFileName(IncludeTrailingPathDelimiter(ExpandFileName(current_module.outputpath^)), p) <> 0 then
  391. begin
  392. { Copy .res file to units output dir. Otherwise .res file will not be found
  393. when only compiled units path is available }
  394. res.FPStr:=ExtractFileName(res.FPStr); //store file name only in PPU.
  395. if not CopyResFile(s,res.FPStr) then exit;
  396. end;
  397. end
  398. else
  399. begin
  400. res.FPStr:=ExtractFileName(res.FPStr);
  401. if (target_res.rcbin='') and (RCCompiler='') then
  402. begin
  403. { if target does not have .rc to .res compiler, create obj }
  404. outfmt:=roOBJ;
  405. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resobjext);
  406. end
  407. else
  408. begin
  409. outfmt:=roRES;
  410. res.FPStr:=ChangeFileExt(res.FPStr,target_info.resext);
  411. end;
  412. resourcefile.compile(outfmt, current_module.outputpath^+res.FPStr);
  413. resourcefile.free;
  414. end;
  415. res:=TCmdStrListItem(res.Next);
  416. end;
  417. end;
  418. procedure CollectResourceFiles;
  419. var
  420. resourcefile : tresourcefile;
  421. procedure ProcessModule(u : tmodule);
  422. var
  423. res : TCmdStrListItem;
  424. s : TCmdStr;
  425. begin
  426. res:=TCmdStrListItem(u.ResourceFiles.First);
  427. while assigned(res) do
  428. begin
  429. if path_absolute(res.FPStr) then
  430. s:=res.FPStr
  431. else
  432. begin
  433. s:=u.path^+res.FPStr;
  434. if not FileExists(s,True) then
  435. s:=u.outputpath^+res.FPStr;
  436. end;
  437. resourcefile.Collect(s);
  438. res:=TCmdStrListItem(res.Next);
  439. end;
  440. end;
  441. var
  442. hp : tused_unit;
  443. s : TCmdStr;
  444. begin
  445. if (target_info.res=res_none) or ((target_res.resbin='')
  446. and (ResCompiler='')) then
  447. exit;
  448. // if cs_link_nolink in current_settings.globalswitches then
  449. // exit;
  450. s:=ChangeFileExt(current_module.ppufilename^,target_info.resobjext);
  451. if (res_arch_in_file_name in target_res.resflags) then
  452. s:=ChangeFileExt(s,'.'+cpu2str[target_cpu]+target_info.resobjext);
  453. resourcefile:=TResourceFile(resinfos[target_info.res]^.resourcefileclass.create(s));
  454. hp:=tused_unit(usedunits.first);
  455. while assigned(hp) do
  456. begin
  457. ProcessModule(hp.u);
  458. hp:=tused_unit(hp.next);
  459. end;
  460. ProcessModule(current_module);
  461. { Finish collection }
  462. resourcefile.EndCollect;
  463. resourcefile.free;
  464. end;
  465. end.