comprsrc.pas 17 KB

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