comprsrc.pas 17 KB

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