comprsrc.pas 16 KB

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